diff --git a/src/main/java/com/jozufozu/flywheel/backend/core/BasicInstancedTileRenderer.java b/src/main/java/com/jozufozu/flywheel/backend/core/BasicInstancedTileRenderer.java new file mode 100644 index 000000000..dcdf7d317 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/core/BasicInstancedTileRenderer.java @@ -0,0 +1,72 @@ +package com.jozufozu.flywheel.backend.core; + +import java.util.ArrayList; + +import com.jozufozu.flywheel.backend.MaterialTypes; +import com.jozufozu.flywheel.backend.gl.shader.ShaderCallback; +import com.jozufozu.flywheel.backend.instancing.InstancedTileRenderer; +import com.jozufozu.flywheel.backend.instancing.RenderMaterial; +import com.simibubi.create.foundation.render.AllProgramSpecs; + +import net.minecraft.client.renderer.ActiveRenderInfo; +import net.minecraft.client.renderer.RenderType; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.MathHelper; +import net.minecraft.util.math.vector.Matrix4f; + +public class BasicInstancedTileRenderer extends InstancedTileRenderer { + public static int MAX_ORIGIN_DISTANCE = 100; + + public BlockPos originCoordinate = BlockPos.ZERO; + + @Override + public void registerMaterials() { + materials.put(MaterialTypes.TRANSFORMED, + new RenderMaterial<>(this, AllProgramSpecs.MODEL, TransformedModel::new)); + materials.put(MaterialTypes.ORIENTED, new RenderMaterial<>(this, AllProgramSpecs.ORIENTED, OrientedModel::new)); + } + + @Override + public BlockPos getOriginCoordinate() { + return originCoordinate; + } + + @Override + public void beginFrame(ActiveRenderInfo info, double cameraX, double cameraY, double cameraZ) { + int cX = MathHelper.floor(cameraX); + int cY = MathHelper.floor(cameraY); + int cZ = MathHelper.floor(cameraZ); + + int dX = Math.abs(cX - originCoordinate.getX()); + int dY = Math.abs(cY - originCoordinate.getY()); + int dZ = Math.abs(cZ - originCoordinate.getZ()); + + if (dX > MAX_ORIGIN_DISTANCE || dY > MAX_ORIGIN_DISTANCE || dZ > MAX_ORIGIN_DISTANCE) { + + originCoordinate = new BlockPos(cX, cY, cZ); + + ArrayList instancedTiles = new ArrayList<>(instances.keySet()); + invalidate(); + instancedTiles.forEach(this::add); + } + + super.beginFrame(info, cameraX, cameraY, cameraZ); + } + + @Override + public void render(RenderType layer, Matrix4f viewProjection, double camX, double camY, double camZ, + ShaderCallback callback) { + BlockPos originCoordinate = getOriginCoordinate(); + + camX -= originCoordinate.getX(); + camY -= originCoordinate.getY(); + camZ -= originCoordinate.getZ(); + + Matrix4f translate = Matrix4f.translate((float) -camX, (float) -camY, (float) -camZ); + + translate.multiplyBackward(viewProjection); + + super.render(layer, translate, camX, camY, camZ, callback); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlShader.java b/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlShader.java index 6b1cd8cca..80c2270c0 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlShader.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlShader.java @@ -28,7 +28,7 @@ public class GlShader extends GlObject { } if (GL20.glGetShaderi(handle, GL20.GL_COMPILE_STATUS) != GL20.GL_TRUE) { - throw new RuntimeException("Could not compile shader. See log for details."); + throw new RuntimeException("Could not compile " + name + ". See log for details."); } setHandle(handle); diff --git a/src/main/java/com/simibubi/create/foundation/render/KineticRenderer.java b/src/main/java/com/simibubi/create/foundation/render/KineticRenderer.java index 510641f19..93cc288bd 100644 --- a/src/main/java/com/simibubi/create/foundation/render/KineticRenderer.java +++ b/src/main/java/com/simibubi/create/foundation/render/KineticRenderer.java @@ -1,84 +1,22 @@ package com.simibubi.create.foundation.render; -import java.util.ArrayList; - -import com.jozufozu.flywheel.backend.MaterialTypes; -import com.jozufozu.flywheel.backend.core.BasicProgram; -import com.jozufozu.flywheel.backend.core.OrientedModel; -import com.jozufozu.flywheel.backend.core.TransformedModel; -import com.jozufozu.flywheel.backend.gl.shader.ShaderCallback; -import com.jozufozu.flywheel.backend.instancing.InstancedTileRenderer; +import com.jozufozu.flywheel.backend.core.BasicInstancedTileRenderer; import com.jozufozu.flywheel.backend.instancing.RenderMaterial; import com.simibubi.create.content.contraptions.base.KineticRenderMaterials; import com.simibubi.create.content.contraptions.base.RotatingModel; import com.simibubi.create.content.contraptions.relays.belt.BeltInstancedModel; import com.simibubi.create.content.logistics.block.FlapModel; -import net.minecraft.client.renderer.ActiveRenderInfo; -import net.minecraft.client.renderer.RenderType; -import net.minecraft.tileentity.TileEntity; -import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.MathHelper; -import net.minecraft.util.math.vector.Matrix4f; - -public class KineticRenderer extends InstancedTileRenderer { - public static int MAX_ORIGIN_DISTANCE = 100; - - public BlockPos originCoordinate = BlockPos.ZERO; +public class KineticRenderer extends BasicInstancedTileRenderer { @Override public void registerMaterials() { - materials.put(MaterialTypes.TRANSFORMED, - new RenderMaterial<>(this, AllProgramSpecs.MODEL, TransformedModel::new)); - materials.put(MaterialTypes.ORIENTED, new RenderMaterial<>(this, AllProgramSpecs.ORIENTED, OrientedModel::new)); + super.registerMaterials(); materials.put(KineticRenderMaterials.BELTS, - new RenderMaterial<>(this, AllProgramSpecs.BELT, BeltInstancedModel::new)); + new RenderMaterial<>(this, AllProgramSpecs.BELT, BeltInstancedModel::new)); materials.put(KineticRenderMaterials.ROTATING, - new RenderMaterial<>(this, AllProgramSpecs.ROTATING, RotatingModel::new)); + new RenderMaterial<>(this, AllProgramSpecs.ROTATING, RotatingModel::new)); materials.put(KineticRenderMaterials.FLAPS, new RenderMaterial<>(this, AllProgramSpecs.FLAPS, FlapModel::new)); } - - @Override - public BlockPos getOriginCoordinate() { - return originCoordinate; - } - - @Override - public void beginFrame(ActiveRenderInfo info, double cameraX, double cameraY, double cameraZ) { - int cX = MathHelper.floor(cameraX); - int cY = MathHelper.floor(cameraY); - int cZ = MathHelper.floor(cameraZ); - - int dX = Math.abs(cX - originCoordinate.getX()); - int dY = Math.abs(cY - originCoordinate.getY()); - int dZ = Math.abs(cZ - originCoordinate.getZ()); - - if (dX > MAX_ORIGIN_DISTANCE || dY > MAX_ORIGIN_DISTANCE || dZ > MAX_ORIGIN_DISTANCE) { - - originCoordinate = new BlockPos(cX, cY, cZ); - - ArrayList instancedTiles = new ArrayList<>(instances.keySet()); - invalidate(); - instancedTiles.forEach(this::add); - } - - super.beginFrame(info, cameraX, cameraY, cameraZ); - } - - @Override - public void render(RenderType layer, Matrix4f viewProjection, double camX, double camY, double camZ, - ShaderCallback callback) { - BlockPos originCoordinate = getOriginCoordinate(); - - camX -= originCoordinate.getX(); - camY -= originCoordinate.getY(); - camZ -= originCoordinate.getZ(); - - Matrix4f translate = Matrix4f.translate((float) -camX, (float) -camY, (float) -camZ); - - translate.multiplyBackward(viewProjection); - - super.render(layer, translate, camX, camY, camZ, callback); - } } diff --git a/src/main/resources/assets/create/flywheel/shaders/belt.vert b/src/main/resources/assets/create/flywheel/shaders/belt.vert index 3a5fefcec..b0f066140 100644 --- a/src/main/resources/assets/create/flywheel/shaders/belt.vert +++ b/src/main/resources/assets/create/flywheel/shaders/belt.vert @@ -24,14 +24,6 @@ varying vec4 Color; varying float Diffuse; varying vec2 Light; -#if defined(CONTRAPTION) -varying vec3 BoxCoord; - -uniform vec3 uLightBoxSize; -uniform vec3 uLightBoxMin; -uniform mat4 uModel; -#endif - uniform float uTime; uniform mat4 uViewProjection; uniform int uDebug; @@ -42,6 +34,12 @@ uniform vec3 uCameraPos; varying float FragDistance; #endif +#ifdef CONTRAPTION +#flwinclude <"create:contraption/finalize.glsl"> +#else +#flwinclude <"create:std/finalize.glsl"> +#endif + void main() { vec3 rotated = rotateVertexByQuat(aPos - .5, aInstanceRot) + aInstancePos + .5; @@ -49,17 +47,8 @@ void main() { vec3 norm = rotateVertexByQuat(aNormal, aInstanceRot); -#ifdef CONTRAPTION - worldPos = uModel * worldPos; - norm = normalize(modelToNormal(uModel) * norm); - - BoxCoord = (worldPos.xyz - uLightBoxMin) / uLightBoxSize; - #if defined(USE_FOG) - FragDistance = length(worldPos.xyz); - #endif -#elif defined(USE_FOG) - FragDistance = length(worldPos.xyz - uCameraPos); -#endif + FLWFinalizeWorldPos(worldPos); + FLWFinalizeNormal(norm); float scrollSize = aScrollTexture.w - aScrollTexture.y; float scroll = fract(aSpeed * uTime / (31.5 * 16.) + aOffset) * scrollSize * aScrollMult; diff --git a/src/main/resources/assets/create/flywheel/shaders/contraption/finalize.glsl b/src/main/resources/assets/create/flywheel/shaders/contraption/finalize.glsl new file mode 100644 index 000000000..ed55cc3a9 --- /dev/null +++ b/src/main/resources/assets/create/flywheel/shaders/contraption/finalize.glsl @@ -0,0 +1,20 @@ +varying vec3 BoxCoord; + +uniform vec3 uLightBoxSize; +uniform vec3 uLightBoxMin; +uniform mat4 uModel; + +void FLWFinalizeWorldPos(inout vec4 worldPos) { + worldPos = uModel * worldPos; + + BoxCoord = (worldPos.xyz - uLightBoxMin) / uLightBoxSize; + #if defined(USE_FOG) + + FragDistance = length(worldPos.xyz); + #endif +} + +void FLWFinalizeNormal(inout vec3 normal) { + normal = modelToNormal(uModel) * normal; +} + diff --git a/src/main/resources/assets/create/flywheel/shaders/flap.vert b/src/main/resources/assets/create/flywheel/shaders/flap.vert index 496051288..bd877cc48 100644 --- a/src/main/resources/assets/create/flywheel/shaders/flap.vert +++ b/src/main/resources/assets/create/flywheel/shaders/flap.vert @@ -26,14 +26,6 @@ varying vec4 Color; varying float Diffuse; varying vec2 Light; -#if defined(CONTRAPTION) -varying vec3 BoxCoord; - -uniform vec3 uLightBoxSize; -uniform vec3 uLightBoxMin; -uniform mat4 uModel; -#endif - uniform float uTime; uniform mat4 uViewProjection; uniform int uDebug; @@ -44,6 +36,12 @@ uniform vec3 uCameraPos; varying float FragDistance; #endif +#ifdef CONTRAPTION +#flwinclude <"create:contraption/finalize.glsl"> +#else +#flwinclude <"create:std/finalize.glsl"> +#endif + float toRad(float degrees) { return fract(degrees / 360.) * PI * 2.; } @@ -73,17 +71,8 @@ void main() { vec4 worldPos = vec4(rotated, 1.); vec3 norm = rotateVertexByQuat(rotateVertexByQuat(aNormal, flapRotation), orientation); - #ifdef CONTRAPTION - worldPos = uModel * worldPos; - norm = modelToNormal(uModel) * norm; - - BoxCoord = (worldPos.xyz - uLightBoxMin) / uLightBoxSize; - #if defined(USE_FOG) - FragDistance = length(worldPos.xyz); - #endif - #elif defined(USE_FOG) - FragDistance = length(worldPos.xyz - uCameraPos); - #endif + FLWFinalizeWorldPos(worldPos); + FLWFinalizeNormal(norm); Diffuse = diffuse(norm); TexCoords = aTexCoords; diff --git a/src/main/resources/assets/create/flywheel/shaders/model.vert b/src/main/resources/assets/create/flywheel/shaders/model.vert index ea4503b75..c6a146695 100644 --- a/src/main/resources/assets/create/flywheel/shaders/model.vert +++ b/src/main/resources/assets/create/flywheel/shaders/model.vert @@ -17,14 +17,6 @@ varying vec4 Color; varying float Diffuse; varying vec2 Light; -#if defined(CONTRAPTION) -varying vec3 BoxCoord; - -uniform vec3 uLightBoxSize; -uniform vec3 uLightBoxMin; -uniform mat4 uModel; -#endif - uniform float uTime; uniform mat4 uViewProjection; uniform int uDebug; @@ -35,22 +27,20 @@ uniform vec3 uCameraPos; varying float FragDistance; #endif +#ifdef CONTRAPTION +#flwinclude <"create:contraption/finalize.glsl"> +#else +#flwinclude <"create:std/finalize.glsl"> +#endif + + void main() { vec4 worldPos = aTransform * vec4(aPos, 1.); vec3 norm = aNormalMat * aNormal; -#ifdef CONTRAPTION - worldPos = uModel * worldPos; - norm = modelToNormal(uModel) * norm; - - BoxCoord = (worldPos.xyz - uLightBoxMin) / uLightBoxSize; - #if defined(USE_FOG) - FragDistance = length(worldPos.xyz); - #endif -#elif defined(USE_FOG) - FragDistance = length(worldPos.xyz - uCameraPos); -#endif + FLWFinalizeNormal(norm); + FLWFinalizeWorldPos(worldPos); norm = normalize(norm); @@ -60,4 +50,4 @@ void main() { gl_Position = uViewProjection * worldPos; Color = aColor; -} \ No newline at end of file +} diff --git a/src/main/resources/assets/create/flywheel/shaders/oriented.vert b/src/main/resources/assets/create/flywheel/shaders/oriented.vert index d32feca3b..79d76cfe9 100644 --- a/src/main/resources/assets/create/flywheel/shaders/oriented.vert +++ b/src/main/resources/assets/create/flywheel/shaders/oriented.vert @@ -19,14 +19,6 @@ varying vec4 Color; varying float Diffuse; varying vec2 Light; -#if defined(CONTRAPTION) -varying vec3 BoxCoord; - -uniform vec3 uLightBoxSize; -uniform vec3 uLightBoxMin; -uniform mat4 uModel; -#endif - uniform float uTime; uniform mat4 uViewProjection; uniform int uDebug; @@ -37,22 +29,19 @@ uniform vec3 uCameraPos; varying float FragDistance; #endif +#ifdef CONTRAPTION +#flwinclude <"create:contraption/finalize.glsl"> +#else +#flwinclude <"create:std/finalize.glsl"> +#endif + void main() { vec4 worldPos = vec4(rotateVertexByQuat(aPos - aPivot, aRotation) + aPivot + aInstancePos, 1.); vec3 norm = rotateVertexByQuat(aNormal, aRotation); -#ifdef CONTRAPTION - worldPos = uModel * worldPos; - norm = normalize(modelToNormal(uModel) * norm); - - BoxCoord = (worldPos.xyz - uLightBoxMin) / uLightBoxSize; - #if defined(USE_FOG) - FragDistance = length(worldPos.xyz); - #endif -#elif defined(USE_FOG) - FragDistance = length(worldPos.xyz - uCameraPos); -#endif + FLWFinalizeWorldPos(worldPos); + FLWFinalizeNormal(norm); Diffuse = diffuse(norm); TexCoords = aTexCoords; @@ -60,4 +49,4 @@ void main() { gl_Position = uViewProjection * worldPos; Color = aColor; -} \ No newline at end of file +} diff --git a/src/main/resources/assets/create/flywheel/shaders/rotating.vert b/src/main/resources/assets/create/flywheel/shaders/rotating.vert index 091e16faa..e428b7049 100644 --- a/src/main/resources/assets/create/flywheel/shaders/rotating.vert +++ b/src/main/resources/assets/create/flywheel/shaders/rotating.vert @@ -21,14 +21,6 @@ varying vec4 Color; varying float Diffuse; varying vec2 Light; -#if defined(CONTRAPTION) -varying vec3 BoxCoord; - -uniform vec3 uLightBoxSize; -uniform vec3 uLightBoxMin; -uniform mat4 uModel; -#endif - uniform float uTime; uniform mat4 uViewProjection; uniform int uDebug; @@ -39,6 +31,12 @@ uniform vec3 uCameraPos; varying float FragDistance; #endif +#ifdef CONTRAPTION +#flwinclude <"create:contraption/finalize.glsl"> +#else +#flwinclude <"create:std/finalize.glsl"> +#endif + mat4 kineticRotation() { float degrees = aOffset + uTime * aSpeed * 3./10.; float angle = fract(degrees / 360.) * PI * 2.; @@ -52,17 +50,8 @@ void main() { vec3 norm = modelToNormal(kineticRotation) * aNormal; - #ifdef CONTRAPTION - worldPos = uModel * worldPos; - norm = normalize(modelToNormal(uModel) * norm); - - BoxCoord = (worldPos.xyz - uLightBoxMin) / uLightBoxSize; - #if defined(USE_FOG) - FragDistance = length(worldPos.xyz); - #endif - #elif defined(USE_FOG) - FragDistance = length(worldPos.xyz - uCameraPos); - #endif + FLWFinalizeWorldPos(worldPos); + FLWFinalizeNormal(norm); Diffuse = diffuse(norm); TexCoords = aTexCoords; @@ -84,4 +73,4 @@ void main() { Color = vec4(1.); } #endif -} \ No newline at end of file +} diff --git a/src/main/resources/assets/create/flywheel/shaders/std/finalize.glsl b/src/main/resources/assets/create/flywheel/shaders/std/finalize.glsl new file mode 100644 index 000000000..e3f3180bf --- /dev/null +++ b/src/main/resources/assets/create/flywheel/shaders/std/finalize.glsl @@ -0,0 +1,10 @@ +void FLWFinalizeWorldPos(inout vec4 worldPos) { + #if defined(USE_FOG) + + FragDistance = length(worldPos.xyz - uCameraPos); + #endif +} + +void FLWFinalizeNormal(inout vec3 normal) { + // noop +}