1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
const int MAX_MARCHING_STEPS = 255; const float MIN_DIST = 0.0; const float MAX_DIST = 100.0; const float EPSILON = 0.0001;
float cubeSDF(vec3 p) { vec3 d = abs(p) - vec3(1.0, 1.0, 1.0); float insideDistance = min(max(d.x, max(d.y, d.z)), 0.0); float outsideDistance = length(max(d, 0.0)); return insideDistance + outsideDistance; }
float sphereSDF(vec3 p) { return length(p) - 1.0; }
float sceneSDF(vec3 samplePoint) { return cubeSDF(samplePoint); }
float shortestDistanceToSurface(vec3 eye, vec3 marchingDirection, float start, float end) { float depth = start; for (int i = 0; i < MAX_MARCHING_STEPS; i++) { float dist = sceneSDF(eye + depth * marchingDirection); if (dist < EPSILON) { return depth; } depth += dist; if (depth >= end) { return end; } } return end; }
vec3 rayDirection(float fieldOfView, vec2 size, vec2 fragCoord) { vec2 xy = fragCoord - size / 2.0; float z = size.y / tan(radians(fieldOfView) / 2.0); return normalize(vec3(xy, -z)); }
vec3 estimateNormal(vec3 p) { return normalize(vec3( sceneSDF(vec3(p.x + EPSILON, p.y, p.z)) - sceneSDF(vec3(p.x - EPSILON, p.y, p.z)), sceneSDF(vec3(p.x, p.y + EPSILON, p.z)) - sceneSDF(vec3(p.x, p.y - EPSILON, p.z)), sceneSDF(vec3(p.x, p.y, p.z + EPSILON)) - sceneSDF(vec3(p.x, p.y, p.z - EPSILON)) )); }
vec3 phongContribForLight(vec3 k_d, vec3 k_s, float alpha, vec3 p, vec3 eye, vec3 lightPos, vec3 lightIntensity) { vec3 N = estimateNormal(p); vec3 L = normalize(lightPos - p); vec3 V = normalize(eye - p); vec3 R = normalize(reflect(-L, N)); float dotLN = dot(L, N); float dotRV = dot(R, V); if (dotLN < 0.0) { return vec3(0.0, 0.0, 0.0); } if (dotRV < 0.0) { return lightIntensity * (k_d * dotLN); } return lightIntensity * (k_d * dotLN + k_s * pow(dotRV, alpha)); }
vec3 phongIllumination(vec3 k_a, vec3 k_d, vec3 k_s, float alpha, vec3 p, vec3 eye) { const vec3 ambientLight = 0.5 * vec3(1.0, 1.0, 1.0); vec3 color = ambientLight * k_a; vec3 light1Pos = vec3(4.0 * sin(iTime), 2.0, 4.0 * cos(iTime)); vec3 light1Intensity = vec3(0.4, 0.4, 0.4); color += phongContribForLight(k_d, k_s, alpha, p, eye, light1Pos, light1Intensity); vec3 light2Pos = vec3(2.0 * sin(0.37 * iTime), 2.0 * cos(0.37 * iTime), 2.0); vec3 light2Intensity = vec3(0.4, 0.4, 0.4); color += phongContribForLight(k_d, k_s, alpha, p, eye, light2Pos, light2Intensity); return color; }
mat4 viewMatrix(vec3 eye, vec3 center, vec3 up) { vec3 f = normalize(center - eye); vec3 s = normalize(cross(f, up)); vec3 u = cross(s, f); return mat4( vec4(s, 0.0), vec4(u, 0.0), vec4(-f, 0.0), vec4(0.0, 0.0, 0.0, 1) ); }
void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec3 viewDir = rayDirection(45.0, iResolution.xy, fragCoord); vec3 eye = vec3(8.0, 5.0, 7.0); mat4 viewToWorld = viewMatrix(eye, vec3(0.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0)); vec3 worldDir = (viewToWorld * vec4(viewDir, 0.0)).xyz; float dist = shortestDistanceToSurface(eye, worldDir, MIN_DIST, MAX_DIST); if (dist > MAX_DIST - EPSILON) { fragColor = vec4(0.0, 0.0, 0.0, 0.0); return; } vec3 p = eye + dist * worldDir; vec3 K_a = vec3(0.2, 0.2, 0.2); vec3 K_d = vec3(0.7, 0.2, 0.2); vec3 K_s = vec3(1.0, 1.0, 1.0); float shininess = 10.0; vec3 color = phongIllumination(K_a, K_d, K_s, shininess, p, eye); fragColor = vec4(color, 1.0); }
|