@tinypoly/gpu-renderer 0.0.0-stage → 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (194) hide show
  1. package/ARCHITECTURE.md +334 -0
  2. package/LICENSE +21 -0
  3. package/README.md +258 -2
  4. package/dist/accumulator.d.ts +27 -0
  5. package/dist/accumulator.d.ts.map +1 -0
  6. package/dist/accumulator.js +57 -0
  7. package/dist/accumulator.js.map +1 -0
  8. package/dist/bvh.d.ts +26 -0
  9. package/dist/bvh.d.ts.map +1 -0
  10. package/dist/bvh.js +207 -0
  11. package/dist/bvh.js.map +1 -0
  12. package/dist/camera.d.ts +29 -0
  13. package/dist/camera.d.ts.map +1 -0
  14. package/dist/camera.js +48 -0
  15. package/dist/camera.js.map +1 -0
  16. package/dist/canvas.d.ts +9 -0
  17. package/dist/canvas.d.ts.map +1 -0
  18. package/dist/canvas.js +11 -0
  19. package/dist/canvas.js.map +1 -0
  20. package/dist/denoise.d.ts +69 -0
  21. package/dist/denoise.d.ts.map +1 -0
  22. package/dist/denoise.js +455 -0
  23. package/dist/denoise.js.map +1 -0
  24. package/dist/device.d.ts +37 -0
  25. package/dist/device.d.ts.map +1 -0
  26. package/dist/device.js +52 -0
  27. package/dist/device.js.map +1 -0
  28. package/dist/dispatchScheduler.d.ts +43 -0
  29. package/dist/dispatchScheduler.d.ts.map +1 -0
  30. package/dist/dispatchScheduler.js +49 -0
  31. package/dist/dispatchScheduler.js.map +1 -0
  32. package/dist/environment.d.ts +39 -0
  33. package/dist/environment.d.ts.map +1 -0
  34. package/dist/environment.js +168 -0
  35. package/dist/environment.js.map +1 -0
  36. package/dist/frameLayout.d.ts +4 -0
  37. package/dist/frameLayout.d.ts.map +1 -0
  38. package/dist/frameLayout.js +58 -0
  39. package/dist/frameLayout.js.map +1 -0
  40. package/dist/geometry.d.ts +75 -0
  41. package/dist/geometry.d.ts.map +1 -0
  42. package/dist/geometry.js +671 -0
  43. package/dist/geometry.js.map +1 -0
  44. package/dist/index.d.ts +25 -0
  45. package/dist/index.d.ts.map +1 -0
  46. package/dist/index.js +17 -0
  47. package/dist/index.js.map +1 -0
  48. package/dist/lights.d.ts +64 -0
  49. package/dist/lights.d.ts.map +1 -0
  50. package/dist/lights.js +264 -0
  51. package/dist/lights.js.map +1 -0
  52. package/dist/materialLayout.d.ts +143 -0
  53. package/dist/materialLayout.d.ts.map +1 -0
  54. package/dist/materialLayout.js +297 -0
  55. package/dist/materialLayout.js.map +1 -0
  56. package/dist/math.d.ts +10 -0
  57. package/dist/math.d.ts.map +1 -0
  58. package/dist/math.js +40 -0
  59. package/dist/math.js.map +1 -0
  60. package/dist/pathTracePass.d.ts +27 -0
  61. package/dist/pathTracePass.d.ts.map +1 -0
  62. package/dist/pathTracePass.js +83 -0
  63. package/dist/pathTracePass.js.map +1 -0
  64. package/dist/planarCapture.d.ts +38 -0
  65. package/dist/planarCapture.d.ts.map +1 -0
  66. package/dist/planarCapture.js +163 -0
  67. package/dist/planarCapture.js.map +1 -0
  68. package/dist/presentPass.d.ts +31 -0
  69. package/dist/presentPass.d.ts.map +1 -0
  70. package/dist/presentPass.js +78 -0
  71. package/dist/presentPass.js.map +1 -0
  72. package/dist/renderer.d.ts +196 -0
  73. package/dist/renderer.d.ts.map +1 -0
  74. package/dist/renderer.js +839 -0
  75. package/dist/renderer.js.map +1 -0
  76. package/dist/sceneAssets.d.ts +24 -0
  77. package/dist/sceneAssets.d.ts.map +1 -0
  78. package/dist/sceneAssets.js +77 -0
  79. package/dist/sceneAssets.js.map +1 -0
  80. package/dist/sceneUpload.d.ts +61 -0
  81. package/dist/sceneUpload.d.ts.map +1 -0
  82. package/dist/sceneUpload.js +98 -0
  83. package/dist/sceneUpload.js.map +1 -0
  84. package/dist/shaderModule.d.ts +7 -0
  85. package/dist/shaderModule.d.ts.map +1 -0
  86. package/dist/shaderModule.js +19 -0
  87. package/dist/shaderModule.js.map +1 -0
  88. package/dist/shaderProgram.d.ts +124 -0
  89. package/dist/shaderProgram.d.ts.map +1 -0
  90. package/dist/shaderProgram.js +1030 -0
  91. package/dist/shaderProgram.js.map +1 -0
  92. package/dist/structLayout.d.ts +28 -0
  93. package/dist/structLayout.d.ts.map +1 -0
  94. package/dist/structLayout.js +58 -0
  95. package/dist/structLayout.js.map +1 -0
  96. package/dist/texturePack.d.ts +54 -0
  97. package/dist/texturePack.d.ts.map +1 -0
  98. package/dist/texturePack.js +86 -0
  99. package/dist/texturePack.js.map +1 -0
  100. package/dist/upstream.d.ts +7 -0
  101. package/dist/upstream.d.ts.map +1 -0
  102. package/dist/upstream.js +11 -0
  103. package/dist/upstream.js.map +1 -0
  104. package/dist/volumetric.d.ts +10 -0
  105. package/dist/volumetric.d.ts.map +1 -0
  106. package/dist/volumetric.js +34 -0
  107. package/dist/volumetric.js.map +1 -0
  108. package/dist/wgsl/bsdf.wgsl.d.ts +13 -0
  109. package/dist/wgsl/bsdf.wgsl.d.ts.map +1 -0
  110. package/dist/wgsl/bsdf.wgsl.js +619 -0
  111. package/dist/wgsl/bsdf.wgsl.js.map +1 -0
  112. package/dist/wgsl/bvh.wgsl.d.ts +3 -0
  113. package/dist/wgsl/bvh.wgsl.d.ts.map +1 -0
  114. package/dist/wgsl/bvh.wgsl.js +218 -0
  115. package/dist/wgsl/bvh.wgsl.js.map +1 -0
  116. package/dist/wgsl/common.wgsl.d.ts +3 -0
  117. package/dist/wgsl/common.wgsl.d.ts.map +1 -0
  118. package/dist/wgsl/common.wgsl.js +193 -0
  119. package/dist/wgsl/common.wgsl.js.map +1 -0
  120. package/dist/wgsl/environment.wgsl.d.ts +10 -0
  121. package/dist/wgsl/environment.wgsl.d.ts.map +1 -0
  122. package/dist/wgsl/environment.wgsl.js +145 -0
  123. package/dist/wgsl/environment.wgsl.js.map +1 -0
  124. package/dist/wgsl/lights.wgsl.d.ts +10 -0
  125. package/dist/wgsl/lights.wgsl.d.ts.map +1 -0
  126. package/dist/wgsl/lights.wgsl.js +434 -0
  127. package/dist/wgsl/lights.wgsl.js.map +1 -0
  128. package/dist/wgsl/pathTrace.wgsl.d.ts +14 -0
  129. package/dist/wgsl/pathTrace.wgsl.d.ts.map +1 -0
  130. package/dist/wgsl/pathTrace.wgsl.js +1020 -0
  131. package/dist/wgsl/pathTrace.wgsl.js.map +1 -0
  132. package/dist/wgsl/present.wgsl.d.ts +7 -0
  133. package/dist/wgsl/present.wgsl.d.ts.map +1 -0
  134. package/dist/wgsl/present.wgsl.js +154 -0
  135. package/dist/wgsl/present.wgsl.js.map +1 -0
  136. package/dist/wgsl/random.wgsl.d.ts +9 -0
  137. package/dist/wgsl/random.wgsl.d.ts.map +1 -0
  138. package/dist/wgsl/random.wgsl.js +91 -0
  139. package/dist/wgsl/random.wgsl.js.map +1 -0
  140. package/dist/wgsl/sampling.wgsl.d.ts +3 -0
  141. package/dist/wgsl/sampling.wgsl.d.ts.map +1 -0
  142. package/dist/wgsl/sampling.wgsl.js +31 -0
  143. package/dist/wgsl/sampling.wgsl.js.map +1 -0
  144. package/dist/wgsl/shaderSupport.wgsl.d.ts +6 -0
  145. package/dist/wgsl/shaderSupport.wgsl.d.ts.map +1 -0
  146. package/dist/wgsl/shaderSupport.wgsl.js +120 -0
  147. package/dist/wgsl/shaderSupport.wgsl.js.map +1 -0
  148. package/dist/wgsl/surface.wgsl.d.ts +10 -0
  149. package/dist/wgsl/surface.wgsl.d.ts.map +1 -0
  150. package/dist/wgsl/surface.wgsl.js +794 -0
  151. package/dist/wgsl/surface.wgsl.js.map +1 -0
  152. package/dist/wgsl/texture.wgsl.d.ts +11 -0
  153. package/dist/wgsl/texture.wgsl.d.ts.map +1 -0
  154. package/dist/wgsl/texture.wgsl.js +132 -0
  155. package/dist/wgsl/texture.wgsl.js.map +1 -0
  156. package/package.json +76 -3
  157. package/src/accumulator.ts +64 -0
  158. package/src/bvh.ts +272 -0
  159. package/src/camera.ts +77 -0
  160. package/src/canvas.ts +13 -0
  161. package/src/denoise.ts +514 -0
  162. package/src/device.ts +81 -0
  163. package/src/dispatchScheduler.ts +71 -0
  164. package/src/environment.ts +233 -0
  165. package/src/frameLayout.ts +59 -0
  166. package/src/geometry.ts +899 -0
  167. package/src/index.ts +61 -0
  168. package/src/lights.ts +365 -0
  169. package/src/materialLayout.ts +408 -0
  170. package/src/math.ts +48 -0
  171. package/src/pathTracePass.ts +97 -0
  172. package/src/planarCapture.ts +225 -0
  173. package/src/presentPass.ts +99 -0
  174. package/src/renderer.ts +1117 -0
  175. package/src/sceneAssets.ts +118 -0
  176. package/src/sceneUpload.ts +185 -0
  177. package/src/shaderModule.ts +19 -0
  178. package/src/shaderProgram.ts +1370 -0
  179. package/src/structLayout.ts +93 -0
  180. package/src/texturePack.ts +152 -0
  181. package/src/upstream.ts +56 -0
  182. package/src/volumetric.ts +42 -0
  183. package/src/wgsl/bsdf.wgsl.ts +618 -0
  184. package/src/wgsl/bvh.wgsl.ts +217 -0
  185. package/src/wgsl/common.wgsl.ts +193 -0
  186. package/src/wgsl/environment.wgsl.ts +144 -0
  187. package/src/wgsl/lights.wgsl.ts +433 -0
  188. package/src/wgsl/pathTrace.wgsl.ts +1019 -0
  189. package/src/wgsl/present.wgsl.ts +153 -0
  190. package/src/wgsl/random.wgsl.ts +90 -0
  191. package/src/wgsl/sampling.wgsl.ts +30 -0
  192. package/src/wgsl/shaderSupport.wgsl.ts +119 -0
  193. package/src/wgsl/surface.wgsl.ts +793 -0
  194. package/src/wgsl/texture.wgsl.ts +131 -0
@@ -0,0 +1,794 @@
1
+ /**
2
+ * What a hit actually is once the maps are read: a material resolved at one point on one triangle. The integrator
3
+ * resolves a hit once and then asks the lobes about it, so no texture is read twice for the same point.
4
+ *
5
+ * The channel each map reads from and the way each value combines mirror Three's own shader chunks (r185), which the
6
+ * CPU renderer follows too: roughness from green, metalness from blue, the alpha map from green, clearcoat from red,
7
+ * its roughness from green, sheen roughness from alpha, and so on.
8
+ */
9
+ export const SURFACE_WGSL = /* wgsl */ `
10
+ struct Surface {
11
+ // The shading normal, maps applied, facing the ray.
12
+ normal: vec3<f32>,
13
+ albedo: vec3<f32>,
14
+ // What the surface radiates by itself: its emission, or the whole colour of a material Three does not light.
15
+ emissive: vec3<f32>,
16
+ // The dielectric's reflectance at normal incidence and at grazing angles, before metalness turns it to the albedo.
17
+ specularColor: vec3<f32>,
18
+ specularF90: f32,
19
+ roughness: f32,
20
+ metalness: f32,
21
+ // How much of the surface actually covers this point: a cutout, a transparency or a discarded fragment.
22
+ opacity: f32,
23
+ // Three's diffuseColor.a, which blending other than normal weighs the surface by.
24
+ alpha: f32,
25
+ flags: u32,
26
+ kind: u32,
27
+ // Share of the diffuse layer that lets light through, with metal already taken out of it.
28
+ transmission: f32,
29
+ ior: f32,
30
+ thickness: f32,
31
+ dispersion: f32,
32
+ // What a unit of distance inside the volume absorbs, per channel: zero for a volume that never darkens.
33
+ absorption: vec3<f32>,
34
+ // The interpolated normal facing the ray, before any map: thin walls, shadow rays and the clearcoat read this one.
35
+ facing: vec3<f32>,
36
+ // Whether the ray arrived from the side the normals point to, which is the outside of a closed volume.
37
+ front: bool,
38
+ clearcoat: f32,
39
+ clearcoatRoughness: f32,
40
+ clearcoatNormal: vec3<f32>,
41
+ sheenColor: vec3<f32>,
42
+ sheenRoughness: f32,
43
+ iridescence: f32,
44
+ iridescenceFresnel: vec3<f32>,
45
+ // Anisotropic GGX: its strength, the roughness along the tangent, and the tangent frame it stretches along.
46
+ anisotropy: f32,
47
+ alphaT: f32,
48
+ anisotropyT: vec3<f32>,
49
+ anisotropyB: vec3<f32>,
50
+ shininess: f32,
51
+ // Phong's specular map, which scales its highlight.
52
+ specularStrength: f32,
53
+ gradientMap: u32,
54
+ // The material's own environment map, which the paths leaving it see instead of the scene's.
55
+ envMap: u32,
56
+ envMapIntensity: f32,
57
+ // Basic, Lambert and Phong blend their environment map over the lit colour: what the map shows here, and how much.
58
+ legacyEnv: vec3<f32>,
59
+ legacyAmount: f32,
60
+ combine: u32,
61
+ // Light a light map bakes into this point, which only the camera sees: the path traces the rest itself.
62
+ lightMap: vec3<f32>,
63
+ blending: u32,
64
+ material: u32,
65
+ };
66
+
67
+ /** Barycentric weights of the two trailing vertices, for a point drawn uniformly over a triangle's area. */
68
+ fn triangleBarycentric(u: vec2<f32>) -> vec2<f32> {
69
+ let root = sqrt(u.x);
70
+
71
+ return vec2<f32>(1.0 - root, u.y * root);
72
+ }
73
+
74
+ fn trianglePointAt(triangle: u32, bary: vec2<f32>) -> vec3<f32> {
75
+ return triangleVertex(triangle, 0u) * (1.0 - bary.x - bary.y)
76
+ + triangleVertex(triangle, 1u) * bary.x
77
+ + triangleVertex(triangle, 2u) * bary.y;
78
+ }
79
+
80
+ fn triangleUv(triangle: u32, bary: vec2<f32>, channel: u32) -> vec2<f32> {
81
+ return attributeUv(triangle, 0u, channel) * (1.0 - bary.x - bary.y)
82
+ + attributeUv(triangle, 1u, channel) * bary.x
83
+ + attributeUv(triangle, 2u, channel) * bary.y;
84
+ }
85
+
86
+ fn triangleColor(triangle: u32, bary: vec2<f32>) -> vec4<f32> {
87
+ return attributeColor(triangle, 0u) * (1.0 - bary.x - bary.y)
88
+ + attributeColor(triangle, 1u) * bary.x
89
+ + attributeColor(triangle, 2u) * bary.y;
90
+ }
91
+
92
+ /** A map slot, sampled through the UV set the texture asks for. Missing maps read as one, so a multiply is always safe. */
93
+ fn sampleMap(index: u32, triangle: u32, bary: vec2<f32>) -> vec4<f32> {
94
+ if (index == NO_TEXTURE) {
95
+ return vec4<f32>(1.0);
96
+ }
97
+
98
+ return sampleTextureAt(index, triangleUv(triangle, bary, textureInfo(index).channel));
99
+ }
100
+
101
+ /** Whether the ray sees the side Three calls the front: the winding, which packing keeps counter-clockwise. */
102
+ fn isFrontFace(triangle: u32, direction: vec3<f32>) -> bool {
103
+ return dot(triangleNormal(triangle), direction) < 0.0;
104
+ }
105
+
106
+ /** Three culls the side a material does not draw. */
107
+ fn isCulled(material: u32, triangle: u32, direction: vec3<f32>) -> bool {
108
+ let side = materials[material].side;
109
+ let front = isFrontFace(triangle, direction);
110
+
111
+ return (side == SIDE_FRONT && !front) || (side == SIDE_BACK && front);
112
+ }
113
+
114
+ /** What the triangle radiates at this point, its emissive map included so direct and indirect light agree on it. */
115
+ fn emissionAt(triangle: u32, bary: vec2<f32>) -> vec3<f32> {
116
+ let m = materialIndexOf(triangle);
117
+ let map = sampleMap(materials[m].emissiveMap, triangle, bary);
118
+
119
+ return materials[m].emissive.rgb * materials[m].emissive.w * map.rgb;
120
+ }
121
+
122
+ /** Three's material clipping planes: a point on the far side of one (or of all, with clipIntersection) is cut away. */
123
+ fn isClipped(m: u32, point: vec3<f32>) -> bool {
124
+ let count = materials[m].clipCount;
125
+
126
+ if (count == 0u) {
127
+ return false;
128
+ }
129
+
130
+ let intersection = (materials[m].flags & MATERIAL_CLIP_INTERSECTION) != 0u;
131
+ var every = true;
132
+
133
+ for (var i = 0u; i < count; i = i + 1u) {
134
+ let plane = extraVec4(materials[m].clipStart + i * 4u);
135
+ let outside = dot(plane.xyz, point) + plane.w < 0.0;
136
+
137
+ if (outside && !intersection) {
138
+ return true;
139
+ }
140
+
141
+ every = every && outside;
142
+ }
143
+
144
+ return intersection && every;
145
+ }
146
+
147
+ /** How large one pixel is at this point, in world units: what a one pixel wide line covers. */
148
+ fn pixelFootprint(point: vec3<f32>) -> f32 {
149
+ let scale = 2.0 / (frame.projection[1][1] * f32(frame.resolution.y));
150
+
151
+ if (frame.cameraKind == 1u) {
152
+ return scale;
153
+ }
154
+
155
+ return scale * max(-(frame.worldToCamera * vec4<f32>(point, 1.0)).z, 1e-6);
156
+ }
157
+
158
+ fn edgeDistance(point: vec3<f32>, a: vec3<f32>, b: vec3<f32>) -> f32 {
159
+ let edge = b - a;
160
+
161
+ return length(cross(point - a, edge)) / max(length(edge), 1e-20);
162
+ }
163
+
164
+ /** A wireframe draws only the edges of each triangle, one pixel wide as WebGL draws every line. */
165
+ fn onWireframe(triangle: u32, point: vec3<f32>) -> bool {
166
+ let a = triangleVertex(triangle, 0u);
167
+ let b = triangleVertex(triangle, 1u);
168
+ let c = triangleVertex(triangle, 2u);
169
+ let nearest = min(edgeDistance(point, a, b), min(edgeDistance(point, b, c), edgeDistance(point, c, a)));
170
+
171
+ return nearest <= 0.5 * pixelFootprint(point);
172
+ }
173
+
174
+ /** Three's diffuseColor.a: opacity, the colour map's alpha, the alpha map's green and the vertex alpha. */
175
+ fn materialAlpha(m: u32, triangle: u32, bary: vec2<f32>) -> f32 {
176
+ var alpha = materials[m].color.w * sampleMap(materials[m].map, triangle, bary).a
177
+ * sampleMap(materials[m].alphaMap, triangle, bary).g;
178
+
179
+ if ((materials[m].flags & MATERIAL_VERTEX_COLORS) != 0u) {
180
+ alpha = alpha * triangleColor(triangle, bary).a;
181
+ }
182
+
183
+ return alpha;
184
+ }
185
+
186
+ /**
187
+ * How much of the surface is there, from its alpha the way Three's chunks use it: alphaTest cuts, alphaHash keeps the
188
+ * share, and only a transparent material with normal blending lets the rest through; an opaque one sets alpha to one.
189
+ */
190
+ fn coverageOf(m: u32, alpha: f32) -> f32 {
191
+ if (materials[m].alphaTest > 0.0 && alpha < materials[m].alphaTest) {
192
+ return 0.0;
193
+ }
194
+
195
+ var coverage = 1.0;
196
+
197
+ if ((materials[m].flags & MATERIAL_ALPHA_HASH) != 0u) {
198
+ coverage = clamp(alpha, 0.0, 1.0);
199
+ }
200
+
201
+ if ((materials[m].flags & MATERIAL_TRANSPARENT) != 0u && materials[m].blending == BLEND_NORMAL) {
202
+ coverage = coverage * clamp(alpha, 0.0, 1.0);
203
+ }
204
+
205
+ return coverage;
206
+ }
207
+
208
+ /** Barycentrics on the triangle's plane of a ray, extrapolated past its edges: where a neighbouring pixel lands. */
209
+ fn planeBarycentric(triangle: u32, origin: vec3<f32>, direction: vec3<f32>, fallback: vec2<f32>) -> vec2<f32> {
210
+ let a = triangleVertex(triangle, 0u);
211
+ let e1 = triangleVertex(triangle, 1u) - a;
212
+ let e2 = triangleVertex(triangle, 2u) - a;
213
+ let n = cross(e1, e2);
214
+ let denominator = dot(n, direction);
215
+
216
+ if (abs(denominator) < 1e-20) {
217
+ return fallback;
218
+ }
219
+
220
+ let p = origin + direction * (dot(n, a - origin) / denominator) - a;
221
+ let d00 = dot(e1, e1);
222
+ let d01 = dot(e1, e2);
223
+ let d11 = dot(e2, e2);
224
+ let d20 = dot(p, e1);
225
+ let d21 = dot(p, e2);
226
+ let det = d00 * d11 - d01 * d01;
227
+
228
+ if (abs(det) < 1e-30) {
229
+ return fallback;
230
+ }
231
+
232
+ return vec2<f32>((d11 * d20 - d01 * d21) / det, (d00 * d21 - d01 * d20) / det);
233
+ }
234
+
235
+ /** The camera ray through a point given in normalized device coordinates: origin on the near plane, and its length. */
236
+ fn ndcRay(ndc: vec2<f32>) -> array<vec4<f32>, 2> {
237
+ let nearView = frame.inverseProjection * vec4<f32>(ndc, -1.0, 1.0);
238
+ let farView = frame.inverseProjection * vec4<f32>(ndc, 1.0, 1.0);
239
+ let near = (frame.cameraToWorld * vec4<f32>(nearView.xyz / nearView.w, 1.0)).xyz;
240
+ let far = (frame.cameraToWorld * vec4<f32>(farView.xyz / farView.w, 1.0)).xyz;
241
+ let span = far - near;
242
+ let extent = max(length(span), 1e-20);
243
+
244
+ return array<vec4<f32>, 2>(vec4<f32>(near, 0.0), vec4<f32>(span / extent, extent));
245
+ }
246
+
247
+ /** What a fragment shader knows about the point it runs at, in GL's own terms. */
248
+ fn shaderInputAt(m: u32, triangle: u32, bary: vec2<f32>, direction: vec3<f32>) -> ShaderInput {
249
+ var input: ShaderInput;
250
+ let point = trianglePointAt(triangle, bary);
251
+ let view = frame.worldToCamera * vec4<f32>(point, 1.0);
252
+ let clip = frame.projection * view;
253
+ let w = select(clip.w, 1e-20, abs(clip.w) < 1e-20);
254
+ let ndc = clip.xyz / w;
255
+ let size = vec2<f32>(frame.resolution);
256
+ let pixel = (ndc.xy * 0.5 + 0.5) * size;
257
+ let right = ndcRay(vec2<f32>(ndc.x + 2.0 / size.x, ndc.y));
258
+ let above = ndcRay(vec2<f32>(ndc.x, ndc.y + 2.0 / size.y));
259
+
260
+ input.data = materials[m].shaderData;
261
+ input.material = m;
262
+ input.triangle = triangle;
263
+ input.frontFacing = isFrontFace(triangle, direction);
264
+ input.bary = bary;
265
+ input.baryDx = planeBarycentric(triangle, right[0].xyz, right[1].xyz, bary);
266
+ input.baryDy = planeBarycentric(triangle, above[0].xyz, above[1].xyz, bary);
267
+ input.fragCoord = vec4<f32>(pixel, ndc.z * 0.5 + 0.5, 1.0 / w);
268
+ let uv = triangleUv(triangle, bary, 0u);
269
+ input.uv = uv;
270
+ input.fragColor = vec4<f32>(0.0, 0.0, 0.0, 1.0);
271
+ // A point's quad carries gl_PointCoord in its first UV set, with v running up as a texture's does.
272
+ input.pointCoord = vec2<f32>(uv.x, 1.0 - uv.y);
273
+ input.direction = direction;
274
+ input.diffuseColor = vec4<f32>(1.0);
275
+ input.normal = vec3<f32>(0.0, 0.0, 1.0);
276
+ input.geometryNormal = vec3<f32>(0.0, 0.0, 1.0);
277
+ input.roughness = 1.0;
278
+ input.metalness = 0.0;
279
+ input.emissive = vec3<f32>(0.0);
280
+ input.viewPosition = -view.xyz;
281
+
282
+ return input;
283
+ }
284
+
285
+ /** World to view space for a direction, which is the space Three's shaders keep their normals in. */
286
+ fn toView(direction: vec3<f32>) -> vec3<f32> {
287
+ return normalize((frame.worldToCamera * vec4<f32>(direction, 0.0)).xyz);
288
+ }
289
+
290
+ fn toWorld(direction: vec3<f32>) -> vec3<f32> {
291
+ return normalize((frame.cameraToWorld * vec4<f32>(direction, 0.0)).xyz);
292
+ }
293
+
294
+ /**
295
+ * How much of the surface covers this point for a shadow ray: the same alpha, cut, clipping and wireframe the camera
296
+ * sees, and a shader's own discard. Blending other than normal is handled by the caller.
297
+ */
298
+ fn coverageAt(triangle: u32, bary: vec2<f32>, direction: vec3<f32>) -> f32 {
299
+ let m = materialIndexOf(triangle);
300
+ let flags = materials[m].flags;
301
+
302
+ if (materials[m].clipCount > 0u || (flags & MATERIAL_WIREFRAME) != 0u) {
303
+ let point = trianglePointAt(triangle, bary);
304
+
305
+ if (isClipped(m, point)) {
306
+ return 0.0;
307
+ }
308
+
309
+ if ((flags & MATERIAL_WIREFRAME) != 0u && !onWireframe(triangle, point)) {
310
+ return 0.0;
311
+ }
312
+ }
313
+
314
+ var alpha = materialAlpha(m, triangle, bary);
315
+
316
+ if (materials[m].program != NO_PROGRAM) {
317
+ var input = shaderInputAt(m, triangle, bary, direction);
318
+ input.diffuseColor = vec4<f32>(materials[m].color.rgb, alpha);
319
+ let output = runProgram(materials[m].program, input);
320
+
321
+ if (output.discarded) {
322
+ return 0.0;
323
+ }
324
+
325
+ alpha = select(output.color.a, output.diffuseColor.a, (flags & MATERIAL_PATCHED) != 0u);
326
+ }
327
+
328
+ return coverageOf(m, alpha);
329
+ }
330
+
331
+ /** A tangent frame from the triangle's own UVs, which is what Three's tangents amount to for a flat triangle. */
332
+ struct TangentFrame {
333
+ tangent: vec3<f32>,
334
+ bitangent: vec3<f32>,
335
+ valid: bool,
336
+ };
337
+
338
+ fn tangentFrame(triangle: u32, channel: u32, normal: vec3<f32>) -> TangentFrame {
339
+ var tangents: TangentFrame;
340
+ tangents.valid = false;
341
+
342
+ let p0 = triangleVertex(triangle, 0u);
343
+ let edge1 = triangleVertex(triangle, 1u) - p0;
344
+ let edge2 = triangleVertex(triangle, 2u) - p0;
345
+ let uv0 = attributeUv(triangle, 0u, channel);
346
+ let delta1 = attributeUv(triangle, 1u, channel) - uv0;
347
+ let delta2 = attributeUv(triangle, 2u, channel) - uv0;
348
+ let determinant = delta1.x * delta2.y - delta2.x * delta1.y;
349
+
350
+ // A triangle with no UV area has no tangent frame to offer.
351
+ if (abs(determinant) < 1e-16) {
352
+ let basis = orthonormalBasis(normal);
353
+ tangents.tangent = basis[0];
354
+ tangents.bitangent = basis[1];
355
+
356
+ return tangents;
357
+ }
358
+
359
+ let raw = (edge1 * delta2.y - edge2 * delta1.y) / determinant;
360
+ // Made perpendicular to the shading normal, so a map tilts the interpolated normal rather than the flat one.
361
+ let tangent = normalize(raw - normal * dot(normal, raw));
362
+
363
+ if (!all(tangent == tangent)) {
364
+ let basis = orthonormalBasis(normal);
365
+ tangents.tangent = basis[0];
366
+ tangents.bitangent = basis[1];
367
+
368
+ return tangents;
369
+ }
370
+
371
+ tangents.tangent = tangent;
372
+ tangents.bitangent = cross(normal, tangent) * select(-1.0, 1.0, determinant >= 0.0);
373
+ tangents.valid = true;
374
+
375
+ return tangents;
376
+ }
377
+
378
+ /** A tangent space normal map over a normal, scaled the way Three's normalScale scales it. */
379
+ fn tangentNormal(index: u32, triangle: u32, bary: vec2<f32>, normal: vec3<f32>, scale: vec2<f32>) -> vec3<f32> {
380
+ if (index == NO_TEXTURE) {
381
+ return normal;
382
+ }
383
+
384
+ let channel = textureInfo(index).channel;
385
+ let tangents = tangentFrame(triangle, channel, normal);
386
+
387
+ if (!tangents.valid) {
388
+ return normal;
389
+ }
390
+
391
+ let texel = sampleTextureAt(index, triangleUv(triangle, bary, channel)).xyz * 2.0 - 1.0;
392
+ let mapped = normalize(
393
+ tangents.tangent * (texel.x * scale.x)
394
+ + tangents.bitangent * (texel.y * scale.y)
395
+ + normal * texel.z);
396
+
397
+ return select(mapped, normal, !all(mapped == mapped));
398
+ }
399
+
400
+ /** The normal the material asks for: an object space or tangent space normal map, or else a bump map. */
401
+ fn mappedNormal(m: u32, triangle: u32, bary: vec2<f32>, normal: vec3<f32>) -> vec3<f32> {
402
+ let normalMap = materials[m].normalMap;
403
+
404
+ if (normalMap != NO_TEXTURE && (materials[m].flags & MATERIAL_OBJECT_NORMAL_MAP) != 0u) {
405
+ let texel = sampleMap(normalMap, triangle, bary).xyz * 2.0 - 1.0;
406
+ let world = normalize(vec3<f32>(
407
+ dot(materials[m].normalRow0.xyz, texel),
408
+ dot(materials[m].normalRow1.xyz, texel),
409
+ dot(materials[m].normalRow2.xyz, texel)));
410
+
411
+ // Seen from behind, a double sided surface turns its map around with it.
412
+ return select(world, -world, dot(world, normal) < 0.0);
413
+ }
414
+
415
+ if (normalMap != NO_TEXTURE) {
416
+ return tangentNormal(normalMap, triangle, bary, normal, vec2<f32>(materials[m].normalScaleX, materials[m].normalScaleY));
417
+ }
418
+
419
+ let bumpMap = materials[m].bumpMap;
420
+
421
+ if (bumpMap != NO_TEXTURE) {
422
+ // The CPU renderer's bump: the height's change over one texel, along the tangent frame of the map's UVs.
423
+ let info = textureInfo(bumpMap);
424
+ let channel = info.channel;
425
+ let tangents = tangentFrame(triangle, channel, normal);
426
+ let uv = triangleUv(triangle, bary, channel);
427
+ let du = 1.0 / f32(max(info.size.x, 1u));
428
+ let dv = 1.0 / f32(max(info.size.y, 1u));
429
+ let scale = materials[m].bumpScale;
430
+ let height = sampleTextureAt(bumpMap, uv).x;
431
+ let dx = (sampleTextureAt(bumpMap, uv + vec2<f32>(du, 0.0)).x - height) * scale;
432
+ let dy = (sampleTextureAt(bumpMap, uv + vec2<f32>(0.0, dv)).x - height) * scale;
433
+ let bumped = normalize(normal - (tangents.tangent * dx + tangents.bitangent * dy));
434
+
435
+ return select(bumped, normal, !all(bumped == bumped));
436
+ }
437
+
438
+ return normal;
439
+ }
440
+
441
+ /** The interpolated vertex normal as the mesh authored it, pointing out of a closed volume. */
442
+ fn authoredNormal(triangle: u32, bary: vec2<f32>) -> vec3<f32> {
443
+ let base = triangle * 9u;
444
+ let n0 = vec3<f32>(attributes[base], attributes[base + 1u], attributes[base + 2u]);
445
+ let n1 = vec3<f32>(attributes[base + 3u], attributes[base + 4u], attributes[base + 5u]);
446
+ let n2 = vec3<f32>(attributes[base + 6u], attributes[base + 7u], attributes[base + 8u]);
447
+
448
+ return normalize(n0 * (1.0 - bary.x - bary.y) + n1 * bary.x + n2 * bary.y);
449
+ }
450
+
451
+ /** The interpolated vertex normal, or the face's for flat shading, turned to face the ray. */
452
+ fn interpolatedNormal(m: u32, triangle: u32, bary: vec2<f32>, direction: vec3<f32>) -> vec3<f32> {
453
+ var normal = authoredNormal(triangle, bary);
454
+
455
+ if ((materials[m].flags & MATERIAL_FLAT) != 0u || !all(normal == normal)) {
456
+ normal = triangleNormal(triangle);
457
+ }
458
+
459
+ return select(normal, -normal, dot(normal, direction) > 0.0);
460
+ }
461
+
462
+ /** Three's transmission at this point: the factor, its map's red channel, and none of it on metal. */
463
+ fn transmissionAt(m: u32, triangle: u32, bary: vec2<f32>, metalness: f32) -> f32 {
464
+ if (materials[m].transmission <= 0.0) {
465
+ return 0.0;
466
+ }
467
+
468
+ let map = sampleMap(materials[m].transmissionMap, triangle, bary).r;
469
+
470
+ return clamp(materials[m].transmission * map, 0.0, 1.0) * (1.0 - metalness);
471
+ }
472
+
473
+ /** Beer-Lambert: the colour reached at the attenuation distance fixes how fast each channel fades. */
474
+ fn absorptionOf(m: u32) -> vec3<f32> {
475
+ let attenuation = materials[m].attenuation;
476
+
477
+ if (attenuation.w <= 0.0) {
478
+ return vec3<f32>(0.0);
479
+ }
480
+
481
+ return -log(max(attenuation.rgb, vec3<f32>(1e-6))) / attenuation.w;
482
+ }
483
+
484
+ /** Three's evalSensitivity: the XYZ response of a thin film, Fourier-fitted, in linear sRGB. */
485
+ fn filmSensitivity(opd: f32, shift: vec3<f32>) -> vec3<f32> {
486
+ let phase = 2.0 * PI * opd * 1.0e-9;
487
+ let value = vec3<f32>(5.4856e-13, 4.4201e-13, 5.2481e-13);
488
+ let position = vec3<f32>(1.6810e+06, 1.7953e+06, 2.2084e+06);
489
+ let variance = vec3<f32>(4.3278e+09, 9.3046e+09, 6.6121e+09);
490
+ var xyz = value * sqrt(2.0 * PI * variance) * cos(position * phase + shift) * exp(-phase * phase * variance);
491
+
492
+ xyz.x = xyz.x + 9.7470e-14 * sqrt(2.0 * PI * 4.5282e+09) * cos(2.2399e+06 * phase + shift.x)
493
+ * exp(-4.5282e+09 * phase * phase);
494
+ xyz = xyz / 1.0685e-7;
495
+
496
+ return mat3x3<f32>(
497
+ vec3<f32>(3.2404542, -0.9692660, 0.0556434),
498
+ vec3<f32>(-1.5371385, 1.8760108, -0.2040259),
499
+ vec3<f32>(-0.4985314, 0.0415560, 1.0572252)) * xyz;
500
+ }
501
+
502
+ fn schlick3(f0: vec3<f32>, f90: f32, cosine: f32) -> vec3<f32> {
503
+ let t = 1.0 - cosine;
504
+ let k = t * t * t * t * t;
505
+
506
+ return f0 + (vec3<f32>(f90) - f0) * k;
507
+ }
508
+
509
+ /** Three's evalIridescence (Belcour and Barla 2017): the Fresnel of a thin film over a base of reflectance baseF0. */
510
+ fn evalIridescence(filmIor: f32, cosTheta1: f32, thickness: f32, baseF0: vec3<f32>) -> vec3<f32> {
511
+ let ior = mix(1.0, filmIor, smoothstep(0.0, 0.03, thickness));
512
+ let sinTheta2Sq = (1.0 / ior) * (1.0 / ior) * (1.0 - cosTheta1 * cosTheta1);
513
+ let cosTheta2Sq = 1.0 - sinTheta2Sq;
514
+
515
+ if (cosTheta2Sq < 0.0) {
516
+ return vec3<f32>(1.0);
517
+ }
518
+
519
+ let cosTheta2 = sqrt(cosTheta2Sq);
520
+ let r0 = ((ior - 1.0) / (ior + 1.0)) * ((ior - 1.0) / (ior + 1.0));
521
+ let r12 = r0 + (1.0 - r0) * pow(1.0 - cosTheta1, 5.0);
522
+ let t121 = 1.0 - r12;
523
+ let phi12 = select(0.0, PI, ior < 1.0);
524
+ let phi21 = PI - phi12;
525
+ let root = sqrt(clamp(baseF0, vec3<f32>(0.0), vec3<f32>(0.9999)));
526
+ let baseIor = (vec3<f32>(1.0) + root) / (vec3<f32>(1.0) - root);
527
+ let r1 = ((baseIor - vec3<f32>(ior)) / (baseIor + vec3<f32>(ior))) * ((baseIor - vec3<f32>(ior)) / (baseIor + vec3<f32>(ior)));
528
+ let r23 = schlick3(r1, 1.0, cosTheta2);
529
+ let phi23 = select(vec3<f32>(0.0), vec3<f32>(PI), baseIor < vec3<f32>(ior));
530
+ let opd = 2.0 * ior * thickness * cosTheta2;
531
+ let phi = vec3<f32>(phi21) + phi23;
532
+ let r123 = clamp(r12 * r23, vec3<f32>(1e-5), vec3<f32>(0.9999));
533
+ let r123Root = sqrt(r123);
534
+ let rs = t121 * t121 * r23 / (vec3<f32>(1.0) - r123);
535
+ var intensity = vec3<f32>(r12) + rs;
536
+ var cm = rs - vec3<f32>(t121);
537
+
538
+ for (var m = 1; m <= 2; m = m + 1) {
539
+ cm = cm * r123Root;
540
+ intensity = intensity + cm * 2.0 * filmSensitivity(f32(m) * opd, f32(m) * phi);
541
+ }
542
+
543
+ return max(intensity, vec3<f32>(0.0));
544
+ }
545
+
546
+ /** Three's toon ramp: the gradient map across dot(n, l) from -1 to 1, or a single step at 0.7 without one. */
547
+ fn toonRamp(gradientMap: u32, cosine: f32) -> f32 {
548
+ let coordinate = cosine * 0.5 + 0.5;
549
+
550
+ if (gradientMap == NO_TEXTURE) {
551
+ return select(0.7, 1.0, coordinate >= 0.7);
552
+ }
553
+
554
+ return sampleTextureRaw(gradientMap, vec2<f32>(coordinate, 0.0)).r;
555
+ }
556
+
557
+ /** Everything the lobes need about one point of one triangle. */
558
+ fn resolveSurface(triangle: u32, bary: vec2<f32>, direction: vec3<f32>) -> Surface {
559
+ let m = materialIndexOf(triangle);
560
+ let flags = materials[m].flags;
561
+ let kind = materials[m].kind;
562
+ let base = sampleMap(materials[m].map, triangle, bary);
563
+ let facing = interpolatedNormal(m, triangle, bary, direction);
564
+ let view = -direction;
565
+ var surface: Surface;
566
+
567
+ surface.material = m;
568
+ surface.flags = flags;
569
+ surface.kind = kind;
570
+ surface.blending = materials[m].blending;
571
+ surface.facing = facing;
572
+ surface.front = dot(authoredNormal(triangle, bary), direction) < 0.0;
573
+ surface.normal = mappedNormal(m, triangle, bary, facing);
574
+
575
+ // A normal map may tilt past the horizon, where the surface would be lit from behind itself.
576
+ if (dot(surface.normal, view) <= 0.0) {
577
+ surface.normal = facing;
578
+ }
579
+
580
+ var albedo = materials[m].color.rgb * base.rgb;
581
+ var alpha = materials[m].color.w * base.a * sampleMap(materials[m].alphaMap, triangle, bary).g;
582
+
583
+ if ((flags & MATERIAL_VERTEX_COLORS) != 0u) {
584
+ let color = triangleColor(triangle, bary);
585
+ albedo = albedo * color.rgb;
586
+ alpha = alpha * color.a;
587
+ }
588
+
589
+ var roughness = materials[m].specular.w * sampleMap(materials[m].roughnessMap, triangle, bary).g;
590
+ var metalness = materials[m].metalness * sampleMap(materials[m].metalnessMap, triangle, bary).b;
591
+ var emissive = emissionAt(triangle, bary);
592
+
593
+ surface.opacity = 1.0;
594
+
595
+ // A built-in material's hooks run on its values before lighting, in Three's view space; a shader material's output
596
+ // is the colour itself.
597
+ if (materials[m].program != NO_PROGRAM) {
598
+ var input = shaderInputAt(m, triangle, bary, direction);
599
+ input.diffuseColor = vec4<f32>(albedo, alpha);
600
+ input.normal = toView(surface.normal);
601
+ input.geometryNormal = toView(facing);
602
+ input.roughness = roughness;
603
+ input.metalness = metalness;
604
+ input.emissive = emissive;
605
+ let output = runProgram(materials[m].program, input);
606
+
607
+ if (output.discarded) {
608
+ surface.opacity = 0.0;
609
+ }
610
+
611
+ if ((flags & MATERIAL_PATCHED) != 0u) {
612
+ albedo = output.diffuseColor.rgb;
613
+ alpha = output.diffuseColor.a;
614
+ roughness = output.roughness;
615
+ metalness = output.metalness;
616
+ emissive = output.emissive;
617
+ let normal = toWorld(output.normal);
618
+
619
+ if (all(normal == normal)) {
620
+ surface.normal = select(normal, -normal, dot(normal, view) < 0.0);
621
+ }
622
+ } else {
623
+ albedo = output.color.rgb;
624
+ alpha = output.color.a;
625
+ }
626
+ }
627
+
628
+ surface.opacity = surface.opacity * coverageOf(m, alpha);
629
+ surface.alpha = alpha;
630
+ surface.albedo = albedo;
631
+ surface.roughness = clamp(roughness, 0.0525, 1.0);
632
+ surface.metalness = clamp(metalness, 0.0, 1.0);
633
+ surface.emissive = emissive;
634
+ surface.transmission = transmissionAt(m, triangle, bary, surface.metalness);
635
+ surface.ior = max(materials[m].ior, 1.0);
636
+ surface.thickness = materials[m].thickness * sampleMap(materials[m].thicknessMap, triangle, bary).g;
637
+ surface.dispersion = materials[m].dispersion;
638
+ surface.absorption = absorptionOf(m);
639
+ surface.shininess = materials[m].shininess;
640
+ surface.specularStrength = sampleMap(materials[m].specularMap, triangle, bary).r;
641
+ surface.gradientMap = materials[m].gradientMap;
642
+ surface.envMap = NO_TEXTURE;
643
+ surface.envMapIntensity = materials[m].envMapIntensity;
644
+ surface.legacyAmount = 0.0;
645
+ surface.legacyEnv = vec3<f32>(0.0);
646
+ surface.combine = materials[m].combine;
647
+ surface.lightMap = vec3<f32>(0.0);
648
+ surface.clearcoat = 0.0;
649
+ surface.iridescence = 0.0;
650
+ surface.anisotropy = 0.0;
651
+ surface.sheenColor = vec3<f32>(0.0);
652
+ surface.specularColor = materials[m].specular.rgb;
653
+ surface.specularF90 = 1.0;
654
+
655
+ if ((flags & MATERIAL_PBR) != 0u) {
656
+ if ((flags & MATERIAL_SPECULAR) != 0u) {
657
+ // A physical material: the index of refraction, the specular colour and intensity and their maps.
658
+ let intensity = materials[m].specularIntensity * sampleMap(materials[m].specularIntensityMap, triangle, bary).a;
659
+ let color = materials[m].specular.rgb * sampleMap(materials[m].specularColorMap, triangle, bary).rgb;
660
+
661
+ surface.specularColor = min(materials[m].dielectric * color, vec3<f32>(1.0)) * intensity;
662
+ surface.specularF90 = mix(intensity, 1.0, surface.metalness);
663
+ } else {
664
+ // Three's standard material has no index of refraction and reflects 0.04.
665
+ surface.specularColor = vec3<f32>(0.04);
666
+ }
667
+
668
+ surface.envMap = materials[m].envMap;
669
+
670
+ if (materials[m].clearcoat > 0.0) {
671
+ surface.clearcoat = clamp(materials[m].clearcoat * sampleMap(materials[m].clearcoatMap, triangle, bary).r, 0.0, 1.0);
672
+ surface.clearcoatRoughness = clamp(
673
+ materials[m].clearcoatRoughness * sampleMap(materials[m].clearcoatRoughnessMap, triangle, bary).g, 0.0525, 1.0);
674
+ // The coat ignores the base's normal map: it has its own, over the interpolated normal.
675
+ surface.clearcoatNormal = tangentNormal(materials[m].clearcoatNormalMap, triangle, bary, facing,
676
+ vec2<f32>(materials[m].clearcoatNormalScaleX, materials[m].clearcoatNormalScaleY));
677
+
678
+ if (dot(surface.clearcoatNormal, view) <= 0.0) {
679
+ surface.clearcoatNormal = facing;
680
+ }
681
+ }
682
+
683
+ if (any(materials[m].sheen.rgb > vec3<f32>(0.0))) {
684
+ let map = sampleMap(materials[m].sheenRoughnessMap, triangle, bary).a;
685
+ surface.sheenColor = materials[m].sheen.rgb * sampleMap(materials[m].sheenColorMap, triangle, bary).rgb;
686
+ surface.sheenRoughness = clamp(materials[m].sheen.w, 0.0001, 1.0) * map;
687
+ }
688
+
689
+ if (materials[m].iridescence > 0.0) {
690
+ var thickness = materials[m].iridescenceThicknessMax;
691
+
692
+ if (materials[m].iridescenceThicknessMap != NO_TEXTURE) {
693
+ thickness = mix(materials[m].iridescenceThicknessMin, materials[m].iridescenceThicknessMax,
694
+ sampleMap(materials[m].iridescenceThicknessMap, triangle, bary).g);
695
+ }
696
+
697
+ if (thickness > 0.0) {
698
+ surface.iridescence = clamp(
699
+ materials[m].iridescence * sampleMap(materials[m].iridescenceMap, triangle, bary).r, 0.0, 1.0);
700
+
701
+ let nv = clamp(dot(surface.normal, view), 0.0, 1.0);
702
+ surface.iridescenceFresnel = mix(
703
+ evalIridescence(materials[m].iridescenceIor, nv, thickness, surface.specularColor),
704
+ evalIridescence(materials[m].iridescenceIor, nv, thickness, albedo),
705
+ surface.metalness);
706
+ }
707
+ }
708
+
709
+ if (materials[m].anisotropy != 0.0) {
710
+ let rotation = materials[m].anisotropyRotation;
711
+ var vector = vec2<f32>(cos(rotation), sin(rotation)) * materials[m].anisotropy;
712
+ let channel = select(0u, textureInfo(materials[m].anisotropyMap).channel, materials[m].anisotropyMap != NO_TEXTURE);
713
+ let tangents = tangentFrame(triangle, channel, surface.normal);
714
+
715
+ if (materials[m].anisotropyMap != NO_TEXTURE) {
716
+ let polar = sampleMap(materials[m].anisotropyMap, triangle, bary).rgb;
717
+ let turn = mat2x2<f32>(vector.x, vector.y, -vector.y, vector.x);
718
+ vector = turn * normalize(2.0 * polar.rg - vec2<f32>(1.0)) * polar.b;
719
+ }
720
+
721
+ let strength = length(vector);
722
+ let along = select(vector / max(strength, 1e-20), vec2<f32>(1.0, 0.0), strength == 0.0);
723
+
724
+ surface.anisotropy = clamp(strength, 0.0, 1.0);
725
+ surface.alphaT = mix(surface.roughness * surface.roughness, 1.0, surface.anisotropy * surface.anisotropy);
726
+ surface.anisotropyT = tangents.tangent * along.x + tangents.bitangent * along.y;
727
+ surface.anisotropyB = tangents.bitangent * along.x - tangents.tangent * along.y;
728
+ }
729
+ } else if (materials[m].envMap != NO_TEXTURE
730
+ && (kind == KIND_BASIC || kind == KIND_LAMBERT || kind == KIND_PHONG)) {
731
+ // Basic, Lambert and Phong blend their own map over the lit colour, mirrored or bent the way the map says.
732
+ let info = textureInfo(materials[m].envMap);
733
+ let bent = refract(direction, surface.normal, materials[m].refractionRatio);
734
+ let lookup = select(reflect(direction, surface.normal), bent, (info.flags & 16u) != 0u && any(bent != vec3<f32>(0.0)));
735
+
736
+ surface.legacyEnv = sampleEquirect(materials[m].envMap, lookup).rgb * materials[m].envMapIntensity;
737
+ surface.legacyAmount = clamp(materials[m].reflectivity * surface.specularStrength, 0.0, 1.0);
738
+ }
739
+
740
+ if (materials[m].lightMap != NO_TEXTURE) {
741
+ let baked = sampleMap(materials[m].lightMap, triangle, bary).rgb * materials[m].lightMapIntensity;
742
+ surface.lightMap = baked * albedo * (1.0 - surface.metalness) / PI;
743
+ }
744
+
745
+ // A material Three does not light reads as its own colour, which in a path tracer is the same as emitting it.
746
+ if ((flags & MATERIAL_LIT) == 0u) {
747
+ var color = albedo;
748
+
749
+ if (kind == KIND_NORMAL) {
750
+ color = toView(surface.normal) * 0.5 + 0.5;
751
+ } else if (kind == KIND_DEPTH) {
752
+ let clip = frame.projection * (frame.worldToCamera * vec4<f32>(trianglePointAt(triangle, bary), 1.0));
753
+ color = vec3<f32>(1.0 - (clip.z / clip.w * 0.5 + 0.5));
754
+ } else if (kind == KIND_MATCAP) {
755
+ let point = trianglePointAt(triangle, bary);
756
+ let viewDirection = normalize(-(frame.worldToCamera * vec4<f32>(point, 1.0)).xyz);
757
+ let x = normalize(vec3<f32>(viewDirection.z, 0.0, -viewDirection.x));
758
+ let y = cross(viewDirection, x);
759
+ let normal = toView(surface.normal);
760
+ let uv = vec2<f32>(dot(x, normal), dot(y, normal)) * 0.495 + 0.5;
761
+ var matcap = vec3<f32>(mix(0.2, 0.8, uv.y));
762
+
763
+ if (materials[m].matcap != NO_TEXTURE) {
764
+ matcap = sampleTextureRaw(materials[m].matcap, uv).rgb;
765
+ }
766
+
767
+ color = albedo * matcap;
768
+ } else if (kind == KIND_BASIC && materials[m].lightMap != NO_TEXTURE) {
769
+ // A basic material with a light map shows the map's light instead of full brightness.
770
+ color = albedo * sampleMap(materials[m].lightMap, triangle, bary).rgb * materials[m].lightMapIntensity / PI;
771
+ }
772
+
773
+ if (surface.legacyAmount > 0.0) {
774
+ let amount = surface.legacyAmount;
775
+
776
+ if (surface.combine == 1u) {
777
+ color = mix(color, surface.legacyEnv, amount);
778
+ } else if (surface.combine == 2u) {
779
+ color = color + surface.legacyEnv * amount;
780
+ } else {
781
+ color = mix(color, color * surface.legacyEnv, amount);
782
+ }
783
+
784
+ surface.legacyAmount = 0.0;
785
+ }
786
+
787
+ surface.emissive = surface.emissive + color;
788
+ surface.lightMap = vec3<f32>(0.0);
789
+ }
790
+
791
+ return surface;
792
+ }
793
+ `;
794
+ //# sourceMappingURL=surface.wgsl.js.map