@react-three/drei 9.30.2 → 9.30.3

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 CHANGED
@@ -1165,18 +1165,48 @@ This material makes your geometry distort following simplex noise.
1165
1165
  <a href="https://codesandbox.io/s/zqrreo"><img width="20%" src="https://codesandbox.io/api/v1/sandboxes/zqrreo/screenshot.png" alt="Demo"/></a>
1166
1166
  </p>
1167
1167
 
1168
- A convincing Glass/Diamond refraction material. Best pair it with a cube-camera!
1168
+ A convincing Glass/Diamond refraction material.
1169
+
1170
+ ```tsx
1171
+ type MeshRefractionMaterialProps = JSX.IntrinsicElements['shaderMaterial'] & {
1172
+ /** Environment map */
1173
+ envMap: THREE.CubeTexture | THREE.Texture
1174
+ /** Number of ray-cast bounces, it can be expensive to have too many, 2 */
1175
+ bounces?: number
1176
+ /** Refraction index, 2.4 */
1177
+ ior?: number
1178
+ /** Fresnel (strip light), 0 */
1179
+ fresnel?: number
1180
+ /** RGB shift intensity, can be expensive, 0 */
1181
+ aberrationStrength?: number
1182
+ /** Color, white */
1183
+ color?: ReactThreeFiber.Color
1184
+ /** If this is on it uses fewer ray casts for the RGB shift sacrificing physical accuracy, true */
1185
+ fastChroma?: boolean
1186
+ }
1187
+ ```
1188
+
1189
+ If you want it to reflect other objects in the scene you best pair it with a cube-camera.
1169
1190
 
1170
1191
  ```jsx
1171
1192
  <CubeCamera>
1172
1193
  {(texture) => (
1173
1194
  <mesh geometry={diamondGeometry} {...props}>
1174
- <RefractionMaterial envMap={texture} toneMapped={false} />
1195
+ <RefractionMaterial envMap={texture} />
1175
1196
  </mesh>
1176
1197
  )}
1177
1198
  </CubeCamera>
1178
1199
  ```
1179
1200
 
1201
+ Otherwise just pass it an environment map.
1202
+
1203
+ ```jsx
1204
+ const texture = useLoader(RGBELoader, "/textures/royal_esplanade_1k.hdr")
1205
+ return (
1206
+ <mesh geometry={diamondGeometry} {...props}>
1207
+ <RefractionMaterial envMap={texture} />
1208
+ ```
1209
+
1180
1210
  #### PointMaterial
1181
1211
 
1182
1212
  <p>
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@babel/runtime/helpers/extends"),r=require("react"),t=require("@react-three/fiber"),a=require("three-mesh-bvh"),n=require("../materials/MeshRefractionMaterial.cjs.js");function i(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function u(e){if(e&&e.__esModule)return e;var r=Object.create(null);return e&&Object.keys(e).forEach((function(t){if("default"!==t){var a=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(r,t,a.get?a:{enumerable:!0,get:function(){return e[t]}})}})),r.default=e,Object.freeze(r)}require("three"),require("./shaderMaterial.cjs.js");var o=i(e),s=u(r);exports.MeshRefractionMaterial=function({frames:e=1,resolution:i=256,near:u=.1,far:l=1e3,aberrationStrength:f=0,envMap:c,...h}){t.extend({MeshRefractionMaterial:n.MeshRefractionMaterial});const M=r.useRef(),{size:d}=t.useThree(),b=r.useMemo((()=>{const e={};if(c){var r,t;const n=(a=c)&&a.isCubeTexture,i=(null!==(r=n?null==(t=c.image[0])?void 0:t.width:c.image.width)&&void 0!==r?r:1024)/4,u=Math.floor(Math.log2(i)),o=Math.pow(2,u),s=3*Math.max(o,112),l=4*o;n&&(e.ENVMAP_TYPE_CUBEM=""),e.CUBEUV_TEXEL_WIDTH=""+1/s,e.CUBEUV_TEXEL_HEIGHT=""+1/l,e.CUBEUV_MAX_MIP=`${u}.0`}var a;return f>0&&(e.CHROMATIC_ABERRATIONS=""),e}),[f]);return r.useLayoutEffect((()=>{var e,r,t;const n=null==(e=M.current)||null==(r=e.__r3f)||null==(t=r.parent)?void 0:t.geometry;n&&M.current.bvh.updateFrom(new a.MeshBVH(n.toNonIndexed(),{lazyGeneration:!1,strategy:a.SAH}))}),[]),s.createElement("meshRefractionMaterial",o.default({key:JSON.stringify(!!f),defines:b,ref:M,resolution:[d.width,d.height],aberrationStrength:f,envMap:c},h))};
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@babel/runtime/helpers/extends"),r=require("react"),t=require("@react-three/fiber"),a=require("three-mesh-bvh"),n=require("../materials/MeshRefractionMaterial.cjs.js");function i(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function u(e){if(e&&e.__esModule)return e;var r=Object.create(null);return e&&Object.keys(e).forEach((function(t){if("default"!==t){var a=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(r,t,a.get?a:{enumerable:!0,get:function(){return e[t]}})}})),r.default=e,Object.freeze(r)}require("three"),require("./shaderMaterial.cjs.js");var o=i(e),s=u(r);exports.MeshRefractionMaterial=function({aberrationStrength:e=0,fastChroma:i=!0,envMap:u,...l}){t.extend({MeshRefractionMaterial:n.MeshRefractionMaterial});const c=r.useRef(),{size:f}=t.useThree(),h=r.useMemo((()=>{var r,t;const a={},n=(o=u)&&o.isCubeTexture;var o;const s=(null!==(r=n?null==(t=u.image[0])?void 0:t.width:u.image.width)&&void 0!==r?r:1024)/4,l=Math.floor(Math.log2(s)),c=Math.pow(2,l),f=3*Math.max(c,112),h=4*c;return n&&(a.ENVMAP_TYPE_CUBEM=""),a.CUBEUV_TEXEL_WIDTH=""+1/f,a.CUBEUV_TEXEL_HEIGHT=""+1/h,a.CUBEUV_MAX_MIP=`${l}.0`,e>0&&(a.CHROMATIC_ABERRATIONS=""),i&&(a.FAST_CHROMA=""),a}),[e,i]);return r.useLayoutEffect((()=>{var e,r,t;const n=null==(e=c.current)||null==(r=e.__r3f)||null==(t=r.parent)?void 0:t.geometry;n&&c.current.bvh.updateFrom(new a.MeshBVH(n.toNonIndexed(),{lazyGeneration:!1,strategy:a.SAH}))}),[]),s.createElement("meshRefractionMaterial",o.default({key:JSON.stringify(h),defines:h,ref:c,resolution:[f.width,f.height],aberrationStrength:e,envMap:u},l))};
@@ -10,16 +10,13 @@ declare global {
10
10
  }
11
11
  }
12
12
  declare type MeshRefractionMaterialProps = JSX.IntrinsicElements['shaderMaterial'] & {
13
+ envMap: THREE.CubeTexture | THREE.Texture;
13
14
  bounces?: number;
14
15
  ior?: number;
15
16
  fresnel?: number;
16
17
  aberrationStrength?: number;
17
18
  color?: ReactThreeFiber.Color;
18
- resolution?: ReactThreeFiber.Vector2;
19
- envMap?: THREE.CubeTexture | THREE.Texture;
20
- frames?: number;
21
- near?: number;
22
- far?: number;
19
+ fastChroma?: boolean;
23
20
  };
24
- export declare function MeshRefractionMaterial({ frames, resolution, near, far, aberrationStrength, envMap, ...props }: MeshRefractionMaterialProps): JSX.Element;
21
+ export declare function MeshRefractionMaterial({ aberrationStrength, fastChroma, envMap, ...props }: MeshRefractionMaterialProps): JSX.Element;
25
22
  export {};
@@ -8,11 +8,8 @@ import { MeshRefractionMaterial as MeshRefractionMaterial$1 } from '../materials
8
8
  const isCubeTexture = def => def && def.isCubeTexture;
9
9
 
10
10
  function MeshRefractionMaterial({
11
- frames = 1,
12
- resolution = 256,
13
- near = 0.1,
14
- far = 1000,
15
11
  aberrationStrength = 0,
12
+ fastChroma = true,
16
13
  envMap,
17
14
  ...props
18
15
  }) {
@@ -24,34 +21,35 @@ function MeshRefractionMaterial({
24
21
  size
25
22
  } = useThree();
26
23
  const defines = useMemo(() => {
27
- const temp = {};
24
+ var _ref, _envMap$image$;
28
25
 
29
- if (envMap) {
30
- var _ref, _envMap$image$;
26
+ const temp = {}; // Sampler2D and SamplerCube need different defines
31
27
 
32
- const isCubeMap = isCubeTexture(envMap);
33
- const w = (_ref = isCubeMap ? (_envMap$image$ = envMap.image[0]) == null ? void 0 : _envMap$image$.width : envMap.image.width) !== null && _ref !== void 0 ? _ref : 1024;
34
- const cubeSize = w / 4;
28
+ const isCubeMap = isCubeTexture(envMap);
29
+ const w = (_ref = isCubeMap ? (_envMap$image$ = envMap.image[0]) == null ? void 0 : _envMap$image$.width : envMap.image.width) !== null && _ref !== void 0 ? _ref : 1024;
30
+ const cubeSize = w / 4;
35
31
 
36
- const _lodMax = Math.floor(Math.log2(cubeSize));
32
+ const _lodMax = Math.floor(Math.log2(cubeSize));
37
33
 
38
- const _cubeSize = Math.pow(2, _lodMax);
34
+ const _cubeSize = Math.pow(2, _lodMax);
39
35
 
40
- const width = 3 * Math.max(_cubeSize, 16 * 7);
41
- const height = 4 * _cubeSize;
42
- if (isCubeMap) temp.ENVMAP_TYPE_CUBEM = '';
43
- temp.CUBEUV_TEXEL_WIDTH = `${1.0 / width}`;
44
- temp.CUBEUV_TEXEL_HEIGHT = `${1.0 / height}`;
45
- temp.CUBEUV_MAX_MIP = `${_lodMax}.0`;
46
- }
36
+ const width = 3 * Math.max(_cubeSize, 16 * 7);
37
+ const height = 4 * _cubeSize;
38
+ if (isCubeMap) temp.ENVMAP_TYPE_CUBEM = '';
39
+ temp.CUBEUV_TEXEL_WIDTH = `${1.0 / width}`;
40
+ temp.CUBEUV_TEXEL_HEIGHT = `${1.0 / height}`;
41
+ temp.CUBEUV_MAX_MIP = `${_lodMax}.0`; // Add defines from chromatic aberration
47
42
 
48
43
  if (aberrationStrength > 0) temp.CHROMATIC_ABERRATIONS = '';
44
+ if (fastChroma) temp.FAST_CHROMA = '';
49
45
  return temp;
50
- }, [aberrationStrength]);
46
+ }, [aberrationStrength, fastChroma]);
51
47
  useLayoutEffect(() => {
52
48
  var _material$current, _material$current$__r, _material$current$__r2;
53
49
 
54
- const geometry = (_material$current = material.current) == null ? void 0 : (_material$current$__r = _material$current.__r3f) == null ? void 0 : (_material$current$__r2 = _material$current$__r.parent) == null ? void 0 : _material$current$__r2.geometry;
50
+ // Get the geometry of this materials parent
51
+ const geometry = (_material$current = material.current) == null ? void 0 : (_material$current$__r = _material$current.__r3f) == null ? void 0 : (_material$current$__r2 = _material$current$__r.parent) == null ? void 0 : _material$current$__r2.geometry; // Update the BVH
52
+
55
53
  if (geometry) material.current.bvh.updateFrom(new MeshBVH(geometry.toNonIndexed(), {
56
54
  lazyGeneration: false,
57
55
  strategy: SAH
@@ -59,7 +57,7 @@ function MeshRefractionMaterial({
59
57
  }, []);
60
58
  return /*#__PURE__*/React.createElement("meshRefractionMaterial", _extends({
61
59
  // @ts-ignore
62
- key: JSON.stringify(!!aberrationStrength) // @ts-ignore
60
+ key: JSON.stringify(defines) // @ts-ignore
63
61
  ,
64
62
  defines: defines,
65
63
  ref: material,