Skip to content

Commit

Permalink
feat: children models
Browse files Browse the repository at this point in the history
  • Loading branch information
vinceh121 committed Sep 24, 2023
1 parent b6df7d1 commit 7064a0b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
27 changes: 26 additions & 1 deletion android/assets/machinegunGuntowers.json
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,32 @@
1.0,
1.0
]
}
},
"children": [
{
"displayModel": "orig/j_guntower01.n/waffe.obj",
"displayTexture": "orig/lib/textures/j_guntower2j_guntower_alpha.ktx",
"animationChannel": "barrelSpin",
"relativeTransform": {
"translation": [
0.0,
3,
0.0
],
"rotation": [
0.0,
0.0,
0.0,
1.0
],
"scale": [
1.0,
1.0,
1.0
]
}
}
]
}
]
}
Expand Down
35 changes: 32 additions & 3 deletions core/src/me/vinceh121/wanderer/entity/DisplayModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.utils.Array;
import com.fasterxml.jackson.annotation.JsonIgnore;

import me.vinceh121.wanderer.WandererConstants;
Expand All @@ -24,6 +25,7 @@ public class DisplayModel {
* This model's transform relative to the entity's
*/
private final Matrix4 relativeTransform = new Matrix4();
private final Array<DisplayModel> children = new Array<>();
@JsonIgnore
private final Matrix4 absoluteTransform = new Matrix4();
private final List<Attribute> textureAttributes = new ArrayList<>();
Expand Down Expand Up @@ -52,9 +54,17 @@ public DisplayModel(final DisplayModel from) {
this.setDisplayTexture(from.getDisplayTexture());
this.setCacheDisplayModel(from.getCacheDisplayModel());
this.setAnimationChannel(from.getAnimationChannel());

for (DisplayModel fromChild : from.getChildren()) {
this.children.add(new DisplayModel(fromChild));
}
}

public void render(final ModelBatch batch, final Environment env) {
for (DisplayModel child : this.children) {
child.render(batch, env);
}

if (this.displayModel == null) {
return;
}
Expand All @@ -78,15 +88,21 @@ public void render(final ModelBatch batch, final Environment env) {
public void loadDisplayModel() {
final Model model = WandererConstants.ASSET_MANAGER.get(this.getDisplayModel(), Model.class);
final ModelInstance instance = new ModelInstance(model);

if (this.displayTexture != null) {
final Texture texture = WandererConstants.ASSET_MANAGER.get(this.displayTexture, Texture.class);
texture.setFilter(TextureFilter.MipMapLinearLinear, TextureFilter.Linear);

instance.materials.get(0).set(TextureAttribute.createDiffuse(texture));
instance.materials.get(0).set(this.textureAttributes); // this is called set but it's more like add
}

this.setCacheDisplayModel(instance);
this.getCacheDisplayModel().transform = this.absoluteTransform;

for (DisplayModel child : this.children) {
child.loadDisplayModel();
}
}

public void updateTransform(final Matrix4 entityTrans) {
Expand All @@ -97,6 +113,19 @@ public void updateTransform(final Matrix4 entityTrans) {
if (this.cacheDisplayModel != null) {
this.cacheDisplayModel.transform = this.absoluteTransform;
}

for (DisplayModel child : this.children) {
child.updateTransform(this.absoluteTransform);
}
}

public Array<DisplayModel> getChildren() {
return this.children;
}

public void setChildren(Array<DisplayModel> children) {
this.children.clear();
this.children.addAll(children);
}

public String getDisplayModel() {
Expand Down Expand Up @@ -169,8 +198,8 @@ public Attribute removeTextureAttribute(final int index) {

@Override
public String toString() {
return "DisplayModel [relativeTransform=" + this.relativeTransform + ", absoluteTransform=" + this.absoluteTransform
+ ", displayModel=" + this.displayModel + ", displayTexture=" + this.displayTexture + ", animationChannel="
+ this.animationChannel + "]";
return "DisplayModel [relativeTransform=" + this.relativeTransform + ", absoluteTransform="
+ this.absoluteTransform + ", displayModel=" + this.displayModel + ", displayTexture="
+ this.displayTexture + ", animationChannel=" + this.animationChannel + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public void tick(float delta) {
.nor();

final Vector3 newDir = this.target.getLookDirection().slerp(closestDir, this.turnSpeed * delta);
// Vector3 newDir = closestDir.cpy();
newDir.rotateRad(Vector3.X, MathUtils.HALF_PI);

final float polar = MathUtilsW.getSphericalPolar(newDir.z);
Expand Down

0 comments on commit 7064a0b

Please sign in to comment.