Initial commit: Minecraft 光追着色器包(从零实现的 path tracing)
This commit is contained in:
@@ -0,0 +1,157 @@
|
||||
// MinecraftPT — Terrain Fragment Shader
|
||||
// Writes the solid and translucent GBuffer
|
||||
|
||||
#include "/Lib/Settings.glsl"
|
||||
#include "/Lib/Utilities.glsl"
|
||||
|
||||
in vec2 texcoord;
|
||||
in vec3 worldPos;
|
||||
in vec3 worldNormal;
|
||||
in vec3 vertexNormal;
|
||||
in vec4 color;
|
||||
in vec2 lightmap;
|
||||
flat in int blockId;
|
||||
flat in float blockLightLevel;
|
||||
|
||||
layout(location = 0) out vec4 colortex0Out; // albedo + emissive
|
||||
layout(location = 1) out vec4 colortex1Out; // normals
|
||||
layout(location = 2) out vec4 colortex2Out; // spec + matID
|
||||
layout(location = 3) out vec4 colortex3Out; // lightmap
|
||||
layout(location = 4) out vec4 colortex4Out; // translucent albedo + alpha
|
||||
layout(location = 5) out vec4 colortex5Out; // translucent normal + matID
|
||||
|
||||
// Material IDs from block.properties
|
||||
#define BLOCK_LEAVES 1
|
||||
#define BLOCK_TRANSLUCENT 2
|
||||
#define BLOCK_WATER 3
|
||||
#define BLOCK_CROSS 4
|
||||
#define BLOCK_TORCH 5
|
||||
#define BLOCK_LANTERN 6
|
||||
#define BLOCK_FIRE 7
|
||||
#define BLOCK_CAMPFIRE 8
|
||||
#define BLOCK_LAVA 9
|
||||
#define BLOCK_GLASS_PANE 10
|
||||
#define BLOCK_IRON_BARS 11
|
||||
#define BLOCK_STAIRS 12
|
||||
#define BLOCK_SLAB 13
|
||||
#define BLOCK_WALL 14
|
||||
#define BLOCK_FENCE 15
|
||||
#define BLOCK_FENCE_GATE 16
|
||||
#define BLOCK_DOOR 17
|
||||
#define BLOCK_RAIL 18
|
||||
#define BLOCK_REDSTONE 19
|
||||
#define BLOCK_ENDROD 20
|
||||
#define BLOCK_CHAIN 21
|
||||
#define BLOCK_AMETHYST 22
|
||||
#define BLOCK_CANDLE 23
|
||||
#define BLOCK_GLOWSTONE 24
|
||||
#define BLOCK_LIGHT 25
|
||||
#define BLOCK_PORTAL 26
|
||||
#define BLOCK_LADDER 27
|
||||
#define BLOCK_SUGARCANE 28
|
||||
#define BLOCK_BAMBOO 29
|
||||
#define BLOCK_CACTUS 30
|
||||
#define BLOCK_CHORUS 31
|
||||
#define BLOCK_CORAL 32
|
||||
#define BLOCK_DRIPSTONE 33
|
||||
#define BLOCK_CONDUIT 34
|
||||
#define BLOCK_LIGHTNING_ROD 35
|
||||
#define BLOCK_POT 36
|
||||
#define BLOCK_SNOW_LAYERS 37
|
||||
#define BLOCK_COBWEB 80
|
||||
|
||||
void main(){
|
||||
vec4 albedo = texture(tex, texcoord) * color;
|
||||
|
||||
// Cutout (alpha test)
|
||||
if (albedo.a < 0.004) discard;
|
||||
|
||||
// Material ID determination
|
||||
float matID = 0.0; // MATID_DEFAULT
|
||||
#ifdef IS_HAND
|
||||
matID = 5.0; // MATID_HAND
|
||||
#endif
|
||||
bool isTranslucent = false;
|
||||
|
||||
if (blockId == BLOCK_LEAVES){
|
||||
matID = 3.0; // MATID_LEAVES
|
||||
}else if (blockId == BLOCK_TRANSLUCENT){
|
||||
matID = 2.0; // MATID_STAINEDGLASS
|
||||
isTranslucent = true;
|
||||
}else if (blockId == BLOCK_WATER){
|
||||
matID = 1.0; // MATID_WATER (should be handled by gbuffers_water)
|
||||
isTranslucent = true;
|
||||
}else if (blockId == BLOCK_CROSS){
|
||||
matID = 4.0; // MATID_GRASS
|
||||
}else if (blockId == BLOCK_TORCH){
|
||||
matID = 11.0; // MATID_TORCH
|
||||
}else if (blockId == BLOCK_LANTERN){
|
||||
matID = 20.0; // MATID_COPPER_LANTERN
|
||||
}else if (blockId == BLOCK_FIRE || blockId == BLOCK_CAMPFIRE){
|
||||
matID = 12.0; // MATID_FIRE
|
||||
}else if (blockId == BLOCK_LAVA){
|
||||
matID = 12.0;
|
||||
}else if (blockId == BLOCK_GLASS_PANE){
|
||||
matID = 2.0; // MATID_STAINEDGLASS
|
||||
isTranslucent = true;
|
||||
}else if (blockId == BLOCK_ENDROD){
|
||||
matID = 13.0; // MATID_ENDROD
|
||||
}else if (blockId == BLOCK_AMETHYST){
|
||||
matID = 14.0; // MATID_AMETHYST
|
||||
}else if (blockId == BLOCK_PORTAL){
|
||||
matID = 18.0; // MATID_END_PORTAL
|
||||
}else if (blockId == BLOCK_COBWEB){
|
||||
matID = 17.0; // MATID_PARTICLE-like
|
||||
isTranslucent = true;
|
||||
}
|
||||
|
||||
// Emissive detection
|
||||
float emissive = 0.0;
|
||||
#ifdef HARDCODED_EMISSIVENESS_MODE
|
||||
if (blockId == BLOCK_LAVA || blockId == BLOCK_FIRE || blockId == BLOCK_CAMPFIRE ||
|
||||
blockId == BLOCK_GLOWSTONE || blockId == BLOCK_ENDROD || blockId == BLOCK_LANTERN ||
|
||||
blockId == BLOCK_AMETHYST || blockId == BLOCK_TORCH || blockId == BLOCK_CANDLE ||
|
||||
blockId == BLOCK_LIGHT || blockId == BLOCK_CONDUIT){
|
||||
emissive = 1.0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef VANILLA_EMISSIVE
|
||||
// Emissive blocks from vanilla light level (at_midBlock.w = 0-15)
|
||||
emissive = max(emissive, step(13.5, blockLightLevel) * blockLightLevel / 15.0);
|
||||
#endif
|
||||
|
||||
// LAB PBR specular
|
||||
vec4 specTex = vec4(0.0);
|
||||
#ifdef TEXTURE_PBR_FORMAT
|
||||
if (TEXTURE_PBR_FORMAT >= 1 && blockId != BLOCK_WATER){
|
||||
specTex = texture(atlasSpecular2D, texcoord);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Lightmap
|
||||
vec2 lm = lightmap;
|
||||
lm.y = SkyLightmapCurve(lightmap.y);
|
||||
|
||||
// Parallax shadow (POM)
|
||||
float parallaxShadow = 1.0;
|
||||
|
||||
// Encode normals (octahedral)
|
||||
vec2 worldNormalEnc = EncodeNormal(worldNormal);
|
||||
vec2 vertexNormalEnc = EncodeNormal(vertexNormal);
|
||||
|
||||
// Solid write
|
||||
colortex0Out = vec4(albedo.rgb, emissive);
|
||||
colortex1Out = vec4(worldNormalEnc, vertexNormalEnc);
|
||||
colortex2Out = vec4(specTex.r, specTex.g, specTex.b, matID / 255.0);
|
||||
colortex3Out = vec4(lm, parallaxShadow, 1.0);
|
||||
|
||||
// Translucent write (glass, panes, cobweb)
|
||||
if (isTranslucent){
|
||||
colortex4Out = vec4(albedo.rgb, albedo.a);
|
||||
colortex5Out = vec4(worldNormalEnc, matID / 255.0, specTex.r);
|
||||
}else{
|
||||
colortex4Out = vec4(0.0);
|
||||
colortex5Out = vec4(0.0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user