@react-three/drei 9.77.0 → 9.77.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.
package/core/Grid.cjs.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@babel/runtime/helpers/extends"),n=require("react"),i=require("three"),o=require("@react-three/fiber"),r=require("./shaderMaterial.cjs.js");function t(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function l(e){if(e&&e.__esModule)return e;var n=Object.create(null);return e&&Object.keys(e).forEach((function(i){if("default"!==i){var o=Object.getOwnPropertyDescriptor(e,i);Object.defineProperty(n,i,o.get?o:{enumerable:!0,get:function(){return e[i]}})}})),n.default=e,Object.freeze(n)}var a=t(e),c=l(n),s=l(i);const f=r.shaderMaterial({cellSize:.5,sectionSize:1,fadeDistance:100,fadeStrength:1,cellThickness:.5,sectionThickness:1,cellColor:new s.Color,sectionColor:new s.Color,infiniteGrid:!1,followCamera:!1},"\n varying vec3 worldPosition;\n uniform float fadeDistance;\n uniform bool infiniteGrid;\n uniform bool followCamera;\n\n void main() {\n worldPosition = position.xzy;\n if (infiniteGrid) worldPosition *= 1.0 + fadeDistance;\n if (followCamera) worldPosition.xz +=cameraPosition.xz;\n\n gl_Position = projectionMatrix * modelViewMatrix * vec4(worldPosition, 1.0);\n }\n ","\n varying vec3 worldPosition;\n uniform float cellSize;\n uniform float sectionSize;\n uniform vec3 cellColor;\n uniform vec3 sectionColor;\n uniform float fadeDistance;\n uniform float fadeStrength;\n uniform float cellThickness;\n uniform float sectionThickness;\n\n float getGrid(float size, float thickness) {\n vec2 r = worldPosition.xz / size;\n vec2 grid = abs(fract(r - 0.5) - 0.5) / fwidth(r);\n float line = min(grid.x, grid.y) + 1. - thickness;\n return 1.0 - min(line, 1.);\n }\n\n void main() {\n float g1 = getGrid(cellSize, cellThickness);\n float g2 = getGrid(sectionSize, sectionThickness);\n\n float d = 1.0 - min(distance(cameraPosition.xz, worldPosition.xz) / fadeDistance, 1.);\n vec3 color = mix(cellColor, sectionColor, min(1.,sectionThickness * g2));\n\n gl_FragColor = vec4(color, (g1 + g2) * pow(d,fadeStrength));\n gl_FragColor.a = mix(0.75 * gl_FragColor.a, gl_FragColor.a, g2);\n if (gl_FragColor.a <= 0.0) discard;\n\n #include <tonemapping_fragment>\n #include <encodings_fragment>\n }\n "),d=c.forwardRef((({args:e,cellColor:n="#000000",sectionColor:i="#2080ff",cellSize:r=.5,sectionSize:t=1,followCamera:l=!1,infiniteGrid:d=!1,fadeDistance:u=100,fadeStrength:g=1,cellThickness:m=.5,sectionThickness:h=1,side:C=s.BackSide,...v},w)=>{o.extend({GridMaterial:f});const z={cellSize:r,sectionSize:t,cellColor:n,sectionColor:i,cellThickness:m,sectionThickness:h},x={fadeDistance:u,fadeStrength:g,infiniteGrid:d,followCamera:l};return c.createElement("mesh",a.default({ref:w,frustumCulled:!1},v),c.createElement("gridMaterial",a.default({transparent:!0,"extensions-derivatives":!0,side:C},z,x)),c.createElement("planeGeometry",{args:e}))}));exports.Grid=d;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@babel/runtime/helpers/extends"),n=require("react"),o=require("three"),i=require("react-merge-refs"),r=require("@react-three/fiber"),t=require("./shaderMaterial.cjs.js");function l(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function a(e){if(e&&e.__esModule)return e;var n=Object.create(null);return e&&Object.keys(e).forEach((function(o){if("default"!==o){var i=Object.getOwnPropertyDescriptor(e,o);Object.defineProperty(n,o,i.get?i:{enumerable:!0,get:function(){return e[o]}})}})),n.default=e,Object.freeze(n)}var s=l(e),c=a(n),f=a(o),d=l(i);const u=t.shaderMaterial({cellSize:.5,sectionSize:1,fadeDistance:100,fadeStrength:1,cellThickness:.5,sectionThickness:1,cellColor:new f.Color,sectionColor:new f.Color,infiniteGrid:!1,followCamera:!1,worldCamProjPosition:new f.Vector3,worldPlanePosition:new f.Vector3},"\n varying vec3 localPosition;\n varying vec4 worldPosition;\n\n uniform vec3 worldCamProjPosition;\n uniform vec3 worldPlanePosition;\n uniform float fadeDistance;\n uniform bool infiniteGrid;\n uniform bool followCamera;\n\n void main() {\n localPosition = position.xzy;\n if (infiniteGrid) localPosition *= 1.0 + fadeDistance;\n \n worldPosition = modelMatrix * vec4(localPosition, 1.0);\n if (followCamera) {\n worldPosition.xyz += (worldCamProjPosition - worldPlanePosition);\n localPosition = (inverse(modelMatrix) * worldPosition).xyz;\n }\n\n gl_Position = projectionMatrix * viewMatrix * worldPosition;\n }\n ","\n varying vec3 localPosition;\n varying vec4 worldPosition;\n\n uniform vec3 worldCamProjPosition;\n uniform float cellSize;\n uniform float sectionSize;\n uniform vec3 cellColor;\n uniform vec3 sectionColor;\n uniform float fadeDistance;\n uniform float fadeStrength;\n uniform float cellThickness;\n uniform float sectionThickness;\n\n float getGrid(float size, float thickness) {\n vec2 r = localPosition.xz / size;\n vec2 grid = abs(fract(r - 0.5) - 0.5) / fwidth(r);\n float line = min(grid.x, grid.y) + 1.0 - thickness;\n return 1.0 - min(line, 1.0);\n }\n\n void main() {\n float g1 = getGrid(cellSize, cellThickness);\n float g2 = getGrid(sectionSize, sectionThickness);\n\n float dist = distance(worldCamProjPosition, worldPosition.xyz);\n float d = 1.0 - min(dist / fadeDistance, 1.0);\n vec3 color = mix(cellColor, sectionColor, min(1.0, sectionThickness * g2));\n\n gl_FragColor = vec4(color, (g1 + g2) * pow(d, fadeStrength));\n gl_FragColor.a = mix(0.75 * gl_FragColor.a, gl_FragColor.a, g2);\n if (gl_FragColor.a <= 0.0) discard;\n\n #include <tonemapping_fragment>\n #include <encodings_fragment>\n }\n "),m=c.forwardRef((({args:e,cellColor:n="#000000",sectionColor:o="#2080ff",cellSize:i=.5,sectionSize:t=1,followCamera:l=!1,infiniteGrid:a=!1,fadeDistance:m=100,fadeStrength:g=1,cellThickness:P=.5,sectionThickness:w=1,side:v=f.BackSide,...C},h)=>{r.extend({GridMaterial:u});const p=c.useRef(null);r.useFrame((e=>{const n=(new f.Plane).setFromNormalAndCoplanarPoint(new f.Vector3(0,1,0),new f.Vector3(0,0,0)).applyMatrix4(p.current.matrixWorld),o=p.current.material,i=o.uniforms.worldCamProjPosition,r=o.uniforms.worldPlanePosition;n.projectPoint(e.camera.position,i.value),r.value.set(0,0,0).applyMatrix4(p.current.matrixWorld)}));const x={cellSize:i,sectionSize:t,cellColor:n,sectionColor:o,cellThickness:P,sectionThickness:w},z={fadeDistance:m,fadeStrength:g,infiniteGrid:a,followCamera:l};return c.createElement("mesh",s.default({ref:d.default([p,h]),frustumCulled:!1},C),c.createElement("gridMaterial",s.default({transparent:!0,"extensions-derivatives":!0,side:v},x,z)),c.createElement("planeGeometry",{args:e}))}));exports.Grid=m;
package/core/Grid.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import _extends from '@babel/runtime/helpers/esm/extends';
2
2
  import * as React from 'react';
3
3
  import * as THREE from 'three';
4
- import { extend } from '@react-three/fiber';
4
+ import mergeRefs from 'react-merge-refs';
5
+ import { extend, useFrame } from '@react-three/fiber';
5
6
  import { shaderMaterial } from './shaderMaterial.js';
6
7
 
7
8
  const GridMaterial = shaderMaterial({
@@ -14,26 +15,40 @@ const GridMaterial = shaderMaterial({
14
15
  cellColor: new THREE.Color(),
15
16
  sectionColor: new THREE.Color(),
16
17
  infiniteGrid: false,
17
- followCamera: false
18
+ followCamera: false,
19
+ worldCamProjPosition: new THREE.Vector3(),
20
+ worldPlanePosition: new THREE.Vector3()
18
21
  },
19
22
  /* glsl */
20
23
  `
21
- varying vec3 worldPosition;
24
+ varying vec3 localPosition;
25
+ varying vec4 worldPosition;
26
+
27
+ uniform vec3 worldCamProjPosition;
28
+ uniform vec3 worldPlanePosition;
22
29
  uniform float fadeDistance;
23
30
  uniform bool infiniteGrid;
24
31
  uniform bool followCamera;
25
32
 
26
33
  void main() {
27
- worldPosition = position.xzy;
28
- if (infiniteGrid) worldPosition *= 1.0 + fadeDistance;
29
- if (followCamera) worldPosition.xz +=cameraPosition.xz;
34
+ localPosition = position.xzy;
35
+ if (infiniteGrid) localPosition *= 1.0 + fadeDistance;
36
+
37
+ worldPosition = modelMatrix * vec4(localPosition, 1.0);
38
+ if (followCamera) {
39
+ worldPosition.xyz += (worldCamProjPosition - worldPlanePosition);
40
+ localPosition = (inverse(modelMatrix) * worldPosition).xyz;
41
+ }
30
42
 
31
- gl_Position = projectionMatrix * modelViewMatrix * vec4(worldPosition, 1.0);
43
+ gl_Position = projectionMatrix * viewMatrix * worldPosition;
32
44
  }
33
45
  `,
34
46
  /* glsl */
35
47
  `
36
- varying vec3 worldPosition;
48
+ varying vec3 localPosition;
49
+ varying vec4 worldPosition;
50
+
51
+ uniform vec3 worldCamProjPosition;
37
52
  uniform float cellSize;
38
53
  uniform float sectionSize;
39
54
  uniform vec3 cellColor;
@@ -44,20 +59,21 @@ const GridMaterial = shaderMaterial({
44
59
  uniform float sectionThickness;
45
60
 
46
61
  float getGrid(float size, float thickness) {
47
- vec2 r = worldPosition.xz / size;
62
+ vec2 r = localPosition.xz / size;
48
63
  vec2 grid = abs(fract(r - 0.5) - 0.5) / fwidth(r);
49
- float line = min(grid.x, grid.y) + 1. - thickness;
50
- return 1.0 - min(line, 1.);
64
+ float line = min(grid.x, grid.y) + 1.0 - thickness;
65
+ return 1.0 - min(line, 1.0);
51
66
  }
52
67
 
53
68
  void main() {
54
69
  float g1 = getGrid(cellSize, cellThickness);
55
70
  float g2 = getGrid(sectionSize, sectionThickness);
56
71
 
57
- float d = 1.0 - min(distance(cameraPosition.xz, worldPosition.xz) / fadeDistance, 1.);
58
- vec3 color = mix(cellColor, sectionColor, min(1.,sectionThickness * g2));
72
+ float dist = distance(worldCamProjPosition, worldPosition.xyz);
73
+ float d = 1.0 - min(dist / fadeDistance, 1.0);
74
+ vec3 color = mix(cellColor, sectionColor, min(1.0, sectionThickness * g2));
59
75
 
60
- gl_FragColor = vec4(color, (g1 + g2) * pow(d,fadeStrength));
76
+ gl_FragColor = vec4(color, (g1 + g2) * pow(d, fadeStrength));
61
77
  gl_FragColor.a = mix(0.75 * gl_FragColor.a, gl_FragColor.a, g2);
62
78
  if (gl_FragColor.a <= 0.0) discard;
63
79
 
@@ -83,6 +99,15 @@ const Grid = /*#__PURE__*/React.forwardRef(({
83
99
  extend({
84
100
  GridMaterial
85
101
  });
102
+ const ref = React.useRef(null);
103
+ useFrame(state => {
104
+ const plane = new THREE.Plane().setFromNormalAndCoplanarPoint(new THREE.Vector3(0, 1, 0), new THREE.Vector3(0, 0, 0)).applyMatrix4(ref.current.matrixWorld);
105
+ const gridMaterial = ref.current.material;
106
+ const worldCamProjPosition = gridMaterial.uniforms.worldCamProjPosition;
107
+ const worldPlanePosition = gridMaterial.uniforms.worldPlanePosition;
108
+ plane.projectPoint(state.camera.position, worldCamProjPosition.value);
109
+ worldPlanePosition.value.set(0, 0, 0).applyMatrix4(ref.current.matrixWorld);
110
+ });
86
111
  const uniforms1 = {
87
112
  cellSize,
88
113
  sectionSize,
@@ -98,7 +123,7 @@ const Grid = /*#__PURE__*/React.forwardRef(({
98
123
  followCamera
99
124
  };
100
125
  return /*#__PURE__*/React.createElement("mesh", _extends({
101
- ref: fRef,
126
+ ref: mergeRefs([ref, fRef]),
102
127
  frustumCulled: false
103
128
  }, props), /*#__PURE__*/React.createElement("gridMaterial", _extends({
104
129
  transparent: true,