Vertex shader abstractions

This commit is contained in:
JozsefA 2021-05-01 23:39:36 -07:00
parent 67e75d747a
commit 2112061db0
10 changed files with 152 additions and 166 deletions

View file

@ -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<BasicProgram> {
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<TileEntity> 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<BasicProgram> 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);
}
}

View file

@ -28,7 +28,7 @@ public class GlShader extends GlObject {
} }
if (GL20.glGetShaderi(handle, GL20.GL_COMPILE_STATUS) != GL20.GL_TRUE) { 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); setHandle(handle);

View file

@ -1,84 +1,22 @@
package com.simibubi.create.foundation.render; package com.simibubi.create.foundation.render;
import java.util.ArrayList; import com.jozufozu.flywheel.backend.core.BasicInstancedTileRenderer;
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.instancing.RenderMaterial; import com.jozufozu.flywheel.backend.instancing.RenderMaterial;
import com.simibubi.create.content.contraptions.base.KineticRenderMaterials; import com.simibubi.create.content.contraptions.base.KineticRenderMaterials;
import com.simibubi.create.content.contraptions.base.RotatingModel; import com.simibubi.create.content.contraptions.base.RotatingModel;
import com.simibubi.create.content.contraptions.relays.belt.BeltInstancedModel; import com.simibubi.create.content.contraptions.relays.belt.BeltInstancedModel;
import com.simibubi.create.content.logistics.block.FlapModel; import com.simibubi.create.content.logistics.block.FlapModel;
import net.minecraft.client.renderer.ActiveRenderInfo; public class KineticRenderer extends BasicInstancedTileRenderer {
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<BasicProgram> {
public static int MAX_ORIGIN_DISTANCE = 100;
public BlockPos originCoordinate = BlockPos.ZERO;
@Override @Override
public void registerMaterials() { public void registerMaterials() {
materials.put(MaterialTypes.TRANSFORMED, super.registerMaterials();
new RenderMaterial<>(this, AllProgramSpecs.MODEL, TransformedModel::new));
materials.put(MaterialTypes.ORIENTED, new RenderMaterial<>(this, AllProgramSpecs.ORIENTED, OrientedModel::new));
materials.put(KineticRenderMaterials.BELTS, materials.put(KineticRenderMaterials.BELTS,
new RenderMaterial<>(this, AllProgramSpecs.BELT, BeltInstancedModel::new)); new RenderMaterial<>(this, AllProgramSpecs.BELT, BeltInstancedModel::new));
materials.put(KineticRenderMaterials.ROTATING, 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)); 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<TileEntity> 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<BasicProgram> 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);
}
} }

View file

@ -24,14 +24,6 @@ varying vec4 Color;
varying float Diffuse; varying float Diffuse;
varying vec2 Light; varying vec2 Light;
#if defined(CONTRAPTION)
varying vec3 BoxCoord;
uniform vec3 uLightBoxSize;
uniform vec3 uLightBoxMin;
uniform mat4 uModel;
#endif
uniform float uTime; uniform float uTime;
uniform mat4 uViewProjection; uniform mat4 uViewProjection;
uniform int uDebug; uniform int uDebug;
@ -42,6 +34,12 @@ uniform vec3 uCameraPos;
varying float FragDistance; varying float FragDistance;
#endif #endif
#ifdef CONTRAPTION
#flwinclude <"create:contraption/finalize.glsl">
#else
#flwinclude <"create:std/finalize.glsl">
#endif
void main() { void main() {
vec3 rotated = rotateVertexByQuat(aPos - .5, aInstanceRot) + aInstancePos + .5; vec3 rotated = rotateVertexByQuat(aPos - .5, aInstanceRot) + aInstancePos + .5;
@ -49,17 +47,8 @@ void main() {
vec3 norm = rotateVertexByQuat(aNormal, aInstanceRot); vec3 norm = rotateVertexByQuat(aNormal, aInstanceRot);
#ifdef CONTRAPTION FLWFinalizeWorldPos(worldPos);
worldPos = uModel * worldPos; FLWFinalizeNormal(norm);
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
float scrollSize = aScrollTexture.w - aScrollTexture.y; float scrollSize = aScrollTexture.w - aScrollTexture.y;
float scroll = fract(aSpeed * uTime / (31.5 * 16.) + aOffset) * scrollSize * aScrollMult; float scroll = fract(aSpeed * uTime / (31.5 * 16.) + aOffset) * scrollSize * aScrollMult;

View file

@ -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;
}

View file

@ -26,14 +26,6 @@ varying vec4 Color;
varying float Diffuse; varying float Diffuse;
varying vec2 Light; varying vec2 Light;
#if defined(CONTRAPTION)
varying vec3 BoxCoord;
uniform vec3 uLightBoxSize;
uniform vec3 uLightBoxMin;
uniform mat4 uModel;
#endif
uniform float uTime; uniform float uTime;
uniform mat4 uViewProjection; uniform mat4 uViewProjection;
uniform int uDebug; uniform int uDebug;
@ -44,6 +36,12 @@ uniform vec3 uCameraPos;
varying float FragDistance; varying float FragDistance;
#endif #endif
#ifdef CONTRAPTION
#flwinclude <"create:contraption/finalize.glsl">
#else
#flwinclude <"create:std/finalize.glsl">
#endif
float toRad(float degrees) { float toRad(float degrees) {
return fract(degrees / 360.) * PI * 2.; return fract(degrees / 360.) * PI * 2.;
} }
@ -73,17 +71,8 @@ void main() {
vec4 worldPos = vec4(rotated, 1.); vec4 worldPos = vec4(rotated, 1.);
vec3 norm = rotateVertexByQuat(rotateVertexByQuat(aNormal, flapRotation), orientation); vec3 norm = rotateVertexByQuat(rotateVertexByQuat(aNormal, flapRotation), orientation);
#ifdef CONTRAPTION FLWFinalizeWorldPos(worldPos);
worldPos = uModel * worldPos; FLWFinalizeNormal(norm);
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
Diffuse = diffuse(norm); Diffuse = diffuse(norm);
TexCoords = aTexCoords; TexCoords = aTexCoords;

View file

@ -17,14 +17,6 @@ varying vec4 Color;
varying float Diffuse; varying float Diffuse;
varying vec2 Light; varying vec2 Light;
#if defined(CONTRAPTION)
varying vec3 BoxCoord;
uniform vec3 uLightBoxSize;
uniform vec3 uLightBoxMin;
uniform mat4 uModel;
#endif
uniform float uTime; uniform float uTime;
uniform mat4 uViewProjection; uniform mat4 uViewProjection;
uniform int uDebug; uniform int uDebug;
@ -35,22 +27,20 @@ uniform vec3 uCameraPos;
varying float FragDistance; varying float FragDistance;
#endif #endif
#ifdef CONTRAPTION
#flwinclude <"create:contraption/finalize.glsl">
#else
#flwinclude <"create:std/finalize.glsl">
#endif
void main() { void main() {
vec4 worldPos = aTransform * vec4(aPos, 1.); vec4 worldPos = aTransform * vec4(aPos, 1.);
vec3 norm = aNormalMat * aNormal; vec3 norm = aNormalMat * aNormal;
#ifdef CONTRAPTION FLWFinalizeNormal(norm);
worldPos = uModel * worldPos; FLWFinalizeWorldPos(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
norm = normalize(norm); norm = normalize(norm);

View file

@ -19,14 +19,6 @@ varying vec4 Color;
varying float Diffuse; varying float Diffuse;
varying vec2 Light; varying vec2 Light;
#if defined(CONTRAPTION)
varying vec3 BoxCoord;
uniform vec3 uLightBoxSize;
uniform vec3 uLightBoxMin;
uniform mat4 uModel;
#endif
uniform float uTime; uniform float uTime;
uniform mat4 uViewProjection; uniform mat4 uViewProjection;
uniform int uDebug; uniform int uDebug;
@ -37,22 +29,19 @@ uniform vec3 uCameraPos;
varying float FragDistance; varying float FragDistance;
#endif #endif
#ifdef CONTRAPTION
#flwinclude <"create:contraption/finalize.glsl">
#else
#flwinclude <"create:std/finalize.glsl">
#endif
void main() { void main() {
vec4 worldPos = vec4(rotateVertexByQuat(aPos - aPivot, aRotation) + aPivot + aInstancePos, 1.); vec4 worldPos = vec4(rotateVertexByQuat(aPos - aPivot, aRotation) + aPivot + aInstancePos, 1.);
vec3 norm = rotateVertexByQuat(aNormal, aRotation); vec3 norm = rotateVertexByQuat(aNormal, aRotation);
#ifdef CONTRAPTION FLWFinalizeWorldPos(worldPos);
worldPos = uModel * worldPos; FLWFinalizeNormal(norm);
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
Diffuse = diffuse(norm); Diffuse = diffuse(norm);
TexCoords = aTexCoords; TexCoords = aTexCoords;

View file

@ -21,14 +21,6 @@ varying vec4 Color;
varying float Diffuse; varying float Diffuse;
varying vec2 Light; varying vec2 Light;
#if defined(CONTRAPTION)
varying vec3 BoxCoord;
uniform vec3 uLightBoxSize;
uniform vec3 uLightBoxMin;
uniform mat4 uModel;
#endif
uniform float uTime; uniform float uTime;
uniform mat4 uViewProjection; uniform mat4 uViewProjection;
uniform int uDebug; uniform int uDebug;
@ -39,6 +31,12 @@ uniform vec3 uCameraPos;
varying float FragDistance; varying float FragDistance;
#endif #endif
#ifdef CONTRAPTION
#flwinclude <"create:contraption/finalize.glsl">
#else
#flwinclude <"create:std/finalize.glsl">
#endif
mat4 kineticRotation() { mat4 kineticRotation() {
float degrees = aOffset + uTime * aSpeed * 3./10.; float degrees = aOffset + uTime * aSpeed * 3./10.;
float angle = fract(degrees / 360.) * PI * 2.; float angle = fract(degrees / 360.) * PI * 2.;
@ -52,17 +50,8 @@ void main() {
vec3 norm = modelToNormal(kineticRotation) * aNormal; vec3 norm = modelToNormal(kineticRotation) * aNormal;
#ifdef CONTRAPTION FLWFinalizeWorldPos(worldPos);
worldPos = uModel * worldPos; FLWFinalizeNormal(norm);
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
Diffuse = diffuse(norm); Diffuse = diffuse(norm);
TexCoords = aTexCoords; TexCoords = aTexCoords;

View file

@ -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
}