mirror of
https://github.com/Jozufozu/Flywheel.git
synced 2024-12-29 08:26:37 +01:00
Vertex shader abstractions
This commit is contained in:
parent
67e75d747a
commit
2112061db0
10 changed files with 152 additions and 166 deletions
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
|
|
@ -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<BasicProgram> {
|
||||
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<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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
Loading…
Reference in a new issue