From 4c7b035f5d8bbc22637eaa5a49e454ee9bc1d8c4 Mon Sep 17 00:00:00 2001 From: PepperCode1 <44146161+PepperCode1@users.noreply.github.com> Date: Fri, 13 May 2022 23:58:24 -0700 Subject: [PATCH] Instant legacy code - Respect fog shape and type - Add uFogShape uniform - Add macro for current FogType - Respect dimension constant ambient light when calculating diffuse - Add uConstantAmbientLight uniform - Redirect diffuse calculation to FLWDiffuse - Do not automatically include diffuse.glsl - Fill CoreShaderInfoMap with info for vanilla and Forge core shaders - Use try-with-resources when querying ResourceManager - Move WorldProgram uniform uploading from bind to uploadUniforms - Use correct layer when rendering bells (cutout -> solid) - Update Starlight --- build.gradle | 3 +- gradle.properties | 2 +- .../com/jozufozu/flywheel/backend/Loader.java | 4 +- .../flywheel/backend/gl/shader/GlShader.java | 4 +- .../instancing/InstancingEngine.java | 48 +++++++--- .../com/jozufozu/flywheel/core/Contexts.java | 2 +- .../flywheel/core/CoreShaderInfoMap.java | 95 +++++++++++++++++++ .../flywheel/core/RenderTypeRegistry.java | 19 ---- .../com/jozufozu/flywheel/core/Templates.java | 6 -- .../core/compile/FragmentCompiler.java | 40 ++++---- .../core/compile/FragmentTemplateData.java | 1 - .../core/compile/InstancingTemplateData.java | 3 +- .../core/compile/OneShotTemplateData.java | 2 +- .../core/compile/ProgramCompiler.java | 2 +- .../flywheel/core/compile/ProgramContext.java | 16 ++-- .../flywheel/core/compile/VertexCompiler.java | 2 - .../core/crumbling/CrumblingRenderer.java | 7 +- .../flywheel/core/shader/WorldFog.java | 9 +- .../flywheel/core/shader/WorldProgram.java | 50 ++++++---- .../flywheel/core/source/ShaderSources.java | 6 +- .../flywheel/vanilla/BellInstance.java | 4 +- .../flywheel/shaders/context/crumbling.glsl | 41 +++++--- .../shaders/{core => context}/diffuse.glsl | 6 +- .../flywheel/shaders/context/fog.glsl | 55 ++++++++--- .../flywheel/shaders/context/world.glsl | 42 +++++--- .../flywheel/shaders/core/matutils.glsl | 1 - .../flywheel/shaders/core/quaternion.glsl | 1 - 27 files changed, 319 insertions(+), 152 deletions(-) create mode 100644 src/main/java/com/jozufozu/flywheel/core/CoreShaderInfoMap.java delete mode 100644 src/main/java/com/jozufozu/flywheel/core/RenderTypeRegistry.java rename src/main/resources/assets/flywheel/flywheel/shaders/{core => context}/diffuse.glsl (52%) diff --git a/build.gradle b/build.gradle index 928cd432d..d65d4f49d 100644 --- a/build.gradle +++ b/build.gradle @@ -36,7 +36,6 @@ println('Java: ' + System.getProperty('java.version') + ' JVM: ' + System.getPro minecraft { mappings channel: 'parchment', version: "${parchment_version}-${minecraft_version}" - runs { client { workingDirectory project.file('run') @@ -107,7 +106,7 @@ repositories { dependencies { minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}" - compileOnly fg.deobf("curse.maven:starlight-526854:3599856") + compileOnly fg.deobf("curse.maven:starlight-526854:3706539") // https://discord.com/channels/313125603924639766/725850371834118214/910619168821354497 // Prevent Mixin annotation processor from getting into IntelliJ's annotation processor settings diff --git a/gradle.properties b/gradle.properties index 941a2c4c3..e527cf99e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,7 +5,7 @@ org.gradle.daemon = false mod_version = 0.7.0 mc_update_version = 1.18 minecraft_version = 1.18.2 -forge_version = 40.0.15 +forge_version = 40.1.0 # build dependency versions forgegradle_version = 5.1.+ diff --git a/src/main/java/com/jozufozu/flywheel/backend/Loader.java b/src/main/java/com/jozufozu/flywheel/backend/Loader.java index f87a1982c..0986282c9 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/Loader.java +++ b/src/main/java/com/jozufozu/flywheel/backend/Loader.java @@ -96,9 +96,7 @@ public class Loader implements ResourceManagerReloadListener { Collection programSpecs = manager.listResources(PROGRAM_DIR, s -> s.endsWith(".json")); for (ResourceLocation location : programSpecs) { - try { - Resource file = manager.getResource(location); - + try (Resource file = manager.getResource(location)) { String s = StringUtil.readToString(file.getInputStream()); ResourceLocation specName = ResourceUtil.trim(location, PROGRAM_DIR, ".json"); 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 7148e134e..de6a7f22f 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 @@ -21,8 +21,8 @@ public class GlShader extends GlObject { GlCompat.safeShaderSource(handle, source); GL20.glCompileShader(handle); - String log = GL20.glGetShaderInfoLog(handle); - +// String log = GL20.glGetShaderInfoLog(handle); +// // if (!log.isEmpty()) { // env.printShaderInfoLog(source, log, this.name); // } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancingEngine.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancingEngine.java index 9f2e66236..5bcfa88ab 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancingEngine.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancingEngine.java @@ -16,16 +16,20 @@ import com.jozufozu.flywheel.backend.instancing.TaskEngine; import com.jozufozu.flywheel.backend.model.MeshPool; import com.jozufozu.flywheel.core.Formats; import com.jozufozu.flywheel.core.RenderContext; -import com.jozufozu.flywheel.core.RenderTypeRegistry; +import com.jozufozu.flywheel.core.CoreShaderInfoMap; +import com.jozufozu.flywheel.core.CoreShaderInfoMap.CoreShaderInfo; import com.jozufozu.flywheel.core.compile.ProgramCompiler; import com.jozufozu.flywheel.core.compile.ProgramContext; import com.jozufozu.flywheel.core.shader.WorldProgram; import com.jozufozu.flywheel.util.Textures; import com.jozufozu.flywheel.util.WeakHashSet; +import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.math.Matrix4f; import net.minecraft.client.Camera; +import net.minecraft.client.multiplayer.ClientLevel; import net.minecraft.client.renderer.RenderType; +import net.minecraft.client.renderer.ShaderInstance; import net.minecraft.core.BlockPos; import net.minecraft.core.Vec3i; import net.minecraft.resources.ResourceLocation; @@ -73,10 +77,10 @@ public class InstancingEngine

implements Engine { // don't want to mutate viewProjection var vp = context.viewProjection().copy(); - vp.multiply(Matrix4f.createTranslateMatrix((float) -camX, (float) -camY, (float) -camZ)); + vp.multiplyWithTranslation((float) -camX, (float) -camY, (float) -camZ); for (RenderType renderType : toRender) { - render(renderType, camX, camY, camZ, vp); + render(renderType, camX, camY, camZ, vp, context.level()); } toRender.clear(); } @@ -90,19 +94,20 @@ public class InstancingEngine

implements Engine { // don't want to mutate viewProjection var vp = context.viewProjection().copy(); - vp.multiply(Matrix4f.createTranslateMatrix((float) -camX, (float) -camY, (float) -camZ)); + vp.multiplyWithTranslation((float) -camX, (float) -camY, (float) -camZ); if (toRender.remove(type)) { - render(type, camX, camY, camZ, vp); + render(type, camX, camY, camZ, vp, context.level()); } } - protected void render(RenderType type, double camX, double camY, double camZ, Matrix4f viewProjection) { + protected void render(RenderType type, double camX, double camY, double camZ, Matrix4f viewProjection, ClientLevel level) { vertexCount = 0; instanceCount = 0; type.setupRenderState(); Textures.bindActiveTextures(); + CoreShaderInfo coreShaderInfo = getCoreShaderInfo(); for (Map.Entry, InstancedMaterial> entry : materials.entrySet()) { InstancedMaterial material = entry.getValue(); @@ -113,7 +118,7 @@ public class InstancingEngine

implements Engine { if (!toRender.isEmpty()) { Instanced instanceType = entry.getKey(); - setup(type, camX, camY, camZ, viewProjection, instanceType.getProgramSpec()); + setup(instanceType.getProgramSpec(), coreShaderInfo, camX, camY, camZ, viewProjection, level); instanceCount += material.getInstanceCount(); vertexCount += material.getVertexCount(); @@ -125,12 +130,33 @@ public class InstancingEngine

implements Engine { type.clearRenderState(); } - protected P setup(RenderType layer, double camX, double camY, double camZ, Matrix4f viewProjection, ResourceLocation programSpec) { - P program = context.getProgram(ProgramContext.create(programSpec, Formats.POS_TEX_NORMAL, RenderTypeRegistry.getAlphaDiscard(layer))); + protected CoreShaderInfo getCoreShaderInfo() { + CoreShaderInfo coreShaderInfo; + ShaderInstance coreShader = RenderSystem.getShader(); + if (coreShader != null) { + String coreShaderName = coreShader.getName(); + coreShaderInfo = CoreShaderInfoMap.getInfo(coreShaderName); + } else { + coreShaderInfo = null; + } + if (coreShaderInfo == null) { + coreShaderInfo = CoreShaderInfo.DEFAULT; + } + return coreShaderInfo; + } + + protected P setup(ResourceLocation programSpec, CoreShaderInfo coreShaderInfo, double camX, double camY, double camZ, Matrix4f viewProjection, ClientLevel level) { + float alphaDiscard = coreShaderInfo.alphaDiscard(); + if (alphaDiscard == 0) { + alphaDiscard = 0.0001f; + } else if (alphaDiscard < 0) { + alphaDiscard = 0; + } + + P program = context.getProgram(ProgramContext.create(programSpec, Formats.POS_TEX_NORMAL, alphaDiscard, coreShaderInfo.fogType())); program.bind(); - program.uploadViewProjection(viewProjection); - program.uploadCameraPos(camX, camY, camZ); + program.uploadUniforms(camX, camY, camZ, viewProjection, level); return program; } diff --git a/src/main/java/com/jozufozu/flywheel/core/Contexts.java b/src/main/java/com/jozufozu/flywheel/core/Contexts.java index 921f8a3dd..215d6a108 100644 --- a/src/main/java/com/jozufozu/flywheel/core/Contexts.java +++ b/src/main/java/com/jozufozu/flywheel/core/Contexts.java @@ -28,7 +28,7 @@ public class Contexts { } public static class Names { - public static final ResourceLocation CRUMBLING = Flywheel.rl("context/crumbling"); public static final ResourceLocation WORLD = Flywheel.rl("context/world"); + public static final ResourceLocation CRUMBLING = Flywheel.rl("context/crumbling"); } } diff --git a/src/main/java/com/jozufozu/flywheel/core/CoreShaderInfoMap.java b/src/main/java/com/jozufozu/flywheel/core/CoreShaderInfoMap.java new file mode 100644 index 000000000..25c187c4f --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/core/CoreShaderInfoMap.java @@ -0,0 +1,95 @@ +package com.jozufozu.flywheel.core; + +import static com.jozufozu.flywheel.core.CoreShaderInfoMap.CoreShaderInfo.FogType.COLOR_FOG; +import static com.jozufozu.flywheel.core.CoreShaderInfoMap.CoreShaderInfo.FogType.FADE_FOG; +import static com.jozufozu.flywheel.core.CoreShaderInfoMap.CoreShaderInfo.FogType.NO_FOG; + +import java.util.HashMap; +import java.util.Map; + +import org.jetbrains.annotations.Nullable; + +import com.jozufozu.flywheel.backend.OptifineHandler; + +public class CoreShaderInfoMap { + private static final Map MAP = new HashMap<>(); + + static { + registerInfo("block", new CoreShaderInfo(-1, false, NO_FOG)); + registerInfo("new_entity", new CoreShaderInfo(-1, false, NO_FOG)); + registerInfo("particle", new CoreShaderInfo(0.1f, false, COLOR_FOG)); + registerInfo("position", new CoreShaderInfo(-1, false, COLOR_FOG)); + registerInfo("position_color", new CoreShaderInfo(0, false, NO_FOG)); + registerInfo("position_color_lightmap", new CoreShaderInfo(-1, false, NO_FOG)); + registerInfo("position_color_tex", new CoreShaderInfo(0.1f, false, NO_FOG)); + registerInfo("position_color_tex_lightmap", new CoreShaderInfo(0.1f, false, NO_FOG)); + registerInfo("position_tex", new CoreShaderInfo(0, false, NO_FOG)); + registerInfo("position_tex_color", new CoreShaderInfo(0.1f, false, NO_FOG)); + registerInfo("position_tex_color_normal", new CoreShaderInfo(0.1f, false, COLOR_FOG)); + registerInfo("position_tex_lightmap_color", new CoreShaderInfo(0.1f, false, NO_FOG)); + registerInfo("rendertype_solid", new CoreShaderInfo(-1, false, COLOR_FOG)); + registerInfo("rendertype_cutout_mipped", new CoreShaderInfo(OptifineHandler.isOptifineInstalled() ? 0.1f : 0.5f, false, COLOR_FOG)); + registerInfo("rendertype_cutout", new CoreShaderInfo(0.1f, false, COLOR_FOG)); + registerInfo("rendertype_translucent", new CoreShaderInfo(-1, false, COLOR_FOG)); + registerInfo("rendertype_translucent_moving_block", new CoreShaderInfo(-1, false, NO_FOG)); + registerInfo("rendertype_translucent_no_crumbling", new CoreShaderInfo(-1, false, NO_FOG)); + registerInfo("rendertype_armor_cutout_no_cull", new CoreShaderInfo(0.1f, true, COLOR_FOG)); + registerInfo("rendertype_entity_solid", new CoreShaderInfo(-1, true, COLOR_FOG)); + registerInfo("rendertype_entity_cutout", new CoreShaderInfo(0.1f, true, COLOR_FOG)); + registerInfo("rendertype_entity_cutout_no_cull", new CoreShaderInfo(0.1f, true, COLOR_FOG)); + registerInfo("rendertype_entity_cutout_no_cull_z_offset", new CoreShaderInfo(0.1f, true, COLOR_FOG)); + registerInfo("rendertype_item_entity_translucent_cull", new CoreShaderInfo(0.1f, true, COLOR_FOG)); + registerInfo("rendertype_entity_translucent_cull", new CoreShaderInfo(0.1f, true, COLOR_FOG)); + registerInfo("rendertype_entity_translucent", new CoreShaderInfo(0.1f, true, COLOR_FOG)); + registerInfo("rendertype_entity_smooth_cutout", new CoreShaderInfo(0.1f, true, COLOR_FOG)); + registerInfo("rendertype_beacon_beam", new CoreShaderInfo(-1, false, COLOR_FOG)); + registerInfo("rendertype_entity_decal", new CoreShaderInfo(0.1f, true, COLOR_FOG)); + registerInfo("rendertype_entity_no_outline", new CoreShaderInfo(-1, true, COLOR_FOG)); + registerInfo("rendertype_entity_shadow", new CoreShaderInfo(-1, false, COLOR_FOG)); + // Special alpha discard + registerInfo("rendertype_entity_alpha", new CoreShaderInfo(-1, false, NO_FOG)); + registerInfo("rendertype_eyes", new CoreShaderInfo(-1, false, FADE_FOG)); + registerInfo("rendertype_energy_swirl", new CoreShaderInfo(0.1f, false, FADE_FOG)); + registerInfo("rendertype_leash", new CoreShaderInfo(-1, false, COLOR_FOG)); + registerInfo("rendertype_water_mask", new CoreShaderInfo(-1, false, NO_FOG)); + registerInfo("rendertype_outline", new CoreShaderInfo(0, false, NO_FOG)); + registerInfo("rendertype_armor_glint", new CoreShaderInfo(0.1f, false, FADE_FOG)); + registerInfo("rendertype_armor_entity_glint", new CoreShaderInfo(0.1f, false, FADE_FOG)); + registerInfo("rendertype_glint_translucent", new CoreShaderInfo(0.1f, false, FADE_FOG)); + registerInfo("rendertype_glint", new CoreShaderInfo(0.1f, false, FADE_FOG)); + registerInfo("rendertype_glint_direct", new CoreShaderInfo(0.1f, false, FADE_FOG)); + registerInfo("rendertype_entity_glint", new CoreShaderInfo(0.1f, false, FADE_FOG)); + registerInfo("rendertype_entity_glint_direct", new CoreShaderInfo(0.1f, false, FADE_FOG)); + registerInfo("rendertype_text", new CoreShaderInfo(0.1f, false, COLOR_FOG)); + registerInfo("rendertype_text_intensity", new CoreShaderInfo(0.1f, false, COLOR_FOG)); + registerInfo("rendertype_text_see_through", new CoreShaderInfo(0.1f, false, NO_FOG)); + registerInfo("rendertype_text_intensity_see_through", new CoreShaderInfo(0.1f, false, NO_FOG)); + registerInfo("rendertype_lightning", new CoreShaderInfo(-1, false, FADE_FOG)); + registerInfo("rendertype_tripwire", new CoreShaderInfo(0.1f, false, COLOR_FOG)); + registerInfo("rendertype_end_portal", new CoreShaderInfo(-1, false, NO_FOG)); + registerInfo("rendertype_end_gateway", new CoreShaderInfo(-1, false, NO_FOG)); + registerInfo("rendertype_lines", new CoreShaderInfo(-1, false, COLOR_FOG)); + registerInfo("rendertype_crumbling", new CoreShaderInfo(0.1f, false, NO_FOG)); + + registerInfo("forge:rendertype_entity_unlit_translucent", new CoreShaderInfo(0.1f, false, COLOR_FOG)); + } + + public static void registerInfo(String name, CoreShaderInfo info) { + MAP.put(name, info); + } + + @Nullable + public static CoreShaderInfo getInfo(String name) { + return MAP.get(name); + } + + public record CoreShaderInfo(float alphaDiscard, boolean appliesDiffuse, FogType fogType) { + public static final CoreShaderInfo DEFAULT = new CoreShaderInfo(-1, false, NO_FOG); + + public enum FogType { + NO_FOG, + COLOR_FOG, + FADE_FOG; + } + } +} diff --git a/src/main/java/com/jozufozu/flywheel/core/RenderTypeRegistry.java b/src/main/java/com/jozufozu/flywheel/core/RenderTypeRegistry.java deleted file mode 100644 index 6f2cda148..000000000 --- a/src/main/java/com/jozufozu/flywheel/core/RenderTypeRegistry.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.jozufozu.flywheel.core; - -import org.jetbrains.annotations.Nullable; - -import net.minecraft.client.renderer.RenderType; - -public class RenderTypeRegistry { - - - /** - * Gets the alpha discard threshold for the given render layer. - * - * @param layer The render layer to get the alpha discard threshold for. - * @return The alpha discard threshold. - */ - public static float getAlphaDiscard(@Nullable RenderType layer) { - return layer == RenderType.cutoutMipped() ? 0.1f : 0f; - } -} diff --git a/src/main/java/com/jozufozu/flywheel/core/Templates.java b/src/main/java/com/jozufozu/flywheel/core/Templates.java index 30987940b..cb9e83610 100644 --- a/src/main/java/com/jozufozu/flywheel/core/Templates.java +++ b/src/main/java/com/jozufozu/flywheel/core/Templates.java @@ -1,18 +1,12 @@ package com.jozufozu.flywheel.core; -import com.jozufozu.flywheel.Flywheel; import com.jozufozu.flywheel.backend.gl.GLSLVersion; import com.jozufozu.flywheel.core.compile.FragmentTemplateData; import com.jozufozu.flywheel.core.compile.InstancingTemplateData; import com.jozufozu.flywheel.core.compile.OneShotTemplateData; import com.jozufozu.flywheel.core.compile.Template; -import com.jozufozu.flywheel.core.source.FileResolution; -import com.jozufozu.flywheel.core.source.Resolver; public class Templates { - - public static final FileResolution DIFFUSE_FILE = Resolver.INSTANCE.get(Flywheel.rl("core/diffuse.glsl")); - public static final Template INSTANCING = new Template<>(GLSLVersion.V330, InstancingTemplateData::new); public static final Template ONE_SHOT = new Template<>(GLSLVersion.V150, OneShotTemplateData::new); public static final Template FRAGMENT = new Template<>(GLSLVersion.V150, FragmentTemplateData::new); diff --git a/src/main/java/com/jozufozu/flywheel/core/compile/FragmentCompiler.java b/src/main/java/com/jozufozu/flywheel/core/compile/FragmentCompiler.java index e5600cc66..6dccf21da 100644 --- a/src/main/java/com/jozufozu/flywheel/core/compile/FragmentCompiler.java +++ b/src/main/java/com/jozufozu/flywheel/core/compile/FragmentCompiler.java @@ -4,6 +4,7 @@ import java.util.Objects; import com.jozufozu.flywheel.backend.gl.shader.GlShader; import com.jozufozu.flywheel.backend.gl.shader.ShaderType; +import com.jozufozu.flywheel.core.CoreShaderInfoMap.CoreShaderInfo.FogType; import com.jozufozu.flywheel.core.shader.ShaderConstants; import com.jozufozu.flywheel.core.shader.StateSnapshot; import com.jozufozu.flywheel.core.source.FileIndexImpl; @@ -11,33 +12,31 @@ import com.jozufozu.flywheel.core.source.FileResolution; import com.jozufozu.flywheel.core.source.SourceFile; public class FragmentCompiler extends Memoizer { + private final Template template; private final FileResolution header; - private final Template fragment; - public FragmentCompiler(Template fragment, FileResolution header) { + public FragmentCompiler(Template template, FileResolution header) { this.header = header; - this.fragment = fragment; + this.template = template; } @Override protected GlShader _create(Context key) { - SourceFile fragmentFile = key.file; - FragmentTemplateData appliedTemplate = fragment.apply(fragmentFile); + StringBuilder finalSource = new StringBuilder(); - StringBuilder builder = new StringBuilder(); + finalSource.append(CompileUtil.generateHeader(template.getVersion(), ShaderType.FRAGMENT)); - builder.append(CompileUtil.generateHeader(fragment.getVersion(), ShaderType.FRAGMENT)); - - key.getShaderConstants().writeInto(builder); + key.getShaderConstants().writeInto(finalSource); FileIndexImpl index = new FileIndexImpl(); - header.getFile().generateFinalSource(index, builder); - fragmentFile.generateFinalSource(index, builder); + header.getFile().generateFinalSource(index, finalSource); + key.file.generateFinalSource(index, finalSource); - builder.append(appliedTemplate.generateFooter()); + FragmentData appliedTemplate = template.apply(key.file); + finalSource.append(appliedTemplate.generateFooter()); - return new GlShader(fragmentFile.name, ShaderType.FRAGMENT, builder.toString()); + return new GlShader(key.file.name, ShaderType.FRAGMENT, finalSource.toString()); } @Override @@ -64,10 +63,16 @@ public class FragmentCompiler extends Memoizer 0) { shaderConstants.define("ALPHA_DISCARD", alphaDiscard); } + shaderConstants.define(fogType.name()); return shaderConstants; } @@ -85,17 +91,17 @@ public class FragmentCompiler extends Memoizer extends Memoizer implements DynamicInstance { - private static final BasicModelSupplier MODEL = new BasicModelSupplier(BellInstance::createBellModel, RenderType.cutoutMipped()); + private static final BasicModelSupplier MODEL = new BasicModelSupplier(BellInstance::createBellModel, Sheets.solidBlockSheet()); private final OrientedData bell; diff --git a/src/main/resources/assets/flywheel/flywheel/shaders/context/crumbling.glsl b/src/main/resources/assets/flywheel/flywheel/shaders/context/crumbling.glsl index 423977c8a..6007383b8 100644 --- a/src/main/resources/assets/flywheel/flywheel/shaders/context/crumbling.glsl +++ b/src/main/resources/assets/flywheel/flywheel/shaders/context/crumbling.glsl @@ -3,6 +3,7 @@ uniform float uTime; uniform mat4 uViewProjection; uniform vec3 uCameraPos; +uniform int uConstantAmbientLight; uniform vec2 uTextureScale; uniform sampler2D uBlockAtlas; @@ -11,13 +12,24 @@ uniform sampler2D uCrumbling; uniform vec2 uWindowSize; -#if defined(VERTEX_SHADER) +#ifdef VERTEX_SHADER + +#use "flywheel:context/diffuse.glsl" + vec4 FLWVertex(inout Vertex v) { - FragDistance = cylindrical_distance(v.pos, uCameraPos); + fragDistance = fog_distance(v.pos, uCameraPos); return uViewProjection * vec4(v.pos, 1.); } +float FLWDiffuse(vec3 normal) { + if (uConstantAmbientLight == 1) { + return diffuseNether(normal); + } else { + return diffuse(normal); + } +} + #elif defined(FRAGMENT_SHADER) out vec4 fragColor; @@ -29,23 +41,24 @@ vec4 FLWBlockTexture(vec2 texCoords) { return cr; } +vec4 FLWLight(vec2 lightCoords) { + return vec4(1.); +} + void FLWFinalizeColor(vec4 color) { - #if defined(USE_FOG) - float a = color.a; - float fog = clamp(FLWFogFactor(), 0., 1.); - - color = mix(uFogColor, color, fog); - color.a = a; - #endif - - if (color.a < 0.1) { + #ifdef ALPHA_DISCARD + if (color.a < ALPHA_DISCARD) { discard; } + #endif + + #ifdef COLOR_FOG + color = linear_fog(color); + #elif defined(FADE_FOG) + color = linear_fog_fade(color); + #endif fragColor = color; } -vec4 FLWLight(vec2 lightCoords) { - return vec4(1.); -} #endif diff --git a/src/main/resources/assets/flywheel/flywheel/shaders/core/diffuse.glsl b/src/main/resources/assets/flywheel/flywheel/shaders/context/diffuse.glsl similarity index 52% rename from src/main/resources/assets/flywheel/flywheel/shaders/core/diffuse.glsl rename to src/main/resources/assets/flywheel/flywheel/shaders/context/diffuse.glsl index bc5792231..53fbdc010 100644 --- a/src/main/resources/assets/flywheel/flywheel/shaders/core/diffuse.glsl +++ b/src/main/resources/assets/flywheel/flywheel/shaders/context/diffuse.glsl @@ -1,5 +1,9 @@ - float diffuse(vec3 normal) { vec3 n2 = normal * normal * vec3(.6, .25, .8); return min(n2.x + n2.y * (3. + normal.y) + n2.z, 1.); } + +float diffuseNether(vec3 normal) { + vec3 n2 = normal * normal * vec3(.6, .9, .8); + return min(n2.x + n2.y + n2.z, 1.); +} diff --git a/src/main/resources/assets/flywheel/flywheel/shaders/context/fog.glsl b/src/main/resources/assets/flywheel/flywheel/shaders/context/fog.glsl index ada0be19e..d038a4acd 100644 --- a/src/main/resources/assets/flywheel/flywheel/shaders/context/fog.glsl +++ b/src/main/resources/assets/flywheel/flywheel/shaders/context/fog.glsl @@ -1,23 +1,50 @@ -#if defined(VERTEX_SHADER) -out float FragDistance; -#elif defined(FRAGMENT_SHADER) -in float FragDistance; -#endif uniform vec4 uFogColor; uniform vec2 uFogRange; +uniform int uFogShape; -float cylindrical_distance(vec3 worldPos, vec3 cameraPos) { - float distXZ = length(worldPos.xz - cameraPos.xz); - float distY = abs(worldPos.y - cameraPos.y); +#ifdef VERTEX_SHADER +out float fragDistance; +#elif defined(FRAGMENT_SHADER) +in float fragDistance; +#endif + +float spherical_distance(vec3 relativePos) { + return length(relativePos); +} + +float cylindrical_distance(vec3 relativePos) { + float distXZ = length(relativePos.xz); + float distY = abs(relativePos.y); return max(distXZ, distY); } -float cylindrical_distance(vec3 worldPos) { - float distXZ = length(worldPos.xz); - float distY = abs(worldPos.y); - return max(distXZ, distY); +float fog_distance(vec3 relativePos) { + if (uFogShape == 0) { + return spherical_distance(relativePos); + } else { + return cylindrical_distance(relativePos); + } } -float FLWFogFactor() { - return (uFogRange.y - FragDistance) / (uFogRange.y - uFogRange.x); +float fog_distance(vec3 worldPos, vec3 cameraPos) { + return fog_distance(worldPos - cameraPos); +} + +vec4 linear_fog(vec4 color) { + if (fragDistance <= uFogRange.x) { + return color; + } + + float fogValue = fragDistance < uFogRange.y ? smoothstep(uFogRange.x, uFogRange.y, fragDistance) : 1.0; + return vec4(mix(color.rgb, uFogColor.rgb, fogValue * uFogColor.a), color.a); +} + +vec4 linear_fog_fade(vec4 color) { + if (fragDistance <= uFogRange.x) { + return color; + } else if (fragDistance >= uFogRange.y) { + return vec4(0.0); + } + + return color * smoothstep(uFogRange.y, uFogRange.x, fragDistance); } diff --git a/src/main/resources/assets/flywheel/flywheel/shaders/context/world.glsl b/src/main/resources/assets/flywheel/flywheel/shaders/context/world.glsl index b6386e92d..0635b13c2 100644 --- a/src/main/resources/assets/flywheel/flywheel/shaders/context/world.glsl +++ b/src/main/resources/assets/flywheel/flywheel/shaders/context/world.glsl @@ -3,6 +3,7 @@ uniform float uTime; uniform mat4 uViewProjection; uniform vec3 uCameraPos; +uniform int uConstantAmbientLight; uniform vec2 uTextureScale; uniform sampler2D uBlockAtlas; @@ -10,19 +11,31 @@ uniform sampler2D uLightMap; uniform vec2 uWindowSize; -#if defined(VERTEX_SHADER) +#ifdef VERTEX_SHADER + +#use "flywheel:context/diffuse.glsl" vec4 FLWVertex(inout Vertex v) { - FragDistance = cylindrical_distance(v.pos, uCameraPos); + fragDistance = fog_distance(v.pos, uCameraPos); return uViewProjection * vec4(v.pos, 1.); } +float FLWDiffuse(vec3 normal) { + if (uConstantAmbientLight == 1) { + return diffuseNether(normal); + } else { + return diffuse(normal); + } +} + #elif defined(FRAGMENT_SHADER) + #use "flywheel:core/lightutil.glsl" + // optimize discard usage -#if defined(ALPHA_DISCARD) -#if defined(GL_ARB_conservative_depth) +#ifdef ALPHA_DISCARD +#ifdef GL_ARB_conservative_depth layout (depth_greater) out float gl_FragDepth; #endif #endif @@ -32,23 +45,24 @@ vec4 FLWBlockTexture(vec2 texCoords) { return texture(uBlockAtlas, texCoords); } +vec4 FLWLight(vec2 lightCoords) { + return texture(uLightMap, shiftLight(lightCoords)); +} + void FLWFinalizeColor(vec4 color) { - float a = color.a; - float fog = clamp(FLWFogFactor(), 0., 1.); - - color = mix(uFogColor, color, fog); - color.a = a; - - #if defined(ALPHA_DISCARD) + #ifdef ALPHA_DISCARD if (color.a < ALPHA_DISCARD) { discard; } #endif + #ifdef COLOR_FOG + color = linear_fog(color); + #elif defined(FADE_FOG) + color = linear_fog_fade(color); + #endif + fragColor = color; } -vec4 FLWLight(vec2 lightCoords) { - return texture(uLightMap, shiftLight(lightCoords)); -} #endif diff --git a/src/main/resources/assets/flywheel/flywheel/shaders/core/matutils.glsl b/src/main/resources/assets/flywheel/flywheel/shaders/core/matutils.glsl index d5f4d9c6c..af8fb301b 100644 --- a/src/main/resources/assets/flywheel/flywheel/shaders/core/matutils.glsl +++ b/src/main/resources/assets/flywheel/flywheel/shaders/core/matutils.glsl @@ -1,4 +1,3 @@ - mat4 rotate(vec3 axis, float angle) { float s = sin(angle); float c = cos(angle); diff --git a/src/main/resources/assets/flywheel/flywheel/shaders/core/quaternion.glsl b/src/main/resources/assets/flywheel/flywheel/shaders/core/quaternion.glsl index fb3e1ae95..fc6f16eae 100644 --- a/src/main/resources/assets/flywheel/flywheel/shaders/core/quaternion.glsl +++ b/src/main/resources/assets/flywheel/flywheel/shaders/core/quaternion.glsl @@ -1,4 +1,3 @@ - #define PIOVER2 1.5707963268 vec4 quat(vec3 axis, float angle) {