Shader sanity

- Keeping up with Flywheel changes
This commit is contained in:
Jozufozu 2021-12-28 14:10:39 -08:00
parent 33d0f8c352
commit 2b6348855a
11 changed files with 46 additions and 137 deletions

View file

@ -7,6 +7,8 @@ import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.backend.RenderLayer;
import com.jozufozu.flywheel.backend.gl.GlStateTracker;
import com.jozufozu.flywheel.backend.gl.GlTextureUnit;
import com.jozufozu.flywheel.backend.gl.GlVertexArray;
import com.jozufozu.flywheel.core.Formats;
import com.jozufozu.flywheel.core.virtual.VirtualRenderWorld;
import com.jozufozu.flywheel.event.RenderLayerEvent;
import com.jozufozu.flywheel.util.Textures;
@ -47,7 +49,7 @@ public class FlwContraptionManager extends ContraptionRenderingWorld<FlwContrapt
Textures.bindActiveTextures();
ContraptionProgram structureShader = CreateContexts.STRUCTURE.getProgram(AllProgramSpecs.STRUCTURE);
ContraptionProgram structureShader = CreateContexts.STRUCTURE.getProgram(AllProgramSpecs.PASSTHRU, Formats.BLOCK);
structureShader.bind();
structureShader.uploadViewProjection(event.viewProjection);

View file

@ -6,11 +6,11 @@ import net.minecraft.resources.ResourceLocation;
public class AllProgramSpecs {
public static final ResourceLocation PASSTHRU = asResource("passthru");
public static final ResourceLocation ROTATING = asResource("rotating");
public static final ResourceLocation CHROMATIC = asResource("chromatic");
public static final ResourceLocation BELT = asResource("belt");
public static final ResourceLocation FLAPS = asResource("flap");
public static final ResourceLocation STRUCTURE = asResource("contraption_structure");
public static final ResourceLocation ACTOR = asResource("contraption_actor");
}

View file

@ -4,12 +4,11 @@ import java.util.stream.Stream;
import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.backend.GameStateRegistry;
import com.jozufozu.flywheel.backend.pipeline.InstancingTemplate;
import com.jozufozu.flywheel.backend.pipeline.OneShotTemplate;
import com.jozufozu.flywheel.backend.pipeline.ShaderPipeline;
import com.jozufozu.flywheel.backend.pipeline.WorldShaderPipeline;
import com.jozufozu.flywheel.backend.source.FileResolution;
import com.jozufozu.flywheel.backend.source.Resolver;
import com.jozufozu.flywheel.core.Templates;
import com.jozufozu.flywheel.core.WorldContext;
import com.jozufozu.flywheel.event.GatherContextEvent;
import com.jozufozu.flywheel.util.ResourceUtil;
@ -33,14 +32,14 @@ public class CreateContexts {
GameStateRegistry.register(RainbowDebugStateProvider.INSTANCE);
FileResolution header = Resolver.INSTANCE.findShader(ResourceUtil.subPath(CONTRAPTION, ".glsl"));
ShaderPipeline<ContraptionProgram> instancing = new WorldShaderPipeline<>(ContraptionProgram::new, InstancingTemplate.INSTANCE, header);
ShaderPipeline<ContraptionProgram> structure = new WorldShaderPipeline<>(ContraptionProgram::new, OneShotTemplate.INSTANCE, header);
ShaderPipeline<ContraptionProgram> instancing = new WorldShaderPipeline<>(ContraptionProgram::new, Templates.INSTANCING, header);
ShaderPipeline<ContraptionProgram> structure = new WorldShaderPipeline<>(ContraptionProgram::new, Templates.ONE_SHOT, header);
CWORLD = backend.register(WorldContext.builder(backend, CONTRAPTION)
.build(instancing));
STRUCTURE = backend.register(WorldContext.builder(backend, CONTRAPTION)
.setSpecStream(() -> Stream.of(AllProgramSpecs.STRUCTURE))
.setSpecStream(() -> Stream.of(AllProgramSpecs.PASSTHRU))
.build(structure));
}

View file

@ -1,5 +1,5 @@
{
"source": "create:contraption_structure.vert",
"source": "create:passthru.vert",
"states": [
{
"when": {

View file

@ -2,7 +2,6 @@
#use "flywheel:core/quaternion.glsl"
#use "flywheel:core/matutils.glsl"
#use "flywheel:core/diffuse.glsl"
struct Belt {
vec2 light;
@ -16,36 +15,22 @@ struct Belt {
float scrollMult;
};
#use "flywheel:data/modelvertex.glsl"
#use "flywheel:block.frag"
#if defined(VERTEX_SHADER)
BlockFrag vertex(Vertex v, Belt instance) {
vec3 rotated = rotateVertexByQuat(v.pos - .5, instance.rotation) + instance.pos + .5;
void vertex(inout Vertex v, Belt instance) {
v.pos = rotateVertexByQuat(v.pos - .5, instance.rotation) + instance.pos + .5;
vec4 worldPos = vec4(rotated, 1.);
vec3 norm = rotateVertexByQuat(v.normal, instance.rotation);
FLWFinalizeWorldPos(worldPos);
FLWFinalizeNormal(norm);
v.normal = rotateVertexByQuat(v.normal, instance.rotation);
float scrollSize = instance.scrollTexture.w - instance.scrollTexture.y;
float scroll = fract(instance.speed * uTime / (31.5 * 16.) + instance.offset) * scrollSize * instance.scrollMult;
BlockFrag b;
b.diffuse = diffuse(norm);
b.texCoords = v.texCoords - instance.sourceTexture + instance.scrollTexture.xy + vec2(0, scroll);
b.light = instance.light;
v.texCoords = v.texCoords - instance.sourceTexture + instance.scrollTexture.xy + vec2(0, scroll);
v.light = instance.light;
#if defined(DEBUG_RAINBOW)
b.color = instance.color;
#elif defined(DEBUG_NORMAL)
b.color = vec4(norm, 1.);
#else
b.color = vec4(1.);
v.color = instance.color;
#endif
return b;
}
#endif

View file

@ -1,4 +1,5 @@
#use "flywheel:context/fog.glsl"
#use "flywheel:core/diffuse.glsl"
uniform sampler3D uLightVolume;
@ -13,26 +14,25 @@ uniform float uTime;
uniform mat4 uViewProjection;
uniform vec3 uCameraPos;
void FLWFinalizeNormal(inout vec3 normal) {
mat3 m;
m[0] = uModel[0].xyz;
m[1] = uModel[1].xyz;
m[2] = uModel[2].xyz;
normal = m * normal;
}
#if defined(VERTEX_SHADER)
out vec3 BoxCoord;
void FLWFinalizeWorldPos(inout vec4 worldPos) {
worldPos = uModel * worldPos;
vec4 FLWVertex(inout Vertex v) {
vec4 worldPos = uModel * vec4(v.pos, 1.);
BoxCoord = (worldPos.xyz - uLightBoxMin) / uLightBoxSize;
FragDistance = max(length(worldPos.xz), abs(worldPos.y)); // cylindrical fog
gl_Position = uViewProjection * worldPos;
mat3 m;
m[0] = uModel[0].xyz;
m[1] = uModel[1].xyz;
m[2] = uModel[2].xyz;
v.normal = m * v.normal;
v.pos = worldPos.xyz;
return uViewProjection * worldPos;
}
#elif defined(FRAGMENT_SHADER)

View file

@ -2,7 +2,6 @@
#use "flywheel:core/matutils.glsl"
#use "flywheel:core/quaternion.glsl"
#use "flywheel:core/diffuse.glsl"
struct Actor {
vec3 pos;
@ -14,34 +13,18 @@ struct Actor {
float speed;
};
#use "flywheel:data/modelvertex.glsl"
#use "flywheel:block.frag"
#if defined(VERTEX_SHADER)
BlockFrag vertex(Vertex v, Actor instance) {
void vertex(inout Vertex v, Actor instance) {
float degrees = instance.offset + uTime * instance.speed / 20.;
//float angle = fract(degrees / 360.) * PI * 2.;
vec4 kineticRot = quat(instance.axis, degrees);
vec3 rotated = rotateVertexByQuat(v.pos - instance.rotationCenter, kineticRot) + instance.rotationCenter;
vec4 worldPos = vec4(rotateVertexByQuat(rotated - .5, instance.rotation) + instance.pos + .5, 1.);
vec3 norm = rotateVertexByQuat(rotateVertexByQuat(v.normal, kineticRot), instance.rotation);
FLWFinalizeWorldPos(worldPos);
FLWFinalizeNormal(norm);
BlockFrag b;
b.diffuse = diffuse(norm);
b.texCoords = v.texCoords;
b.light = instance.light;
#if defined(DEBUG_NORMAL)
b.color = vec4(norm, 1.);
#else
b.color = vec4(1.);
#endif
return b;
v.pos = rotateVertexByQuat(rotated - .5, instance.rotation) + instance.pos + .5;
v.normal = rotateVertexByQuat(rotateVertexByQuat(v.normal, kineticRot), instance.rotation);
v.light = instance.light;
}
#endif

View file

@ -1,37 +0,0 @@
#define PI 3.1415926538
#use "flywheel:core/matutils.glsl"
#use "flywheel:core/diffuse.glsl"
struct Vertex {
vec3 pos;
vec4 color;
vec2 texCoords;
vec2 modelLight;
vec3 normal;
};
#use "flywheel:block.frag"
#if defined(VERTEX_SHADER)
BlockFrag vertex(Vertex v) {
vec4 worldPos = vec4(v.pos, 1.);
vec3 norm = v.normal;
FLWFinalizeWorldPos(worldPos);
FLWFinalizeNormal(norm);
BlockFrag b;
b.diffuse = diffuse(norm);
b.texCoords = v.texCoords;
b.light = v.modelLight;
#if defined(DEBUG_NORMAL)
b.color = vec4(norm, 1.);
#else
b.color = v.color;
#endif
return b;
}
#endif

View file

@ -2,7 +2,6 @@
#use "flywheel:core/matutils.glsl"
#use "flywheel:core/quaternion.glsl"
#use "flywheel:core/diffuse.glsl"
struct Flap {
vec3 instancePos;
@ -15,7 +14,6 @@ struct Flap {
float flapness;
};
#use "flywheel:data/modelvertex.glsl"
#use "flywheel:block.frag"
#if defined(VERTEX_SHADER)
@ -37,7 +35,7 @@ float getFlapAngle(float flapness, float intensity, float scale) {
return degrees;
}
BlockFrag vertex(Vertex v, Flap flap) {
void vertex(inout Vertex v, Flap flap) {
float flapAngle = getFlapAngle(flap.flapness, flap.intensity, flap.flapScale);
vec4 orientation = quat(vec3(0., 1., 0.), -flap.horizontalAngle);
@ -46,21 +44,8 @@ BlockFrag vertex(Vertex v, Flap flap) {
vec3 rotated = rotateVertexByQuat(v.pos - flap.pivot, flapRotation) + flap.pivot + flap.segmentOffset;
rotated = rotateVertexByQuat(rotated - .5, orientation) + flap.instancePos + .5;
vec4 worldPos = vec4(rotated, 1.);
vec3 norm = rotateVertexByQuat(rotateVertexByQuat(v.normal, flapRotation), orientation);
FLWFinalizeWorldPos(worldPos);
FLWFinalizeNormal(norm);
BlockFrag b;
b.diffuse = diffuse(norm);
b.texCoords = v.texCoords;
b.light = flap.light;
#if defined(DEBUG_NORMAL)
b.color = vec4(norm, 1.);
#else
b.color = vec4(1.);
#endif
return b;
v.pos = rotated;
v.normal = rotateVertexByQuat(rotateVertexByQuat(v.normal, flapRotation), orientation);
v.light = flap.light;
}
#endif

View file

@ -0,0 +1,7 @@
#use "flywheel:block.frag"
#if defined(VERTEX_SHADER)
void vertex(inout Vertex v) {
}
#endif

View file

@ -1,7 +1,6 @@
#define PI 3.1415926538
#use "flywheel:core/matutils.glsl"
#use "flywheel:core/diffuse.glsl"
struct Rotating {
vec2 light;
@ -12,7 +11,6 @@ struct Rotating {
vec3 axis;
};
#use "flywheel:data/modelvertex.glsl"
#use "flywheel:block.frag"
mat4 kineticRotation(float offset, float speed, vec3 axis) {
@ -23,30 +21,17 @@ mat4 kineticRotation(float offset, float speed, vec3 axis) {
}
#if defined(VERTEX_SHADER)
BlockFrag vertex(Vertex v, Rotating instance) {
void vertex(inout Vertex v, Rotating instance) {
mat4 spin = kineticRotation(instance.offset, instance.speed, instance.axis);
vec4 worldPos = spin * vec4(v.pos - .5, 1.);
worldPos += vec4(instance.pos + .5, 0.);
v.pos = worldPos.xyz + instance.pos + .5;
vec3 norm = modelToNormal(spin) * v.normal;
FLWFinalizeWorldPos(worldPos);
FLWFinalizeNormal(norm);
BlockFrag b;
b.diffuse = diffuse(norm);
b.texCoords = v.texCoords;
b.light = instance.light;
v.normal = modelToNormal(spin) * v.normal;
v.light = instance.light;
#if defined(DEBUG_RAINBOW)
b.color = instance.color;
#elif defined(DEBUG_NORMAL)
b.color = vec4(norm, 1.);
#else
b.color = vec4(1.);
v.color = instance.color;
#endif
return b;
}
#endif