@react-three/drei 9.51.22 → 9.51.24
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.
- package/README.md +28 -10
- package/core/MeshTransmissionMaterial.cjs.js +1 -1
- package/core/MeshTransmissionMaterial.d.ts +1 -3
- package/core/MeshTransmissionMaterial.js +4 -14
- package/core/shaderMaterial.cjs.js +1 -1
- package/core/shaderMaterial.js +5 -3
- package/core/useFBO.cjs.js +1 -1
- package/core/useFBO.d.ts +3 -3
- package/core/useFBO.js +14 -2
- package/index.cjs.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1630,16 +1630,15 @@ Injects [percent closer soft shadows (pcss)](https://threejs.org/examples/#webgl
|
|
|
1630
1630
|
<a href="https://codesandbox.io/s/ni6v4"><img width="20%" src="https://codesandbox.io/api/v1/sandboxes/ni6v4/screenshot.png" alt="Demo"/></a>
|
|
1631
1631
|
</p>
|
|
1632
1632
|
|
|
1633
|
-
Creates a THREE.ShaderMaterial for you with easier handling of uniforms, which are
|
|
1633
|
+
Creates a THREE.ShaderMaterial for you with easier handling of uniforms, which are automatically declared as setter/getters on the object and allowed as constructor arguments.
|
|
1634
1634
|
|
|
1635
1635
|
```jsx
|
|
1636
1636
|
import { extend } from '@react-three/fiber'
|
|
1637
|
-
import glsl from 'babel-plugin-glsl/macro'
|
|
1638
1637
|
|
|
1639
1638
|
const ColorShiftMaterial = shaderMaterial(
|
|
1640
1639
|
{ time: 0, color: new THREE.Color(0.2, 0.0, 0.1) },
|
|
1641
1640
|
// vertex shader
|
|
1642
|
-
glsl
|
|
1641
|
+
/*glsl*/`
|
|
1643
1642
|
varying vec2 vUv;
|
|
1644
1643
|
void main() {
|
|
1645
1644
|
vUv = uv;
|
|
@@ -1647,7 +1646,7 @@ const ColorShiftMaterial = shaderMaterial(
|
|
|
1647
1646
|
}
|
|
1648
1647
|
`,
|
|
1649
1648
|
// fragment shader
|
|
1650
|
-
glsl
|
|
1649
|
+
/*glsl*/`
|
|
1651
1650
|
uniform float time;
|
|
1652
1651
|
uniform vec3 color;
|
|
1653
1652
|
varying vec2 vUv;
|
|
@@ -1657,12 +1656,16 @@ const ColorShiftMaterial = shaderMaterial(
|
|
|
1657
1656
|
`
|
|
1658
1657
|
)
|
|
1659
1658
|
|
|
1659
|
+
// declaratively
|
|
1660
1660
|
extend({ ColorShiftMaterial })
|
|
1661
|
-
|
|
1662
|
-
// in your component
|
|
1661
|
+
...
|
|
1663
1662
|
<mesh>
|
|
1664
1663
|
<colorShiftMaterial color="hotpink" time={1} />
|
|
1665
1664
|
</mesh>
|
|
1665
|
+
|
|
1666
|
+
// imperatively, all uniforms are available as setter/getters and constructor args
|
|
1667
|
+
const material = new ColorShiftMaterial({ color: new THREE.Color("hotpink") })
|
|
1668
|
+
material.time = 1
|
|
1666
1669
|
```
|
|
1667
1670
|
|
|
1668
1671
|
`shaderMaterial` attaches a unique `key` property to the prototype class. If you wire it to Reacts own `key` property, you can enable hot-reload.
|
|
@@ -1916,11 +1919,26 @@ return <SomethingThatNeedsADepthBuffer depthBuffer={depthBuffer} />
|
|
|
1916
1919
|
|
|
1917
1920
|
Creates a `THREE.WebGLRenderTarget`.
|
|
1918
1921
|
|
|
1922
|
+
```tsx
|
|
1923
|
+
type FBOSettings = {
|
|
1924
|
+
/** Defines the count of MSAA samples. Can only be used with WebGL 2. Default: 0 */
|
|
1925
|
+
samples?: number
|
|
1926
|
+
/** If set, the scene depth will be rendered into buffer.depthTexture. Default: false */
|
|
1927
|
+
depth?: boolean
|
|
1928
|
+
} & THREE.WebGLRenderTargetOptions
|
|
1929
|
+
|
|
1930
|
+
export function useFBO(
|
|
1931
|
+
/** Width in pixels, or settings (will render fullscreen by default) */
|
|
1932
|
+
width?: number | FBOSettings,
|
|
1933
|
+
/** Height in pixels */
|
|
1934
|
+
height?: number,
|
|
1935
|
+
/**Settings */
|
|
1936
|
+
settings?: FBOSettings
|
|
1937
|
+
): THREE.WebGLRenderTarget {
|
|
1938
|
+
```
|
|
1939
|
+
|
|
1919
1940
|
```jsx
|
|
1920
|
-
const target = useFBO({
|
|
1921
|
-
multisample: true,
|
|
1922
|
-
stencilBuffer: false,
|
|
1923
|
-
})
|
|
1941
|
+
const target = useFBO({ stencilBuffer: false })
|
|
1924
1942
|
```
|
|
1925
1943
|
|
|
1926
1944
|
The rendertarget is automatically disposed when unmounted.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var n=require("@babel/runtime/helpers/extends"),e=require("three"),t=require("react"),o=require("@react-three/fiber"),a=require("./useFBO.cjs.js");function r(n){return n&&"object"==typeof n&&"default"in n?n:{default:n}}function i(n){if(n&&n.__esModule)return n;var e=Object.create(null);return n&&Object.keys(n).forEach((function(t){if("default"!==t){var o=Object.getOwnPropertyDescriptor(n,t);Object.defineProperty(e,t,o.get?o:{enumerable:!0,get:function(){return n[t]}})}})),e.default=n,Object.freeze(e)}var s=r(n),l=i(e),c=i(t);class m extends l.MeshPhysicalMaterial{constructor(n=6,e=!1){super(),this.uniforms={chromaticAberration:{value:.05},transmission:{value:0},_transmission:{value:1},transmissionMap:{value:null},roughness:{value:0},thickness:{value:0},thicknessMap:{value:null},attenuationDistance:{value:1/0},attenuationColor:{value:new l.Color("white")},anisotropy:{value:.1},time:{value:0},distortion:{value:0},distortionScale:{value:.5},temporalDistortion:{value:0},buffer:{value:null},resolution:{value:new l.Vector2}},this.onBeforeCompile=t=>{t.uniforms={...t.uniforms,...this.uniforms},e?t.defines.USE_SAMPLER="":t.defines.USE_TRANSMISSION="",t.fragmentShader="\n uniform float chromaticAberration;\n uniform vec2 resolution; \n uniform float anisotropy; \n uniform float time;\n uniform float distortion;\n uniform float distortionScale;\n uniform float temporalDistortion;\n uniform sampler2D buffer;\n\n vec3 random3(vec3 c) {\n float j = 4096.0*sin(dot(c,vec3(17.0, 59.4, 15.0)));\n vec3 r;\n r.z = fract(512.0*j);\n j *= .125;\n r.x = fract(512.0*j);\n j *= .125;\n r.y = fract(512.0*j);\n return r-0.5;\n }\n\n float seed = 0.0;\n uint hash( uint x ) {\n x += ( x << 10u );\n x ^= ( x >> 6u );\n x += ( x << 3u );\n x ^= ( x >> 11u );\n x += ( x << 15u );\n return x;\n }\n\n // Compound versions of the hashing algorithm I whipped together.\n uint hash( uvec2 v ) { return hash( v.x ^ hash(v.y) ); }\n uint hash( uvec3 v ) { return hash( v.x ^ hash(v.y) ^ hash(v.z) ); }\n uint hash( uvec4 v ) { return hash( v.x ^ hash(v.y) ^ hash(v.z) ^ hash(v.w) ); }\n\n // Construct a float with half-open range [0:1] using low 23 bits.\n // All zeroes yields 0.0, all ones yields the next smallest representable value below 1.0.\n float floatConstruct( uint m ) {\n const uint ieeeMantissa = 0x007FFFFFu; // binary32 mantissa bitmask\n const uint ieeeOne = 0x3F800000u; // 1.0 in IEEE binary32\n m &= ieeeMantissa; // Keep only mantissa bits (fractional part)\n m |= ieeeOne; // Add fractional part to 1.0\n float f = uintBitsToFloat( m ); // Range [1:2]\n return f - 1.0; // Range [0:1]\n }\n\n // Pseudo-random value in half-open range [0:1].\n float random( float x ) { return floatConstruct(hash(floatBitsToUint(x))); }\n float random( vec2 v ) { return floatConstruct(hash(floatBitsToUint(v))); }\n float random( vec3 v ) { return floatConstruct(hash(floatBitsToUint(v))); }\n float random( vec4 v ) { return floatConstruct(hash(floatBitsToUint(v))); }\n\n float rand() {\n float result = random(vec3(gl_FragCoord.xy, seed));\n seed += 1.0;\n return result;\n }\n\n const float F3 = 0.3333333;\n const float G3 = 0.1666667;\n\n float snoise(vec3 p) {\n vec3 s = floor(p + dot(p, vec3(F3)));\n vec3 x = p - s + dot(s, vec3(G3));\n vec3 e = step(vec3(0.0), x - x.yzx);\n vec3 i1 = e*(1.0 - e.zxy);\n vec3 i2 = 1.0 - e.zxy*(1.0 - e);\n vec3 x1 = x - i1 + G3;\n vec3 x2 = x - i2 + 2.0*G3;\n vec3 x3 = x - 1.0 + 3.0*G3;\n vec4 w, d;\n w.x = dot(x, x);\n w.y = dot(x1, x1);\n w.z = dot(x2, x2);\n w.w = dot(x3, x3);\n w = max(0.6 - w, 0.0);\n d.x = dot(random3(s), x);\n d.y = dot(random3(s + i1), x1);\n d.z = dot(random3(s + i2), x2);\n d.w = dot(random3(s + 1.0), x3);\n w *= w;\n w *= w;\n d *= w;\n return dot(d, vec4(52.0));\n }\n\n float snoiseFractal(vec3 m) {\n return 0.5333333* snoise(m)\n +0.2666667* snoise(2.0*m)\n +0.1333333* snoise(4.0*m)\n +0.0666667* snoise(8.0*m);\n }\n"+t.fragmentShader,t.fragmentShader=t.fragmentShader.replace("#include <transmission_pars_fragment>","\n #ifdef USE_TRANSMISSION\n // Transmission code is based on glTF-Sampler-Viewer\n // https://github.com/KhronosGroup/glTF-Sample-Viewer\n uniform float _transmission;\n uniform float thickness;\n uniform float attenuationDistance;\n uniform vec3 attenuationColor;\n #ifdef USE_TRANSMISSIONMAP\n uniform sampler2D transmissionMap;\n #endif\n #ifdef USE_THICKNESSMAP\n uniform sampler2D thicknessMap;\n #endif\n uniform vec2 transmissionSamplerSize;\n uniform sampler2D transmissionSamplerMap;\n uniform mat4 modelMatrix;\n uniform mat4 projectionMatrix;\n varying vec3 vWorldPosition;\n vec3 getVolumeTransmissionRay( const in vec3 n, const in vec3 v, const in float thickness, const in float ior, const in mat4 modelMatrix ) {\n // Direction of refracted light.\n vec3 refractionVector = refract( - v, normalize( n ), 1.0 / ior );\n // Compute rotation-independant scaling of the model matrix.\n vec3 modelScale;\n modelScale.x = length( vec3( modelMatrix[ 0 ].xyz ) );\n modelScale.y = length( vec3( modelMatrix[ 1 ].xyz ) );\n modelScale.z = length( vec3( modelMatrix[ 2 ].xyz ) );\n // The thickness is specified in local space.\n return normalize( refractionVector ) * thickness * modelScale;\n }\n float applyIorToRoughness( const in float roughness, const in float ior ) {\n // Scale roughness with IOR so that an IOR of 1.0 results in no microfacet refraction and\n // an IOR of 1.5 results in the default amount of microfacet refraction.\n return roughness * clamp( ior * 2.0 - 2.0, 0.0, 1.0 );\n }\n vec4 getTransmissionSample( const in vec2 fragCoord, const in float roughness, const in float ior ) {\n float framebufferLod = log2( transmissionSamplerSize.x ) * applyIorToRoughness( roughness, ior ); \n #ifdef USE_SAMPLER\n #ifdef texture2DLodEXT\n return texture2DLodEXT(transmissionSamplerMap, fragCoord.xy, framebufferLod);\n #else\n return texture2D(transmissionSamplerMap, fragCoord.xy, framebufferLod);\n #endif\n #else\n return texture2D(buffer, fragCoord.xy);\n #endif\n }\n vec3 applyVolumeAttenuation( const in vec3 radiance, const in float transmissionDistance, const in vec3 attenuationColor, const in float attenuationDistance ) {\n if ( isinf( attenuationDistance ) ) {\n // Attenuation distance is +∞, i.e. the transmitted color is not attenuated at all.\n return radiance;\n } else {\n // Compute light attenuation using Beer's law.\n vec3 attenuationCoefficient = -log( attenuationColor ) / attenuationDistance;\n vec3 transmittance = exp( - attenuationCoefficient * transmissionDistance ); // Beer's law\n return transmittance * radiance;\n }\n }\n vec4 getIBLVolumeRefraction( const in vec3 n, const in vec3 v, const in float roughness, const in vec3 diffuseColor,\n const in vec3 specularColor, const in float specularF90, const in vec3 position, const in mat4 modelMatrix,\n const in mat4 viewMatrix, const in mat4 projMatrix, const in float ior, const in float thickness,\n const in vec3 attenuationColor, const in float attenuationDistance ) {\n vec3 transmissionRay = getVolumeTransmissionRay( n, v, thickness, ior, modelMatrix );\n vec3 refractedRayExit = position + transmissionRay;\n // Project refracted vector on the framebuffer, while mapping to normalized device coordinates.\n vec4 ndcPos = projMatrix * viewMatrix * vec4( refractedRayExit, 1.0 );\n vec2 refractionCoords = ndcPos.xy / ndcPos.w;\n refractionCoords += 1.0;\n refractionCoords /= 2.0;\n // Sample framebuffer to get pixel the refracted ray hits.\n vec4 transmittedLight = getTransmissionSample( refractionCoords, roughness, ior );\n vec3 attenuatedColor = applyVolumeAttenuation( transmittedLight.rgb, length( transmissionRay ), attenuationColor, attenuationDistance );\n // Get the specular component.\n vec3 F = EnvironmentBRDF( n, v, specularColor, specularF90, roughness );\n return vec4( ( 1.0 - F ) * attenuatedColor * diffuseColor, transmittedLight.a );\n }\n #endif\n"),t.fragmentShader=t.fragmentShader.replace("#include <transmission_fragment>",`\n vec2 uv = gl_FragCoord.xy / resolution.xy; \n // Improve the refraction to use the world pos\n material.transmission = _transmission;\n material.transmissionAlpha = 1.0;\n material.thickness = thickness;\n material.attenuationDistance = attenuationDistance;\n material.attenuationColor = attenuationColor;\n #ifdef USE_TRANSMISSIONMAP\n material.transmission *= texture2D( transmissionMap, vUv ).r;\n #endif\n #ifdef USE_THICKNESSMAP\n material.thickness *= texture2D( thicknessMap, vUv ).g;\n #endif\n \n vec3 pos = vWorldPosition;\n vec3 v = normalize( cameraPosition - pos );\n vec3 n = inverseTransformDirection( normal, viewMatrix );\n vec3 transmission = vec3(0.0);\n float transmissionR, transmissionB, transmissionG;\n float randomCoords = rand();\n float thickness_smear = thickness * max(pow(roughness, 0.33), anisotropy);\n vec3 distortionNormal = vec3(0.0);\n vec3 temporalOffset = vec3(time, -time, -time) * temporalDistortion;\n if (distortion > 0.0) {\n distortionNormal = distortion * vec3(snoiseFractal(vec3((pos * distortionScale + temporalOffset))), snoiseFractal(vec3(pos.zxy * distortionScale - temporalOffset)), snoiseFractal(vec3(pos.yxz * distortionScale + temporalOffset)));\n }\n for (float i = 0.0; i < ${n}.0; i ++) {\n vec3 sampleNorm = normalize(n + roughness * roughness * 2.0 * normalize(vec3(rand() - 0.5, rand() - 0.5, rand() - 0.5)) * pow(rand(), 0.33) + distortionNormal);\n transmissionR = getIBLVolumeRefraction(\n sampleNorm, v, material.roughness, material.diffuseColor, material.specularColor, material.specularF90,\n pos, modelMatrix, viewMatrix, projectionMatrix, material.ior, material.thickness + thickness_smear * (i + randomCoords) / float(${n}),\n material.attenuationColor, material.attenuationDistance\n ).r;\n transmissionG = getIBLVolumeRefraction(\n sampleNorm, v, material.roughness, material.diffuseColor, material.specularColor, material.specularF90,\n pos, modelMatrix, viewMatrix, projectionMatrix, material.ior * (1.0 + chromaticAberration * (i + randomCoords) / float(${n})) , material.thickness + thickness_smear * (i + randomCoords) / float(${n}),\n material.attenuationColor, material.attenuationDistance\n ).g;\n transmissionB = getIBLVolumeRefraction(\n sampleNorm, v, material.roughness, material.diffuseColor, material.specularColor, material.specularF90,\n pos, modelMatrix, viewMatrix, projectionMatrix, material.ior * (1.0 + 2.0 * chromaticAberration * (i + randomCoords) / float(${n})), material.thickness + thickness_smear * (i + randomCoords) / float(${n}),\n material.attenuationColor, material.attenuationDistance\n ).b;\n transmission.r += transmissionR;\n transmission.g += transmissionG;\n transmission.b += transmissionB;\n }\n transmission /= ${n}.0;\n totalDiffuse = mix( totalDiffuse, transmission.rgb, material.transmission );\n`)},Object.keys(this.uniforms).forEach((n=>Object.defineProperty(this,n,{get:()=>this.uniforms[n].value,set:e=>this.uniforms[n].value=e})))}}const u=c.forwardRef((({buffer:n,transmissionSampler:e=!1,transmission:t=1,samples:r=10,resolution:i,background:u,...f},d)=>{o.extend({MeshTransmissionMaterial:m});const v=c.useRef(null),{size:h,viewport:p}=o.useThree(),x=a.useFBO(i);let g,M,S,C;return o.useFrame((t=>{v.current.time=t.clock.getElapsedTime(),n||e||(C=v.current.__r3f.parent,C&&(S=t.gl.toneMapping,M=C.visible,t.gl.toneMapping=l.NoToneMapping,C.visible=!1,t.gl.setRenderTarget(x),g=t.scene.background,u&&(t.scene.background=u),t.gl.render(t.scene,t.camera),t.scene.background=g,t.gl.setRenderTarget(null),C.visible=M,t.gl.toneMapping=S))})),c.useImperativeHandle(d,(()=>v.current),[]),c.createElement("meshTransmissionMaterial",s.default({args:[r,e],ref:v},f,{buffer:n||x.texture,_transmission:t,transmission:e?t:0,resolution:[h.width*p.dpr,h.height*p.dpr]}))}));exports.MeshTransmissionMaterial=u;
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var n=require("@babel/runtime/helpers/extends"),e=require("three"),t=require("react"),a=require("@react-three/fiber"),o=require("./useFBO.cjs.js");function r(n){return n&&"object"==typeof n&&"default"in n?n:{default:n}}function i(n){if(n&&n.__esModule)return n;var e=Object.create(null);return n&&Object.keys(n).forEach((function(t){if("default"!==t){var a=Object.getOwnPropertyDescriptor(n,t);Object.defineProperty(e,t,a.get?a:{enumerable:!0,get:function(){return n[t]}})}})),e.default=n,Object.freeze(e)}var s=r(n),l=i(e),c=i(t);class m extends l.MeshPhysicalMaterial{constructor(n=6,e=!1){super(),this.uniforms={chromaticAberration:{value:.05},transmission:{value:0},_transmission:{value:1},transmissionMap:{value:null},roughness:{value:0},thickness:{value:0},thicknessMap:{value:null},attenuationDistance:{value:1/0},attenuationColor:{value:new l.Color("white")},anisotropy:{value:.1},time:{value:0},distortion:{value:0},distortionScale:{value:.5},temporalDistortion:{value:0},buffer:{value:null}},this.onBeforeCompile=t=>{t.uniforms={...t.uniforms,...this.uniforms},e?t.defines.USE_SAMPLER="":t.defines.USE_TRANSMISSION="",t.fragmentShader="\n uniform float chromaticAberration; \n uniform float anisotropy; \n uniform float time;\n uniform float distortion;\n uniform float distortionScale;\n uniform float temporalDistortion;\n uniform sampler2D buffer;\n\n vec3 random3(vec3 c) {\n float j = 4096.0*sin(dot(c,vec3(17.0, 59.4, 15.0)));\n vec3 r;\n r.z = fract(512.0*j);\n j *= .125;\n r.x = fract(512.0*j);\n j *= .125;\n r.y = fract(512.0*j);\n return r-0.5;\n }\n\n float seed = 0.0;\n uint hash( uint x ) {\n x += ( x << 10u );\n x ^= ( x >> 6u );\n x += ( x << 3u );\n x ^= ( x >> 11u );\n x += ( x << 15u );\n return x;\n }\n\n // Compound versions of the hashing algorithm I whipped together.\n uint hash( uvec2 v ) { return hash( v.x ^ hash(v.y) ); }\n uint hash( uvec3 v ) { return hash( v.x ^ hash(v.y) ^ hash(v.z) ); }\n uint hash( uvec4 v ) { return hash( v.x ^ hash(v.y) ^ hash(v.z) ^ hash(v.w) ); }\n\n // Construct a float with half-open range [0:1] using low 23 bits.\n // All zeroes yields 0.0, all ones yields the next smallest representable value below 1.0.\n float floatConstruct( uint m ) {\n const uint ieeeMantissa = 0x007FFFFFu; // binary32 mantissa bitmask\n const uint ieeeOne = 0x3F800000u; // 1.0 in IEEE binary32\n m &= ieeeMantissa; // Keep only mantissa bits (fractional part)\n m |= ieeeOne; // Add fractional part to 1.0\n float f = uintBitsToFloat( m ); // Range [1:2]\n return f - 1.0; // Range [0:1]\n }\n\n // Pseudo-random value in half-open range [0:1].\n float random( float x ) { return floatConstruct(hash(floatBitsToUint(x))); }\n float random( vec2 v ) { return floatConstruct(hash(floatBitsToUint(v))); }\n float random( vec3 v ) { return floatConstruct(hash(floatBitsToUint(v))); }\n float random( vec4 v ) { return floatConstruct(hash(floatBitsToUint(v))); }\n\n float rand() {\n float result = random(vec3(gl_FragCoord.xy, seed));\n seed += 1.0;\n return result;\n }\n\n const float F3 = 0.3333333;\n const float G3 = 0.1666667;\n\n float snoise(vec3 p) {\n vec3 s = floor(p + dot(p, vec3(F3)));\n vec3 x = p - s + dot(s, vec3(G3));\n vec3 e = step(vec3(0.0), x - x.yzx);\n vec3 i1 = e*(1.0 - e.zxy);\n vec3 i2 = 1.0 - e.zxy*(1.0 - e);\n vec3 x1 = x - i1 + G3;\n vec3 x2 = x - i2 + 2.0*G3;\n vec3 x3 = x - 1.0 + 3.0*G3;\n vec4 w, d;\n w.x = dot(x, x);\n w.y = dot(x1, x1);\n w.z = dot(x2, x2);\n w.w = dot(x3, x3);\n w = max(0.6 - w, 0.0);\n d.x = dot(random3(s), x);\n d.y = dot(random3(s + i1), x1);\n d.z = dot(random3(s + i2), x2);\n d.w = dot(random3(s + 1.0), x3);\n w *= w;\n w *= w;\n d *= w;\n return dot(d, vec4(52.0));\n }\n\n float snoiseFractal(vec3 m) {\n return 0.5333333* snoise(m)\n +0.2666667* snoise(2.0*m)\n +0.1333333* snoise(4.0*m)\n +0.0666667* snoise(8.0*m);\n }\n"+t.fragmentShader,t.fragmentShader=t.fragmentShader.replace("#include <transmission_pars_fragment>","\n #ifdef USE_TRANSMISSION\n // Transmission code is based on glTF-Sampler-Viewer\n // https://github.com/KhronosGroup/glTF-Sample-Viewer\n uniform float _transmission;\n uniform float thickness;\n uniform float attenuationDistance;\n uniform vec3 attenuationColor;\n #ifdef USE_TRANSMISSIONMAP\n uniform sampler2D transmissionMap;\n #endif\n #ifdef USE_THICKNESSMAP\n uniform sampler2D thicknessMap;\n #endif\n uniform vec2 transmissionSamplerSize;\n uniform sampler2D transmissionSamplerMap;\n uniform mat4 modelMatrix;\n uniform mat4 projectionMatrix;\n varying vec3 vWorldPosition;\n vec3 getVolumeTransmissionRay( const in vec3 n, const in vec3 v, const in float thickness, const in float ior, const in mat4 modelMatrix ) {\n // Direction of refracted light.\n vec3 refractionVector = refract( - v, normalize( n ), 1.0 / ior );\n // Compute rotation-independant scaling of the model matrix.\n vec3 modelScale;\n modelScale.x = length( vec3( modelMatrix[ 0 ].xyz ) );\n modelScale.y = length( vec3( modelMatrix[ 1 ].xyz ) );\n modelScale.z = length( vec3( modelMatrix[ 2 ].xyz ) );\n // The thickness is specified in local space.\n return normalize( refractionVector ) * thickness * modelScale;\n }\n float applyIorToRoughness( const in float roughness, const in float ior ) {\n // Scale roughness with IOR so that an IOR of 1.0 results in no microfacet refraction and\n // an IOR of 1.5 results in the default amount of microfacet refraction.\n return roughness * clamp( ior * 2.0 - 2.0, 0.0, 1.0 );\n }\n vec4 getTransmissionSample( const in vec2 fragCoord, const in float roughness, const in float ior ) {\n float framebufferLod = log2( transmissionSamplerSize.x ) * applyIorToRoughness( roughness, ior ); \n #ifdef USE_SAMPLER\n #ifdef texture2DLodEXT\n return texture2DLodEXT(transmissionSamplerMap, fragCoord.xy, framebufferLod);\n #else\n return texture2D(transmissionSamplerMap, fragCoord.xy, framebufferLod);\n #endif\n #else\n return texture2D(buffer, fragCoord.xy);\n #endif\n }\n vec3 applyVolumeAttenuation( const in vec3 radiance, const in float transmissionDistance, const in vec3 attenuationColor, const in float attenuationDistance ) {\n if ( isinf( attenuationDistance ) ) {\n // Attenuation distance is +∞, i.e. the transmitted color is not attenuated at all.\n return radiance;\n } else {\n // Compute light attenuation using Beer's law.\n vec3 attenuationCoefficient = -log( attenuationColor ) / attenuationDistance;\n vec3 transmittance = exp( - attenuationCoefficient * transmissionDistance ); // Beer's law\n return transmittance * radiance;\n }\n }\n vec4 getIBLVolumeRefraction( const in vec3 n, const in vec3 v, const in float roughness, const in vec3 diffuseColor,\n const in vec3 specularColor, const in float specularF90, const in vec3 position, const in mat4 modelMatrix,\n const in mat4 viewMatrix, const in mat4 projMatrix, const in float ior, const in float thickness,\n const in vec3 attenuationColor, const in float attenuationDistance ) {\n vec3 transmissionRay = getVolumeTransmissionRay( n, v, thickness, ior, modelMatrix );\n vec3 refractedRayExit = position + transmissionRay;\n // Project refracted vector on the framebuffer, while mapping to normalized device coordinates.\n vec4 ndcPos = projMatrix * viewMatrix * vec4( refractedRayExit, 1.0 );\n vec2 refractionCoords = ndcPos.xy / ndcPos.w;\n refractionCoords += 1.0;\n refractionCoords /= 2.0;\n // Sample framebuffer to get pixel the refracted ray hits.\n vec4 transmittedLight = getTransmissionSample( refractionCoords, roughness, ior );\n vec3 attenuatedColor = applyVolumeAttenuation( transmittedLight.rgb, length( transmissionRay ), attenuationColor, attenuationDistance );\n // Get the specular component.\n vec3 F = EnvironmentBRDF( n, v, specularColor, specularF90, roughness );\n return vec4( ( 1.0 - F ) * attenuatedColor * diffuseColor, transmittedLight.a );\n }\n #endif\n"),t.fragmentShader=t.fragmentShader.replace("#include <transmission_fragment>",` \n // Improve the refraction to use the world pos\n material.transmission = _transmission;\n material.transmissionAlpha = 1.0;\n material.thickness = thickness;\n material.attenuationDistance = attenuationDistance;\n material.attenuationColor = attenuationColor;\n #ifdef USE_TRANSMISSIONMAP\n material.transmission *= texture2D( transmissionMap, vUv ).r;\n #endif\n #ifdef USE_THICKNESSMAP\n material.thickness *= texture2D( thicknessMap, vUv ).g;\n #endif\n \n vec3 pos = vWorldPosition;\n vec3 v = normalize( cameraPosition - pos );\n vec3 n = inverseTransformDirection( normal, viewMatrix );\n vec3 transmission = vec3(0.0);\n float transmissionR, transmissionB, transmissionG;\n float randomCoords = rand();\n float thickness_smear = thickness * max(pow(roughness, 0.33), anisotropy);\n vec3 distortionNormal = vec3(0.0);\n vec3 temporalOffset = vec3(time, -time, -time) * temporalDistortion;\n if (distortion > 0.0) {\n distortionNormal = distortion * vec3(snoiseFractal(vec3((pos * distortionScale + temporalOffset))), snoiseFractal(vec3(pos.zxy * distortionScale - temporalOffset)), snoiseFractal(vec3(pos.yxz * distortionScale + temporalOffset)));\n }\n for (float i = 0.0; i < ${n}.0; i ++) {\n vec3 sampleNorm = normalize(n + roughness * roughness * 2.0 * normalize(vec3(rand() - 0.5, rand() - 0.5, rand() - 0.5)) * pow(rand(), 0.33) + distortionNormal);\n transmissionR = getIBLVolumeRefraction(\n sampleNorm, v, material.roughness, material.diffuseColor, material.specularColor, material.specularF90,\n pos, modelMatrix, viewMatrix, projectionMatrix, material.ior, material.thickness + thickness_smear * (i + randomCoords) / float(${n}),\n material.attenuationColor, material.attenuationDistance\n ).r;\n transmissionG = getIBLVolumeRefraction(\n sampleNorm, v, material.roughness, material.diffuseColor, material.specularColor, material.specularF90,\n pos, modelMatrix, viewMatrix, projectionMatrix, material.ior * (1.0 + chromaticAberration * (i + randomCoords) / float(${n})) , material.thickness + thickness_smear * (i + randomCoords) / float(${n}),\n material.attenuationColor, material.attenuationDistance\n ).g;\n transmissionB = getIBLVolumeRefraction(\n sampleNorm, v, material.roughness, material.diffuseColor, material.specularColor, material.specularF90,\n pos, modelMatrix, viewMatrix, projectionMatrix, material.ior * (1.0 + 2.0 * chromaticAberration * (i + randomCoords) / float(${n})), material.thickness + thickness_smear * (i + randomCoords) / float(${n}),\n material.attenuationColor, material.attenuationDistance\n ).b;\n transmission.r += transmissionR;\n transmission.g += transmissionG;\n transmission.b += transmissionB;\n }\n transmission /= ${n}.0;\n totalDiffuse = mix( totalDiffuse, transmission.rgb, material.transmission );\n`)},Object.keys(this.uniforms).forEach((n=>Object.defineProperty(this,n,{get:()=>this.uniforms[n].value,set:e=>this.uniforms[n].value=e})))}}const u=c.forwardRef((({buffer:n,transmissionSampler:e=!1,transmission:t=1,samples:r=10,resolution:i,background:u,...f},d)=>{a.extend({MeshTransmissionMaterial:m});const v=c.useRef(null),h=o.useFBO(i);let p,x,g,M;return a.useFrame((t=>{v.current.time=t.clock.getElapsedTime(),n||e||(M=v.current.__r3f.parent,M&&(g=t.gl.toneMapping,x=M.visible,t.gl.toneMapping=l.NoToneMapping,M.visible=!1,t.gl.setRenderTarget(h),p=t.scene.background,u&&(t.scene.background=u),t.gl.render(t.scene,t.camera),t.scene.background=p,t.gl.setRenderTarget(null),M.visible=x,t.gl.toneMapping=g))})),c.useImperativeHandle(d,(()=>v.current),[]),c.createElement("meshTransmissionMaterial",s.default({args:[r,e],ref:v},f,{buffer:n||h.texture,_transmission:t,transmission:e?t:0}))}));exports.MeshTransmissionMaterial=u;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import * as THREE from 'three';
|
|
2
2
|
import * as React from 'react';
|
|
3
|
-
import { ReactThreeFiber } from '@react-three/fiber';
|
|
4
3
|
declare type MeshTransmissionMaterialType = Omit<JSX.IntrinsicElements['meshPhysicalMaterial'], 'args' | 'roughness' | 'thickness' | 'transmission'> & {
|
|
5
4
|
transmission?: number;
|
|
6
5
|
thickness?: number;
|
|
@@ -12,7 +11,6 @@ declare type MeshTransmissionMaterialType = Omit<JSX.IntrinsicElements['meshPhys
|
|
|
12
11
|
temporalDistortion: number;
|
|
13
12
|
buffer?: THREE.Texture;
|
|
14
13
|
time?: number;
|
|
15
|
-
resolution?: ReactThreeFiber.Vector2;
|
|
16
14
|
args?: [samples: number, transmissionSampler: boolean];
|
|
17
15
|
};
|
|
18
16
|
declare global {
|
|
@@ -22,7 +20,7 @@ declare global {
|
|
|
22
20
|
}
|
|
23
21
|
}
|
|
24
22
|
}
|
|
25
|
-
export declare const MeshTransmissionMaterial: React.ForwardRefExoticComponent<Pick<Omit<MeshTransmissionMaterialType, "args"
|
|
23
|
+
export declare const MeshTransmissionMaterial: React.ForwardRefExoticComponent<Pick<Omit<MeshTransmissionMaterialType, "args"> & {
|
|
26
24
|
transmissionSampler?: boolean | undefined;
|
|
27
25
|
resolution?: number | undefined;
|
|
28
26
|
samples?: number | undefined;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import _extends from '@babel/runtime/helpers/esm/extends';
|
|
2
2
|
import * as THREE from 'three';
|
|
3
3
|
import * as React from 'react';
|
|
4
|
-
import { extend,
|
|
4
|
+
import { extend, useFrame } from '@react-three/fiber';
|
|
5
5
|
import { useFBO } from './useFBO.js';
|
|
6
6
|
|
|
7
7
|
class MeshTransmissionMaterialImpl extends THREE.MeshPhysicalMaterial {
|
|
@@ -55,9 +55,6 @@ class MeshTransmissionMaterialImpl extends THREE.MeshPhysicalMaterial {
|
|
|
55
55
|
},
|
|
56
56
|
buffer: {
|
|
57
57
|
value: null
|
|
58
|
-
},
|
|
59
|
-
resolution: {
|
|
60
|
-
value: new THREE.Vector2()
|
|
61
58
|
}
|
|
62
59
|
};
|
|
63
60
|
|
|
@@ -73,8 +70,7 @@ class MeshTransmissionMaterialImpl extends THREE.MeshPhysicalMaterial {
|
|
|
73
70
|
shader.fragmentShader =
|
|
74
71
|
/*glsl*/
|
|
75
72
|
`
|
|
76
|
-
uniform float chromaticAberration;
|
|
77
|
-
uniform vec2 resolution;
|
|
73
|
+
uniform float chromaticAberration;
|
|
78
74
|
uniform float anisotropy;
|
|
79
75
|
uniform float time;
|
|
80
76
|
uniform float distortion;
|
|
@@ -248,8 +244,7 @@ class MeshTransmissionMaterialImpl extends THREE.MeshPhysicalMaterial {
|
|
|
248
244
|
|
|
249
245
|
shader.fragmentShader = shader.fragmentShader.replace('#include <transmission_fragment>',
|
|
250
246
|
/*glsl*/
|
|
251
|
-
`
|
|
252
|
-
vec2 uv = gl_FragCoord.xy / resolution.xy;
|
|
247
|
+
`
|
|
253
248
|
// Improve the refraction to use the world pos
|
|
254
249
|
material.transmission = _transmission;
|
|
255
250
|
material.transmissionAlpha = 1.0;
|
|
@@ -321,10 +316,6 @@ const MeshTransmissionMaterial = /*#__PURE__*/React.forwardRef(({
|
|
|
321
316
|
MeshTransmissionMaterial: MeshTransmissionMaterialImpl
|
|
322
317
|
});
|
|
323
318
|
const ref = React.useRef(null);
|
|
324
|
-
const {
|
|
325
|
-
size,
|
|
326
|
-
viewport
|
|
327
|
-
} = useThree();
|
|
328
319
|
const fbo = useFBO(resolution);
|
|
329
320
|
let oldBg;
|
|
330
321
|
let oldVis;
|
|
@@ -373,8 +364,7 @@ const MeshTransmissionMaterial = /*#__PURE__*/React.forwardRef(({
|
|
|
373
364
|
// This is because THREE.WebGLRenderer will check for transmission > 0 and execute extra renders.
|
|
374
365
|
// The exception is when transmissionSampler is set, in which case we are using three's built in sampler.
|
|
375
366
|
,
|
|
376
|
-
transmission: transmissionSampler ? transmission : 0
|
|
377
|
-
resolution: [size.width * viewport.dpr, size.height * viewport.dpr]
|
|
367
|
+
transmission: transmissionSampler ? transmission : 0
|
|
378
368
|
}));
|
|
379
369
|
});
|
|
380
370
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";function e(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var
|
|
1
|
+
"use strict";function e(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var s=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,s.get?s:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}Object.defineProperty(exports,"__esModule",{value:!0});var t=e(require("three"));exports.shaderMaterial=function(e,r,s,n){const i=class extends t.ShaderMaterial{constructor(i={}){const a=Object.entries(e);super({uniforms:a.reduce(((e,[r,s])=>({...e,...t.UniformsUtils.clone({[r]:{value:s}})})),{}),vertexShader:r,fragmentShader:s}),this.key="",a.forEach((([e])=>Object.defineProperty(this,e,{get:()=>this.uniforms[e].value,set:t=>this.uniforms[e].value=t}))),Object.assign(this,i),n&&n(this)}};return i.key=t.MathUtils.generateUUID(),i};
|
package/core/shaderMaterial.js
CHANGED
|
@@ -17,15 +17,17 @@ function shaderMaterial(uniforms, vertexShader, fragmentShader, onInit) {
|
|
|
17
17
|
};
|
|
18
18
|
}, {}),
|
|
19
19
|
vertexShader,
|
|
20
|
-
fragmentShader
|
|
21
|
-
...parameters
|
|
20
|
+
fragmentShader
|
|
22
21
|
}); // Create getter/setters
|
|
23
22
|
|
|
24
23
|
this.key = '';
|
|
25
24
|
entries.forEach(([name]) => Object.defineProperty(this, name, {
|
|
26
25
|
get: () => this.uniforms[name].value,
|
|
27
26
|
set: v => this.uniforms[name].value = v
|
|
28
|
-
}));
|
|
27
|
+
})); // Assign parameters, this might include uniforms
|
|
28
|
+
|
|
29
|
+
Object.assign(this, parameters); // Call onInit
|
|
30
|
+
|
|
29
31
|
if (onInit) onInit(this);
|
|
30
32
|
}
|
|
31
33
|
|
package/core/useFBO.cjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("three"),r=require("@react-three/fiber");function u(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var u=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,u.get?u:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var n=u(e),i=u(t);exports.useFBO=function(e,t,u){const{gl:o,size:s,viewport:a}=r.useThree(),p="number"==typeof e?e:s.width*a.dpr,c="number"==typeof t?t:s.height*a.dpr,f=("number"==typeof e?u:e)||{},{samples:l=0,depth:d,...b}=f,y=n.useMemo((()=>{let e;return e=new i.WebGLRenderTarget(p,c,{minFilter:i.LinearFilter,magFilter:i.LinearFilter,encoding:o.outputEncoding,type:i.HalfFloatType,...b}),d&&(e.depthTexture=new i.DepthTexture(p,c,i.FloatType)),e.samples=l,e}),[]);return n.useLayoutEffect((()=>{y.setSize(p,c),l&&(y.samples=l)}),[l,y,p,c]),n.useEffect((()=>()=>y.dispose()),[]),y};
|
package/core/useFBO.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as THREE from 'three';
|
|
2
|
-
declare type FBOSettings
|
|
3
|
-
multisample?: T;
|
|
2
|
+
declare type FBOSettings = {
|
|
4
3
|
samples?: number;
|
|
4
|
+
depth?: boolean;
|
|
5
5
|
} & THREE.WebGLRenderTargetOptions;
|
|
6
|
-
export declare function useFBO
|
|
6
|
+
export declare function useFBO(width?: number | FBOSettings, height?: number, settings?: FBOSettings): THREE.WebGLRenderTarget;
|
|
7
7
|
export {};
|
package/core/useFBO.js
CHANGED
|
@@ -4,7 +4,13 @@ import { useThree } from '@react-three/fiber';
|
|
|
4
4
|
|
|
5
5
|
// 👇 uncomment when TS version supports function overloads
|
|
6
6
|
// export function useFBO(settings?: FBOSettings)
|
|
7
|
-
function useFBO(
|
|
7
|
+
function useFBO(
|
|
8
|
+
/** Width in pixels, or settings (will render fullscreen by default) */
|
|
9
|
+
width,
|
|
10
|
+
/** Height in pixels */
|
|
11
|
+
height,
|
|
12
|
+
/**Settings */
|
|
13
|
+
settings) {
|
|
8
14
|
const {
|
|
9
15
|
gl,
|
|
10
16
|
size,
|
|
@@ -18,7 +24,8 @@ function useFBO(width, height, settings) {
|
|
|
18
24
|
const _settings = (typeof width === 'number' ? settings : width) || {};
|
|
19
25
|
|
|
20
26
|
const {
|
|
21
|
-
samples,
|
|
27
|
+
samples = 0,
|
|
28
|
+
depth,
|
|
22
29
|
...targetSettings
|
|
23
30
|
} = _settings;
|
|
24
31
|
const target = React.useMemo(() => {
|
|
@@ -30,6 +37,11 @@ function useFBO(width, height, settings) {
|
|
|
30
37
|
type: THREE.HalfFloatType,
|
|
31
38
|
...targetSettings
|
|
32
39
|
});
|
|
40
|
+
|
|
41
|
+
if (depth) {
|
|
42
|
+
target.depthTexture = new THREE.DepthTexture(_width, _height, THREE.FloatType);
|
|
43
|
+
}
|
|
44
|
+
|
|
33
45
|
target.samples = samples;
|
|
34
46
|
return target;
|
|
35
47
|
}, []);
|