URL: http://glslsandbox.com/e#22751.0
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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 |
//-------------------------------------------------------------------------------------------- // Simple GPU Ray Tracer // By: Brandon Fogerty // bfogerty at gmail dot com // xdpixel.com //-------------------------------------------------------------------------------------------- #ifdef GL_ES precision mediump float; #endif uniform float time; uniform vec2 mouse; uniform vec2 resolution; //-------------------------------------------------------------------------------------------- // Begin Region: Defines //-------------------------------------------------------------------------------------------- #define PrimitiveType_None 0.0 #define PrimitiveType_Sphere 1.0 #define PrimitiveType_Plane 2.0 #define MAX_SPHERES 5 #define MAX_PLANES 6 #define TimeValue time #define ResolutionValue resolution #define Epsilon 0.000001 #define MaterialIndex0 0 #define MaterialIndex1 1 #define MaterialIndex2 2 #define MaterialIndex3 3 #define MaterialIndex4 4 #define MaterialIndex5 5 #define MaterialIndex6 6 #define MaterialIndex7 7 #define MaterialIndex8 8 #define MaterialIndex9 9 #define MaterialIndex10 10 //-------------------------------------------------------------------------------------------- // End Region //-------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------- // Begin Region: Data Structures //-------------------------------------------------------------------------------------------- struct Ray { vec3 origin; vec3 direction; }; struct Material { vec3 diffuseColor; vec3 specularColor; float specularIntensity; float receiveShadow; float shadowCasterIntensity; float reflectionIntensity; float refractionIndex; float refractionIntensity; }; struct SphereParms { vec3 position; float radius; int materialIndex; int lightVisualizer; int id; }; struct PlaneParams { vec3 pointOnPlane; vec3 normal; int materialIndex; int id; }; struct HitInfo { vec3 position; vec3 normal; vec3 rayDir; float t; float dist; int materialIndex; int lightVisualizer; float primitiveType; float hit; int id; }; //-------------------------------------------------------------------------------------------- // End Region //-------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------- // Begin Region: Global Variables //-------------------------------------------------------------------------------------------- SphereParms spheres[MAX_SPHERES]; HitInfo sphereHitInfos[MAX_SPHERES]; PlaneParams planes[MAX_PLANES]; HitInfo planeHitInfos[MAX_PLANES]; Material materials[11]; vec3 lightPos; //-------------------------------------------------------------------------------------------- // End Region //-------------------------------------------------------------------------------------------- Material getMaterial( int materialIndex ) { // GLSL does not allow variables to be used as indicies into an array. // This is a work around but not as efficient as it could be... if( materialIndex == MaterialIndex0 ) return materials[0]; if( materialIndex == MaterialIndex1 ) return materials[1]; if( materialIndex == MaterialIndex2 ) return materials[2]; if( materialIndex == MaterialIndex3 ) return materials[3]; if( materialIndex == MaterialIndex4 ) return materials[4]; if( materialIndex == MaterialIndex5 ) return materials[5]; if( materialIndex == MaterialIndex6 ) return materials[6]; if( materialIndex == MaterialIndex7 ) return materials[7]; if( materialIndex == MaterialIndex8 ) return materials[8]; if( materialIndex == MaterialIndex9 ) return materials[9]; return materials[10]; } //-------------------------------------------------------------------------------------------- vec3 checkerBoardTexture( vec3 hitPos ) { vec2 p = floor( hitPos.xz * 1.0 ); float s = mod( p.x + p.y, 2.0 ); vec3 c = vec3( 0.4 ) * s; c = mix( c, vec3( 0.9 ), hitPos.z / 30.0 ); return c; } //-------------------------------------------------------------------------------------------- void sphere( Ray ray, SphereParms sphereParams, inout HitInfo hitInfo ) { hitInfo.primitiveType = PrimitiveType_None; hitInfo.t = -1.0; hitInfo.hit = 0.0; vec3 newO = ray.origin - sphereParams.position; float b = 2.0 * dot( newO, ray.direction ); float c = dot( newO, newO ) - (sphereParams.radius*sphereParams.radius); float h = b*b - (4.0*c); if( h < 0.0 ) return; float t = (-b - sqrt( h ) ) / 2.0; if ( t < Epsilon ) return; hitInfo.position = ray.origin + ray.direction * t; hitInfo.normal = normalize( ( hitInfo.position - sphereParams.position ) / sphereParams.radius ); hitInfo.t = t; hitInfo.dist = length( hitInfo.position - ray.origin ); hitInfo.rayDir = ray.direction; hitInfo.materialIndex = sphereParams.materialIndex; hitInfo.lightVisualizer = sphereParams.lightVisualizer; hitInfo.primitiveType = PrimitiveType_Sphere; hitInfo.id = sphereParams.id; hitInfo.hit = 1.0; } //-------------------------------------------------------------------------------------------- void plane( Ray ray, PlaneParams planeParams, inout HitInfo hitInfo ) { hitInfo.primitiveType = PrimitiveType_None; hitInfo.t = -1.0; hitInfo.hit = 0.0; float d = dot( planeParams.pointOnPlane, planeParams.normal ); float t = ( d - dot( ray.origin, planeParams.normal ) ) / dot( ray.direction, planeParams.normal ); if( t < 0.00 ) { return; } hitInfo.position = ray.origin + ray.direction * t; hitInfo.normal = normalize( planeParams.normal ); hitInfo.t = t; hitInfo.dist = length( hitInfo.position - ray.origin ); hitInfo.rayDir = ray.direction; hitInfo.materialIndex = planeParams.materialIndex; hitInfo.lightVisualizer = 0; hitInfo.primitiveType = PrimitiveType_Plane; hitInfo.id = planeParams.id; hitInfo.hit = 1.0; } //-------------------------------------------------------------------------------------------- HitInfo intersect( Ray ray ) { HitInfo hitInfo; hitInfo.primitiveType = PrimitiveType_None; for( int i = 0; i < MAX_SPHERES; ++i ) { sphere( ray, spheres[i], sphereHitInfos[i] ); if( sphereHitInfos[i].primitiveType > PrimitiveType_None ) { if( sphereHitInfos[i].dist < hitInfo.dist || hitInfo.primitiveType <= PrimitiveType_None ) { hitInfo = sphereHitInfos[i]; } } } for( int i = 0; i < MAX_PLANES; ++i ) { plane( ray, planes[i], planeHitInfos[i] ); if( planeHitInfos[i].primitiveType > PrimitiveType_None ) { if( planeHitInfos[i].dist < hitInfo.dist || hitInfo.primitiveType <= PrimitiveType_None ) { hitInfo = planeHitInfos[i]; } } } return hitInfo; } //-------------------------------------------------------------------------------------------- void initializeMaterials() { materials[0].diffuseColor = vec3( 1.0, 1.0, 0.0 ); materials[0].specularColor = vec3( 1.0 ); materials[0].specularIntensity = 10.0; materials[0].shadowCasterIntensity = 1.0; materials[0].reflectionIntensity = 0.00; materials[0].refractionIndex = 1.0; materials[0].refractionIntensity = 0.00; materials[1].diffuseColor = vec3( 1.0, 1.4, 1.0 ); materials[1].specularColor = vec3( 1.0 ); materials[1].specularIntensity = 10.0; materials[1].shadowCasterIntensity = 0.5; materials[1].reflectionIntensity = 0.00; materials[1].refractionIndex = 1.0; materials[1].refractionIntensity = 0.00; materials[2].diffuseColor = vec3( 1.0, 0.4, 1.0 ); materials[2].specularColor = vec3( 1.0 ); materials[2].specularIntensity = 20.0; materials[2].shadowCasterIntensity = 0.5; materials[2].reflectionIntensity = 0.7; materials[2].refractionIndex = 1.0; materials[2].refractionIntensity = 0.00; materials[3].diffuseColor = vec3( 0.0, 1.0, 1.4 ); materials[3].specularColor = vec3( 1.0 ); materials[3].specularIntensity = 10.0; materials[3].shadowCasterIntensity = 0.5; materials[3].reflectionIntensity = 0.00; materials[3].refractionIndex = 1.0; materials[3].refractionIntensity = 0.00; materials[4].diffuseColor = vec3( 0.8 ); materials[4].specularColor = vec3( 1.0 ); materials[4].specularIntensity = 10.0; materials[4].shadowCasterIntensity = 1.0; materials[4].reflectionIntensity = 0.00; materials[4].refractionIndex = 1.0; materials[4].refractionIntensity = 0.00; materials[5].diffuseColor = vec3( 1.0, 0.0, 0.0 ); materials[5].specularColor = vec3( 1.0 ); materials[5].specularIntensity = 10.0; materials[5].shadowCasterIntensity = 1.0; materials[5].reflectionIntensity = 0.00; materials[5].refractionIndex = 1.0; materials[5].refractionIntensity = 0.00; materials[6].diffuseColor = vec3( 0.0, 0.0, 1.0 ); materials[6].specularColor = vec3( 1.0 ); materials[6].specularIntensity = 10.0; materials[6].shadowCasterIntensity = 1.0; materials[6].reflectionIntensity = 0.00; materials[6].refractionIndex = 1.0; materials[6].refractionIntensity = 0.00; materials[7].diffuseColor = vec3( 0.0, 1.0, 0.0 ); materials[7].specularColor = vec3( 1.0 ); materials[7].specularIntensity = 10.0; materials[7].shadowCasterIntensity = 1.0; materials[7].reflectionIntensity = 0.00; materials[7].refractionIndex = 1.0; materials[7].refractionIntensity = 0.00; materials[8].diffuseColor = vec3( 0.0, 1.0, 1.0 ); materials[8].specularColor = vec3( 1.0 ); materials[8].specularIntensity = 10.0; materials[8].shadowCasterIntensity = 1.0; materials[8].reflectionIntensity = 0.00; materials[8].refractionIndex = 1.0; materials[8].refractionIntensity = 0.00; materials[9].diffuseColor = vec3( 1.0, 1.0, 0.0 ); materials[9].specularColor = vec3( 1.0 ); materials[9].specularIntensity = 10.0; materials[9].shadowCasterIntensity = 1.0; materials[9].reflectionIntensity = 0.00; materials[9].refractionIndex = 1.0; materials[9].refractionIntensity = 0.00; materials[10].diffuseColor = vec3( 0.3, 0.0, 1.0 ); materials[10].specularColor = vec3( 0.0 ); materials[10].specularIntensity = 1.0; materials[10].shadowCasterIntensity = 0.5; materials[10].reflectionIntensity = 0.00; materials[10].refractionIndex = 1.0; materials[10].refractionIntensity = 1.0; } //-------------------------------------------------------------------------------------------- void initializeScene() { int id = -1; lightPos = vec3( 0.0, 3.0, 9.0 ); // Light Visualizer spheres[0].position = lightPos; spheres[0].radius = 0.5; spheres[0].materialIndex = 0; spheres[0].lightVisualizer = 1; spheres[0].id = ++id; spheres[1].position = vec3( -5.0, 1.0, -3.0 ); spheres[1].radius = 1.0; spheres[1].materialIndex = 1; spheres[1].id = ++id; spheres[2].position = vec3( 0.0, 1.0, 1.0 ); spheres[2].radius = 1.0; spheres[2].materialIndex = 2; spheres[2].id = ++id; spheres[3].position = vec3( 5.0, 1.0, -3.0 ); spheres[3].radius = 1.0; spheres[3].materialIndex = 3; spheres[3].id = ++id; spheres[4].position = vec3( 0.0, 1.0, 1.0 ); spheres[4].radius = 1.0; spheres[4].materialIndex = 10; spheres[4].id = ++id; planes[0].pointOnPlane = vec3( 0.0, 0.0, 0.0 ); planes[0].normal = vec3( 0.0, 1.0, 0.0 ); planes[0].materialIndex = 4; planes[0].id = ++id; planes[1].pointOnPlane = vec3( -10.0, 0.0, 0.0 ); planes[1].normal = vec3( 1.0, 0.0, 0.0 ); planes[1].materialIndex = 5; planes[1].id = ++id; planes[2].pointOnPlane = vec3( 10.0, 0.0, 0.0 ); planes[2].normal = vec3( -1.0, 0.0, 0.0 ); planes[2].materialIndex = 6; planes[2].id = ++id; planes[3].pointOnPlane = vec3( 0.0, 0.0, -10.0 ); planes[3].normal = vec3( 0.0, 0.0, 1.0 ); planes[3].materialIndex = 7; planes[3].id = ++id; planes[4].pointOnPlane = vec3( 0.0, 15.0, 0.0 ); planes[4].normal = vec3( 0.0, -1.0, 0.0 ); planes[4].materialIndex = 8; planes[4].id = ++id; planes[5].pointOnPlane = vec3( 0.0, 0.0, 25.0 ); planes[5].normal = vec3( 0.0, 0.0, -1.0 ); planes[5].materialIndex = 9; planes[5].id = ++id; } //-------------------------------------------------------------------------------------------- void onAnimateScene() { float animationTime = TimeValue * 0.5; lightPos = vec3( sin(TimeValue* 0.3)*5.0, 4.3, cos(TimeValue * 0.3)*10.0 ); spheres[0].position = lightPos; spheres[2].position.x += sin( animationTime * 1.0 ) * 3.0; spheres[2].position.y += abs( sin( animationTime * 4.0 ) * 2.0 ); spheres[2].position.z += sin( cos( animationTime) * 3.0 ) * 2.0; spheres[4].position.x -= sin( animationTime * 1.0 ) * 3.0; spheres[4].position.y += abs( sin( animationTime * 5.3 ) * 2.0 ); spheres[4].position.z -= sin( cos( animationTime) * 3.0 ) * 2.0; } //-------------------------------------------------------------------------------------------- vec3 getObjectsBaseColor(HitInfo hitInfo) { Material mat = getMaterial(hitInfo.materialIndex); if( hitInfo.id == 5 || hitInfo.id == 9 ) { return checkerBoardTexture( hitInfo.position ) * mat.diffuseColor; } return mat.diffuseColor; } //-------------------------------------------------------------------------------------------- vec3 calculateReflection( vec3 viewDir, HitInfo hitInfo ) { Material objectMaterial = getMaterial(hitInfo.materialIndex); if( objectMaterial.reflectionIntensity <= 0.00 ) { return vec3( 0.0 ); } Ray reflectionRay; reflectionRay.direction = normalize( reflect( hitInfo.rayDir, hitInfo.normal ) ); reflectionRay.origin = hitInfo.position + hitInfo.normal * Epsilon; HitInfo rHitInfo = intersect( reflectionRay ); vec3 color = getObjectsBaseColor(rHitInfo) * objectMaterial.reflectionIntensity; return color; } //-------------------------------------------------------------------------------------------- vec3 calculateRefraction( vec3 viewDir, HitInfo hitInfo ) { Material objectMaterial = getMaterial(hitInfo.materialIndex); if( objectMaterial.refractionIntensity <= 0.00 ) { return vec3( 0.0 ); } Ray refractionRay; refractionRay.direction = normalize( refract( hitInfo.rayDir, hitInfo.normal, objectMaterial.refractionIndex ) ); refractionRay.origin = hitInfo.position - hitInfo.normal * Epsilon; HitInfo rHitInfo = intersect( refractionRay ); for( int i = 0; i < 10; ++i ) { if( rHitInfo.id == hitInfo.id ) { refractionRay.origin = rHitInfo.position - rHitInfo.normal * Epsilon; rHitInfo = intersect( refractionRay ); } else { break; } } vec3 color = getObjectsBaseColor(rHitInfo) * objectMaterial.refractionIntensity; return color; } //-------------------------------------------------------------------------------------------- float calculateShadow( vec3 lightDir, HitInfo hitInfo ) { Ray shadowRay; shadowRay.direction = normalize( lightDir ); shadowRay.origin = hitInfo.position + (shadowRay.direction * 0.001); HitInfo shadowHitInfo = intersect( shadowRay ); Material mat = getMaterial(shadowHitInfo.materialIndex); return clamp( mat.shadowCasterIntensity, 0.00, 1.0 ); } //-------------------------------------------------------------------------------------------- void main(void) { float mx = (mouse.x * 2.0 - 1.0) * (ResolutionValue.x / ResolutionValue.y); float my = (mouse.y * 2.0 - 1.0); vec2 mouseOffset = vec2( mx, my ); vec2 uv = gl_FragCoord.xy / ResolutionValue.x; vec2 uvp = (uv * 2.0 - 1.0) + mouseOffset; Ray ray; ray.origin = vec3( 0.0, 5.0, 10.0 ); ray.direction = normalize( vec3( uvp, -1.0 ) ); initializeMaterials(); initializeScene(); onAnimateScene(); HitInfo hitInfo = intersect( ray ); vec3 c = vec3( 0.9 ); if( hitInfo.primitiveType <= PrimitiveType_None ) { gl_FragColor = vec4( c, 1.0 ); return; } vec3 lightDir = normalize( lightPos - hitInfo.position ); float shadowFactor = calculateShadow( lightDir, hitInfo ); // Calculate Lighting Material mat = getMaterial( hitInfo.materialIndex ); float diffuseFactor = clamp( dot( hitInfo.normal, lightDir ), 0.00, 1.00 ); vec3 reflectionVector = normalize( reflect( lightDir, hitInfo.normal ) ); vec3 cameraToSurface = ray.direction; float specularFactor = pow( clamp( dot( reflectionVector, cameraToSurface ), 0.00, 1.0 ), mat.specularIntensity ); vec3 baseColor = getObjectsBaseColor(hitInfo); vec3 reflection = calculateReflection( ray.direction, hitInfo ); vec3 refraction = calculateRefraction( ray.direction, hitInfo ); vec3 diffuse = (baseColor + reflection + refraction) * diffuseFactor; vec3 specular = (mat.specularColor * specularFactor) * step( 1.0, shadowFactor ); c = (diffuse + specular) * shadowFactor; gl_FragColor = vec4( c, 1.0 ); } |