@octanejs/drei 0.0.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 (152) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +11 -0
  3. package/UPSTREAM.md +92 -0
  4. package/package.json +96 -0
  5. package/src/config.ts +14 -0
  6. package/src/core/AccumulativeShadows.three.tsrx +313 -0
  7. package/src/core/AdaptivePerformance.three.tsrx +49 -0
  8. package/src/core/AsciiRenderer.three.tsrx +61 -0
  9. package/src/core/Bounds.three.tsrx +323 -0
  10. package/src/core/Bvh.three.tsrx +116 -0
  11. package/src/core/CameraControls.three.tsrx +181 -0
  12. package/src/core/CameraShake.three.tsrx +104 -0
  13. package/src/core/Caustics.three.tsrx +107 -0
  14. package/src/core/Center.three.tsrx +145 -0
  15. package/src/core/Clone.three.tsrx +167 -0
  16. package/src/core/Cloud.three.tsrx +314 -0
  17. package/src/core/ComputedAttribute.three.tsrx +27 -0
  18. package/src/core/ContactShadows.three.tsrx +106 -0
  19. package/src/core/CubeTexture.three.tsrx +32 -0
  20. package/src/core/CurveModifier.three.tsrx +37 -0
  21. package/src/core/Decal.three.tsrx +132 -0
  22. package/src/core/Detailed.three.tsrx +28 -0
  23. package/src/core/DetectGPU.three.tsrx +22 -0
  24. package/src/core/Effects.three.tsrx +123 -0
  25. package/src/core/Environment.three.tsrx +220 -0
  26. package/src/core/Example.three.tsrx +56 -0
  27. package/src/core/Fbo.three.tsrx +70 -0
  28. package/src/core/Fbx.three.tsrx +25 -0
  29. package/src/core/Fisheye.three.tsrx +82 -0
  30. package/src/core/GizmoHelper.three.tsrx +115 -0
  31. package/src/core/GizmoViewcube.three.tsrx +82 -0
  32. package/src/core/GizmoViewport.three.tsrx +122 -0
  33. package/src/core/Gltf.three.tsrx +62 -0
  34. package/src/core/GradientTexture.three.tsrx +66 -0
  35. package/src/core/Grid.three.tsrx +170 -0
  36. package/src/core/Helper.three.tsrx +56 -0
  37. package/src/core/Hud.three.tsrx +52 -0
  38. package/src/core/Image.three.tsrx +146 -0
  39. package/src/core/Instances.three.tsrx +332 -0
  40. package/src/core/Ktx2.three.tsrx +59 -0
  41. package/src/core/Lightformer.three.tsrx +75 -0
  42. package/src/core/MarchingCubes.three.tsrx +125 -0
  43. package/src/core/MeshPortalMaterial.three.tsrx +103 -0
  44. package/src/core/MeshReflectorMaterial.three.tsrx +67 -0
  45. package/src/core/MeshRefractionMaterial.three.tsrx +70 -0
  46. package/src/core/MeshTransmissionMaterial.three.tsrx +71 -0
  47. package/src/core/MotionPathControls.three.tsrx +139 -0
  48. package/src/core/MultiMaterial.three.tsrx +32 -0
  49. package/src/core/Outlines.three.tsrx +162 -0
  50. package/src/core/PerformanceMonitor.three.tsrx +144 -0
  51. package/src/core/PointerLockControls.three.tsrx +84 -0
  52. package/src/core/Points.three.tsrx +242 -0
  53. package/src/core/PositionalAudio.three.tsrx +76 -0
  54. package/src/core/Preload.three.tsrx +40 -0
  55. package/src/core/Progress.three.tsrx +47 -0
  56. package/src/core/RenderCubeTexture.three.tsrx +111 -0
  57. package/src/core/RenderTexture.three.tsrx +102 -0
  58. package/src/core/Resize.three.tsrx +59 -0
  59. package/src/core/RoundedBox.three.tsrx +106 -0
  60. package/src/core/Sampler.three.tsrx +116 -0
  61. package/src/core/ScreenSizer.three.tsrx +43 -0
  62. package/src/core/ScreenSpace.three.tsrx +21 -0
  63. package/src/core/Segments.three.tsrx +119 -0
  64. package/src/core/Sky.three.tsrx +63 -0
  65. package/src/core/Sparkles.three.tsrx +145 -0
  66. package/src/core/Splat.three.tsrx +52 -0
  67. package/src/core/SpotLight.three.tsrx +350 -0
  68. package/src/core/SpriteAnimator.three.tsrx +338 -0
  69. package/src/core/Stage.three.tsrx +112 -0
  70. package/src/core/Stars.three.tsrx +103 -0
  71. package/src/core/Stats.three.tsrx +35 -0
  72. package/src/core/StatsGl.three.tsrx +60 -0
  73. package/src/core/Svg.three.tsrx +82 -0
  74. package/src/core/Text.three.tsrx +96 -0
  75. package/src/core/Text3D.three.tsrx +90 -0
  76. package/src/core/Texture.three.tsrx +72 -0
  77. package/src/core/Trail.three.tsrx +167 -0
  78. package/src/core/TrailTexture.three.tsrx +171 -0
  79. package/src/core/TransformControls.three.tsrx +155 -0
  80. package/src/core/VideoTexture.three.tsrx +116 -0
  81. package/src/core/Wireframe.three.tsrx +92 -0
  82. package/src/core/basic-controls.three.tsrx +353 -0
  83. package/src/core/calculateScaleFactor.ts +42 -0
  84. package/src/core/cameras.three.tsrx +230 -0
  85. package/src/core/catalog-resource.ts +12 -0
  86. package/src/core/catalog-textures.three.tsrx +105 -0
  87. package/src/core/compiler-slot.ts +9 -0
  88. package/src/core/effects-utils.ts +8 -0
  89. package/src/core/environment-loader.ts +87 -0
  90. package/src/core/font-resource.ts +31 -0
  91. package/src/core/gltf-loader.ts +33 -0
  92. package/src/core/gradient-types.ts +4 -0
  93. package/src/core/index.ts +295 -0
  94. package/src/core/lines.three.tsrx +329 -0
  95. package/src/core/meshBounds.ts +26 -0
  96. package/src/core/motion-materials.three.tsrx +93 -0
  97. package/src/core/octane-children.ts +18 -0
  98. package/src/core/octane-element.ts +16 -0
  99. package/src/core/resource.ts +29 -0
  100. package/src/core/scene-motion.three.tsrx +183 -0
  101. package/src/core/shaderMaterial.ts +50 -0
  102. package/src/core/shadows.three.tsrx +164 -0
  103. package/src/core/shapes.three.tsrx +170 -0
  104. package/src/core/simple-materials.three.tsrx +124 -0
  105. package/src/core/softShadows.three.tsrx +151 -0
  106. package/src/core/splat-resource.ts +598 -0
  107. package/src/core/useAnimations.three.tsrx +57 -0
  108. package/src/core/useAspect.three.tsrx +10 -0
  109. package/src/core/useBoxProjectedEnv.three.tsrx +113 -0
  110. package/src/core/useCamera.three.tsrx +26 -0
  111. package/src/core/useContextBridge.three.tsrx +25 -0
  112. package/src/core/useDepthBuffer.three.tsrx +35 -0
  113. package/src/core/useEnvironment.three.tsrx +81 -0
  114. package/src/core/useFont.three.tsrx +12 -0
  115. package/src/core/useIntersect.three.tsrx +38 -0
  116. package/src/core/useSpriteLoader.three.tsrx +249 -0
  117. package/src/core/video-texture-resource.ts +14 -0
  118. package/src/index.ts +2 -0
  119. package/src/materials/BlurPass.ts +94 -0
  120. package/src/materials/CausticsMaterials.ts +197 -0
  121. package/src/materials/ConvolutionMaterial.ts +65 -0
  122. package/src/materials/MeshReflectorMaterial.ts +180 -0
  123. package/src/materials/MeshRefractionMaterial.ts +131 -0
  124. package/src/materials/MeshTransmissionMaterial.ts +182 -0
  125. package/src/materials/PortalMaterial.ts +38 -0
  126. package/src/materials/WireframeMaterial.ts +241 -0
  127. package/src/materials/makeSDFGenerator.ts +155 -0
  128. package/src/troika-three-text.d.ts +19 -0
  129. package/src/web/CycleRaycast.three.tsrx +81 -0
  130. package/src/web/DragControls.three.tsrx +82 -0
  131. package/src/web/FaceControls.three.tsrx +178 -0
  132. package/src/web/FaceLandmarker.three.tsrx +48 -0
  133. package/src/web/Facemesh.three.tsrx +464 -0
  134. package/src/web/Html.three.tsrx +390 -0
  135. package/src/web/KeyboardControls.three.tsrx +98 -0
  136. package/src/web/Loader.tsrx +61 -0
  137. package/src/web/PresentationControls.three.tsrx +63 -0
  138. package/src/web/ScrollControls.three.tsrx +120 -0
  139. package/src/web/Select.three.tsrx +155 -0
  140. package/src/web/View.three.tsrx +46 -0
  141. package/src/web/face-landmarker-resource.ts +18 -0
  142. package/src/web/facemesh-data.ts +54 -0
  143. package/src/web/index.ts +41 -0
  144. package/src/web/keyboard-controls-types.ts +33 -0
  145. package/src/web/media-textures.three.tsrx +50 -0
  146. package/src/web/pivotControls/annotation.tsrx +17 -0
  147. package/src/web/pivotControls/context.ts +38 -0
  148. package/src/web/pivotControls/index.three.tsrx +877 -0
  149. package/src/web/scroll-html-root.tsrx +27 -0
  150. package/src/web/useCursor.three.tsrx +19 -0
  151. package/src/web/useGesture.ts +27 -0
  152. package/status.json +14 -0
@@ -0,0 +1,94 @@
1
+ import {
2
+ BufferAttribute,
3
+ BufferGeometry,
4
+ Camera,
5
+ HalfFloatType,
6
+ LinearFilter,
7
+ Mesh,
8
+ Scene,
9
+ Vector2,
10
+ WebGLRenderTarget,
11
+ type WebGLRenderer,
12
+ } from 'three';
13
+ import { ConvolutionMaterial } from './ConvolutionMaterial.js';
14
+
15
+ export interface BlurPassProps {
16
+ gl: WebGLRenderer;
17
+ resolution: number;
18
+ width?: number;
19
+ height?: number;
20
+ minDepthThreshold?: number;
21
+ maxDepthThreshold?: number;
22
+ depthScale?: number;
23
+ depthToBlurRatioBias?: number;
24
+ }
25
+
26
+ export class BlurPass {
27
+ readonly renderTargetA: WebGLRenderTarget;
28
+ readonly renderTargetB: WebGLRenderTarget;
29
+ readonly convolutionMaterial: ConvolutionMaterial;
30
+ readonly scene: Scene;
31
+ readonly camera: Camera;
32
+ readonly screen: Mesh;
33
+ renderToScreen = false;
34
+ constructor({
35
+ resolution,
36
+ width = 500,
37
+ height = 500,
38
+ minDepthThreshold = 0,
39
+ maxDepthThreshold = 1,
40
+ depthScale = 0,
41
+ depthToBlurRatioBias = 0.25,
42
+ }: BlurPassProps) {
43
+ this.renderTargetA = new WebGLRenderTarget(resolution, resolution, {
44
+ minFilter: LinearFilter,
45
+ magFilter: LinearFilter,
46
+ stencilBuffer: false,
47
+ depthBuffer: false,
48
+ type: HalfFloatType,
49
+ });
50
+ this.renderTargetB = this.renderTargetA.clone();
51
+ this.convolutionMaterial = new ConvolutionMaterial();
52
+ this.convolutionMaterial.setTexelSize(1 / width, 1 / height);
53
+ this.convolutionMaterial.setResolution(new Vector2(width, height));
54
+ this.convolutionMaterial.uniforms.minDepthThreshold.value = minDepthThreshold;
55
+ this.convolutionMaterial.uniforms.maxDepthThreshold.value = maxDepthThreshold;
56
+ this.convolutionMaterial.uniforms.depthScale.value = depthScale;
57
+ this.convolutionMaterial.uniforms.depthToBlurRatioBias.value = depthToBlurRatioBias;
58
+ this.convolutionMaterial.defines.USE_DEPTH = depthScale > 0;
59
+ this.scene = new Scene();
60
+ this.camera = new Camera();
61
+ const geometry = new BufferGeometry();
62
+ geometry.setAttribute(
63
+ 'position',
64
+ new BufferAttribute(new Float32Array([-1, -1, 0, 3, -1, 0, -1, 3, 0]), 3),
65
+ );
66
+ geometry.setAttribute('uv', new BufferAttribute(new Float32Array([0, 0, 2, 0, 0, 2]), 2));
67
+ this.screen = new Mesh(geometry, this.convolutionMaterial);
68
+ this.screen.frustumCulled = false;
69
+ this.scene.add(this.screen);
70
+ }
71
+ render(
72
+ renderer: WebGLRenderer,
73
+ inputBuffer: WebGLRenderTarget,
74
+ outputBuffer: WebGLRenderTarget,
75
+ ): void {
76
+ const uniforms = this.convolutionMaterial.uniforms;
77
+ uniforms.depthBuffer.value = inputBuffer.depthTexture;
78
+ let last = inputBuffer;
79
+ let destination: WebGLRenderTarget;
80
+ let index = 0;
81
+ for (; index < this.convolutionMaterial.kernel.length - 1; index++) {
82
+ destination = (index & 1) === 0 ? this.renderTargetA : this.renderTargetB;
83
+ uniforms.kernel.value = this.convolutionMaterial.kernel[index];
84
+ uniforms.inputBuffer.value = last.texture;
85
+ renderer.setRenderTarget(destination);
86
+ renderer.render(this.scene, this.camera);
87
+ last = destination;
88
+ }
89
+ uniforms.kernel.value = this.convolutionMaterial.kernel[index];
90
+ uniforms.inputBuffer.value = last.texture;
91
+ renderer.setRenderTarget(this.renderToScreen ? null : outputBuffer);
92
+ renderer.render(this.scene, this.camera);
93
+ }
94
+ }
@@ -0,0 +1,197 @@
1
+ import * as THREE from 'three';
2
+ import { shaderMaterial } from '../core/shaderMaterial.js';
3
+
4
+ export type CausticsMaterialType = THREE.ShaderMaterial & {
5
+ cameraMatrixWorld?: THREE.Matrix4;
6
+ cameraProjectionMatrixInv?: THREE.Matrix4;
7
+ lightPlaneNormal?: THREE.Vector3;
8
+ lightPlaneConstant?: number;
9
+ normalTexture?: THREE.Texture | null;
10
+ depthTexture?: THREE.Texture | null;
11
+ lightDir?: THREE.Vector3;
12
+ near?: number;
13
+ far?: number;
14
+ modelMatrix?: THREE.Matrix4;
15
+ worldRadius?: number;
16
+ ior?: number;
17
+ bounces?: number;
18
+ resolution?: number;
19
+ size?: number;
20
+ intensity?: number;
21
+ };
22
+
23
+ export type CausticsProjectionMaterialType = THREE.ShaderMaterial & {
24
+ color?: THREE.Color;
25
+ causticsTexture?: THREE.Texture;
26
+ causticsTextureB?: THREE.Texture;
27
+ lightProjMatrix?: THREE.Matrix4;
28
+ lightViewMatrix?: THREE.Matrix4;
29
+ };
30
+
31
+ export type NormalMaterialType = THREE.MeshNormalMaterial & {
32
+ viewMatrix: { value: THREE.Matrix4 };
33
+ };
34
+
35
+ export function createNormalMaterial(side: THREE.Side = THREE.FrontSide) {
36
+ const viewMatrix = { value: new THREE.Matrix4() };
37
+ return Object.assign(new THREE.MeshNormalMaterial({ side }) as NormalMaterialType, {
38
+ viewMatrix,
39
+ onBeforeCompile: (shader: THREE.WebGLProgramParametersWithUniforms) => {
40
+ shader.uniforms.viewMatrix = viewMatrix;
41
+ shader.fragmentShader =
42
+ `vec3 inverseTransformDirection( in vec3 dir, in mat4 matrix ) {
43
+ return normalize( ( vec4( dir, 0.0 ) * matrix ).xyz );
44
+ }\n` +
45
+ shader.fragmentShader.replace(
46
+ '#include <normal_fragment_maps>',
47
+ `#include <normal_fragment_maps>
48
+ normal = inverseTransformDirection( normal, viewMatrix );\n`,
49
+ );
50
+ },
51
+ });
52
+ }
53
+
54
+ export const CausticsProjectionMaterial = shaderMaterial(
55
+ {
56
+ causticsTexture: null,
57
+ causticsTextureB: null,
58
+ color: new THREE.Color(),
59
+ lightProjMatrix: new THREE.Matrix4(),
60
+ lightViewMatrix: new THREE.Matrix4(),
61
+ },
62
+ `varying vec3 vWorldPosition;
63
+ void main() {
64
+ gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position, 1.);
65
+ vec4 worldPosition = modelMatrix * vec4(position, 1.);
66
+ vWorldPosition = worldPosition.xyz;
67
+ }`,
68
+ `varying vec3 vWorldPosition;
69
+ uniform vec3 color;
70
+ uniform sampler2D causticsTexture;
71
+ uniform sampler2D causticsTextureB;
72
+ uniform mat4 lightProjMatrix;
73
+ uniform mat4 lightViewMatrix;
74
+ void main() {
75
+ vec4 lightSpacePos = lightProjMatrix * lightViewMatrix * vec4(vWorldPosition, 1.0);
76
+ lightSpacePos.xyz /= lightSpacePos.w;
77
+ lightSpacePos.xyz = lightSpacePos.xyz * 0.5 + 0.5;
78
+ vec3 front = texture2D(causticsTexture, lightSpacePos.xy).rgb;
79
+ vec3 back = texture2D(causticsTextureB, lightSpacePos.xy).rgb;
80
+ gl_FragColor = vec4((front + back) * color, 1.0);
81
+ #include <tonemapping_fragment>
82
+ #include <colorspace_fragment>
83
+ }`,
84
+ );
85
+
86
+ export const CausticsMaterial = shaderMaterial(
87
+ {
88
+ cameraMatrixWorld: new THREE.Matrix4(),
89
+ cameraProjectionMatrixInv: new THREE.Matrix4(),
90
+ normalTexture: null,
91
+ depthTexture: null,
92
+ lightDir: new THREE.Vector3(0, 1, 0),
93
+ lightPlaneNormal: new THREE.Vector3(0, 1, 0),
94
+ lightPlaneConstant: 0,
95
+ near: 0.1,
96
+ far: 100,
97
+ modelMatrix: new THREE.Matrix4(),
98
+ worldRadius: 1 / 40,
99
+ ior: 1.1,
100
+ bounces: 0,
101
+ resolution: 1024,
102
+ size: 10,
103
+ intensity: 0.5,
104
+ },
105
+ `varying vec2 vUv;
106
+ void main() {
107
+ vUv = uv;
108
+ gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
109
+ }`,
110
+ `uniform mat4 cameraMatrixWorld;
111
+ uniform mat4 cameraProjectionMatrixInv;
112
+ uniform vec3 lightDir;
113
+ uniform vec3 lightPlaneNormal;
114
+ uniform float lightPlaneConstant;
115
+ uniform float near;
116
+ uniform float far;
117
+ uniform float time;
118
+ uniform float worldRadius;
119
+ uniform float resolution;
120
+ uniform float size;
121
+ uniform float intensity;
122
+ uniform float ior;
123
+ precision highp isampler2D;
124
+ precision highp usampler2D;
125
+ uniform sampler2D normalTexture;
126
+ uniform sampler2D depthTexture;
127
+ uniform float bounces;
128
+ varying vec2 vUv;
129
+ vec3 WorldPosFromDepth(float depth, vec2 coord) {
130
+ float z = depth * 2.0 - 1.0;
131
+ vec4 clipSpacePosition = vec4(coord * 2.0 - 1.0, z, 1.0);
132
+ vec4 viewSpacePosition = cameraProjectionMatrixInv * clipSpacePosition;
133
+ viewSpacePosition /= viewSpacePosition.w;
134
+ vec4 worldSpacePosition = cameraMatrixWorld * viewSpacePosition;
135
+ return worldSpacePosition.xyz;
136
+ }
137
+ float sdPlane(vec3 p, vec3 n, float h) { return dot(p,n) + h; }
138
+ float planeIntersect(vec3 ro, vec3 rd, vec4 p) { return -(dot(ro,p.xyz)+p.w)/dot(rd,p.xyz); }
139
+ vec3 totalInternalReflection(vec3 ro, vec3 rd, vec3 pos, vec3 normal, float ior, out vec3 rayOrigin, out vec3 rayDirection) {
140
+ rayOrigin = ro;
141
+ rayDirection = rd;
142
+ rayDirection = refract(rayDirection, normal, 1.0 / ior);
143
+ rayOrigin = pos + rayDirection * 0.1;
144
+ return rayDirection;
145
+ }
146
+ void main() {
147
+ float caustic = 0.0;
148
+ float causticTexelSize = (1.0 / resolution) * size * 2.0;
149
+ float texelsNeeded = worldRadius / causticTexelSize;
150
+ float sampleRadius = texelsNeeded / resolution;
151
+ float sum = 0.0;
152
+ if (texture2D(depthTexture, vUv).x == 1.0) { gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0); return; }
153
+ vec2 offset1 = vec2(-0.5, -0.5);
154
+ vec2 offset2 = vec2(-0.5, 0.5);
155
+ vec2 offset3 = vec2(0.5, 0.5);
156
+ vec2 offset4 = vec2(0.5, -0.5);
157
+ vec2 uv1 = vUv + offset1 * sampleRadius;
158
+ vec2 uv2 = vUv + offset2 * sampleRadius;
159
+ vec2 uv3 = vUv + offset3 * sampleRadius;
160
+ vec2 uv4 = vUv + offset4 * sampleRadius;
161
+ vec3 normal1 = texture2D(normalTexture, uv1, -10.0).rgb * 2.0 - 1.0;
162
+ vec3 normal2 = texture2D(normalTexture, uv2, -10.0).rgb * 2.0 - 1.0;
163
+ vec3 normal3 = texture2D(normalTexture, uv3, -10.0).rgb * 2.0 - 1.0;
164
+ vec3 normal4 = texture2D(normalTexture, uv4, -10.0).rgb * 2.0 - 1.0;
165
+ float depth1 = texture2D(depthTexture, uv1, -10.0).x;
166
+ float depth2 = texture2D(depthTexture, uv2, -10.0).x;
167
+ float depth3 = texture2D(depthTexture, uv3, -10.0).x;
168
+ float depth4 = texture2D(depthTexture, uv4, -10.0).x;
169
+ if (depth1 == 1.0 || depth2 == 1.0 || depth3 == 1.0 || depth4 == 1.0) { gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0); return; }
170
+ vec3 pos1 = WorldPosFromDepth(depth1, uv1);
171
+ vec3 pos2 = WorldPosFromDepth(depth2, uv2);
172
+ vec3 pos3 = WorldPosFromDepth(depth3, uv3);
173
+ vec3 pos4 = WorldPosFromDepth(depth4, uv4);
174
+ vec3 originPos1 = WorldPosFromDepth(0.0, uv1);
175
+ vec3 originPos2 = WorldPosFromDepth(0.0, uv2);
176
+ vec3 originPos3 = WorldPosFromDepth(0.0, uv3);
177
+ vec3 originPos4 = WorldPosFromDepth(0.0, uv4);
178
+ vec3 endPos1, endPos2, endPos3, endPos4;
179
+ vec3 endDir1, endDir2, endDir3, endDir4;
180
+ totalInternalReflection(originPos1, lightDir, pos1, normal1, ior, endPos1, endDir1);
181
+ totalInternalReflection(originPos2, lightDir, pos2, normal2, ior, endPos2, endDir2);
182
+ totalInternalReflection(originPos3, lightDir, pos3, normal3, ior, endPos3, endDir3);
183
+ totalInternalReflection(originPos4, lightDir, pos4, normal4, ior, endPos4, endDir4);
184
+ float lightPosArea = length(cross(originPos2 - originPos1, originPos3 - originPos1)) + length(cross(originPos3 - originPos1, originPos4 - originPos1));
185
+ float t1 = planeIntersect(endPos1, endDir1, vec4(lightPlaneNormal, lightPlaneConstant));
186
+ float t2 = planeIntersect(endPos2, endDir2, vec4(lightPlaneNormal, lightPlaneConstant));
187
+ float t3 = planeIntersect(endPos3, endDir3, vec4(lightPlaneNormal, lightPlaneConstant));
188
+ float t4 = planeIntersect(endPos4, endDir4, vec4(lightPlaneNormal, lightPlaneConstant));
189
+ vec3 finalPos1 = endPos1 + endDir1 * t1;
190
+ vec3 finalPos2 = endPos2 + endDir2 * t2;
191
+ vec3 finalPos3 = endPos3 + endDir3 * t3;
192
+ vec3 finalPos4 = endPos4 + endDir4 * t4;
193
+ float finalArea = length(cross(finalPos2 - finalPos1, finalPos3 - finalPos1)) + length(cross(finalPos3 - finalPos1, finalPos4 - finalPos1));
194
+ caustic += intensity * (lightPosArea / finalArea);
195
+ gl_FragColor = vec4(vec3(max(caustic, 0.0)), 1.0);
196
+ }`,
197
+ );
@@ -0,0 +1,65 @@
1
+ import * as THREE from 'three';
2
+
3
+ export class ConvolutionMaterial extends THREE.ShaderMaterial {
4
+ readonly kernel: Float32Array;
5
+ constructor(texelSize = new THREE.Vector2()) {
6
+ super({
7
+ uniforms: {
8
+ inputBuffer: new THREE.Uniform(null),
9
+ depthBuffer: new THREE.Uniform(null),
10
+ resolution: new THREE.Uniform(new THREE.Vector2()),
11
+ texelSize: new THREE.Uniform(new THREE.Vector2()),
12
+ halfTexelSize: new THREE.Uniform(new THREE.Vector2()),
13
+ kernel: new THREE.Uniform(0),
14
+ scale: new THREE.Uniform(1),
15
+ cameraNear: new THREE.Uniform(0),
16
+ cameraFar: new THREE.Uniform(1),
17
+ minDepthThreshold: new THREE.Uniform(0),
18
+ maxDepthThreshold: new THREE.Uniform(1),
19
+ depthScale: new THREE.Uniform(0),
20
+ depthToBlurRatioBias: new THREE.Uniform(0.25),
21
+ },
22
+ fragmentShader: `#include <common>
23
+ #include <dithering_pars_fragment>
24
+ uniform sampler2D inputBuffer; uniform sampler2D depthBuffer; uniform float cameraNear; uniform float cameraFar;
25
+ uniform float minDepthThreshold; uniform float maxDepthThreshold; uniform float depthScale; uniform float depthToBlurRatioBias;
26
+ varying vec2 vUv; varying vec2 vUv0; varying vec2 vUv1; varying vec2 vUv2; varying vec2 vUv3;
27
+ void main() {
28
+ float depthFactor = 0.0;
29
+ #ifdef USE_DEPTH
30
+ vec4 depth = texture2D(depthBuffer, vUv);
31
+ depthFactor = smoothstep(minDepthThreshold, maxDepthThreshold, 1.0-(depth.r * depth.a));
32
+ depthFactor *= depthScale; depthFactor = max(0.0, min(1.0, depthFactor + 0.25));
33
+ #endif
34
+ vec4 sum = texture2D(inputBuffer, mix(vUv0, vUv, depthFactor));
35
+ sum += texture2D(inputBuffer, mix(vUv1, vUv, depthFactor)); sum += texture2D(inputBuffer, mix(vUv2, vUv, depthFactor));
36
+ sum += texture2D(inputBuffer, mix(vUv3, vUv, depthFactor)); gl_FragColor = sum * 0.25;
37
+ #include <dithering_fragment>
38
+ #include <tonemapping_fragment>
39
+ #include <colorspace_fragment>
40
+ }`,
41
+ vertexShader: `uniform vec2 texelSize; uniform vec2 halfTexelSize; uniform float kernel; uniform float scale;
42
+ varying vec2 vUv; varying vec2 vUv0; varying vec2 vUv1; varying vec2 vUv2; varying vec2 vUv3;
43
+ void main() {
44
+ vec2 uv = position.xy * 0.5 + 0.5; vUv = uv;
45
+ vec2 dUv = (texelSize * vec2(kernel) + halfTexelSize) * scale;
46
+ vUv0 = vec2(uv.x - dUv.x, uv.y + dUv.y); vUv1 = vec2(uv.x + dUv.x, uv.y + dUv.y);
47
+ vUv2 = vec2(uv.x + dUv.x, uv.y - dUv.y); vUv3 = vec2(uv.x - dUv.x, uv.y - dUv.y);
48
+ gl_Position = vec4(position.xy, 1.0, 1.0);
49
+ }`,
50
+ blending: THREE.NoBlending,
51
+ depthWrite: false,
52
+ depthTest: false,
53
+ });
54
+ this.toneMapped = false;
55
+ this.setTexelSize(texelSize.x, texelSize.y);
56
+ this.kernel = new Float32Array([0, 1, 2, 2, 3]);
57
+ }
58
+ setTexelSize(x: number, y: number): void {
59
+ this.uniforms.texelSize.value.set(x, y);
60
+ this.uniforms.halfTexelSize.value.set(x, y).multiplyScalar(0.5);
61
+ }
62
+ setResolution(resolution: THREE.Vector2): void {
63
+ this.uniforms.resolution.value.copy(resolution);
64
+ }
65
+ }
@@ -0,0 +1,180 @@
1
+ import { Matrix4, MeshStandardMaterial, Texture } from 'three';
2
+
3
+ type Uniform<Value> = { value: Value | null };
4
+
5
+ export class MeshReflectorMaterialImpl extends MeshStandardMaterial {
6
+ private _tDepth: Uniform<Texture> = { value: null };
7
+ private _distortionMap: Uniform<Texture> = { value: null };
8
+ private _tDiffuse: Uniform<Texture> = { value: null };
9
+ private _tDiffuseBlur: Uniform<Texture> = { value: null };
10
+ private _textureMatrix: Uniform<Matrix4> = { value: null };
11
+ private _hasBlur = { value: false };
12
+ private _mirror = { value: 0 };
13
+ private _mixBlur = { value: 0 };
14
+ private _mixStrength = { value: 0.5 };
15
+ private _minDepthThreshold = { value: 0.9 };
16
+ private _maxDepthThreshold = { value: 1 };
17
+ private _depthScale = { value: 0 };
18
+ private _depthToBlurRatioBias = { value: 0.25 };
19
+ private _distortion = { value: 1 };
20
+ private _mixContrast = { value: 1 };
21
+ constructor(parameters = {}) {
22
+ super(parameters);
23
+ this.setValues(parameters);
24
+ }
25
+ override onBeforeCompile(shader: any): void {
26
+ shader.defines ??= {};
27
+ if (!shader.defines.USE_UV) shader.defines.USE_UV = '';
28
+ Object.assign(shader.uniforms, {
29
+ hasBlur: this._hasBlur,
30
+ tDiffuse: this._tDiffuse,
31
+ tDepth: this._tDepth,
32
+ distortionMap: this._distortionMap,
33
+ tDiffuseBlur: this._tDiffuseBlur,
34
+ textureMatrix: this._textureMatrix,
35
+ mirror: this._mirror,
36
+ mixBlur: this._mixBlur,
37
+ mixStrength: this._mixStrength,
38
+ minDepthThreshold: this._minDepthThreshold,
39
+ maxDepthThreshold: this._maxDepthThreshold,
40
+ depthScale: this._depthScale,
41
+ depthToBlurRatioBias: this._depthToBlurRatioBias,
42
+ distortion: this._distortion,
43
+ mixContrast: this._mixContrast,
44
+ });
45
+ shader.vertexShader =
46
+ `uniform mat4 textureMatrix; varying vec4 my_vUv; ${shader.vertexShader}`.replace(
47
+ '#include <project_vertex>',
48
+ '#include <project_vertex>\nmy_vUv = textureMatrix * vec4(position, 1.0);\ngl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);',
49
+ );
50
+ shader.fragmentShader = `uniform sampler2D tDiffuse; uniform sampler2D tDiffuseBlur; uniform sampler2D tDepth;
51
+ uniform sampler2D distortionMap; uniform float distortion; uniform float cameraNear; uniform float cameraFar; uniform bool hasBlur;
52
+ uniform float mixBlur; uniform float mirror; uniform float mixStrength; uniform float minDepthThreshold; uniform float maxDepthThreshold;
53
+ uniform float mixContrast; uniform float depthScale; uniform float depthToBlurRatioBias; varying vec4 my_vUv; ${shader.fragmentShader}`;
54
+ shader.fragmentShader = shader.fragmentShader.replace(
55
+ '#include <emissivemap_fragment>',
56
+ `#include <emissivemap_fragment>
57
+ float distortionFactor = 0.0;
58
+ #ifdef USE_DISTORTION
59
+ distortionFactor = texture2D(distortionMap, vUv).r * distortion;
60
+ #endif
61
+ vec4 new_vUv = my_vUv; new_vUv.xy += distortionFactor;
62
+ vec4 base = texture2DProj(tDiffuse, new_vUv); vec4 blur = texture2DProj(tDiffuseBlur, new_vUv); vec4 merge = base;
63
+ #ifdef USE_NORMALMAP
64
+ vec4 normalColor = texture2D(normalMap, vUv * normalScale);
65
+ vec3 my_normal = normalize(vec3(normalColor.r * 2.0 - 1.0, normalColor.b, normalColor.g * 2.0 - 1.0));
66
+ vec3 coord = new_vUv.xyz / new_vUv.w; vec2 normal_uv = coord.xy + coord.z * my_normal.xz * 0.05;
67
+ merge = texture2D(tDiffuse, normal_uv); blur = texture2D(tDiffuseBlur, normal_uv);
68
+ #endif
69
+ float depthFactor = 0.0001; float blurFactor = 0.0;
70
+ #ifdef USE_DEPTH
71
+ vec4 depth = texture2DProj(tDepth, new_vUv); depthFactor = smoothstep(minDepthThreshold, maxDepthThreshold, 1.0-(depth.r * depth.a));
72
+ depthFactor = max(0.0001, min(1.0, depthFactor * depthScale));
73
+ #ifdef USE_BLUR
74
+ blur *= min(1.0, depthFactor + depthToBlurRatioBias); merge *= min(1.0, depthFactor + 0.5);
75
+ #else
76
+ merge *= depthFactor;
77
+ #endif
78
+ #endif
79
+ float reflectorRoughnessFactor = roughness;
80
+ #ifdef USE_ROUGHNESSMAP
81
+ reflectorRoughnessFactor *= texture2D(roughnessMap, vUv).g;
82
+ #endif
83
+ #ifdef USE_BLUR
84
+ blurFactor = min(1.0, mixBlur * reflectorRoughnessFactor); merge = mix(merge, blur, blurFactor);
85
+ #endif
86
+ vec4 newMerge = vec4((merge.rgb - 0.5) * mixContrast + 0.5, 1.0);
87
+ diffuseColor.rgb *= ((1.0 - min(1.0, mirror)) + newMerge.rgb * mixStrength);`,
88
+ );
89
+ }
90
+ get tDiffuse() {
91
+ return this._tDiffuse.value;
92
+ }
93
+ set tDiffuse(value) {
94
+ this._tDiffuse.value = value;
95
+ }
96
+ get tDepth() {
97
+ return this._tDepth.value;
98
+ }
99
+ set tDepth(value) {
100
+ this._tDepth.value = value;
101
+ }
102
+ get distortionMap() {
103
+ return this._distortionMap.value;
104
+ }
105
+ set distortionMap(value) {
106
+ this._distortionMap.value = value;
107
+ }
108
+ get tDiffuseBlur() {
109
+ return this._tDiffuseBlur.value;
110
+ }
111
+ set tDiffuseBlur(value) {
112
+ this._tDiffuseBlur.value = value;
113
+ }
114
+ get textureMatrix() {
115
+ return this._textureMatrix.value;
116
+ }
117
+ set textureMatrix(value) {
118
+ this._textureMatrix.value = value;
119
+ }
120
+ get hasBlur() {
121
+ return this._hasBlur.value;
122
+ }
123
+ set hasBlur(value) {
124
+ this._hasBlur.value = value;
125
+ }
126
+ get mirror() {
127
+ return this._mirror.value;
128
+ }
129
+ set mirror(value) {
130
+ this._mirror.value = value;
131
+ }
132
+ get mixBlur() {
133
+ return this._mixBlur.value;
134
+ }
135
+ set mixBlur(value) {
136
+ this._mixBlur.value = value;
137
+ }
138
+ get mixStrength() {
139
+ return this._mixStrength.value;
140
+ }
141
+ set mixStrength(value) {
142
+ this._mixStrength.value = value;
143
+ }
144
+ get minDepthThreshold() {
145
+ return this._minDepthThreshold.value;
146
+ }
147
+ set minDepthThreshold(value) {
148
+ this._minDepthThreshold.value = value;
149
+ }
150
+ get maxDepthThreshold() {
151
+ return this._maxDepthThreshold.value;
152
+ }
153
+ set maxDepthThreshold(value) {
154
+ this._maxDepthThreshold.value = value;
155
+ }
156
+ get depthScale() {
157
+ return this._depthScale.value;
158
+ }
159
+ set depthScale(value) {
160
+ this._depthScale.value = value;
161
+ }
162
+ get depthToBlurRatioBias() {
163
+ return this._depthToBlurRatioBias.value;
164
+ }
165
+ set depthToBlurRatioBias(value) {
166
+ this._depthToBlurRatioBias.value = value;
167
+ }
168
+ get distortion() {
169
+ return this._distortion.value;
170
+ }
171
+ set distortion(value) {
172
+ this._distortion.value = value;
173
+ }
174
+ get mixContrast() {
175
+ return this._mixContrast.value;
176
+ }
177
+ set mixContrast(value) {
178
+ this._mixContrast.value = value;
179
+ }
180
+ }
@@ -0,0 +1,131 @@
1
+ import * as THREE from 'three';
2
+ import { MeshBVHUniformStruct, shaderIntersectFunction, shaderStructs } from 'three-mesh-bvh';
3
+ import { shaderMaterial } from '../core/shaderMaterial.js';
4
+
5
+ export const MeshRefractionMaterialImpl = shaderMaterial(
6
+ {
7
+ envMap: null,
8
+ bounces: 3,
9
+ ior: 2.4,
10
+ correctMips: true,
11
+ aberrationStrength: 0.01,
12
+ fresnel: 0,
13
+ bvh: new MeshBVHUniformStruct(),
14
+ color: new THREE.Color('white'),
15
+ opacity: 1,
16
+ resolution: new THREE.Vector2(),
17
+ viewMatrixInverse: new THREE.Matrix4(),
18
+ projectionMatrixInverse: new THREE.Matrix4(),
19
+ },
20
+ `uniform mat4 viewMatrixInverse;
21
+ varying vec3 vWorldPosition;
22
+ varying vec3 vNormal;
23
+ varying mat4 vModelMatrixInverse;
24
+ #include <color_pars_vertex>
25
+ void main() {
26
+ #include <color_vertex>
27
+ vec4 transformedNormal = vec4(normal, 0.0);
28
+ vec4 transformedPosition = vec4(position, 1.0);
29
+ #ifdef USE_INSTANCING
30
+ transformedNormal = instanceMatrix * transformedNormal;
31
+ transformedPosition = instanceMatrix * transformedPosition;
32
+ vModelMatrixInverse = inverse(modelMatrix * instanceMatrix);
33
+ #else
34
+ vModelMatrixInverse = inverse(modelMatrix);
35
+ #endif
36
+ vWorldPosition = (modelMatrix * transformedPosition).xyz;
37
+ vNormal = normalize((viewMatrixInverse * vec4(normalMatrix * transformedNormal.xyz, 0.0)).xyz);
38
+ gl_Position = projectionMatrix * viewMatrix * modelMatrix * transformedPosition;
39
+ }`,
40
+ `#define ENVMAP_TYPE_CUBE_UV
41
+ precision highp isampler2D;
42
+ precision highp usampler2D;
43
+ varying vec3 vWorldPosition;
44
+ varying vec3 vNormal;
45
+ varying mat4 vModelMatrixInverse;
46
+ #include <color_pars_fragment>
47
+ #ifdef ENVMAP_TYPE_CUBEM
48
+ uniform samplerCube envMap;
49
+ #else
50
+ uniform sampler2D envMap;
51
+ #endif
52
+ uniform float bounces;
53
+ ${shaderStructs}
54
+ ${shaderIntersectFunction}
55
+ uniform BVH bvh;
56
+ uniform float ior;
57
+ uniform bool correctMips;
58
+ uniform vec2 resolution;
59
+ uniform float fresnel;
60
+ uniform mat4 modelMatrix;
61
+ uniform mat4 projectionMatrixInverse;
62
+ uniform mat4 viewMatrixInverse;
63
+ uniform float aberrationStrength;
64
+ uniform vec3 color;
65
+ uniform float opacity;
66
+ float fresnelFunc(vec3 viewDirection, vec3 worldNormal) {
67
+ return pow(1.0 + dot(viewDirection, worldNormal), 10.0);
68
+ }
69
+ vec3 totalInternalReflection(vec3 ro, vec3 rd, vec3 normal, float ior, mat4 modelMatrixInverse) {
70
+ vec3 rayOrigin = ro;
71
+ vec3 rayDirection = refract(rd, normal, 1.0 / ior);
72
+ rayOrigin = vWorldPosition + rayDirection * 0.001;
73
+ rayOrigin = (modelMatrixInverse * vec4(rayOrigin, 1.0)).xyz;
74
+ rayDirection = normalize((modelMatrixInverse * vec4(rayDirection, 0.0)).xyz);
75
+ for(float i = 0.0; i < bounces; i++) {
76
+ uvec4 faceIndices = uvec4(0u);
77
+ vec3 faceNormal = vec3(0.0, 0.0, 1.0);
78
+ vec3 barycoord = vec3(0.0);
79
+ float side = 1.0;
80
+ float dist = 0.0;
81
+ bvhIntersectFirstHit(bvh, rayOrigin, rayDirection, faceIndices, faceNormal, barycoord, side, dist);
82
+ vec3 hitPos = rayOrigin + rayDirection * max(dist - 0.001, 0.0);
83
+ vec3 tempDir = refract(rayDirection, faceNormal, ior);
84
+ if (length(tempDir) != 0.0) { rayDirection = tempDir; break; }
85
+ rayDirection = reflect(rayDirection, faceNormal);
86
+ rayOrigin = hitPos + rayDirection * 0.01;
87
+ }
88
+ return normalize((modelMatrix * vec4(rayDirection, 0.0)).xyz);
89
+ }
90
+ #include <common>
91
+ #include <cube_uv_reflection_fragment>
92
+ #ifdef ENVMAP_TYPE_CUBEM
93
+ vec4 textureGradient(samplerCube envMap, vec3 rayDirection, vec3 directionCamPerfect) {
94
+ return textureGrad(envMap, rayDirection, dFdx(correctMips ? directionCamPerfect : rayDirection), dFdy(correctMips ? directionCamPerfect : rayDirection));
95
+ }
96
+ #else
97
+ vec4 textureGradient(sampler2D envMap, vec3 rayDirection, vec3 directionCamPerfect) {
98
+ vec2 uvv = equirectUv(rayDirection);
99
+ vec2 smoothUv = equirectUv(directionCamPerfect);
100
+ return textureGrad(envMap, uvv, dFdx(correctMips ? smoothUv : uvv), dFdy(correctMips ? smoothUv : uvv));
101
+ }
102
+ #endif
103
+ void main() {
104
+ vec2 uv = gl_FragCoord.xy / resolution;
105
+ vec3 directionCamPerfect = (projectionMatrixInverse * vec4(uv * 2.0 - 1.0, 0.0, 1.0)).xyz;
106
+ directionCamPerfect = normalize((viewMatrixInverse * vec4(directionCamPerfect, 0.0)).xyz);
107
+ vec3 normal = vNormal;
108
+ vec3 rayOrigin = cameraPosition;
109
+ vec3 rayDirection = normalize(vWorldPosition - cameraPosition);
110
+ vec4 diffuseColor = vec4(color, opacity);
111
+ #include <color_fragment>
112
+ #ifdef CHROMATIC_ABERRATIONS
113
+ vec3 rayDirectionG = totalInternalReflection(rayOrigin, rayDirection, normal, max(ior, 1.0), vModelMatrixInverse);
114
+ #ifdef FAST_CHROMA
115
+ vec3 rayDirectionR = normalize(rayDirectionG + vec3(aberrationStrength / 2.0));
116
+ vec3 rayDirectionB = normalize(rayDirectionG - vec3(aberrationStrength / 2.0));
117
+ #else
118
+ vec3 rayDirectionR = totalInternalReflection(rayOrigin, rayDirection, normal, max(ior * (1.0 - aberrationStrength), 1.0), vModelMatrixInverse);
119
+ vec3 rayDirectionB = totalInternalReflection(rayOrigin, rayDirection, normal, max(ior * (1.0 + aberrationStrength), 1.0), vModelMatrixInverse);
120
+ #endif
121
+ diffuseColor.rgb *= vec3(textureGradient(envMap, rayDirectionR, directionCamPerfect).r, textureGradient(envMap, rayDirectionG, directionCamPerfect).g, textureGradient(envMap, rayDirectionB, directionCamPerfect).b);
122
+ #else
123
+ rayDirection = totalInternalReflection(rayOrigin, rayDirection, normal, max(ior, 1.0), vModelMatrixInverse);
124
+ diffuseColor.rgb *= textureGradient(envMap, rayDirection, directionCamPerfect).rgb;
125
+ #endif
126
+ float nFresnel = fresnelFunc(normalize(vWorldPosition - cameraPosition), normal) * fresnel;
127
+ gl_FragColor = vec4(mix(diffuseColor.rgb, vec3(1.0), nFresnel), diffuseColor.a);
128
+ #include <tonemapping_fragment>
129
+ #include <colorspace_fragment>
130
+ }`,
131
+ );