-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Rename TransformedInstance -> PosedInstance - Add TransformedInstance, which does not have a normal matrix - Use mat3(transpose(inverse(i.pose))) in the vertex shader, but for most cases that will be overkill
- Loading branch information
Showing
6 changed files
with
143 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
common/src/lib/java/dev/engine_room/flywheel/lib/instance/PosedInstance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package dev.engine_room.flywheel.lib.instance; | ||
|
||
import org.joml.Matrix3f; | ||
import org.joml.Matrix3fc; | ||
import org.joml.Matrix4f; | ||
import org.joml.Matrix4fc; | ||
import org.joml.Quaternionfc; | ||
|
||
import com.mojang.blaze3d.vertex.PoseStack; | ||
|
||
import dev.engine_room.flywheel.api.instance.InstanceHandle; | ||
import dev.engine_room.flywheel.api.instance.InstanceType; | ||
import dev.engine_room.flywheel.lib.transform.Transform; | ||
import net.minecraft.util.Mth; | ||
|
||
public class PosedInstance extends ColoredLitInstance implements Transform<PosedInstance> { | ||
public final Matrix4f model = new Matrix4f(); | ||
public final Matrix3f normal = new Matrix3f(); | ||
|
||
public PosedInstance(InstanceType<? extends PosedInstance> type, InstanceHandle handle) { | ||
super(type, handle); | ||
} | ||
|
||
@Override | ||
public PosedInstance mulPose(Matrix4fc pose) { | ||
this.model.mul(pose); | ||
return this; | ||
} | ||
|
||
@Override | ||
public PosedInstance mulNormal(Matrix3fc normal) { | ||
this.normal.mul(normal); | ||
return this; | ||
} | ||
|
||
@Override | ||
public PosedInstance rotateAround(Quaternionfc quaternion, float x, float y, float z) { | ||
model.rotateAround(quaternion, x, y, z); | ||
normal.rotate(quaternion); | ||
return this; | ||
} | ||
|
||
@Override | ||
public PosedInstance translate(float x, float y, float z) { | ||
model.translate(x, y, z); | ||
return this; | ||
} | ||
|
||
@Override | ||
public PosedInstance rotate(Quaternionfc quaternion) { | ||
model.rotate(quaternion); | ||
normal.rotate(quaternion); | ||
return this; | ||
} | ||
|
||
@Override | ||
public PosedInstance scale(float x, float y, float z) { | ||
model.scale(x, y, z); | ||
|
||
if (x == y && y == z) { | ||
if (x < 0.0f) { | ||
normal.scale(-1.0f); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
float invX = 1.0f / x; | ||
float invY = 1.0f / y; | ||
float invZ = 1.0f / z; | ||
float f = Mth.fastInvCubeRoot(Math.abs(invX * invY * invZ)); | ||
normal.scale(f * invX, f * invY, f * invZ); | ||
return this; | ||
} | ||
|
||
public PosedInstance setTransform(PoseStack.Pose pose) { | ||
model.set(pose.pose()); | ||
normal.set(pose.normal()); | ||
return this; | ||
} | ||
|
||
public PosedInstance setTransform(PoseStack stack) { | ||
return setTransform(stack.last()); | ||
} | ||
|
||
public PosedInstance setIdentityTransform() { | ||
model.identity(); | ||
normal.identity(); | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the transform matrices to be all zeros. | ||
* | ||
* <p> | ||
* This will allow the GPU to quickly discard all geometry for this instance, effectively "turning it off". | ||
* </p> | ||
*/ | ||
public PosedInstance setZeroTransform() { | ||
model.zero(); | ||
normal.zero(); | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
common/src/lib/resources/assets/flywheel/flywheel/instance/cull/posed.glsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#include "flywheel:util/matrix.glsl" | ||
|
||
void flw_transformBoundingSphere(in FlwInstance i, inout vec3 center, inout float radius) { | ||
transformBoundingSphere(i.pose, center, radius); | ||
} |
8 changes: 8 additions & 0 deletions
8
common/src/lib/resources/assets/flywheel/flywheel/instance/posed.vert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
void flw_instanceVertex(in FlwInstance i) { | ||
flw_vertexPos = i.pose * flw_vertexPos; | ||
flw_vertexNormal = i.normal * flw_vertexNormal; | ||
flw_vertexColor *= i.color; | ||
flw_vertexOverlay = i.overlay; | ||
// Some drivers have a bug where uint over float division is invalid, so use an explicit cast. | ||
flw_vertexLight = vec2(i.light) / 256.0; | ||
} |
2 changes: 1 addition & 1 deletion
2
common/src/lib/resources/assets/flywheel/flywheel/instance/transformed.vert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters