├── pack.mcmeta ├── assets └── minecraft │ ├── textures │ └── block │ │ └── hole.png │ ├── shaders │ ├── program │ │ ├── empty.fsh │ │ ├── empty.json │ │ ├── copy.json │ │ ├── copy.vsh │ │ ├── render.json │ │ ├── render.fsh │ │ ├── render.vsh │ │ └── scene │ │ │ ├── render.vsh │ │ │ └── render.fsh │ ├── core │ │ ├── rendertype_entity_cutout.vsh │ │ ├── rendertype_entity_cutout.json │ │ └── rendertype_entity_cutout.fsh │ └── post │ │ └── transparency.json │ └── models │ └── item │ └── blackstone.json ├── README.md └── LICENSE /pack.mcmeta: -------------------------------------------------------------------------------- 1 | { 2 | "pack": { 3 | "pack_format": 32, 4 | "description": "hihixd" 5 | } 6 | } -------------------------------------------------------------------------------- /assets/minecraft/textures/block/hole.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Godlander/raytracing/HEAD/assets/minecraft/textures/block/hole.png -------------------------------------------------------------------------------- /assets/minecraft/shaders/program/empty.fsh: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | out vec4 fragColor; 4 | 5 | void main() { 6 | fragColor = vec4(0); 7 | } -------------------------------------------------------------------------------- /assets/minecraft/shaders/program/empty.json: -------------------------------------------------------------------------------- 1 | { 2 | "blend": { 3 | "func": "add", 4 | "srcrgb": "one", 5 | "dstrgb": "zero" 6 | }, 7 | "vertex": "copy", 8 | "fragment": "empty", 9 | "attributes": [ "Position" ] 10 | } -------------------------------------------------------------------------------- /assets/minecraft/models/item/blackstone.json: -------------------------------------------------------------------------------- 1 | { 2 | "textures": { 3 | "a": "block/hole" 4 | }, 5 | "elements": [ 6 | { 7 | "from": [8, 0, 8], 8 | "to": [8.0001, 0, 8.0001], 9 | "faces": { 10 | "north": {"uv": [0, 0, 16, 16], "texture": "#a"} 11 | } 12 | } 13 | ] 14 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Raytracing Resourcepack 2 | 3 | To see the shader, equip the resourcepack, load into a world, and summon an item display with blackstone. 4 | 5 | ```mcfunction 6 | summon item_display ~ ~ ~ {item:{id:"minecraft:blackstone",count:1}} 7 | ``` 8 | 9 | The position of the item display will be the origin of the scene. 10 | 11 | The demo scene consists of a (mathematically inaccurate) black hole. 12 | 13 | https://github.com/Godlander/raytracing/assets/16228717/f1aa3b7a-bdc9-4f36-b0dd-0262be8ef0ed 14 | -------------------------------------------------------------------------------- /assets/minecraft/shaders/program/copy.json: -------------------------------------------------------------------------------- 1 | { 2 | "blend": { 3 | "func": "add", 4 | "srcrgb": "one", 5 | "dstrgb": "zero" 6 | }, 7 | "vertex": "copy", 8 | "fragment": "blit", 9 | "attributes": [ "Position" ], 10 | "samplers": [ 11 | { "name": "DiffuseSampler" } 12 | ], 13 | "uniforms": [ 14 | { "name": "OutSize", "type": "float", "count": 2, "values": [ 1.0, 1.0 ] }, 15 | { "name": "ColorModulate", "type": "float", "count": 4, "values": [ 1.0, 1.0, 1.0, 1.0 ] } 16 | ] 17 | } -------------------------------------------------------------------------------- /assets/minecraft/shaders/program/copy.vsh: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | in vec4 Position; 4 | 5 | uniform mat4 ProjMat; 6 | uniform vec2 OutSize; 7 | uniform vec2 InSize; 8 | 9 | out vec2 texCoord; 10 | flat out vec2 oneTexel; 11 | 12 | // Modified blit to work for copying between buffers of different sizes 13 | void main() { 14 | float x = -1.0; 15 | float y = -1.0; 16 | if (Position.x > 0.001) x = 1.0; 17 | if (Position.y > 0.001) y = 1.0; 18 | 19 | gl_Position = vec4(x, y, 0.2, 1.0); 20 | texCoord = Position.xy / OutSize; 21 | oneTexel = 1.0 / OutSize; 22 | } -------------------------------------------------------------------------------- /assets/minecraft/shaders/program/render.json: -------------------------------------------------------------------------------- 1 | { 2 | "blend": { 3 | "func": "add", 4 | "srcrgb": "one", 5 | "dstrgb": "zero" 6 | }, 7 | "vertex": "render", 8 | "fragment": "render", 9 | "attributes": [ "Position" ], 10 | "samplers": [ 11 | { "name": "DiffuseSampler" }, 12 | { "name": "DepthSampler" }, 13 | { "name": "ImageSampler" } 14 | ], 15 | "uniforms": [ 16 | { "name": "ProjMat", "type": "matrix4x4", "count": 16, "values": [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ] }, 17 | { "name": "OutSize", "type": "float", "count": 2, "values": [ 1.0, 1.0 ] } 18 | ] 19 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Godlander 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /assets/minecraft/shaders/core/rendertype_entity_cutout.vsh: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | #moj_import 4 | #moj_import 5 | 6 | in vec3 Position; 7 | in vec4 Color; 8 | in vec2 UV0; 9 | in ivec2 UV2; 10 | in vec3 Normal; 11 | 12 | uniform sampler2D Sampler0; 13 | uniform sampler2D Sampler2; 14 | 15 | uniform mat4 ModelViewMat; 16 | uniform mat4 ProjMat; 17 | uniform int FogShape; 18 | 19 | uniform vec3 Light0_Direction; 20 | uniform vec3 Light1_Direction; 21 | 22 | out vec2 texCoord0; 23 | out float vertexDistance; 24 | out vec4 vertexColor; 25 | out vec4 lightColor; 26 | 27 | out vec3 pos; 28 | 29 | const vec2[] corners = vec2[](vec2(0, 1), vec2(0, 0), vec2(1, 0), vec2(1, 1)); 30 | 31 | void main() { 32 | gl_Position = ProjMat * ModelViewMat * vec4(Position, 1.0); 33 | 34 | vertexDistance = fog_distance(Position, FogShape); 35 | vertexColor = minecraft_mix_light(Light0_Direction, Light1_Direction, Normal, Color); 36 | lightColor = minecraft_sample_lightmap(Sampler2, UV2); 37 | texCoord0 = UV0; 38 | 39 | if (ivec4(texture(Sampler0, UV0)*255) == ivec4(12, 34, 56, 78)) { 40 | gl_Position = vec4(corners[gl_VertexID]/10-1,-1,1); 41 | pos = Position; 42 | } 43 | } -------------------------------------------------------------------------------- /assets/minecraft/shaders/core/rendertype_entity_cutout.json: -------------------------------------------------------------------------------- 1 | { 2 | "blend": { 3 | "func": "add", 4 | "srcrgb": "srcalpha", 5 | "dstrgb": "1-srcalpha" 6 | }, 7 | "vertex": "rendertype_entity_cutout", 8 | "fragment": "rendertype_entity_cutout", 9 | "samplers": [ 10 | { "name": "Sampler0" }, 11 | { "name": "Sampler2" } 12 | ], 13 | "uniforms": [ 14 | { "name": "ModelViewMat", "type": "matrix4x4", "count": 16, "values": [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ] }, 15 | { "name": "ProjMat", "type": "matrix4x4", "count": 16, "values": [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ] }, 16 | { "name": "ColorModulator", "type": "float", "count": 4, "values": [ 1.0, 1.0, 1.0, 1.0 ] }, 17 | { "name": "Light0_Direction", "type": "float", "count": 3, "values": [0.0, 0.0, 0.0] }, 18 | { "name": "Light1_Direction", "type": "float", "count": 3, "values": [0.0, 0.0, 0.0] }, 19 | { "name": "FogStart", "type": "float", "count": 1, "values": [ 0.0 ] }, 20 | { "name": "FogEnd", "type": "float", "count": 1, "values": [ 1.0 ] }, 21 | { "name": "FogColor", "type": "float", "count": 4, "values": [ 0.0, 0.0, 0.0, 0.0 ] }, 22 | { "name": "FogShape", "type": "int", "count": 1, "values": [ 0 ] } 23 | ] 24 | } -------------------------------------------------------------------------------- /assets/minecraft/shaders/post/transparency.json: -------------------------------------------------------------------------------- 1 | { 2 | "targets": [ 3 | "water", 4 | "translucent", 5 | "itemEntity", 6 | "particles", 7 | "clouds", 8 | "weather", 9 | "image", 10 | "final" 11 | ], 12 | "passes": [ 13 | { 14 | "name": "transparency", 15 | "intarget": "minecraft:main", 16 | "outtarget": "image", 17 | "auxtargets": [ 18 | {"name": "DiffuseDepthSampler", "id": "minecraft:main:depth"}, 19 | {"name": "TranslucentSampler", "id": "translucent"}, 20 | {"name": "TranslucentDepthSampler", "id": "translucent:depth"}, 21 | {"name": "ItemEntitySampler", "id": "itemEntity"}, 22 | {"name": "ItemEntityDepthSampler", "id": "itemEntity:depth"}, 23 | {"name": "ParticlesSampler", "id": "particles"}, 24 | {"name": "ParticlesDepthSampler", "id": "particles:depth"}, 25 | {"name": "CloudsSampler", "id": "clouds"}, 26 | {"name": "CloudsDepthSampler", "id": "clouds:depth"}, 27 | {"name": "WeatherSampler", "id": "weather"}, 28 | {"name": "WeatherDepthSampler", "id": "weather:depth"} 29 | ] 30 | }, 31 | { 32 | "name": "render", 33 | "intarget": "minecraft:main", 34 | "outtarget": "final", 35 | "auxtargets": [ 36 | {"name": "DepthSampler", "id": "minecraft:main:depth"}, 37 | {"name": "ImageSampler", "id": "image"} 38 | ] 39 | }, 40 | {"name": "blit", "intarget": "final", "outtarget": "minecraft:main"} 41 | ] 42 | } 43 | -------------------------------------------------------------------------------- /assets/minecraft/shaders/program/render.fsh: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | uniform sampler2D DiffuseSampler; 4 | uniform sampler2D DepthSampler; 5 | uniform sampler2D ImageSampler; 6 | uniform vec2 OutSize; 7 | 8 | in vec2 texCoord; 9 | 10 | out vec4 fragColor; 11 | 12 | in vec3 pos; 13 | in vec2 proj; 14 | in vec2 ratio; 15 | in mat3 viewmat; 16 | 17 | const float inf = uintBitsToFloat(0x7F800000u); 18 | 19 | #define near 0.05 20 | #define far 1000.0 21 | float linearizeDepth(float depth) { 22 | float z = depth * 2.0 - 1.0; 23 | return (2.0 * near * far) / (far + near - z * (far - near)); 24 | } 25 | 26 | vec4 Sphere(vec3 ro, vec3 rd, float r) { 27 | vec3 rc = -ro; 28 | float c = dot(rc, rc) - (r*r); 29 | float b = dot(rd, rc); 30 | float d = b * b - c; 31 | float t = -b - sqrt(abs(d)); 32 | float st = step(0, min(t,d)); 33 | t = mix(-1, t, st); 34 | if (t < 0) t = inf; 35 | vec3 norm = normalize(-ro+rd*t); 36 | return vec4(norm, t); 37 | } 38 | 39 | vec3 render(vec2 uv, float maindepth, vec3 col) { 40 | vec3 ro = pos; 41 | vec3 rd = vec3((uv*2-1) / proj, -1) * viewmat; 42 | float l = length(rd); 43 | rd /= l; 44 | maindepth = maindepth * l; 45 | vec3 position = ro + rd*maindepth; 46 | vec4 hit; 47 | 48 | //screenspace center 49 | vec3 clip = viewmat * ro; 50 | vec2 screencenter = (-clip.xy / clip.z * proj + 1) / 2; 51 | //if (all(lessThan(abs((uv - screencenter) * ratio), vec2(0.01)))) return vec3(1,0,0); 52 | 53 | //center sphere 54 | hit = Sphere(ro, rd, 3); 55 | if (hit.w < maindepth) { 56 | col = vec3(1-dot(-rd, hit.xyz))/2 - 0.2; 57 | return col; 58 | } 59 | 60 | //distortion 61 | hit = Sphere(ro, rd, 7); 62 | if (hit.w < maindepth) { 63 | //push towards center 64 | vec2 dir = 0.2*normalize(uv - screencenter); 65 | float strength = dot(-rd, hit.xyz) * (atan(proj[1]) * l); 66 | uv = uv - dir * strength; 67 | uv.y = clamp(uv.y, 2/OutSize.y, 1); 68 | col = texture(ImageSampler, uv).rgb; 69 | return col; 70 | } 71 | 72 | return col; 73 | } 74 | 75 | void main() { 76 | vec2 uv = texCoord; 77 | uv.y = clamp(uv.y, 2/OutSize.y, 1); 78 | vec4 image = texture(ImageSampler, uv); 79 | float depth = linearizeDepth(texture(DepthSampler, texCoord).r); 80 | 81 | //render 82 | vec3 color = render(uv, depth, image.rgb); 83 | 84 | fragColor = vec4(color, 1); 85 | 86 | //debug pixels 87 | ivec2 coord = ivec2(gl_FragCoord.xy); 88 | if (all(lessThan(coord/10, ivec2(15,1)))) fragColor = texelFetch(DiffuseSampler, coord/10, 0); 89 | } -------------------------------------------------------------------------------- /assets/minecraft/shaders/program/render.vsh: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | uniform sampler2D DiffuseSampler; 4 | 5 | in vec4 Position; 6 | 7 | uniform mat4 ProjMat; 8 | uniform vec2 OutSize; 9 | uniform vec2 InSize; 10 | 11 | out vec2 texCoord; 12 | flat out vec2 oneTexel; 13 | 14 | out vec3 pos; 15 | out vec2 proj; 16 | out vec2 ratio; 17 | out mat3 viewmat; 18 | 19 | float decodeF(uint raw) { // From bálint 20 | uint sign = raw >> 31u; 21 | uint exponent = (raw >> 23u) & 255u; 22 | uint mantissa = 8388608u | (raw & 8388607u); 23 | return (float(sign) * -2.0 + 1.0) * float(mantissa) * exp2(float(exponent) - 150.0); 24 | } 25 | vec3 decodepos(int x, int y) { 26 | ivec3 n0 = ivec3(texelFetch(DiffuseSampler, ivec2(x , y), 0).rgb * 255.); 27 | ivec3 n1 = ivec3(texelFetch(DiffuseSampler, ivec2(x+1, y), 0).rgb * 255.); 28 | ivec3 n2 = ivec3(texelFetch(DiffuseSampler, ivec2(x+2, y), 0).rgb * 255.); 29 | ivec3 n3 = ivec3(texelFetch(DiffuseSampler, ivec2(x+3, y), 0).rgb * 255.); 30 | return vec3( 31 | decodeF(uint(n0.x) << 24u | uint(n0.y) << 16u | uint(n0.z) << 8u | uint(n1.x)), 32 | decodeF(uint(n1.y) << 24u | uint(n1.z) << 16u | uint(n2.x) << 8u | uint(n2.y)), 33 | decodeF(uint(n2.z) << 24u | uint(n3.x) << 16u | uint(n3.y) << 8u | uint(n3.z)) 34 | ); 35 | } 36 | int decodeInt(vec3 ivec) { 37 | ivec *= 255.0; 38 | int s = ivec.b >= 128.0 ? -1 : 1; 39 | return s * (int(ivec.r) + int(ivec.g) * 256 + (int(ivec.b) - 64 + s * 64) * 256 * 256); 40 | } 41 | float decodemat(vec3 ivec) {return decodeInt(ivec) / 2000000.;} 42 | 43 | void main() { 44 | float x = -1.0; 45 | float y = -1.0; 46 | if (Position.x > 0.001) x = 1.0; 47 | if (Position.y > 0.001) y = 1.0; 48 | 49 | gl_Position = vec4(x, y, 0.2, 1.0); 50 | texCoord = Position.xy / OutSize; 51 | oneTexel = 1.0 / OutSize; 52 | 53 | pos = decodepos(0,0); 54 | proj = vec2(decodemat(texelFetch(DiffuseSampler,ivec2(4,0),0).rgb), decodemat(texelFetch(DiffuseSampler,ivec2(5,0),0).rgb)); 55 | viewmat = mat3( 56 | decodemat(texelFetch(DiffuseSampler,ivec2(6,0),0).rgb), decodemat(texelFetch(DiffuseSampler,ivec2(9,0),0).rgb), decodemat(texelFetch(DiffuseSampler,ivec2(12,0),0).rgb), 57 | decodemat(texelFetch(DiffuseSampler,ivec2(7,0),0).rgb), decodemat(texelFetch(DiffuseSampler,ivec2(10,0),0).rgb), decodemat(texelFetch(DiffuseSampler,ivec2(13,0),0).rgb), 58 | decodemat(texelFetch(DiffuseSampler,ivec2(8,0),0).rgb), decodemat(texelFetch(DiffuseSampler,ivec2(11,0),0).rgb), decodemat(texelFetch(DiffuseSampler,ivec2(14,0),0).rgb) 59 | ); 60 | ratio = vec2(OutSize.x / OutSize.y, 1.0); 61 | } -------------------------------------------------------------------------------- /assets/minecraft/shaders/program/scene/render.vsh: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | uniform sampler2D DiffuseSampler; 4 | 5 | in vec4 Position; 6 | 7 | uniform mat4 ProjMat; 8 | uniform vec2 OutSize; 9 | uniform vec2 InSize; 10 | 11 | out vec2 texCoord; 12 | flat out vec2 oneTexel; 13 | out vec3 position; 14 | out mat3 viewmat; 15 | out vec2 proj; 16 | out vec2 ratio; 17 | flat out int nobjs; 18 | 19 | float decodeFloat(uint raw) { // From bálint 20 | uint sign = raw >> 31u; 21 | uint exponent = (raw >> 23u) & 255u; 22 | uint mantissa = 8388608u | (raw & 8388607u); 23 | return (float(sign) * -2.0 + 1.0) * float(mantissa) * exp2(float(exponent) - 150.0); 24 | } 25 | 26 | vec3 getpos(int x, int y) { 27 | ivec3 n0 = ivec3(texelFetch(DiffuseSampler, ivec2(x , y), 0).rgb * 255.); 28 | ivec3 n1 = ivec3(texelFetch(DiffuseSampler, ivec2(x+1, y), 0).rgb * 255.); 29 | ivec3 n2 = ivec3(texelFetch(DiffuseSampler, ivec2(x+2, y), 0).rgb * 255.); 30 | ivec3 n3 = ivec3(texelFetch(DiffuseSampler, ivec2(x+3, y), 0).rgb * 255.); 31 | return vec3( 32 | decodeFloat(uint(n0.x) << 24u | uint(n0.y) << 16u | uint(n0.z) << 8u | uint(n1.x)), 33 | decodeFloat(uint(n1.y) << 24u | uint(n1.z) << 16u | uint(n2.x) << 8u | uint(n2.y)), 34 | decodeFloat(uint(n2.z) << 24u | uint(n3.x) << 16u | uint(n3.y) << 8u | uint(n3.z)) 35 | ); 36 | } 37 | 38 | int decodeInt(vec3 ivec) { 39 | ivec *= 255.0; 40 | int s = ivec.b >= 128.0 ? -1 : 1; 41 | return s * (int(ivec.r) + int(ivec.g) * 256 + (int(ivec.b) - 64 + s * 64) * 256 * 256); 42 | } 43 | float decodeFloat(vec3 ivec) {return decodeInt(ivec) / 2000000.;} 44 | 45 | void main() { 46 | float x = -1.0; 47 | float y = -1.0; 48 | if (Position.x > 0.001) x = 1.0; 49 | if (Position.y > 0.001) y = 1.0; 50 | 51 | position = getpos(0,0); 52 | 53 | viewmat = mat3( 54 | decodeFloat(texelFetch(DiffuseSampler, ivec2(6,0), 0).rgb), decodeFloat(texelFetch(DiffuseSampler, ivec2(7,0), 0).rgb), decodeFloat(texelFetch(DiffuseSampler, ivec2(8,0), 0).rgb), 55 | decodeFloat(texelFetch(DiffuseSampler, ivec2(9,0), 0).rgb), decodeFloat(texelFetch(DiffuseSampler, ivec2(10,0), 0).rgb), decodeFloat(texelFetch(DiffuseSampler, ivec2(11,0), 0).rgb), 56 | decodeFloat(texelFetch(DiffuseSampler, ivec2(12,0), 0).rgb), decodeFloat(texelFetch(DiffuseSampler, ivec2(13,0), 0).rgb), decodeFloat(texelFetch(DiffuseSampler, ivec2(14,0), 0).rgb) 57 | ); 58 | ratio = vec2(OutSize.x / OutSize.y, 1.0); 59 | proj = vec2(decodeFloat(texelFetch(DiffuseSampler, ivec2(4,0), 0).rgb), decodeFloat(texelFetch(DiffuseSampler, ivec2(5,0), 0).rgb)); 60 | 61 | ivec4 objmeta = ivec4(texelFetch(DiffuseSampler, ivec2(0,4), 0)*255); 62 | nobjs = 0; 63 | if (objmeta.gba == ivec3(0,0,255)) { 64 | nobjs = objmeta.r+1; 65 | } 66 | 67 | gl_Position = vec4(x, y, 0.2, 1.0); 68 | texCoord = Position.xy / OutSize; 69 | oneTexel = 1.0 / OutSize; 70 | } -------------------------------------------------------------------------------- /assets/minecraft/shaders/core/rendertype_entity_cutout.fsh: -------------------------------------------------------------------------------- 1 | #version 150 2 | 3 | #moj_import 4 | 5 | uniform sampler2D Sampler0; 6 | 7 | uniform mat4 ModelViewMat; 8 | uniform mat4 ProjMat; 9 | 10 | uniform vec4 ColorModulator; 11 | uniform float FogStart; 12 | uniform float FogEnd; 13 | uniform vec4 FogColor; 14 | 15 | in vec2 texCoord0; 16 | in float vertexDistance; 17 | in vec4 vertexColor; 18 | in vec4 lightColor; 19 | 20 | in vec3 pos; 21 | 22 | out vec4 fragColor; 23 | 24 | uint encodeF(float f) { // From bálint 25 | uint sgn = f >= 0.0 ? 0u : 1u; 26 | uint exponent = uint(clamp(log2(abs(f)) + 127.0, 0.0, 255.0)); 27 | uint mantissa = uint(abs(f) * exp2(-float(exponent) + 150.0)) & 8388607u; 28 | return (sgn << 31u) | (exponent << 23u) | mantissa; 29 | } 30 | vec4 encodepos(vec3 f, int coord) { 31 | uint v0 = encodeF(f.x); 32 | uint v1 = encodeF(f.y); 33 | uint v2 = encodeF(f.z); 34 | switch (coord) { 35 | case 0: return vec4(v0 >>24u &255u, v0 >>16u &255u, v0 >>8u &255u, 255)/255; break; 36 | case 1: return vec4(v0 &255u, v1 >>24u &255u, v1 >>16u &255u, 255)/255; break; 37 | case 2: return vec4(v1 >>8u &255u, v1 &255u, v2 >>24u &255u, 255)/255; break; 38 | case 3: return vec4(v2 >>16u &255u, v2 >>8u &255u, v2 &255u, 255)/255; break; 39 | } 40 | return vec4(0); 41 | } 42 | 43 | int intmod(int i, int base) {return i - (i / base * base);} 44 | vec3 encodeI(int i) { 45 | int s = int(i < 0) * 128; 46 | i = abs(i); 47 | int r = intmod(i, 256); 48 | int g = intmod(i/256, 256); 49 | int b = intmod(i/65536, 128); 50 | return vec3(float(r) / 255.0, float(g) / 255.0, float(b + s) / 255.0); 51 | } 52 | vec3 encodemat(float i) {return encodeI(int(i * 2000000));} 53 | 54 | void main() { 55 | vec4 color = texture(Sampler0, texCoord0); 56 | 57 | if (ivec4(color*255) == ivec4(12, 34, 56, 78)) { 58 | ivec2 coord = ivec2(gl_FragCoord.xy); 59 | if (coord.y == 0) switch (coord.x) { 60 | //4 pixels for x y z position 61 | case 0: case 1: case 2: case 3: 62 | fragColor = encodepos(pos, coord.x); 63 | break; 64 | //proj[0][0] and proj[1][1] 65 | case 4: case 5: 66 | vec2 proj = vec2(ProjMat[0][0],ProjMat[1][1]); 67 | fragColor = vec4(encodemat(proj[coord.x%2]), 1); 68 | break; 69 | //3x3 view matrix 70 | case 6: case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 14: 71 | fragColor = vec4(encodemat(ModelViewMat[coord.x%3][(coord.x/3)-2]), 1); 72 | break; 73 | default: discard; 74 | } 75 | else discard; return; 76 | } 77 | 78 | if (color.a < 0.1) discard; 79 | color *= vertexColor * ColorModulator; 80 | color *= lightColor; 81 | fragColor = linear_fog(color, vertexDistance, FogStart, FogEnd, FogColor); 82 | } -------------------------------------------------------------------------------- /assets/minecraft/shaders/program/scene/render.fsh: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | uniform sampler2D DiffuseSampler; 4 | uniform sampler2D DepthSampler; 5 | uniform sampler2D StorageSampler; 6 | 7 | uniform vec2 OutSize; 8 | 9 | in vec2 texCoord; 10 | in vec2 OneTexel; 11 | in vec2 ratio; 12 | in vec3 position; 13 | in mat3 viewmat; 14 | in vec2 proj; 15 | flat in int nobjs; 16 | 17 | out vec4 fragColor; 18 | 19 | #define AA 1 20 | 21 | #define renderdistance 80 22 | #define fogstart 40 23 | 24 | #define PI 3.14159265358979323846 25 | 26 | #define FPRECISION 2000000.0 27 | int decodeInt(vec3 ivec) { 28 | ivec *= 255.0; 29 | int s = ivec.b >= 128.0 ? -1 : 1; 30 | return s * (int(ivec.r) + int(ivec.g) * 256 + (int(ivec.b) - 64 + s * 64) * 256 * 256); 31 | } 32 | float decodeFloat(vec3 ivec) { 33 | return decodeInt(ivec) / FPRECISION; 34 | } 35 | #define near 0.05 36 | #define far 1000.0 37 | float linearizeDepth(float depth) { 38 | float z = depth * 2.0 - 1.0; 39 | return (2.0 * near * far) / (far + near - z * (far - near)); 40 | } 41 | //-------------------------------------------------------------------------------- 42 | //sdfs 43 | struct obj {float depth; int type;}; 44 | obj Plane (vec3 p, int type) {return obj(p.y, type);} 45 | obj Sphere (vec3 p, float r, int type) {return obj(length(p)-r, type);} 46 | obj Cube (vec3 p, vec3 b, int type) { 47 | vec3 d = abs(p) - b; 48 | return obj(max(d.x, max(d.y, d.z)), type); 49 | } 50 | //-------------------------------------------------------------------------------- 51 | //operations 52 | obj Add(obj a, obj b) {return a.depth < b.depth? a : b;} 53 | obj Sub(obj a, obj b) {return -a.depth > b.depth? a : b;} 54 | obj Intersect(obj a, obj b) {return a.depth > b.depth? a : b;} 55 | obj SmoothAdd(obj a, obj b, float k) { 56 | float h = clamp(0.5 + 0.5*(b.depth-a.depth)/k, 0.0, 1.0); 57 | return obj(mix(b.depth, a.depth, h) - k*h*(1.0-h), a.type); 58 | } 59 | obj SmoothSub(obj a, obj b, float k) { 60 | float h = clamp(0.5 - 0.5*(a.depth+b.depth)/k, 0.0, 1.0); 61 | return obj(mix(a.depth, -b.depth, h) + k*h*(1.0-h), a.type); 62 | } 63 | obj SmoothIntersect(obj a, obj b, float k) { 64 | float h = clamp(0.5 - 0.5*(b.depth-a.depth)/k, 0.0, 1.0); 65 | return obj(mix(b.depth, a.depth, h) + k*h*(1.0-h), a.type); 66 | } 67 | //-------------------------------------------------------------------------------- 68 | //scene 69 | obj hit(in vec3 pos) {//obj pos size material smoothness 70 | obj o = Plane( pos + 2, 1); 71 | o = SmoothAdd(o, Sphere( pos + vec3(0,2.5,0), 2, 1), 0.5); 72 | o = SmoothSub(o, Sphere( pos + vec3(-2,1.5,0), 2, 1), 0.5); 73 | o = Add(o, Sphere( pos + vec3(2,1,4), 1, 2)); 74 | o = Add(o, Cube( pos + vec3(5,1,1), vec3(1), 3)); 75 | o = SmoothAdd(o, Sphere( pos + vec3(5.5,0.5,.5), 1, 3), 0.5); 76 | 77 | //add 20 spheres 78 | for (int i = 0; i < 20; i++) { 79 | float r = 1 + 0.5*sin(i*PI/10.0); 80 | o = Add(o, Sphere(pos + 8*vec3(r*cos(i*PI/10.0), -1, r*sin(i*PI/10.0)) + vec3(-5, 0, -5), r, 2)); 81 | } 82 | 83 | for (int i = 0; i < nobjs; i++) { 84 | o = Add(o, Sphere( pos - position + ((vec3(255.0, 1.0, 1.0 / 255.0) * mat3( 85 | texelFetch(DiffuseSampler, ivec2(3*i + 3,4), 0).rgb, 86 | texelFetch(DiffuseSampler, ivec2(3*i + 4,4), 0).rgb, 87 | texelFetch(DiffuseSampler, ivec2(3*i + 5,4), 0).rgb)) - 128), 1, 2)); 88 | } 89 | return o; 90 | } 91 | //-------------------------------------------------------------------------------- 92 | //filters 93 | float checkerboard(in vec2 p) { 94 | vec2 w = max(abs(dFdx(p)), abs(dFdy(p))) + 0.01; 95 | vec2 i = 2.0*(abs(fract((p-0.5*w)/2.0)-0.5)-abs(fract((p+0.5*w)/2.0)-0.5))/w; 96 | return 0.5 - 0.5*i.x*i.y; 97 | } 98 | float shadows(in vec3 ro, in vec3 rd, in float tmin, in float tmax) { 99 | float soft = 1.0; 100 | float ph = renderdistance; 101 | for (float t = tmin; t < tmax;) { 102 | float h = hit(ro + rd*t).depth; 103 | if (h < 0.0001) return 0.; 104 | 105 | float y = h*h/(2.0*ph); 106 | float d = sqrt(h*h-y*y); 107 | soft = min(soft, 16*d/max(0,t-y)); 108 | ph = h; 109 | 110 | t += h; 111 | } 112 | return soft; 113 | } 114 | float AO(in vec3 pos, in vec3 norm) { 115 | float occ = 0.0; 116 | float sca = 1.0; 117 | for(int i=0; i<5; i++) { 118 | float h = 0.01 + 0.12*float(i)/5.0; 119 | float d = hit(pos + norm*h).depth; 120 | occ += (h-d)*sca; 121 | sca *= 0.95; 122 | if(occ > 0.35) break; 123 | } 124 | return clamp(1.0 - 3.0 * occ, 0.0, 1.0) * (0.5 + 0.5 * norm.y); 125 | } 126 | //-------------------------------------------------------------------------------- 127 | //drawing 128 | vec3 getnormal(in vec3 pos) { 129 | vec2 e = vec2(0.001,0); 130 | return normalize(vec3(hit(pos+e.xyy).depth-hit(pos-e.xyy).depth, 131 | hit(pos+e.yxy).depth-hit(pos-e.yxy).depth, 132 | hit(pos+e.yyx).depth-hit(pos-e.yyx).depth)); 133 | } 134 | vec3 render(vec3 ro, vec3 rd, float fardepth, vec3 maincolor) { 135 | //raymarching 136 | float t = 0.; 137 | obj o; 138 | for(int i = 0; i < 100; i++) { 139 | o = hit(ro + t*rd); 140 | //if (h.depth < o.depth) o = h; 141 | //if hit 142 | if (o.depth < 0.0001) break; 143 | t += o.depth; 144 | //exceed far plane 145 | if (t >= fardepth) break; 146 | } 147 | //coloring 148 | vec3 sky = vec3(0.7, 0.9, 1.1); 149 | vec3 sunlight = vec3(1.5,1.2,1.0); 150 | vec3 skylight = vec3(0.1,0.2,0.3); 151 | vec3 indlight = vec3(0.4,0.3,0.2); 152 | //fake atmosphere by dimming up 153 | vec3 color = sky - max(rd.y,0.0)*0.3; 154 | vec3 sundir = normalize(vec3(-0.5, 0.6, -0.6)); 155 | float sunamount = clamp(dot(sundir,rd), 0.0, 1.0); 156 | //scene 157 | if (t < fardepth) { 158 | vec3 pos = ro + t*rd; 159 | vec3 norm = getnormal(pos); 160 | //materials 161 | switch(o.type) { 162 | case 1: //plane 163 | color = vec3(checkerboard(pos.xz) + 0.3); 164 | break; 165 | case 2: //sphere 166 | color = vec3(0.6, 0.3, 0.4); 167 | color += dot(norm, sundir) * sunlight; 168 | break; 169 | default: color = (norm+1)/2; 170 | } 171 | //lighting 172 | float skyamount = clamp(0.5 + 0.5*norm.y, 0.0, 1.0); 173 | float indamount = clamp(dot(norm, normalize(sundir*vec3(-1.0,0.0,-1.0))), 0.0, 1.0); 174 | 175 | float ao = AO(pos, norm); 176 | float shadow = shadows(pos, sundir, 0.001, 40); 177 | shadow = clamp(mix(0,shadow, smoothstep(0, 1, dot(norm, sundir))), 0,1); 178 | 179 | vec3 light = 0.4 * sunlight * pow(vec3(shadow),vec3(1.0,1.2,1.5)); 180 | light += skyamount * skylight * ao; 181 | light += indamount * indlight * ao; 182 | 183 | color *= light; 184 | 185 | //fog 186 | color = mix(color, sky, smoothstep(0,1, clamp((t-fogstart)/(renderdistance-fogstart) ,0,1))); 187 | } 188 | //world 189 | else if (t < renderdistance) { 190 | color = maincolor; 191 | //fog 192 | color = mix(color, sky, smoothstep(0,1, clamp((fardepth-fogstart)/(renderdistance-fogstart) ,0,1))); 193 | } 194 | //sun glare 195 | color += 0.25*vec3(0.8,0.4,0.2)*pow(sunamount, 4.0); 196 | 197 | //return color 198 | color = clamp(color, 0.0, 1.0); 199 | return color; 200 | } 201 | //-------------------------------------------------------------------------------- 202 | void main() { 203 | //data 204 | vec3 color = vec3(0); 205 | vec3 maincolor = texture(DiffuseSampler, texCoord).rgb; 206 | float depth = linearizeDepth(texture(DepthSampler, texCoord).r); 207 | 208 | //msaa 209 | #if AA > 1 210 | for(int m=0; m 1 230 | } 231 | color /= float(AA*AA); 232 | #endif 233 | 234 | fragColor = vec4(color, 1); 235 | 236 | //debug data pixels bottom left 237 | #ifdef DEBUG 238 | #define datasize vec3(32,5, 10) 239 | if (all(lessThan(gl_FragCoord.xy / datasize.z, datasize.xy))) { 240 | fragColor = texelFetch(DiffuseSampler, ivec2(gl_FragCoord.xy / datasize.z), 0); 241 | } 242 | #endif 243 | } 244 | --------------------------------------------------------------------------------