Skip to content

Commit

Permalink
fix(rendering): replicate SkeletalMesh fields, fix debug skeleton sca…
Browse files Browse the repository at this point in the history
…le (#4897)
  • Loading branch information
pollend authored Nov 14, 2021
1 parent 984d9e9 commit f105255
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public class SkeletalMeshComponent implements VisualComponent<SkeletalMeshCompon
public EntityRef rootBone = EntityRef.NULL;
public float animationTime;

@Replicate
public Vector3f scale = new Vector3f(1, 1, 1);
@Replicate
public Vector3f translate = new Vector3f();

@Replicate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ public void renderOpaque() {
location.getWorldPosition(worldPos);
float worldScale = location.getWorldScale();


aabb = aabb.transform(new Matrix4f().translationRotateScale(worldPos, worldRot, worldScale), new AABBf());

//Scale bounding box for skeletalMesh.
Expand Down Expand Up @@ -310,9 +309,9 @@ public void renderOpaque() {
} else {
logger.warn("Unable to resolve bone \"{}\"", bone.getName());
boneTransforms[bone.getIndex()] = new Matrix4f();

}
}

((OpenGLSkeletalMesh) skeletalMesh.mesh).setScaleTranslate(skeletalMesh.scale, skeletalMesh.translate);
((OpenGLSkeletalMesh) skeletalMesh.mesh).render(Arrays.asList(boneTransforms));
}
Expand All @@ -329,42 +328,75 @@ public void renderOverlay() {

Vector3f cameraPosition = worldRenderer.getActiveCamera().getPosition();

Vector3f worldPos = new Vector3f();
Vector3f worldPositionCameraSpace = new Vector3f();
worldPos.sub(cameraPosition, worldPositionCameraSpace);
Matrix4f matrixCameraSpace = new Matrix4f().translationRotateScale(worldPositionCameraSpace, new Quaternionf(), 1.0f);
Matrix4f modelViewMatrix = new Matrix4f(worldRenderer.getActiveCamera().getViewMatrix()).mul(matrixCameraSpace);
material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix());
material.setMatrix4("modelViewMatrix", modelViewMatrix, true);
Matrix4f relMat = new Matrix4f();
Matrix4f relFinal = new Matrix4f();
Matrix4f entityTransform = new Matrix4f();

Matrix4f result = new Matrix4f();
Vector3f currentPos = new Vector3f();

int index = 0;
for (EntityRef entity : entityManager.getEntitiesWith(SkeletalMeshComponent.class, LocationComponent.class)) {
SkeletalMeshComponent skeletalMesh = entity.getComponent(SkeletalMeshComponent.class);
LocationComponent locationComponent = entity.getComponent(LocationComponent.class);
if (skeletalMesh.boneEntities == null) {
continue;
}

Vector3f location = locationComponent.getWorldPosition(new Vector3f());
Quaternionf rotation = locationComponent.getWorldRotation(new Quaternionf());
entityTransform.translationRotateScale(location, rotation, 1.0f); // transformation of the entity

// position is referenced around (0,0,0) (worldposition - cameraposition)
Vector3f worldPositionCameraSpace = cameraPosition.negate(new Vector3f());

// same heightOffset is applied to worldPositionCameraSpace from #renderOpaque()
// TODO: resolve repeated logic for transformation applied to bones
worldPositionCameraSpace.y += skeletalMesh.heightOffset;

Matrix4f matrixCameraSpace = new Matrix4f().translationRotateScale(worldPositionCameraSpace, new Quaternionf(), 1.0f);
Matrix4f modelViewMatrix = new Matrix4f(worldRenderer.getActiveCamera().getViewMatrix()).mul(matrixCameraSpace);
material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix());
material.setMatrix4("modelViewMatrix", modelViewMatrix, true);

for (Bone bone : skeletalMesh.mesh.getBones()) {
Bone parentBone = bone.getParent();
EntityRef boneEntity = skeletalMesh.boneEntities.get(bone.getName());
if (parentBone == null) {
continue;
}

// TODO: the position of the bone is de-coupled from the actual translation/scale
EntityRef boneParentEntity = skeletalMesh.boneEntities.get(parentBone.getName());

LocationComponent locCompA = boneEntity.getComponent(LocationComponent.class);
LocationComponent locCompB = boneParentEntity.getComponent(LocationComponent.class);

Vector3f worldPosA = locCompA.getWorldPosition(new Vector3f());
Vector3f worldPosB = locCompB.getWorldPosition(new Vector3f());
// need to calculate the relative transformation from the entity to the start of the bone
locCompA.getRelativeTransform(relMat.identity(), entity);
// entityTransform * (scale, translation) * relativeMat * [x,y,z,1]
result.set(entityTransform)
.mul(relFinal.identity()
.scale(skeletalMesh.scale)
.translate(skeletalMesh.translate)
.mul(relMat))
.transformPosition(currentPos.zero()); // get the position of the start of the bone
meshData.position.put(currentPos); // the start of the bone

// need to calculate the relative transformation from the entity to the connecting bone
locCompB.getRelativeTransform(relMat.identity(), entity);
// entityTransform * (scale, translation) * relativeMat * [x,y,z,1]
result.set(entityTransform)
.mul(relFinal
.identity()
.scale(skeletalMesh.scale)
.translate(skeletalMesh.translate)
.mul(relMat))
.transformPosition(currentPos.zero()); // get the position to the connecting bone
meshData.position.put(currentPos); // the end of the bone

meshData.color0.put(Color.white);
meshData.color0.put(Color.white);

meshData.position.put(worldPosA);
meshData.position.put(worldPosB);

meshData.indices.putAll(new int[]{
index, index + 1
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,11 @@ public void doRender(List<Vector3f> verts, List<Vector3f> normals) {
}

public void render() {
// preRender();
doRender(data.getBindPoseVertexPositions(), data.getBindPoseVertexNormals());
// postRender();
}

public void render(List<Matrix4f> boneTransforms) {
// preRender();
doRender(data.getVertexPositions(boneTransforms), data.getVertexNormals(boneTransforms));
// postRender();
}

@Override
Expand Down

0 comments on commit f105255

Please sign in to comment.