super-three 0.158.0 → 0.160.1

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 (191) hide show
  1. package/build/three.cjs +1720 -380
  2. package/build/three.js +1720 -380
  3. package/build/three.min.js +1 -1
  4. package/build/three.module.js +1719 -381
  5. package/build/three.module.min.js +1 -1
  6. package/examples/jsm/Addons.js +2 -1
  7. package/examples/jsm/controls/OrbitControls.js +50 -40
  8. package/examples/jsm/csm/CSMShader.js +52 -47
  9. package/examples/jsm/exporters/GLTFExporter.js +79 -1
  10. package/examples/jsm/exporters/USDZExporter.js +27 -19
  11. package/examples/jsm/helpers/TextureHelper.js +237 -0
  12. package/examples/jsm/interactive/HTMLMesh.js +9 -2
  13. package/examples/jsm/lines/LineMaterial.js +16 -33
  14. package/examples/jsm/loaders/ColladaLoader.js +0 -6
  15. package/examples/jsm/loaders/DRACOLoader.js +4 -3
  16. package/examples/jsm/loaders/FBXLoader.js +294 -124
  17. package/examples/jsm/loaders/GLTFLoader.js +71 -3
  18. package/examples/jsm/loaders/MaterialXLoader.js +73 -54
  19. package/examples/jsm/loaders/NRRDLoader.js +0 -13
  20. package/examples/jsm/loaders/SVGLoader.js +4 -4
  21. package/examples/jsm/loaders/USDZLoader.js +3 -17
  22. package/examples/jsm/math/Octree.js +15 -4
  23. package/examples/jsm/misc/Timer.js +119 -0
  24. package/examples/jsm/misc/TubePainter.js +3 -6
  25. package/examples/jsm/modifiers/CurveModifier.js +20 -2
  26. package/examples/jsm/nodes/Nodes.js +11 -7
  27. package/examples/jsm/nodes/accessors/CameraNode.js +18 -2
  28. package/examples/jsm/nodes/accessors/CubeTextureNode.js +5 -73
  29. package/examples/jsm/nodes/accessors/MaterialReferenceNode.js +11 -2
  30. package/examples/jsm/nodes/accessors/ModelNode.js +1 -1
  31. package/examples/jsm/nodes/accessors/ModelViewProjectionNode.js +12 -3
  32. package/examples/jsm/nodes/accessors/MorphNode.js +191 -16
  33. package/examples/jsm/nodes/accessors/ReferenceNode.js +25 -1
  34. package/examples/jsm/nodes/accessors/TextureNode.js +122 -36
  35. package/examples/jsm/nodes/code/CodeNode.js +3 -3
  36. package/examples/jsm/nodes/core/AttributeNode.js +7 -1
  37. package/examples/jsm/nodes/core/CacheNode.js +4 -1
  38. package/examples/jsm/nodes/core/ConstNode.js +1 -1
  39. package/examples/jsm/nodes/core/Node.js +6 -3
  40. package/examples/jsm/nodes/core/NodeBuilder.js +84 -27
  41. package/examples/jsm/nodes/core/NodeFrame.js +10 -8
  42. package/examples/jsm/nodes/core/NodeUniform.js +12 -0
  43. package/examples/jsm/nodes/core/NodeUtils.js +3 -7
  44. package/examples/jsm/nodes/core/OutputStructNode.js +3 -3
  45. package/examples/jsm/nodes/core/PropertyNode.js +17 -2
  46. package/examples/jsm/nodes/core/StructTypeNode.js +4 -4
  47. package/examples/jsm/nodes/core/UniformGroup.js +13 -0
  48. package/examples/jsm/nodes/core/UniformGroupNode.js +36 -0
  49. package/examples/jsm/nodes/core/UniformNode.js +17 -0
  50. package/examples/jsm/nodes/core/VarNode.js +2 -0
  51. package/examples/jsm/nodes/core/VaryingNode.js +1 -7
  52. package/examples/jsm/nodes/display/BumpMapNode.js +1 -1
  53. package/examples/jsm/nodes/display/ColorAdjustmentNode.js +6 -10
  54. package/examples/jsm/nodes/display/GaussianBlurNode.js +170 -0
  55. package/examples/jsm/nodes/display/PassNode.js +182 -0
  56. package/examples/jsm/nodes/display/ViewportDepthNode.js +31 -3
  57. package/examples/jsm/nodes/display/ViewportDepthTextureNode.js +2 -2
  58. package/examples/jsm/nodes/display/ViewportNode.js +5 -8
  59. package/examples/jsm/nodes/functions/PhysicalLightingModel.js +1 -1
  60. package/examples/jsm/nodes/lighting/AnalyticLightNode.js +52 -17
  61. package/examples/jsm/nodes/lighting/EnvironmentNode.js +8 -12
  62. package/examples/jsm/nodes/lighting/LightsNode.js +34 -32
  63. package/examples/jsm/nodes/materials/InstancedPointsNodeMaterial.js +1 -1
  64. package/examples/jsm/nodes/materials/Line2NodeMaterial.js +12 -12
  65. package/examples/jsm/nodes/materials/MeshBasicNodeMaterial.js +1 -0
  66. package/examples/jsm/nodes/materials/MeshNormalNodeMaterial.js +1 -1
  67. package/examples/jsm/nodes/materials/NodeMaterial.js +66 -41
  68. package/examples/jsm/nodes/materials/PointsNodeMaterial.js +0 -10
  69. package/examples/jsm/nodes/materials/SpriteNodeMaterial.js +0 -7
  70. package/examples/jsm/nodes/materialx/MaterialXNodes.js +10 -10
  71. package/examples/jsm/nodes/materialx/lib/mx_hsv.js +128 -54
  72. package/examples/jsm/nodes/materialx/lib/mx_noise.js +1430 -618
  73. package/examples/jsm/nodes/materialx/lib/mx_transform_color.js +24 -14
  74. package/examples/jsm/nodes/math/MathNode.js +2 -0
  75. package/examples/jsm/nodes/math/OperatorNode.js +19 -6
  76. package/examples/jsm/nodes/shadernode/ShaderNode.js +32 -3
  77. package/examples/jsm/nodes/utils/FunctionOverloadingNode.js +95 -0
  78. package/examples/jsm/nodes/utils/RemapNode.js +2 -2
  79. package/examples/jsm/nodes/utils/SplitNode.js +8 -2
  80. package/examples/jsm/nodes/utils/TriplanarTexturesNode.js +3 -3
  81. package/examples/jsm/objects/QuadMesh.js +60 -0
  82. package/examples/jsm/postprocessing/GTAOPass.js +572 -0
  83. package/examples/jsm/postprocessing/OutputPass.js +2 -0
  84. package/examples/jsm/renderers/common/Animation.js +10 -21
  85. package/examples/jsm/renderers/common/Backend.js +19 -3
  86. package/examples/jsm/renderers/common/Background.js +20 -22
  87. package/examples/jsm/renderers/common/Bindings.js +30 -21
  88. package/examples/jsm/renderers/common/ChainMap.js +3 -3
  89. package/examples/jsm/renderers/common/Color4.js +37 -0
  90. package/examples/jsm/renderers/common/Geometries.js +4 -4
  91. package/examples/jsm/renderers/common/Info.js +12 -2
  92. package/examples/jsm/renderers/common/Pipelines.js +3 -51
  93. package/examples/jsm/renderers/common/PostProcessing.js +25 -0
  94. package/examples/jsm/renderers/common/RenderContext.js +1 -1
  95. package/examples/jsm/renderers/common/RenderContexts.js +1 -1
  96. package/examples/jsm/renderers/common/RenderObject.js +13 -1
  97. package/examples/jsm/renderers/common/RenderObjects.js +4 -2
  98. package/examples/jsm/renderers/common/Renderer.js +137 -37
  99. package/examples/jsm/renderers/common/StorageTexture.js +1 -0
  100. package/examples/jsm/renderers/common/Textures.js +4 -1
  101. package/examples/jsm/renderers/common/nodes/NodeBuilderState.js +10 -2
  102. package/examples/jsm/renderers/common/nodes/NodeUniformsGroup.js +44 -0
  103. package/examples/jsm/renderers/common/nodes/Nodes.js +74 -7
  104. package/examples/jsm/renderers/webgl/WebGLBackend.js +91 -33
  105. package/examples/jsm/renderers/webgl/nodes/GLSLNodeBuilder.js +100 -31
  106. package/examples/jsm/renderers/webgl/utils/WebGLAttributeUtils.js +57 -5
  107. package/examples/jsm/renderers/webgl/utils/WebGLCapabilities.js +36 -0
  108. package/examples/jsm/renderers/webgl/utils/WebGLExtensions.js +11 -1
  109. package/examples/jsm/renderers/webgl/utils/WebGLTextureUtils.js +71 -2
  110. package/examples/jsm/renderers/webgl/utils/WebGLUtils.js +42 -0
  111. package/examples/jsm/renderers/webgl-legacy/nodes/SlotNode.js +1 -1
  112. package/examples/jsm/renderers/webgl-legacy/nodes/WebGLNodeBuilder.js +33 -3
  113. package/examples/jsm/renderers/webgl-legacy/nodes/WebGLNodes.js +4 -14
  114. package/examples/jsm/renderers/webgpu/WebGPUBackend.js +186 -137
  115. package/examples/jsm/renderers/webgpu/WebGPURenderer.js +1 -1
  116. package/examples/jsm/renderers/webgpu/nodes/WGSLNodeBuilder.js +209 -90
  117. package/examples/jsm/renderers/webgpu/utils/WebGPUAttributeUtils.js +15 -10
  118. package/examples/jsm/renderers/webgpu/utils/WebGPUBindingUtils.js +11 -3
  119. package/examples/jsm/renderers/webgpu/utils/WebGPUPipelineUtils.js +2 -3
  120. package/examples/jsm/renderers/webgpu/utils/WebGPUTextureUtils.js +88 -6
  121. package/examples/jsm/shaders/FXAAShader.js +133 -133
  122. package/examples/jsm/shaders/GTAOShader.js +424 -0
  123. package/examples/jsm/shaders/OutputShader.js +4 -0
  124. package/examples/jsm/shaders/PoissonDenoiseShader.js +226 -0
  125. package/examples/jsm/shaders/SAOShader.js +2 -3
  126. package/examples/jsm/shaders/SSAOShader.js +2 -3
  127. package/examples/jsm/transpiler/AST.js +40 -1
  128. package/examples/jsm/transpiler/GLSLDecoder.js +50 -8
  129. package/examples/jsm/transpiler/ShaderToyDecoder.js +4 -4
  130. package/examples/jsm/transpiler/TSLEncoder.js +124 -20
  131. package/examples/jsm/utils/SortUtils.js +158 -0
  132. package/examples/jsm/utils/TextureUtils.js +33 -21
  133. package/examples/jsm/webxr/ARButton.js +14 -0
  134. package/examples/jsm/webxr/VRButton.js +23 -8
  135. package/examples/jsm/webxr/XRButton.js +25 -11
  136. package/package.json +5 -6
  137. package/src/Three.js +1 -0
  138. package/src/constants.js +2 -1
  139. package/src/core/BufferAttribute.js +21 -2
  140. package/src/core/InterleavedBuffer.js +21 -1
  141. package/src/core/Object3D.js +60 -12
  142. package/src/core/UniformsGroup.js +7 -1
  143. package/src/extras/curves/LineCurve3.js +4 -0
  144. package/src/loaders/AudioLoader.js +1 -1
  145. package/src/loaders/BufferGeometryLoader.js +0 -7
  146. package/src/loaders/ImageBitmapLoader.js +25 -1
  147. package/src/loaders/ObjectLoader.js +49 -0
  148. package/src/materials/ShaderMaterial.js +2 -1
  149. package/src/math/ColorManagement.js +0 -16
  150. package/src/math/Quaternion.js +5 -4
  151. package/src/math/Sphere.js +2 -0
  152. package/src/math/Triangle.js +17 -5
  153. package/src/objects/BatchedMesh.js +1020 -0
  154. package/src/objects/Skeleton.js +1 -3
  155. package/src/renderers/WebGL3DRenderTarget.js +2 -2
  156. package/src/renderers/WebGLArrayRenderTarget.js +2 -2
  157. package/src/renderers/WebGLRenderer.js +24 -19
  158. package/src/renderers/shaders/ShaderChunk/batching_pars_vertex.glsl.js +19 -0
  159. package/src/renderers/shaders/ShaderChunk/batching_vertex.glsl.js +5 -0
  160. package/src/renderers/shaders/ShaderChunk/cube_uv_reflection_fragment.glsl.js +0 -5
  161. package/src/renderers/shaders/ShaderChunk/defaultnormal_vertex.glsl.js +31 -4
  162. package/src/renderers/shaders/ShaderChunk/lights_physical_fragment.glsl.js +9 -4
  163. package/src/renderers/shaders/ShaderChunk/project_vertex.glsl.js +6 -0
  164. package/src/renderers/shaders/ShaderChunk/skinning_pars_vertex.glsl.js +9 -17
  165. package/src/renderers/shaders/ShaderChunk/tonemapping_pars_fragment.glsl.js +84 -0
  166. package/src/renderers/shaders/ShaderChunk/worldpos_vertex.glsl.js +6 -0
  167. package/src/renderers/shaders/ShaderChunk.js +4 -0
  168. package/src/renderers/shaders/ShaderLib/depth.glsl.js +2 -0
  169. package/src/renderers/shaders/ShaderLib/distanceRGBA.glsl.js +2 -0
  170. package/src/renderers/shaders/ShaderLib/meshbasic.glsl.js +2 -0
  171. package/src/renderers/shaders/ShaderLib/meshlambert.glsl.js +2 -0
  172. package/src/renderers/shaders/ShaderLib/meshmatcap.glsl.js +3 -0
  173. package/src/renderers/shaders/ShaderLib/meshnormal.glsl.js +2 -0
  174. package/src/renderers/shaders/ShaderLib/meshphong.glsl.js +2 -0
  175. package/src/renderers/shaders/ShaderLib/meshphysical.glsl.js +2 -0
  176. package/src/renderers/shaders/ShaderLib/meshtoon.glsl.js +2 -0
  177. package/src/renderers/shaders/ShaderLib/shadow.glsl.js +3 -0
  178. package/src/renderers/webgl/WebGLAttributes.js +39 -5
  179. package/src/renderers/webgl/WebGLBufferRenderer.js +31 -0
  180. package/src/renderers/webgl/WebGLCubeMaps.js +1 -1
  181. package/src/renderers/webgl/WebGLExtensions.js +1 -0
  182. package/src/renderers/webgl/WebGLIndexedBufferRenderer.js +31 -0
  183. package/src/renderers/webgl/WebGLLights.js +11 -2
  184. package/src/renderers/webgl/WebGLProgram.js +20 -1
  185. package/src/renderers/webgl/WebGLPrograms.js +5 -0
  186. package/src/renderers/webgl/WebGLShadowMap.js +35 -0
  187. package/src/renderers/webgl/WebGLTextures.js +7 -11
  188. package/src/renderers/webgl/WebGLUniforms.js +11 -1
  189. package/src/renderers/webgl/WebGLUniformsGroups.js +72 -93
  190. package/src/renderers/webxr/WebXRManager.js +16 -0
  191. package/examples/jsm/objects/BatchedMesh.js +0 -586
@@ -0,0 +1,424 @@
1
+ import {
2
+ DataTexture,
3
+ Matrix4,
4
+ RepeatWrapping,
5
+ Vector2,
6
+ Vector3,
7
+ } from 'three';
8
+
9
+ /**
10
+ * References:
11
+ * - implemented algorithm - GTAO
12
+ * - https://iryoku.com/downloads/Practical-Realtime-Strategies-for-Accurate-Indirect-Occlusion.pdf
13
+ * - https://github.com/Patapom/GodComplex/blob/master/Tests/TestHBIL/2018%20Mayaux%20-%20Horizon-Based%20Indirect%20Lighting%20(HBIL).pdf
14
+ *
15
+ * - other AO algorithms that are not implemented here:
16
+ * - Screen Space Ambient Occlusion (SSAO), see also SSAOShader.js
17
+ * - http://john-chapman-graphics.blogspot.com/2013/01/ssao-tutorial.html
18
+ * - https://learnopengl.com/Advanced-Lighting/SSAO
19
+ * - https://creativecoding.soe.ucsc.edu/courses/cmpm164/_schedule/AmbientOcclusion.pdf
20
+ * - https://drive.google.com/file/d/1SyagcEVplIm2KkRD3WQYSO9O0Iyi1hfy/edit
21
+ * - Scalable Ambient Occlusion (SAO), see also SAOShader.js
22
+ * - https://casual-effects.com/research/McGuire2012SAO/index.html
23
+ * - https://research.nvidia.com/sites/default/files/pubs/2012-06_Scalable-Ambient-Obscurance/McGuire12SAO.pdf
24
+ * - N8HO
25
+ * - https://github.com/N8python/n8ao
26
+ * - Horizon Based Ambient Occlusion (HBAO)
27
+ * - http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.577.2286&rep=rep1&type=pdf
28
+ * - https://www.derschmale.com/2013/12/20/an-alternative-implementation-for-hbao-2/
29
+ *
30
+ * - further reading
31
+ * - https://ceur-ws.org/Vol-3027/paper5.pdf
32
+ * - https://www.comp.nus.edu.sg/~lowkl/publications/mssao_visual_computer_2012.pdf
33
+ * - https://web.ics.purdue.edu/~tmcgraw/papers/mcgraw-ao-2008.pdf
34
+ * - https://www.activision.com/cdn/research/Practical_Real_Time_Strategies_for_Accurate_Indirect_Occlusion_NEW%20VERSION_COLOR.pdf
35
+ * - https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.390.2463&rep=rep1&type=pdf
36
+ * - https://www.intel.com/content/www/us/en/developer/articles/technical/adaptive-screen-space-ambient-occlusion.html
37
+ */
38
+
39
+ const GTAOShader = {
40
+
41
+ name: 'GTAOShader',
42
+
43
+ defines: {
44
+ PERSPECTIVE_CAMERA: 1,
45
+ SAMPLES: 16,
46
+ NORMAL_VECTOR_TYPE: 1,
47
+ DEPTH_SWIZZLING: 'x',
48
+ SCREEN_SPACE_RADIUS: 0,
49
+ SCREEN_SPACE_RADIUS_SCALE: 100.0,
50
+ SCENE_CLIP_BOX: 0,
51
+ },
52
+
53
+ uniforms: {
54
+ tNormal: { value: null },
55
+ tDepth: { value: null },
56
+ tNoise: { value: null },
57
+ resolution: { value: new Vector2() },
58
+ cameraNear: { value: null },
59
+ cameraFar: { value: null },
60
+ cameraProjectionMatrix: { value: new Matrix4() },
61
+ cameraProjectionMatrixInverse: { value: new Matrix4() },
62
+ cameraWorldMatrix: { value: new Matrix4() },
63
+ radius: { value: 0.25 },
64
+ distanceExponent: { value: 1. },
65
+ thickness: { value: 1. },
66
+ distanceFallOff: { value: 1. },
67
+ scale: { value: 1. },
68
+ sceneBoxMin: { value: new Vector3( - 1, - 1, - 1 ) },
69
+ sceneBoxMax: { value: new Vector3( 1, 1, 1 ) },
70
+ },
71
+
72
+ vertexShader: /* glsl */`
73
+
74
+ varying vec2 vUv;
75
+
76
+ void main() {
77
+ vUv = uv;
78
+ gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
79
+ }`,
80
+
81
+ fragmentShader: /* glsl */`
82
+ varying vec2 vUv;
83
+ uniform highp sampler2D tNormal;
84
+ uniform highp sampler2D tDepth;
85
+ uniform sampler2D tNoise;
86
+ uniform vec2 resolution;
87
+ uniform float cameraNear;
88
+ uniform float cameraFar;
89
+ uniform mat4 cameraProjectionMatrix;
90
+ uniform mat4 cameraProjectionMatrixInverse;
91
+ uniform mat4 cameraWorldMatrix;
92
+ uniform float radius;
93
+ uniform float distanceExponent;
94
+ uniform float thickness;
95
+ uniform float distanceFallOff;
96
+ uniform float scale;
97
+ #if SCENE_CLIP_BOX == 1
98
+ uniform vec3 sceneBoxMin;
99
+ uniform vec3 sceneBoxMax;
100
+ #endif
101
+
102
+ #include <common>
103
+ #include <packing>
104
+
105
+ #ifndef FRAGMENT_OUTPUT
106
+ #define FRAGMENT_OUTPUT vec4(vec3(ao), 1.)
107
+ #endif
108
+
109
+ vec3 getViewPosition(const in vec2 screenPosition, const in float depth) {
110
+ vec4 clipSpacePosition = vec4(vec3(screenPosition, depth) * 2.0 - 1.0, 1.0);
111
+ vec4 viewSpacePosition = cameraProjectionMatrixInverse * clipSpacePosition;
112
+ return viewSpacePosition.xyz / viewSpacePosition.w;
113
+ }
114
+
115
+ float getDepth(const vec2 uv) {
116
+ return textureLod(tDepth, uv.xy, 0.0).DEPTH_SWIZZLING;
117
+ }
118
+
119
+ float fetchDepth(const ivec2 uv) {
120
+ return texelFetch(tDepth, uv.xy, 0).DEPTH_SWIZZLING;
121
+ }
122
+
123
+ float getViewZ(const in float depth) {
124
+ #if PERSPECTIVE_CAMERA == 1
125
+ return perspectiveDepthToViewZ(depth, cameraNear, cameraFar);
126
+ #else
127
+ return orthographicDepthToViewZ(depth, cameraNear, cameraFar);
128
+ #endif
129
+ }
130
+
131
+ vec3 computeNormalFromDepth(const vec2 uv) {
132
+ vec2 size = vec2(textureSize(tDepth, 0));
133
+ ivec2 p = ivec2(uv * size);
134
+ float c0 = fetchDepth(p);
135
+ float l2 = fetchDepth(p - ivec2(2, 0));
136
+ float l1 = fetchDepth(p - ivec2(1, 0));
137
+ float r1 = fetchDepth(p + ivec2(1, 0));
138
+ float r2 = fetchDepth(p + ivec2(2, 0));
139
+ float b2 = fetchDepth(p - ivec2(0, 2));
140
+ float b1 = fetchDepth(p - ivec2(0, 1));
141
+ float t1 = fetchDepth(p + ivec2(0, 1));
142
+ float t2 = fetchDepth(p + ivec2(0, 2));
143
+ float dl = abs((2.0 * l1 - l2) - c0);
144
+ float dr = abs((2.0 * r1 - r2) - c0);
145
+ float db = abs((2.0 * b1 - b2) - c0);
146
+ float dt = abs((2.0 * t1 - t2) - c0);
147
+ vec3 ce = getViewPosition(uv, c0).xyz;
148
+ vec3 dpdx = (dl < dr) ? ce - getViewPosition((uv - vec2(1.0 / size.x, 0.0)), l1).xyz : -ce + getViewPosition((uv + vec2(1.0 / size.x, 0.0)), r1).xyz;
149
+ vec3 dpdy = (db < dt) ? ce - getViewPosition((uv - vec2(0.0, 1.0 / size.y)), b1).xyz : -ce + getViewPosition((uv + vec2(0.0, 1.0 / size.y)), t1).xyz;
150
+ return normalize(cross(dpdx, dpdy));
151
+ }
152
+
153
+ vec3 getViewNormal(const vec2 uv) {
154
+ #if NORMAL_VECTOR_TYPE == 2
155
+ return normalize(textureLod(tNormal, uv, 0.).rgb);
156
+ #elif NORMAL_VECTOR_TYPE == 1
157
+ return unpackRGBToNormal(textureLod(tNormal, uv, 0.).rgb);
158
+ #else
159
+ return computeNormalFromDepth(uv);
160
+ #endif
161
+ }
162
+
163
+ vec3 getSceneUvAndDepth(vec3 sampleViewPos) {
164
+ vec4 sampleClipPos = cameraProjectionMatrix * vec4(sampleViewPos, 1.);
165
+ vec2 sampleUv = sampleClipPos.xy / sampleClipPos.w * 0.5 + 0.5;
166
+ float sampleSceneDepth = getDepth(sampleUv);
167
+ return vec3(sampleUv, sampleSceneDepth);
168
+ }
169
+
170
+ void main() {
171
+ float depth = getDepth(vUv.xy);
172
+ if (depth >= 1.0) {
173
+ discard;
174
+ return;
175
+ }
176
+ vec3 viewPos = getViewPosition(vUv, depth);
177
+ vec3 viewNormal = getViewNormal(vUv);
178
+
179
+ float radiusToUse = radius;
180
+ float distanceFalloffToUse = thickness;
181
+ #if SCREEN_SPACE_RADIUS == 1
182
+ float radiusScale = getViewPosition(vec2(0.5 + float(SCREEN_SPACE_RADIUS_SCALE) / resolution.x, 0.0), depth).x;
183
+ radiusToUse *= radiusScale;
184
+ distanceFalloffToUse *= radiusScale;
185
+ #endif
186
+
187
+ #if SCENE_CLIP_BOX == 1
188
+ vec3 worldPos = (cameraWorldMatrix * vec4(viewPos, 1.0)).xyz;
189
+ float boxDistance = length(max(vec3(0.0), max(sceneBoxMin - worldPos, worldPos - sceneBoxMax)));
190
+ if (boxDistance > radiusToUse) {
191
+ discard;
192
+ return;
193
+ }
194
+ #endif
195
+
196
+ vec2 noiseResolution = vec2(textureSize(tNoise, 0));
197
+ vec2 noiseUv = vUv * resolution / noiseResolution;
198
+ vec4 noiseTexel = textureLod(tNoise, noiseUv, 0.0);
199
+ vec3 randomVec = noiseTexel.xyz * 2.0 - 1.0;
200
+ vec3 tangent = normalize(vec3(randomVec.xy, 0.));
201
+ vec3 bitangent = vec3(-tangent.y, tangent.x, 0.);
202
+ mat3 kernelMatrix = mat3(tangent, bitangent, vec3(0., 0., 1.));
203
+
204
+ const int DIRECTIONS = SAMPLES < 30 ? 3 : 5;
205
+ const int STEPS = (SAMPLES + DIRECTIONS - 1) / DIRECTIONS;
206
+ float ao = 0.0, totalWeight = 0.0;
207
+ for (int i = 0; i < DIRECTIONS; ++i) {
208
+
209
+ float angle = float(i) / float(DIRECTIONS) * PI;
210
+ vec4 sampleDir = vec4(cos(angle), sin(angle), 0., 0.5 + 0.5 * noiseTexel.w);
211
+ sampleDir.xyz = normalize(kernelMatrix * sampleDir.xyz);
212
+
213
+ vec3 viewDir = normalize(-viewPos.xyz);
214
+ vec3 sliceBitangent = normalize(cross(sampleDir.xyz, viewDir));
215
+ vec3 sliceTangent = cross(sliceBitangent, viewDir);
216
+ vec3 normalInSlice = normalize(viewNormal - sliceBitangent * dot(viewNormal, sliceBitangent));
217
+
218
+ vec3 tangentToNormalInSlice = cross(normalInSlice, sliceBitangent);
219
+ vec2 cosHorizons = vec2(dot(viewDir, tangentToNormalInSlice), dot(viewDir, -tangentToNormalInSlice));
220
+
221
+ for (int j = 0; j < STEPS; ++j) {
222
+ vec3 sampleViewOffset = sampleDir.xyz * radiusToUse * sampleDir.w * pow(float(j + 1) / float(STEPS), distanceExponent);
223
+
224
+ vec3 sampleSceneUvDepth = getSceneUvAndDepth(viewPos + sampleViewOffset);
225
+ vec3 sampleSceneViewPos = getViewPosition(sampleSceneUvDepth.xy, sampleSceneUvDepth.z);
226
+ vec3 viewDelta = sampleSceneViewPos - viewPos;
227
+ if (abs(viewDelta.z) < thickness) {
228
+ float sampleCosHorizon = dot(viewDir, normalize(viewDelta));
229
+ cosHorizons.x += max(0., (sampleCosHorizon - cosHorizons.x) * mix(1., 2. / float(j + 2), distanceFallOff));
230
+ }
231
+
232
+ sampleSceneUvDepth = getSceneUvAndDepth(viewPos - sampleViewOffset);
233
+ sampleSceneViewPos = getViewPosition(sampleSceneUvDepth.xy, sampleSceneUvDepth.z);
234
+ viewDelta = sampleSceneViewPos - viewPos;
235
+ if (abs(viewDelta.z) < thickness) {
236
+ float sampleCosHorizon = dot(viewDir, normalize(viewDelta));
237
+ cosHorizons.y += max(0., (sampleCosHorizon - cosHorizons.y) * mix(1., 2. / float(j + 2), distanceFallOff));
238
+ }
239
+ }
240
+
241
+ vec2 sinHorizons = sqrt(1. - cosHorizons * cosHorizons);
242
+ float nx = dot(normalInSlice, sliceTangent);
243
+ float ny = dot(normalInSlice, viewDir);
244
+ float nxb = 1. / 2. * (acos(cosHorizons.y) - acos(cosHorizons.x) + sinHorizons.x * cosHorizons.x - sinHorizons.y * cosHorizons.y);
245
+ float nyb = 1. / 2. * (2. - cosHorizons.x * cosHorizons.x - cosHorizons.y * cosHorizons.y);
246
+ float occlusion = nx * nxb + ny * nyb;
247
+ ao += occlusion;
248
+ }
249
+
250
+ ao = clamp(ao / float(DIRECTIONS), 0., 1.);
251
+ #if SCENE_CLIP_BOX == 1
252
+ ao = mix(ao, 1., smoothstep(0., radiusToUse, boxDistance));
253
+ #endif
254
+ ao = pow(ao, scale);
255
+
256
+ gl_FragColor = FRAGMENT_OUTPUT;
257
+ }`
258
+
259
+ };
260
+
261
+ const GTAODepthShader = {
262
+
263
+ name: 'GTAODepthShader',
264
+
265
+ defines: {
266
+ PERSPECTIVE_CAMERA: 1
267
+ },
268
+
269
+ uniforms: {
270
+ tDepth: { value: null },
271
+ cameraNear: { value: null },
272
+ cameraFar: { value: null },
273
+ },
274
+
275
+ vertexShader: /* glsl */`
276
+ varying vec2 vUv;
277
+
278
+ void main() {
279
+ vUv = uv;
280
+ gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
281
+ }`,
282
+
283
+ fragmentShader: /* glsl */`
284
+ uniform sampler2D tDepth;
285
+ uniform float cameraNear;
286
+ uniform float cameraFar;
287
+ varying vec2 vUv;
288
+
289
+ #include <packing>
290
+
291
+ float getLinearDepth( const in vec2 screenPosition ) {
292
+ #if PERSPECTIVE_CAMERA == 1
293
+ float fragCoordZ = texture2D( tDepth, screenPosition ).x;
294
+ float viewZ = perspectiveDepthToViewZ( fragCoordZ, cameraNear, cameraFar );
295
+ return viewZToOrthographicDepth( viewZ, cameraNear, cameraFar );
296
+ #else
297
+ return texture2D( tDepth, screenPosition ).x;
298
+ #endif
299
+ }
300
+
301
+ void main() {
302
+ float depth = getLinearDepth( vUv );
303
+ gl_FragColor = vec4( vec3( 1.0 - depth ), 1.0 );
304
+
305
+ }`
306
+
307
+ };
308
+
309
+ const GTAOBlendShader = {
310
+
311
+ name: 'GTAOBlendShader',
312
+
313
+ uniforms: {
314
+ tDiffuse: { value: null },
315
+ intensity: { value: 1.0 }
316
+ },
317
+
318
+ vertexShader: /* glsl */`
319
+ varying vec2 vUv;
320
+
321
+ void main() {
322
+ vUv = uv;
323
+ gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
324
+ }`,
325
+
326
+ fragmentShader: /* glsl */`
327
+ uniform float intensity;
328
+ uniform sampler2D tDiffuse;
329
+ varying vec2 vUv;
330
+
331
+ void main() {
332
+ vec4 texel = texture2D( tDiffuse, vUv );
333
+ gl_FragColor = vec4(mix(vec3(1.), texel.rgb, intensity), texel.a);
334
+ }`
335
+
336
+ };
337
+
338
+
339
+ function generateMagicSquareNoise( size = 5 ) {
340
+
341
+ const noiseSize = Math.floor( size ) % 2 === 0 ? Math.floor( size ) + 1 : Math.floor( size );
342
+ const magicSquare = generateMagicSquare( noiseSize );
343
+ const noiseSquareSize = magicSquare.length;
344
+ const data = new Uint8Array( noiseSquareSize * 4 );
345
+
346
+ for ( let inx = 0; inx < noiseSquareSize; ++ inx ) {
347
+
348
+ const iAng = magicSquare[ inx ];
349
+ const angle = ( 2 * Math.PI * iAng ) / noiseSquareSize;
350
+ const randomVec = new Vector3(
351
+ Math.cos( angle ),
352
+ Math.sin( angle ),
353
+ 0
354
+ ).normalize();
355
+ data[ inx * 4 ] = ( randomVec.x * 0.5 + 0.5 ) * 255;
356
+ data[ inx * 4 + 1 ] = ( randomVec.y * 0.5 + 0.5 ) * 255;
357
+ data[ inx * 4 + 2 ] = 127;
358
+ data[ inx * 4 + 3 ] = 255;
359
+
360
+ }
361
+
362
+ const noiseTexture = new DataTexture( data, noiseSize, noiseSize );
363
+ noiseTexture.wrapS = RepeatWrapping;
364
+ noiseTexture.wrapT = RepeatWrapping;
365
+ noiseTexture.needsUpdate = true;
366
+
367
+ return noiseTexture;
368
+
369
+ }
370
+
371
+ function generateMagicSquare( size ) {
372
+
373
+ const noiseSize = Math.floor( size ) % 2 === 0 ? Math.floor( size ) + 1 : Math.floor( size );
374
+ const noiseSquareSize = noiseSize * noiseSize;
375
+ const magicSquare = Array( noiseSquareSize ).fill( 0 );
376
+ let i = Math.floor( noiseSize / 2 );
377
+ let j = noiseSize - 1;
378
+
379
+ for ( let num = 1; num <= noiseSquareSize; ) {
380
+
381
+ if ( i === - 1 && j === noiseSize ) {
382
+
383
+ j = noiseSize - 2;
384
+ i = 0;
385
+
386
+ } else {
387
+
388
+ if ( j === noiseSize ) {
389
+
390
+ j = 0;
391
+
392
+ }
393
+
394
+ if ( i < 0 ) {
395
+
396
+ i = noiseSize - 1;
397
+
398
+ }
399
+
400
+ }
401
+
402
+ if ( magicSquare[ i * noiseSize + j ] !== 0 ) {
403
+
404
+ j -= 2;
405
+ i ++;
406
+ continue;
407
+
408
+ } else {
409
+
410
+ magicSquare[ i * noiseSize + j ] = num ++;
411
+
412
+ }
413
+
414
+ j ++;
415
+ i --;
416
+
417
+ }
418
+
419
+ return magicSquare;
420
+
421
+ }
422
+
423
+
424
+ export { generateMagicSquareNoise, GTAOShader, GTAODepthShader, GTAOBlendShader };
@@ -60,6 +60,10 @@ const OutputShader = {
60
60
 
61
61
  gl_FragColor.rgb = ACESFilmicToneMapping( gl_FragColor.rgb );
62
62
 
63
+ #elif defined( AGX_TONE_MAPPING )
64
+
65
+ gl_FragColor.rgb = AgXToneMapping( gl_FragColor.rgb );
66
+
63
67
  #endif
64
68
 
65
69
  // color space
@@ -0,0 +1,226 @@
1
+ import {
2
+ Matrix4,
3
+ Vector2,
4
+ Vector3,
5
+ } from 'three';
6
+
7
+ /**
8
+ * References:
9
+ * https://openaccess.thecvf.com/content/WACV2021/papers/Khademi_Self-Supervised_Poisson-Gaussian_Denoising_WACV_2021_paper.pdf
10
+ * https://arxiv.org/pdf/2206.01856.pdf
11
+ */
12
+
13
+ const PoissonDenoiseShader = {
14
+
15
+ name: 'PoissonDenoiseShader',
16
+
17
+ defines: {
18
+ 'SAMPLES': 16,
19
+ 'SAMPLE_VECTORS': generatePdSamplePointInitializer( 16, 2, 1 ),
20
+ 'NORMAL_VECTOR_TYPE': 1,
21
+ 'DEPTH_VALUE_SOURCE': 0,
22
+ },
23
+
24
+ uniforms: {
25
+ 'tDiffuse': { value: null },
26
+ 'tNormal': { value: null },
27
+ 'tDepth': { value: null },
28
+ 'tNoise': { value: null },
29
+ 'resolution': { value: new Vector2() },
30
+ 'cameraProjectionMatrixInverse': { value: new Matrix4() },
31
+ 'lumaPhi': { value: 5. },
32
+ 'depthPhi': { value: 5. },
33
+ 'normalPhi': { value: 5. },
34
+ 'radius': { value: 4. },
35
+ 'index': { value: 0 }
36
+ },
37
+
38
+ vertexShader: /* glsl */`
39
+
40
+ varying vec2 vUv;
41
+
42
+ void main() {
43
+ vUv = uv;
44
+ gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
45
+ }`,
46
+
47
+ fragmentShader: /* glsl */`
48
+
49
+ varying vec2 vUv;
50
+
51
+ uniform sampler2D tDiffuse;
52
+ uniform sampler2D tNormal;
53
+ uniform sampler2D tDepth;
54
+ uniform sampler2D tNoise;
55
+ uniform vec2 resolution;
56
+ uniform mat4 cameraProjectionMatrixInverse;
57
+ uniform float lumaPhi;
58
+ uniform float depthPhi;
59
+ uniform float normalPhi;
60
+ uniform float radius;
61
+ uniform int index;
62
+
63
+ #include <common>
64
+ #include <packing>
65
+
66
+ #ifndef SAMPLE_LUMINANCE
67
+ #define SAMPLE_LUMINANCE dot(vec3(0.2125, 0.7154, 0.0721), a)
68
+ #endif
69
+
70
+ #ifndef FRAGMENT_OUTPUT
71
+ #define FRAGMENT_OUTPUT vec4(denoised, 1.)
72
+ #endif
73
+
74
+ float getLuminance(const in vec3 a) {
75
+ return SAMPLE_LUMINANCE;
76
+ }
77
+
78
+ const vec3 poissonDisk[SAMPLES] = SAMPLE_VECTORS;
79
+
80
+ vec3 getViewPosition(const in vec2 screenPosition, const in float depth) {
81
+ vec4 clipSpacePosition = vec4(vec3(screenPosition, depth) * 2.0 - 1.0, 1.0);
82
+ vec4 viewSpacePosition = cameraProjectionMatrixInverse * clipSpacePosition;
83
+ return viewSpacePosition.xyz / viewSpacePosition.w;
84
+ }
85
+
86
+ float getDepth(const vec2 uv) {
87
+ #if DEPTH_VALUE_SOURCE == 1
88
+ return textureLod(tDepth, uv.xy, 0.0).a;
89
+ #else
90
+ return textureLod(tDepth, uv.xy, 0.0).r;
91
+ #endif
92
+ }
93
+
94
+ float fetchDepth(const ivec2 uv) {
95
+ #if DEPTH_VALUE_SOURCE == 1
96
+ return texelFetch(tDepth, uv.xy, 0).a;
97
+ #else
98
+ return texelFetch(tDepth, uv.xy, 0).r;
99
+ #endif
100
+ }
101
+
102
+ vec3 computeNormalFromDepth(const vec2 uv) {
103
+ vec2 size = vec2(textureSize(tDepth, 0));
104
+ ivec2 p = ivec2(uv * size);
105
+ float c0 = fetchDepth(p);
106
+ float l2 = fetchDepth(p - ivec2(2, 0));
107
+ float l1 = fetchDepth(p - ivec2(1, 0));
108
+ float r1 = fetchDepth(p + ivec2(1, 0));
109
+ float r2 = fetchDepth(p + ivec2(2, 0));
110
+ float b2 = fetchDepth(p - ivec2(0, 2));
111
+ float b1 = fetchDepth(p - ivec2(0, 1));
112
+ float t1 = fetchDepth(p + ivec2(0, 1));
113
+ float t2 = fetchDepth(p + ivec2(0, 2));
114
+ float dl = abs((2.0 * l1 - l2) - c0);
115
+ float dr = abs((2.0 * r1 - r2) - c0);
116
+ float db = abs((2.0 * b1 - b2) - c0);
117
+ float dt = abs((2.0 * t1 - t2) - c0);
118
+ vec3 ce = getViewPosition(uv, c0).xyz;
119
+ vec3 dpdx = (dl < dr) ? ce - getViewPosition((uv - vec2(1.0 / size.x, 0.0)), l1).xyz
120
+ : -ce + getViewPosition((uv + vec2(1.0 / size.x, 0.0)), r1).xyz;
121
+ vec3 dpdy = (db < dt) ? ce - getViewPosition((uv - vec2(0.0, 1.0 / size.y)), b1).xyz
122
+ : -ce + getViewPosition((uv + vec2(0.0, 1.0 / size.y)), t1).xyz;
123
+ return normalize(cross(dpdx, dpdy));
124
+ }
125
+
126
+ vec3 getViewNormal(const vec2 uv) {
127
+ #if NORMAL_VECTOR_TYPE == 2
128
+ return normalize(textureLod(tNormal, uv, 0.).rgb);
129
+ #elif NORMAL_VECTOR_TYPE == 1
130
+ return unpackRGBToNormal(textureLod(tNormal, uv, 0.).rgb);
131
+ #else
132
+ return computeNormalFromDepth(uv);
133
+ #endif
134
+ }
135
+
136
+ void denoiseSample(in vec3 center, in vec3 viewNormal, in vec3 viewPos, in vec2 sampleUv, inout vec3 denoised, inout float totalWeight) {
137
+ vec4 sampleTexel = textureLod(tDiffuse, sampleUv, 0.0);
138
+ float sampleDepth = getDepth(sampleUv);
139
+ vec3 sampleNormal = getViewNormal(sampleUv);
140
+ vec3 neighborColor = sampleTexel.rgb;
141
+ vec3 viewPosSample = getViewPosition(sampleUv, sampleDepth);
142
+
143
+ float normalDiff = dot(viewNormal, sampleNormal);
144
+ float normalSimilarity = pow(max(normalDiff, 0.), normalPhi);
145
+ float lumaDiff = abs(getLuminance(neighborColor) - getLuminance(center));
146
+ float lumaSimilarity = max(1.0 - lumaDiff / lumaPhi, 0.0);
147
+ float depthDiff = abs(dot(viewPos - viewPosSample, viewNormal));
148
+ float depthSimilarity = max(1. - depthDiff / depthPhi, 0.);
149
+ float w = lumaSimilarity * depthSimilarity * normalSimilarity;
150
+
151
+ denoised += w * neighborColor;
152
+ totalWeight += w;
153
+ }
154
+
155
+ void main() {
156
+ float depth = getDepth(vUv.xy);
157
+ vec3 viewNormal = getViewNormal(vUv);
158
+ if (depth == 1. || dot(viewNormal, viewNormal) == 0.) {
159
+ discard;
160
+ return;
161
+ }
162
+ vec4 texel = textureLod(tDiffuse, vUv, 0.0);
163
+ vec3 center = texel.rgb;
164
+ vec3 viewPos = getViewPosition(vUv, depth);
165
+
166
+ vec2 noiseResolution = vec2(textureSize(tNoise, 0));
167
+ vec2 noiseUv = vUv * resolution / noiseResolution;
168
+ vec4 noiseTexel = textureLod(tNoise, noiseUv, 0.0);
169
+ vec2 noiseVec = vec2(sin(noiseTexel[index % 4] * 2. * PI), cos(noiseTexel[index % 4] * 2. * PI));
170
+ mat2 rotationMatrix = mat2(noiseVec.x, -noiseVec.y, noiseVec.x, noiseVec.y);
171
+
172
+ float totalWeight = 1.0;
173
+ vec3 denoised = texel.rgb;
174
+ for (int i = 0; i < SAMPLES; i++) {
175
+ vec3 sampleDir = poissonDisk[i];
176
+ vec2 offset = rotationMatrix * (sampleDir.xy * (1. + sampleDir.z * (radius - 1.)) / resolution);
177
+ vec2 sampleUv = vUv + offset;
178
+ denoiseSample(center, viewNormal, viewPos, sampleUv, denoised, totalWeight);
179
+ }
180
+
181
+ if (totalWeight > 0.) {
182
+ denoised /= totalWeight;
183
+ }
184
+ gl_FragColor = FRAGMENT_OUTPUT;
185
+ }`
186
+
187
+ };
188
+
189
+ function generatePdSamplePointInitializer( samples, rings, radiusExponent ) {
190
+
191
+ const poissonDisk = generateDenoiseSamples(
192
+ samples,
193
+ rings,
194
+ radiusExponent,
195
+ );
196
+
197
+ let glslCode = 'vec3[SAMPLES](';
198
+
199
+ for ( let i = 0; i < samples; i ++ ) {
200
+
201
+ const sample = poissonDisk[ i ];
202
+ glslCode += `vec3(${sample.x}, ${sample.y}, ${sample.z})${( i < samples - 1 ) ? ',' : ')'}`;
203
+
204
+ }
205
+
206
+ return glslCode;
207
+
208
+ }
209
+
210
+ function generateDenoiseSamples( numSamples, numRings, radiusExponent ) {
211
+
212
+ const samples = [];
213
+
214
+ for ( let i = 0; i < numSamples; i ++ ) {
215
+
216
+ const angle = 2 * Math.PI * numRings * i / numSamples;
217
+ const radius = Math.pow( i / ( numSamples - 1 ), radiusExponent );
218
+ samples.push( new Vector3( Math.cos( angle ), Math.sin( angle ), radius ) );
219
+
220
+ }
221
+
222
+ return samples;
223
+
224
+ }
225
+
226
+ export { generatePdSamplePointInitializer, PoissonDenoiseShader };
@@ -49,7 +49,6 @@ const SAOShader = {
49
49
  }`,
50
50
 
51
51
  fragmentShader: /* glsl */`
52
-
53
52
  #include <common>
54
53
 
55
54
  varying vec2 vUv;
@@ -58,8 +57,8 @@ const SAOShader = {
58
57
  uniform sampler2D tDiffuse;
59
58
  #endif
60
59
 
61
- uniform sampler2D tDepth;
62
- uniform sampler2D tNormal;
60
+ uniform highp sampler2D tDepth;
61
+ uniform highp sampler2D tNormal;
63
62
 
64
63
  uniform float cameraNear;
65
64
  uniform float cameraFar;
@@ -49,9 +49,8 @@ const SSAOShader = {
49
49
  }`,
50
50
 
51
51
  fragmentShader: /* glsl */`
52
-
53
- uniform sampler2D tNormal;
54
- uniform sampler2D tDepth;
52
+ uniform highp sampler2D tNormal;
53
+ uniform highp sampler2D tDepth;
55
54
  uniform sampler2D tNoise;
56
55
 
57
56
  uniform vec3 kernel[ KERNEL_SIZE ];