1 // This file has been automatically generated from resource file src/Shaders/RaytraceBase.fs
3 static const char Shaders_RaytraceBase_fs[] =
4 "#ifdef ADAPTIVE_SAMPLING\n"
5 " #extension GL_ARB_shader_image_load_store : require\n"
7 "#ifdef ADAPTIVE_SAMPLING_ATOMIC\n"
8 " #extension GL_NV_shader_atomic_float : require\n"
11 "#ifdef USE_TEXTURES\n"
12 " #extension GL_ARB_bindless_texture : require\n"
15 "//! Normalized pixel coordinates.\n"
18 "//! Sub-pixel offset in for FSAA.\n"
19 "uniform vec2 uFsaaOffset;\n"
20 "//! Sub-pixel offset in Y direction for FSAA.\n"
21 "uniform float uOffsetY;\n"
23 "//! Origin of viewing ray in left-top corner.\n"
24 "uniform vec3 uOriginLT;\n"
25 "//! Origin of viewing ray in left-bottom corner.\n"
26 "uniform vec3 uOriginLB;\n"
27 "//! Origin of viewing ray in right-top corner.\n"
28 "uniform vec3 uOriginRT;\n"
29 "//! Origin of viewing ray in right-bottom corner.\n"
30 "uniform vec3 uOriginRB;\n"
32 "//! Width of the rendering window.\n"
33 "uniform int uWinSizeX;\n"
34 "//! Height of the rendering window.\n"
35 "uniform int uWinSizeY;\n"
37 "//! Direction of viewing ray in left-top corner.\n"
38 "uniform vec3 uDirectLT;\n"
39 "//! Direction of viewing ray in left-bottom corner.\n"
40 "uniform vec3 uDirectLB;\n"
41 "//! Direction of viewing ray in right-top corner.\n"
42 "uniform vec3 uDirectRT;\n"
43 "//! Direction of viewing ray in right-bottom corner.\n"
44 "uniform vec3 uDirectRB;\n"
46 "//! Inverse model-view-projection matrix.\n"
47 "uniform mat4 uUnviewMat;\n"
49 "//! Model-view-projection matrix.\n"
50 "uniform mat4 uViewMat;\n"
52 "//! Texture buffer of data records of bottom-level BVH nodes.\n"
53 "uniform isamplerBuffer uSceneNodeInfoTexture;\n"
54 "//! Texture buffer of minimum points of bottom-level BVH nodes.\n"
55 "uniform samplerBuffer uSceneMinPointTexture;\n"
56 "//! Texture buffer of maximum points of bottom-level BVH nodes.\n"
57 "uniform samplerBuffer uSceneMaxPointTexture;\n"
58 "//! Texture buffer of transformations of high-level BVH nodes.\n"
59 "uniform samplerBuffer uSceneTransformTexture;\n"
61 "//! Texture buffer of vertex coords.\n"
62 "uniform samplerBuffer uGeometryVertexTexture;\n"
63 "//! Texture buffer of vertex normals.\n"
64 "uniform samplerBuffer uGeometryNormalTexture;\n"
65 "#ifdef USE_TEXTURES\n"
66 " //! Texture buffer of per-vertex UV-coordinates.\n"
67 " uniform samplerBuffer uGeometryTexCrdTexture;\n"
69 "//! Texture buffer of triangle indices.\n"
70 "uniform isamplerBuffer uGeometryTriangTexture;\n"
72 "//! Texture buffer of material properties.\n"
73 "uniform samplerBuffer uRaytraceMaterialTexture;\n"
74 "//! Texture buffer of light source properties.\n"
75 "uniform samplerBuffer uRaytraceLightSrcTexture;\n"
77 "#ifdef BACKGROUND_CUBEMAP\n"
78 " //! Environment cubemap texture.\n"
79 " uniform samplerCube uEnvMapTexture;\n"
80 " //! Coefficient of Y controlling horizontal flip of cubemap\n"
81 " uniform int uYCoeff;\n"
82 " //! Coefficient of Z controlling vertical flip of cubemap\n"
83 " uniform int uZCoeff;\n"
85 " //! Environment map texture.\n"
86 " uniform sampler2D uEnvMapTexture;\n"
89 "//! Total number of light sources.\n"
90 "uniform int uLightCount;\n"
91 "//! Intensity of global ambient light.\n"
92 "uniform vec4 uGlobalAmbient;\n"
94 "//! Enables/disables hard shadows.\n"
95 "uniform int uShadowsEnabled;\n"
96 "//! Enables/disables specular reflections.\n"
97 "uniform int uReflectEnabled;\n"
98 "//! Enables/disables environment map lighting.\n"
99 "uniform int uEnvMapEnabled;\n"
100 "//! Enables/disables environment map background.\n"
101 "uniform int uEnvMapForBack;\n"
103 "//! Radius of bounding sphere of the scene.\n"
104 "uniform float uSceneRadius;\n"
105 "//! Scene epsilon to prevent self-intersections.\n"
106 "uniform float uSceneEpsilon;\n"
108 "#ifdef USE_TEXTURES\n"
109 " //! Unique 64-bit handles of OpenGL textures.\n"
110 " uniform uvec2 uTextureSamplers[MAX_TEX_NUMBER];\n"
113 "#ifdef ADAPTIVE_SAMPLING\n"
114 " //! OpenGL image used for accumulating rendering result.\n"
115 " volatile restrict layout(r32f) uniform image2D uRenderImage;\n"
117 "#ifdef ADAPTIVE_SAMPLING_ATOMIC\n"
118 " //! OpenGL image storing offsets of sampled pixels blocks.\n"
119 " coherent restrict layout(rg32i) uniform iimage2D uOffsetImage;\n"
121 " //! OpenGL image defining per-tile amount of samples.\n"
122 " volatile restrict layout(r32i) uniform iimage2D uTilesImage;\n"
125 " //! Screen space tile size.\n"
126 " uniform ivec2 uTileSize;\n"
129 "//! Top color of gradient background.\n"
130 "uniform vec4 uBackColorTop;\n"
131 "//! Bottom color of gradient background.\n"
132 "uniform vec4 uBackColorBot;\n"
134 "//! Aperture radius of camera used for depth-of-field\n"
135 "uniform float uApertureRadius;\n"
137 "//! Focal distance of camera used for depth-of field\n"
138 "uniform float uFocalPlaneDist;\n"
140 "//! Camera position used for projective mode\n"
141 "uniform vec3 uEyeOrig;\n"
143 "//! Camera view direction used for projective mode\n"
144 "uniform vec3 uEyeView;\n"
146 "//! Camera's screen vertical direction used for projective mode\n"
147 "uniform vec3 uEyeVert;\n"
149 "//! Camera's screen horizontal direction used for projective mode\n"
150 "uniform vec3 uEyeSide;\n"
152 "//! Camera's screen size used for projective mode\n"
153 "uniform vec2 uEyeSize;\n"
155 "/////////////////////////////////////////////////////////////////////////////////////////\n"
156 "// Specific data types\n"
158 "//! Stores ray parameters.\n"
165 "//! Stores intersection parameters.\n"
166 "struct SIntersect\n"
173 "//! Stores triangle's vertex indexes and vertexes itself\n"
180 "/////////////////////////////////////////////////////////////////////////////////////////\n"
181 "// Some useful constants\n"
183 "#define MAXFLOAT 1e15f\n"
185 "#define SMALL vec3 (exp2 (-80.0f))\n"
187 "#define ZERO vec3 (0.0f, 0.0f, 0.0f)\n"
188 "#define UNIT vec3 (1.0f, 1.0f, 1.0f)\n"
190 "#define AXIS_X vec3 (1.0f, 0.0f, 0.0f)\n"
191 "#define AXIS_Y vec3 (0.0f, 1.0f, 0.0f)\n"
192 "#define AXIS_Z vec3 (0.0f, 0.0f, 1.0f)\n"
194 "#define M_PI 3.141592653f\n"
195 "#define M_2_PI 6.283185307f\n"
196 "#define M_PI_2 1.570796327f\n"
198 "#define LUMA vec3 (0.2126f, 0.7152f, 0.0722f)\n"
200 "// =======================================================================\n"
201 "// function : MatrixRowMultiplyDir\n"
202 "// purpose : Multiplies a vector by matrix\n"
203 "// =======================================================================\n"
204 "vec3 MatrixRowMultiplyDir (in vec3 v,\n"
209 " return vec3 (dot (m0.xyz, v),\n"
210 " dot (m1.xyz, v),\n"
211 " dot (m2.xyz, v));\n"
214 "//! 32-bit state of random number generator.\n"
217 "// =======================================================================\n"
218 "// function : SeedRand\n"
219 "// purpose : Applies hash function by Thomas Wang to randomize seeds\n"
220 "// (see http://www.burtleburtle.net/bob/hash/integer.html)\n"
221 "// =======================================================================\n"
222 "void SeedRand (in int theSeed, in int theSizeX, in int theRadius)\n"
224 " RandState = uint (int (gl_FragCoord.y) / theRadius * theSizeX + int (gl_FragCoord.x) / theRadius + theSeed);\n"
226 " RandState = (RandState + 0x479ab41du) + (RandState << 8);\n"
227 " RandState = (RandState ^ 0xe4aa10ceu) ^ (RandState >> 5);\n"
228 " RandState = (RandState + 0x9942f0a6u) - (RandState << 14);\n"
229 " RandState = (RandState ^ 0x5aedd67du) ^ (RandState >> 3);\n"
230 " RandState = (RandState + 0x17bea992u) + (RandState << 7);\n"
233 "// =======================================================================\n"
234 "// function : RandInt\n"
235 "// purpose : Generates integer using Xorshift algorithm by G. Marsaglia\n"
236 "// =======================================================================\n"
239 " RandState ^= (RandState << 13);\n"
240 " RandState ^= (RandState >> 17);\n"
241 " RandState ^= (RandState << 5);\n"
243 " return RandState;\n"
246 "// =======================================================================\n"
247 "// function : RandFloat\n"
248 "// purpose : Generates a random float in 0 <= x < 1 range\n"
249 "// =======================================================================\n"
250 "float RandFloat()\n"
252 " return float (RandInt()) * (1.f / 4294967296.f);\n"
255 "// =======================================================================\n"
256 "// function : MatrixColMultiplyPnt\n"
257 "// purpose : Multiplies a vector by matrix\n"
258 "// =======================================================================\n"
259 "vec3 MatrixColMultiplyPnt (in vec3 v,\n"
265 " return vec3 (m0.x * v.x + m1.x * v.y + m2.x * v.z + m3.x,\n"
266 " m0.y * v.x + m1.y * v.y + m2.y * v.z + m3.y,\n"
267 " m0.z * v.x + m1.z * v.y + m2.z * v.z + m3.z);\n"
270 "// =======================================================================\n"
271 "// function : MatrixColMultiplyDir\n"
272 "// purpose : Multiplies a vector by matrix\n"
273 "// =======================================================================\n"
274 "vec3 MatrixColMultiplyDir (in vec3 v,\n"
279 " return vec3 (m0.x * v.x + m1.x * v.y + m2.x * v.z,\n"
280 " m0.y * v.x + m1.y * v.y + m2.y * v.z,\n"
281 " m0.z * v.x + m1.z * v.y + m2.z * v.z);\n"
284 "//=======================================================================\n"
285 "// function : InverseDirection\n"
286 "// purpose : Returns safely inverted direction of the given one\n"
287 "//=======================================================================\n"
288 "vec3 InverseDirection (in vec3 theInput)\n"
290 " vec3 anInverse = 1.f / max (abs (theInput), SMALL);\n"
292 " return mix (-anInverse, anInverse, step (ZERO, theInput));\n"
295 "//=======================================================================\n"
296 "// function : BackgroundColor\n"
297 "// purpose : Returns color of gradient background\n"
298 "//=======================================================================\n"
299 "vec4 BackgroundColor()\n"
301 "#ifdef ADAPTIVE_SAMPLING_ATOMIC\n"
303 " ivec2 aFragCoord = ivec2 (gl_FragCoord.xy);\n"
305 " ivec2 aTileXY = imageLoad (uOffsetImage, aFragCoord / uTileSize).xy * uTileSize;\n"
307 " aTileXY.y += aFragCoord.y % min (uWinSizeY - aTileXY.y, uTileSize.y);\n"
309 " return mix (uBackColorBot, uBackColorTop, float (aTileXY.y) / uWinSizeY);\n"
313 " return mix (uBackColorBot, uBackColorTop, vPixel.y);\n"
318 "/////////////////////////////////////////////////////////////////////////////////////////\n"
319 "// Functions for compute ray-object intersection\n"
321 "//=======================================================================\n"
322 "// function : sampleUniformDisk\n"
324 "//=======================================================================\n"
325 "vec2 sampleUniformDisk ()\n"
329 " float aKsi1 = 2.f * RandFloat () - 1.f;\n"
330 " float aKsi2 = 2.f * RandFloat () - 1.f;\n"
332 " if (aKsi1 > -aKsi2)\n"
334 " if (aKsi1 > aKsi2)\n"
335 " aPoint = vec2 (aKsi1, (M_PI / 4.f) * (0.f + aKsi2 / aKsi1));\n"
337 " aPoint = vec2 (aKsi2, (M_PI / 4.f) * (2.f - aKsi1 / aKsi2));\n"
341 " if (aKsi1 < aKsi2)\n"
342 " aPoint = vec2 (-aKsi1, (M_PI / 4.f) * (4.f + aKsi2 / aKsi1));\n"
344 " aPoint = vec2 (-aKsi2, (M_PI / 4.f) * (6.f - aKsi1 / aKsi2));\n"
347 " return vec2 (sin (aPoint.y), cos (aPoint.y)) * aPoint.x;\n"
350 "// =======================================================================\n"
351 "// function : GenerateRay\n"
353 "// =======================================================================\n"
354 "SRay GenerateRay (in vec2 thePixel)\n"
356 "#ifndef DEPTH_OF_FIELD\n"
358 " vec3 aP0 = mix (uOriginLB, uOriginRB, thePixel.x);\n"
359 " vec3 aP1 = mix (uOriginLT, uOriginRT, thePixel.x);\n"
361 " vec3 aD0 = mix (uDirectLB, uDirectRB, thePixel.x);\n"
362 " vec3 aD1 = mix (uDirectLT, uDirectRT, thePixel.x);\n"
364 " vec3 aDirection = normalize (mix (aD0, aD1, thePixel.y));\n"
366 " return SRay (mix (aP0, aP1, thePixel.y), aDirection);\n"
370 " vec2 aPixel = uEyeSize * (thePixel - vec2 (0.5f)) * 2.f;\n"
372 " vec2 aAperturePnt = sampleUniformDisk () * uApertureRadius;\n"
374 " vec3 aLocalDir = normalize (vec3 (\n"
375 " aPixel * uFocalPlaneDist - aAperturePnt, uFocalPlaneDist));\n"
377 " vec3 aOrigin = uEyeOrig +\n"
378 " uEyeSide * aAperturePnt.x +\n"
379 " uEyeVert * aAperturePnt.y;\n"
381 " vec3 aDirect = uEyeView * aLocalDir.z +\n"
382 " uEyeSide * aLocalDir.x +\n"
383 " uEyeVert * aLocalDir.y;\n"
385 " return SRay (aOrigin, aDirect);\n"
390 "// =======================================================================\n"
391 "// function : IntersectSphere\n"
392 "// purpose : Computes ray-sphere intersection\n"
393 "// =======================================================================\n"
394 "float IntersectSphere (in SRay theRay, in float theRadius)\n"
396 " float aDdotD = dot (theRay.Direct, theRay.Direct);\n"
397 " float aDdotO = dot (theRay.Direct, theRay.Origin);\n"
398 " float aOdotO = dot (theRay.Origin, theRay.Origin);\n"
400 " float aD = aDdotO * aDdotO - aDdotD * (aOdotO - theRadius * theRadius);\n"
404 " float aTime = (sqrt (aD) - aDdotO) * (1.0f / aDdotD);\n"
406 " return aTime > 0.0f ? aTime : MAXFLOAT;\n"
409 " return MAXFLOAT;\n"
412 "// =======================================================================\n"
413 "// function : IntersectTriangle\n"
414 "// purpose : Computes ray-triangle intersection (branchless version)\n"
415 "// =======================================================================\n"
416 "void IntersectTriangle (in SRay theRay,\n"
417 " in vec3 thePnt0,\n"
418 " in vec3 thePnt1,\n"
419 " in vec3 thePnt2,\n"
420 " out vec3 theUVT,\n"
421 " out vec3 theNorm)\n"
423 " vec3 aToTrg = thePnt0 - theRay.Origin;\n"
425 " vec3 aEdge0 = thePnt1 - thePnt0;\n"
426 " vec3 aEdge1 = thePnt0 - thePnt2;\n"
428 " theNorm = cross (aEdge1, aEdge0);\n"
430 " vec3 theVect = cross (theRay.Direct, aToTrg);\n"
432 " theUVT = vec3 (dot (theNorm, aToTrg),\n"
433 " dot (theVect, aEdge1),\n"
434 " dot (theVect, aEdge0)) * (1.f / dot (theNorm, theRay.Direct));\n"
436 " theUVT.x = any (lessThan (theUVT, ZERO)) || (theUVT.y + theUVT.z) > 1.f ? MAXFLOAT : theUVT.x;\n"
439 "#define EMPTY_ROOT ivec4(0)\n"
441 "//! Utility structure containing information about\n"
442 "//! currently traversing sub-tree of scene's BVH.\n"
445 " //! Transformed ray.\n"
448 " //! Inversed ray direction.\n"
451 " //! Parameters of sub-root node.\n"
455 "#define MATERIAL_AMBN(index) (19 * index + 0)\n"
456 "#define MATERIAL_DIFF(index) (19 * index + 1)\n"
457 "#define MATERIAL_SPEC(index) (19 * index + 2)\n"
458 "#define MATERIAL_EMIS(index) (19 * index + 3)\n"
459 "#define MATERIAL_REFL(index) (19 * index + 4)\n"
460 "#define MATERIAL_REFR(index) (19 * index + 5)\n"
461 "#define MATERIAL_TRAN(index) (19 * index + 6)\n"
462 "#define MATERIAL_TRS1(index) (19 * index + 7)\n"
463 "#define MATERIAL_TRS2(index) (19 * index + 8)\n"
464 "#define MATERIAL_TRS3(index) (19 * index + 9)\n"
466 "#define TRS_OFFSET(treelet) treelet.SubData.x\n"
467 "#define BVH_OFFSET(treelet) treelet.SubData.y\n"
468 "#define VRT_OFFSET(treelet) treelet.SubData.z\n"
469 "#define TRG_OFFSET(treelet) treelet.SubData.w\n"
471 "//! Identifies the absence of intersection.\n"
472 "#define INVALID_HIT ivec4 (-1)\n"
474 "//! Global stack shared between traversal functions.\n"
475 "int Stack[STACK_SIZE];\n"
477 "// =======================================================================\n"
478 "// function : pop\n"
480 "// =======================================================================\n"
481 "int pop (inout int theHead)\n"
483 " int aData = Stack[theHead];\n"
485 " int aMask = aData >> 26;\n"
486 " int aNode = aMask & 0x3;\n"
490 " if ((aMask & 0x3) == aNode)\n"
496 " aMask |= (aMask << 2) & 0x30;\n"
498 " Stack[theHead] = (aData & 0x03FFFFFF) | (aMask << 26);\n"
501 " return (aData & 0x03FFFFFF) + aNode;\n"
504 "// =======================================================================\n"
505 "// function : SceneNearestHit\n"
506 "// purpose : Finds intersection with nearest scene triangle\n"
507 "// =======================================================================\n"
508 "STriangle SceneNearestHit (in SRay theRay, in vec3 theInverse, inout SIntersect theHit, out int theTrsfId)\n"
510 " STriangle aTriangle = STriangle (INVALID_HIT, vec3[](vec3(0.0), vec3(0.0), vec3(0.0)));\n"
512 " int aNode = 0; // node to traverse\n"
513 " int aHead = -1; // pointer of stack\n"
514 " int aStop = -1; // BVH level switch\n"
516 " SSubTree aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);\n"
518 " for (bool toContinue = true; toContinue; /* none */)\n"
520 " ivec4 aData = texelFetch (uSceneNodeInfoTexture, aNode);\n"
522 " if (aData.x == 0) // if inner node\n"
524 " aData.y += BVH_OFFSET (aSubTree);\n"
526 " vec4 aHitTimes = vec4 (MAXFLOAT,\n"
531 " vec3 aRayOriginInverse = -aSubTree.TrsfRay.Origin * aSubTree.Inverse;\n"
533 " vec3 aNodeMin0 = texelFetch (uSceneMinPointTexture, aData.y + 0).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
534 " vec3 aNodeMin1 = texelFetch (uSceneMinPointTexture, aData.y + 1).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
535 " vec3 aNodeMin2 = texelFetch (uSceneMinPointTexture, aData.y + min (2, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
536 " vec3 aNodeMin3 = texelFetch (uSceneMinPointTexture, aData.y + min (3, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
537 " vec3 aNodeMax0 = texelFetch (uSceneMaxPointTexture, aData.y + 0).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
538 " vec3 aNodeMax1 = texelFetch (uSceneMaxPointTexture, aData.y + 1).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
539 " vec3 aNodeMax2 = texelFetch (uSceneMaxPointTexture, aData.y + min (2, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
540 " vec3 aNodeMax3 = texelFetch (uSceneMaxPointTexture, aData.y + min (3, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
542 " vec3 aTimeMax = max (aNodeMin0, aNodeMax0);\n"
543 " vec3 aTimeMin = min (aNodeMin0, aNodeMax0);\n"
545 " float aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
546 " float aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
548 " aHitTimes.x = (aTimeEnter <= aTimeLeave && aTimeEnter <= theHit.Time && aTimeLeave >= 0.f) ? aTimeEnter : MAXFLOAT;\n"
550 " aTimeMax = max (aNodeMin1, aNodeMax1);\n"
551 " aTimeMin = min (aNodeMin1, aNodeMax1);\n"
553 " aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
554 " aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
556 " aHitTimes.y = (aTimeEnter <= aTimeLeave && aTimeEnter <= theHit.Time && aTimeLeave >= 0.f) ? aTimeEnter : MAXFLOAT;\n"
558 " aTimeMax = max (aNodeMin2, aNodeMax2);\n"
559 " aTimeMin = min (aNodeMin2, aNodeMax2);\n"
561 " aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
562 " aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
564 " aHitTimes.z = (aTimeEnter <= aTimeLeave && aTimeEnter <= theHit.Time && aTimeLeave >= 0.f && aData.z > 1) ? aTimeEnter : MAXFLOAT;\n"
566 " aTimeMax = max (aNodeMin3, aNodeMax3);\n"
567 " aTimeMin = min (aNodeMin3, aNodeMax3);\n"
569 " aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
570 " aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
572 " aHitTimes.w = (aTimeEnter <= aTimeLeave && aTimeEnter <= theHit.Time && aTimeLeave >= 0.f && aData.z > 2) ? aTimeEnter : MAXFLOAT;\n"
574 " ivec4 aChildren = ivec4 (0, 1, 2, 3);\n"
576 " aChildren.xy = aHitTimes.y < aHitTimes.x ? aChildren.yx : aChildren.xy;\n"
577 " aHitTimes.xy = aHitTimes.y < aHitTimes.x ? aHitTimes.yx : aHitTimes.xy;\n"
578 " aChildren.zw = aHitTimes.w < aHitTimes.z ? aChildren.wz : aChildren.zw;\n"
579 " aHitTimes.zw = aHitTimes.w < aHitTimes.z ? aHitTimes.wz : aHitTimes.zw;\n"
580 " aChildren.xz = aHitTimes.z < aHitTimes.x ? aChildren.zx : aChildren.xz;\n"
581 " aHitTimes.xz = aHitTimes.z < aHitTimes.x ? aHitTimes.zx : aHitTimes.xz;\n"
582 " aChildren.yw = aHitTimes.w < aHitTimes.y ? aChildren.wy : aChildren.yw;\n"
583 " aHitTimes.yw = aHitTimes.w < aHitTimes.y ? aHitTimes.wy : aHitTimes.yw;\n"
584 " aChildren.yz = aHitTimes.z < aHitTimes.y ? aChildren.zy : aChildren.yz;\n"
585 " aHitTimes.yz = aHitTimes.z < aHitTimes.y ? aHitTimes.zy : aHitTimes.yz;\n"
587 " if (aHitTimes.x != MAXFLOAT)\n"
589 " int aHitMask = (aHitTimes.w != MAXFLOAT ? aChildren.w : aChildren.z) << 2\n"
590 " | (aHitTimes.z != MAXFLOAT ? aChildren.z : aChildren.y);\n"
592 " if (aHitTimes.y != MAXFLOAT)\n"
593 " Stack[++aHead] = aData.y | (aHitMask << 2 | aChildren.y) << 26;\n"
595 " aNode = aData.y + aChildren.x;\n"
599 " toContinue = (aHead >= 0);\n"
601 " if (aHead == aStop) // go to top-level BVH\n"
603 " aStop = -1; aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);\n"
607 " aNode = pop (aHead);\n"
610 " else if (aData.x < 0) // leaf node (contains triangles)\n"
615 " for (int anIdx = aData.y; anIdx <= aData.z; ++anIdx)\n"
617 " ivec4 aTriIndex = texelFetch (uGeometryTriangTexture, anIdx + TRG_OFFSET (aSubTree));\n"
618 " vec3 aPoints[3];\n"
620 " aPoints[0] = texelFetch (uGeometryVertexTexture, aTriIndex.x += VRT_OFFSET (aSubTree)).xyz;\n"
621 " aPoints[1] = texelFetch (uGeometryVertexTexture, aTriIndex.y += VRT_OFFSET (aSubTree)).xyz;\n"
622 " aPoints[2] = texelFetch (uGeometryVertexTexture, aTriIndex.z += VRT_OFFSET (aSubTree)).xyz;\n"
624 " IntersectTriangle (aSubTree.TrsfRay, aPoints[0], aPoints[1], aPoints[2], aTimeUV, aNormal);\n"
626 " if (aTimeUV.x < theHit.Time)\n"
628 " aTriangle.TriIndex = aTriIndex;\n"
629 " for (int i = 0; i < 3; ++i)\n"
631 " aTriangle.Points[i] = aPoints[i];\n"
634 " theTrsfId = TRS_OFFSET (aSubTree);\n"
636 " theHit = SIntersect (aTimeUV.x, aTimeUV.yz, aNormal);\n"
640 " toContinue = (aHead >= 0);\n"
642 " if (aHead == aStop) // go to top-level BVH\n"
644 " aStop = -1; aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);\n"
648 " aNode = pop (aHead);\n"
650 " else if (aData.x > 0) // switch node\n"
652 " aSubTree.SubData = ivec4 (4 * aData.x - 4, aData.yzw); // store BVH sub-root\n"
654 " vec4 aInvTransf0 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 0);\n"
655 " vec4 aInvTransf1 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 1);\n"
656 " vec4 aInvTransf2 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 2);\n"
657 " vec4 aInvTransf3 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 3);\n"
659 " aSubTree.TrsfRay.Direct = MatrixColMultiplyDir (theRay.Direct,\n"
664 " aSubTree.Inverse = mix (-UNIT, UNIT, step (ZERO, aSubTree.TrsfRay.Direct)) /\n"
665 " max (abs (aSubTree.TrsfRay.Direct), SMALL);\n"
667 " aSubTree.TrsfRay.Origin = MatrixColMultiplyPnt (theRay.Origin,\n"
673 " aNode = BVH_OFFSET (aSubTree); // go to sub-root node\n"
675 " aStop = aHead; // store current stack pointer\n"
679 " return aTriangle;\n"
682 "// =======================================================================\n"
683 "// function : SceneAnyHit\n"
684 "// purpose : Finds intersection with any scene triangle\n"
685 "// =======================================================================\n"
686 "float SceneAnyHit (in SRay theRay, in vec3 theInverse, in float theDistance)\n"
688 " float aFactor = 1.f;\n"
690 " int aNode = 0; // node to traverse\n"
691 " int aHead = -1; // pointer of stack\n"
692 " int aStop = -1; // BVH level switch\n"
694 " SSubTree aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);\n"
696 " for (bool toContinue = true; toContinue; /* none */)\n"
698 " ivec4 aData = texelFetch (uSceneNodeInfoTexture, aNode);\n"
700 " if (aData.x == 0) // if inner node\n"
702 " aData.y += BVH_OFFSET (aSubTree);\n"
704 " vec4 aHitTimes = vec4 (MAXFLOAT,\n"
709 " vec3 aRayOriginInverse = -aSubTree.TrsfRay.Origin * aSubTree.Inverse;\n"
711 " vec3 aNodeMin0 = texelFetch (uSceneMinPointTexture, aData.y + 0).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
712 " vec3 aNodeMin1 = texelFetch (uSceneMinPointTexture, aData.y + 1).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
713 " vec3 aNodeMin2 = texelFetch (uSceneMinPointTexture, aData.y + min (2, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
714 " vec3 aNodeMin3 = texelFetch (uSceneMinPointTexture, aData.y + min (3, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
715 " vec3 aNodeMax0 = texelFetch (uSceneMaxPointTexture, aData.y + 0).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
716 " vec3 aNodeMax1 = texelFetch (uSceneMaxPointTexture, aData.y + 1).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
717 " vec3 aNodeMax2 = texelFetch (uSceneMaxPointTexture, aData.y + min (2, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
718 " vec3 aNodeMax3 = texelFetch (uSceneMaxPointTexture, aData.y + min (3, aData.z)).xyz * aSubTree.Inverse + aRayOriginInverse;\n"
720 " vec3 aTimeMax = max (aNodeMin0, aNodeMax0);\n"
721 " vec3 aTimeMin = min (aNodeMin0, aNodeMax0);\n"
723 " float aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
724 " float aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
726 " aHitTimes.x = (aTimeEnter <= aTimeLeave && aTimeEnter <= theDistance && aTimeLeave >= 0.f) ? aTimeEnter : MAXFLOAT;\n"
728 " aTimeMax = max (aNodeMin1, aNodeMax1);\n"
729 " aTimeMin = min (aNodeMin1, aNodeMax1);\n"
731 " aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
732 " aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
734 " aHitTimes.y = (aTimeEnter <= aTimeLeave && aTimeEnter <= theDistance && aTimeLeave >= 0.f) ? aTimeEnter : MAXFLOAT;\n"
736 " aTimeMax = max (aNodeMin2, aNodeMax2);\n"
737 " aTimeMin = min (aNodeMin2, aNodeMax2);\n"
739 " aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
740 " aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
742 " aHitTimes.z = (aTimeEnter <= aTimeLeave && aTimeEnter <= theDistance && aTimeLeave >= 0.f && aData.z > 1) ? aTimeEnter : MAXFLOAT;\n"
744 " aTimeMax = max (aNodeMin3, aNodeMax3);\n"
745 " aTimeMin = min (aNodeMin3, aNodeMax3);\n"
747 " aTimeLeave = min (aTimeMax.x, min (aTimeMax.y, aTimeMax.z));\n"
748 " aTimeEnter = max (aTimeMin.x, max (aTimeMin.y, aTimeMin.z));\n"
750 " aHitTimes.w = (aTimeEnter <= aTimeLeave && aTimeEnter <= theDistance && aTimeLeave >= 0.f && aData.z > 2) ? aTimeEnter : MAXFLOAT;\n"
752 " ivec4 aChildren = ivec4 (0, 1, 2, 3);\n"
754 " aChildren.xy = aHitTimes.y < aHitTimes.x ? aChildren.yx : aChildren.xy;\n"
755 " aHitTimes.xy = aHitTimes.y < aHitTimes.x ? aHitTimes.yx : aHitTimes.xy;\n"
756 " aChildren.zw = aHitTimes.w < aHitTimes.z ? aChildren.wz : aChildren.zw;\n"
757 " aHitTimes.zw = aHitTimes.w < aHitTimes.z ? aHitTimes.wz : aHitTimes.zw;\n"
758 " aChildren.xz = aHitTimes.z < aHitTimes.x ? aChildren.zx : aChildren.xz;\n"
759 " aHitTimes.xz = aHitTimes.z < aHitTimes.x ? aHitTimes.zx : aHitTimes.xz;\n"
760 " aChildren.yw = aHitTimes.w < aHitTimes.y ? aChildren.wy : aChildren.yw;\n"
761 " aHitTimes.yw = aHitTimes.w < aHitTimes.y ? aHitTimes.wy : aHitTimes.yw;\n"
762 " aChildren.yz = aHitTimes.z < aHitTimes.y ? aChildren.zy : aChildren.yz;\n"
763 " aHitTimes.yz = aHitTimes.z < aHitTimes.y ? aHitTimes.zy : aHitTimes.yz;\n"
765 " if (aHitTimes.x != MAXFLOAT)\n"
767 " int aHitMask = (aHitTimes.w != MAXFLOAT ? aChildren.w : aChildren.z) << 2\n"
768 " | (aHitTimes.z != MAXFLOAT ? aChildren.z : aChildren.y);\n"
770 " if (aHitTimes.y != MAXFLOAT)\n"
771 " Stack[++aHead] = aData.y | (aHitMask << 2 | aChildren.y) << 26;\n"
773 " aNode = aData.y + aChildren.x;\n"
777 " toContinue = (aHead >= 0);\n"
779 " if (aHead == aStop) // go to top-level BVH\n"
781 " aStop = -1; aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);\n"
785 " aNode = pop (aHead);\n"
788 " else if (aData.x < 0) // leaf node\n"
793 " for (int anIdx = aData.y; anIdx <= aData.z; ++anIdx)\n"
795 " ivec4 aTriangle = texelFetch (uGeometryTriangTexture, anIdx + TRG_OFFSET (aSubTree));\n"
797 " vec3 aPoint0 = texelFetch (uGeometryVertexTexture, aTriangle.x += VRT_OFFSET (aSubTree)).xyz;\n"
798 " vec3 aPoint1 = texelFetch (uGeometryVertexTexture, aTriangle.y += VRT_OFFSET (aSubTree)).xyz;\n"
799 " vec3 aPoint2 = texelFetch (uGeometryVertexTexture, aTriangle.z += VRT_OFFSET (aSubTree)).xyz;\n"
801 " IntersectTriangle (aSubTree.TrsfRay, aPoint0, aPoint1, aPoint2, aTimeUV, aNormal);\n"
803 "#ifdef TRANSPARENT_SHADOWS\n"
804 " if (aTimeUV.x < theDistance)\n"
806 " aFactor *= 1.f - texelFetch (uRaytraceMaterialTexture, MATERIAL_TRAN (aTriangle.w)).x;\n"
809 " if (aTimeUV.x < theDistance)\n"
816 " toContinue = (aHead >= 0) && (aFactor > 0.1f);\n"
818 " if (aHead == aStop) // go to top-level BVH\n"
820 " aStop = -1; aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);\n"
824 " aNode = pop (aHead);\n"
826 " else if (aData.x > 0) // switch node\n"
828 " aSubTree.SubData = ivec4 (4 * aData.x - 4, aData.yzw); // store BVH sub-root\n"
830 " vec4 aInvTransf0 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 0);\n"
831 " vec4 aInvTransf1 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 1);\n"
832 " vec4 aInvTransf2 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 2);\n"
833 " vec4 aInvTransf3 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 3);\n"
835 " aSubTree.TrsfRay.Direct = MatrixColMultiplyDir (theRay.Direct,\n"
840 " aSubTree.TrsfRay.Origin = MatrixColMultiplyPnt (theRay.Origin,\n"
846 " aSubTree.Inverse = mix (-UNIT, UNIT, step (ZERO, aSubTree.TrsfRay.Direct)) / max (abs (aSubTree.TrsfRay.Direct), SMALL);\n"
848 " aNode = BVH_OFFSET (aSubTree); // go to sub-root node\n"
850 " aStop = aHead; // store current stack pointer\n"
857 "#define PI 3.1415926f\n"
859 "// =======================================================================\n"
860 "// function : Latlong\n"
861 "// purpose : Converts world direction to environment texture coordinates\n"
862 "// =======================================================================\n"
863 "vec2 Latlong (in vec3 thePoint, in float theRadius)\n"
865 " float aPsi = acos (-thePoint.z / theRadius);\n"
867 " float aPhi = atan (thePoint.y, thePoint.x) + PI;\n"
869 " return vec2 (aPhi * 0.1591549f,\n"
870 " aPsi * 0.3183098f);\n"
873 "#ifdef BACKGROUND_CUBEMAP\n"
874 "//! Transform texture coordinates for cubemap lookup.\n"
875 "vec3 cubemapVectorTransform (in vec3 theVec, in float theRadius)\n"
877 " vec3 aVec = theVec.yzx;\n"
878 " aVec.y *= float(uYCoeff);\n"
879 " aVec.z *= float(uZCoeff);\n"
884 "// =======================================================================\n"
885 "// function : SmoothNormal\n"
886 "// purpose : Interpolates normal across the triangle\n"
887 "// =======================================================================\n"
888 "vec3 SmoothNormal (in vec2 theUV, in ivec4 theTriangle)\n"
890 " vec3 aNormal0 = texelFetch (uGeometryNormalTexture, theTriangle.x).xyz;\n"
891 " vec3 aNormal1 = texelFetch (uGeometryNormalTexture, theTriangle.y).xyz;\n"
892 " vec3 aNormal2 = texelFetch (uGeometryNormalTexture, theTriangle.z).xyz;\n"
894 " return normalize (aNormal1 * theUV.x +\n"
895 " aNormal2 * theUV.y +\n"
896 " aNormal0 * (1.0f - theUV.x - theUV.y));\n"
899 "#define POLYGON_OFFSET_UNIT 0.f\n"
900 "#define POLYGON_OFFSET_FACTOR 1.f\n"
901 "#define POLYGON_OFFSET_SCALE 0.006f\n"
903 "// =======================================================================\n"
904 "// function : PolygonOffset\n"
905 "// purpose : Computes OpenGL polygon offset\n"
906 "// =======================================================================\n"
907 "float PolygonOffset (in vec3 theNormal, in vec3 thePoint)\n"
909 " vec4 aProjectedNorm = vec4 (theNormal, -dot (theNormal, thePoint)) * uUnviewMat;\n"
911 " float aPolygonOffset = POLYGON_OFFSET_UNIT;\n"
913 " if (aProjectedNorm.z * aProjectedNorm.z > 1e-20f)\n"
915 " aProjectedNorm.xy *= 1.f / aProjectedNorm.z;\n"
917 " aPolygonOffset += POLYGON_OFFSET_FACTOR * max (abs (aProjectedNorm.x),\n"
918 " abs (aProjectedNorm.y));\n"
921 " return aPolygonOffset;\n"
924 "// =======================================================================\n"
925 "// function : SmoothUV\n"
926 "// purpose : Interpolates UV coordinates across the triangle\n"
927 "// =======================================================================\n"
928 "#ifdef USE_TEXTURES\n"
929 "vec2 SmoothUV (in vec2 theUV, in ivec4 theTriangle, out vec2[3] theUVs)\n"
931 " theUVs[0] = texelFetch (uGeometryTexCrdTexture, theTriangle.x).st;\n"
932 " theUVs[1] = texelFetch (uGeometryTexCrdTexture, theTriangle.y).st;\n"
933 " theUVs[2] = texelFetch (uGeometryTexCrdTexture, theTriangle.z).st;\n"
935 " return theUVs[1] * theUV.x +\n"
936 " theUVs[2] * theUV.y +\n"
937 " theUVs[0] * (1.0f - theUV.x - theUV.y);\n"
940 "vec2 SmoothUV (in vec2 theUV, in ivec4 theTriangle)\n"
943 " return SmoothUV (theUV, theTriangle, aUVs);\n"
947 "// =======================================================================\n"
948 "// function : FetchEnvironment\n"
950 "// =======================================================================\n"
951 "vec4 FetchEnvironment (in vec3 theTexCoord, in float theRadius, in bool theIsBackground)\n"
953 " if (uEnvMapEnabled == 0)\n"
955 "#ifdef PATH_TRACING\n"
956 " return theIsBackground ? vec4 (0.0, 0.0, 0.0, 1.0) : uGlobalAmbient;\n"
958 " return vec4 (0.0, 0.0, 0.0, 1.0);\n"
962 " vec4 anAmbScale = theIsBackground ? vec4(1.0) : uGlobalAmbient;\n"
963 " vec4 anEnvColor =\n"
964 "#ifdef BACKGROUND_CUBEMAP\n"
965 " textureLod (uEnvMapTexture, cubemapVectorTransform (theTexCoord, theRadius), 0.0);\n"
967 " textureLod (uEnvMapTexture, Latlong (theTexCoord, theRadius), 0.0);\n"
969 " return anEnvColor * anAmbScale;\n"
972 "// =======================================================================\n"
973 "// function : Refract\n"
974 "// purpose : Computes refraction ray (also handles TIR)\n"
975 "// =======================================================================\n"
976 "#ifndef PATH_TRACING\n"
977 "vec3 Refract (in vec3 theInput,\n"
978 " in vec3 theNormal,\n"
979 " in float theRefractIndex,\n"
980 " in float theInvRefractIndex)\n"
982 " float aNdotI = dot (theInput, theNormal);\n"
984 " float anIndex = aNdotI < 0.0f\n"
985 " ? theInvRefractIndex\n"
986 " : theRefractIndex;\n"
988 " float aSquare = anIndex * anIndex * (1.0f - aNdotI * aNdotI);\n"
990 " if (aSquare > 1.0f)\n"
992 " return reflect (theInput, theNormal);\n"
995 " float aNdotT = sqrt (1.0f - aSquare);\n"
997 " return normalize (anIndex * theInput -\n"
998 " (anIndex * aNdotI + (aNdotI < 0.0f ? aNdotT : -aNdotT)) * theNormal);\n"
1002 "#define MIN_SLOPE 0.0001f\n"
1003 "#define EPS_SCALE 8.0000f\n"
1005 "#define THRESHOLD vec3 (0.1f)\n"
1007 "#define INVALID_BOUNCES 1000\n"
1009 "#define LIGHT_POS(index) (2 * index + 1)\n"
1010 "#define LIGHT_PWR(index) (2 * index + 0)\n"
1012 "// =======================================================================\n"
1013 "// function : Radiance\n"
1014 "// purpose : Computes color along the given ray\n"
1015 "// =======================================================================\n"
1016 "#ifndef PATH_TRACING\n"
1017 "vec4 Radiance (in SRay theRay, in vec3 theInverse)\n"
1019 " vec3 aResult = vec3 (0.0f);\n"
1020 " vec4 aWeight = vec4 (1.0f);\n"
1024 " float aRaytraceDepth = MAXFLOAT;\n"
1025 " float aRefractionIdx = 0.0;\n"
1027 " for (int aDepth = 0; aDepth < NB_BOUNCES; ++aDepth)\n"
1029 " SIntersect aHit = SIntersect (MAXFLOAT, vec2 (ZERO), ZERO);\n"
1031 " ivec4 aTriIndex = SceneNearestHit (theRay, theInverse, aHit, aTrsfId).TriIndex;\n"
1033 " if (aTriIndex.x == -1)\n"
1035 " vec4 aColor = vec4 (0.0);\n"
1037 " if (bool(uEnvMapForBack) || aWeight.w == 0.0 /* reflection */)\n"
1039 " float aRadius = uSceneRadius;\n"
1040 " vec3 aTexCoord = vec3 (0.0);\n"
1042 " if (aDepth == 0 || (aRefractionIdx == 1.0 && aWeight.w != 0.0))\n"
1044 " vec2 aPixel = uEyeSize * (vPixel - vec2 (0.5)) * 2.0;\n"
1045 " vec2 anAperturePnt = sampleUniformDisk() * uApertureRadius;\n"
1046 " vec3 aLocalDir = normalize (vec3 (aPixel * uFocalPlaneDist - anAperturePnt, uFocalPlaneDist));\n"
1047 " vec3 aDirect = uEyeView * aLocalDir.z +\n"
1048 " uEyeSide * aLocalDir.x +\n"
1049 " uEyeVert * aLocalDir.y;\n"
1051 " aTexCoord = aDirect * uSceneRadius;\n"
1052 " aRadius = length (aTexCoord);\n"
1056 " float aTime = IntersectSphere (theRay, uSceneRadius);\n"
1057 " aTexCoord = theRay.Direct * aTime + theRay.Origin;\n"
1060 " aColor = FetchEnvironment (aTexCoord, aRadius, aWeight.w != 0.0);\n"
1064 " aColor = BackgroundColor();\n"
1067 " aResult += aWeight.xyz * aColor.xyz; aWeight.w *= aColor.w;\n"
1069 " break; // terminate path\n"
1072 " vec3 aInvTransf0 = texelFetch (uSceneTransformTexture, aTrsfId + 0).xyz;\n"
1073 " vec3 aInvTransf1 = texelFetch (uSceneTransformTexture, aTrsfId + 1).xyz;\n"
1074 " vec3 aInvTransf2 = texelFetch (uSceneTransformTexture, aTrsfId + 2).xyz;\n"
1076 " aHit.Normal = normalize (vec3 (dot (aInvTransf0, aHit.Normal),\n"
1077 " dot (aInvTransf1, aHit.Normal),\n"
1078 " dot (aInvTransf2, aHit.Normal)));\n"
1080 " theRay.Origin += theRay.Direct * aHit.Time; // intersection point\n"
1082 " // Evaluate depth on first hit\n"
1083 " if (aDepth == 0)\n"
1085 " vec4 aNDCPoint = uViewMat * vec4 (theRay.Origin, 1.f);\n"
1087 " float aPolygonOffset = PolygonOffset (aHit.Normal, theRay.Origin);\n"
1088 " aRaytraceDepth = (aNDCPoint.z / aNDCPoint.w + aPolygonOffset * POLYGON_OFFSET_SCALE) * 0.5f + 0.5f;\n"
1091 " vec3 aNormal = SmoothNormal (aHit.UV, aTriIndex);\n"
1093 " aNormal = normalize (vec3 (dot (aInvTransf0, aNormal),\n"
1094 " dot (aInvTransf1, aNormal),\n"
1095 " dot (aInvTransf2, aNormal)));\n"
1097 " vec3 aAmbient = texelFetch (\n"
1098 " uRaytraceMaterialTexture, MATERIAL_AMBN (aTriIndex.w)).rgb;\n"
1099 " vec4 aDiffuse = texelFetch (\n"
1100 " uRaytraceMaterialTexture, MATERIAL_DIFF (aTriIndex.w));\n"
1101 " vec4 aSpecular = texelFetch (\n"
1102 " uRaytraceMaterialTexture, MATERIAL_SPEC (aTriIndex.w));\n"
1103 " vec4 aOpacity = texelFetch (\n"
1104 " uRaytraceMaterialTexture, MATERIAL_TRAN (aTriIndex.w));\n"
1106 "#ifdef USE_TEXTURES\n"
1107 " if (aDiffuse.w >= 0.f)\n"
1109 " vec4 aTexCoord = vec4 (SmoothUV (aHit.UV, aTriIndex), 0.f, 1.f);\n"
1111 " vec4 aTrsfRow1 = texelFetch (\n"
1112 " uRaytraceMaterialTexture, MATERIAL_TRS1 (aTriIndex.w));\n"
1113 " vec4 aTrsfRow2 = texelFetch (\n"
1114 " uRaytraceMaterialTexture, MATERIAL_TRS2 (aTriIndex.w));\n"
1116 " aTexCoord.st = vec2 (dot (aTrsfRow1, aTexCoord),\n"
1117 " dot (aTrsfRow2, aTexCoord));\n"
1119 " vec4 aTexColor = textureLod (\n"
1120 " sampler2D (uTextureSamplers[int(aDiffuse.w)]), aTexCoord.st, 0.f);\n"
1122 " aDiffuse.rgb *= aTexColor.rgb;\n"
1123 " aAmbient.rgb *= aTexColor.rgb;\n"
1125 " // keep refractive index untouched (Z component)\n"
1126 " aOpacity.xy = vec2 (aTexColor.w * aOpacity.x, 1.0f - aTexColor.w * aOpacity.x);\n"
1130 " vec3 aEmission = texelFetch (\n"
1131 " uRaytraceMaterialTexture, MATERIAL_EMIS (aTriIndex.w)).rgb;\n"
1133 " float aGeomFactor = dot (aNormal, theRay.Direct);\n"
1135 " aResult.xyz += aWeight.xyz * aOpacity.x * (\n"
1136 " uGlobalAmbient.xyz * aAmbient * max (abs (aGeomFactor), 0.5f) + aEmission);\n"
1138 " vec3 aSidedNormal = mix (aNormal, -aNormal, step (0.0f, aGeomFactor));\n"
1140 " for (int aLightIdx = 0; aLightIdx < uLightCount; ++aLightIdx)\n"
1142 " vec4 aLight = texelFetch (\n"
1143 " uRaytraceLightSrcTexture, LIGHT_POS (aLightIdx));\n"
1145 " float aDistance = MAXFLOAT;\n"
1147 " if (aLight.w != 0.0f) // point light source\n"
1149 " aDistance = length (aLight.xyz -= theRay.Origin);\n"
1151 " aLight.xyz *= 1.0f / aDistance;\n"
1154 " float aLdotN = dot (aLight.xyz, aSidedNormal);\n"
1156 " if (aLdotN > 0.0f) // first check if light source is important\n"
1158 " float aVisibility = 1.0f;\n"
1160 " if (bool(uShadowsEnabled))\n"
1162 " SRay aShadow = SRay (theRay.Origin, aLight.xyz);\n"
1164 " aShadow.Origin += uSceneEpsilon * (aLight.xyz +\n"
1165 " mix (-aHit.Normal, aHit.Normal, step (0.0f, dot (aHit.Normal, aLight.xyz))));\n"
1167 " vec3 aInverse = 1.0f / max (abs (aLight.xyz), SMALL);\n"
1169 " aVisibility = SceneAnyHit (\n"
1170 " aShadow, mix (-aInverse, aInverse, step (ZERO, aLight.xyz)), aDistance);\n"
1173 " if (aVisibility > 0.0f)\n"
1175 " vec3 aIntensity = min (UNIT, vec3 (texelFetch (\n"
1176 " uRaytraceLightSrcTexture, LIGHT_PWR (aLightIdx))));\n"
1178 " float aRdotV = dot (reflect (aLight.xyz, aSidedNormal), theRay.Direct);\n"
1180 " aResult.xyz += aWeight.xyz * (aOpacity.x * aVisibility) * aIntensity *\n"
1181 " (aDiffuse.xyz * aLdotN + aSpecular.xyz * pow (max (0.f, aRdotV), aSpecular.w));\n"
1186 " if (aOpacity.x != 1.0f)\n"
1188 " aWeight *= aOpacity.y;\n"
1189 " aRefractionIdx = aOpacity.z;\n"
1191 " if (aOpacity.z != 1.0f)\n"
1193 " theRay.Direct = Refract (theRay.Direct, aNormal, aOpacity.z, aOpacity.w);\n"
1198 " aWeight *= bool(uReflectEnabled) ?\n"
1199 " texelFetch (uRaytraceMaterialTexture, MATERIAL_REFL (aTriIndex.w)) : vec4 (0.0f);\n"
1201 " vec3 aReflect = reflect (theRay.Direct, aNormal);\n"
1203 " if (dot (aReflect, aHit.Normal) * dot (theRay.Direct, aHit.Normal) > 0.0f)\n"
1205 " aReflect = reflect (theRay.Direct, aHit.Normal);\n"
1208 " theRay.Direct = aReflect;\n"
1211 " if (all (lessThanEqual (aWeight.xyz, THRESHOLD)))\n"
1213 " aDepth = INVALID_BOUNCES;\n"
1215 " else if (aOpacity.x == 1.0f || aOpacity.z != 1.0f) // if no simple transparency\n"
1217 " theRay.Origin += aHit.Normal * mix (\n"
1218 " -uSceneEpsilon, uSceneEpsilon, step (0.0f, dot (aHit.Normal, theRay.Direct)));\n"
1220 " theInverse = 1.0f / max (abs (theRay.Direct), SMALL);\n"
1222 " theInverse = mix (-theInverse, theInverse, step (ZERO, theRay.Direct));\n"
1225 " theRay.Origin += theRay.Direct * uSceneEpsilon;\n"
1228 " gl_FragDepth = aRaytraceDepth;\n"
1230 " return vec4 (aResult.x,\n"