123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- // Author: Fyrestar https://mevedia.com (https://github.com/Fyrestar/THREE.InfiniteGridHelper)
- THREE.InfiniteGridHelper = function InfiniteGridHelper( size1, size2, color, distance, axes = 'xzy' ) {
- color = color || new THREE.Color( 'white' );
- size1 = size1 || 10;
- size2 = size2 || 100;
- distance = distance || 8000;
- const planeAxes = axes.substr( 0, 2 );
- const geometry = new THREE.PlaneBufferGeometry( 2, 2, 1, 1 );
- const material = new THREE.ShaderMaterial( {
- side: THREE.DoubleSide,
- uniforms: {
- uSize1: {
- value: size1
- },
- uSize2: {
- value: size2
- },
- uColor: {
- value: color
- },
- uDistance: {
- value: distance
- }
- },
- transparent: true,
- vertexShader: `
-
- varying vec3 worldPosition;
-
- uniform float uDistance;
-
- void main() {
-
- vec3 pos = position.${axes} * uDistance;
- pos.${planeAxes} += cameraPosition.${planeAxes};
-
- worldPosition = pos;
-
- gl_Position = projectionMatrix * modelViewMatrix * vec4(pos, 1.0);
-
- }
- `,
- fragmentShader: `
-
- varying vec3 worldPosition;
-
- uniform float uSize1;
- uniform float uSize2;
- uniform vec3 uColor;
- uniform float uDistance;
-
-
-
- float getGrid(float size) {
-
- vec2 r = worldPosition.${planeAxes} / size;
-
-
- vec2 grid = abs(fract(r - 0.5) - 0.5) / fwidth(r);
- float line = min(grid.x, grid.y);
-
-
- return 1.0 - min(line, 1.0);
- }
-
- void main() {
-
-
- float d = 1.0 - min(distance(cameraPosition.${planeAxes}, worldPosition.${planeAxes}) / uDistance, 1.0);
-
- float g1 = getGrid(uSize1);
- float g2 = getGrid(uSize2);
-
-
- gl_FragColor = vec4(uColor.rgb, mix(g2, g1, g1) * pow(d, 3.0));
- gl_FragColor.a = mix(0.5 * gl_FragColor.a, gl_FragColor.a, g2);
-
- if ( gl_FragColor.a <= 0.0 ) discard;
-
-
- }
-
- `,
- extensions: {
- derivatives: true
- }
- } );
- THREE.Mesh.call( this, geometry, material );
- this.frustumCulled = false;
- };
- THREE.InfiniteGridHelper.prototype = {
- ...THREE.Mesh.prototype,
- ...THREE.Object3D.prototype,
- ...THREE.EventDispatcher.prototype
- };
- if ( parseInt( THREE.REVISION ) > 126 ) {
- class InfiniteGridHelper extends THREE.Mesh {
- constructor ( size1, size2, color, distance, axes = 'xzy' ) {
- color = color || new THREE.Color( 'white' );
- size1 = size1 || 10;
- size2 = size2 || 100;
- distance = distance || 8000;
- const planeAxes = axes.substr( 0, 2 );
- const geometry = new THREE.PlaneBufferGeometry( 2, 2, 1, 1 );
- const material = new THREE.ShaderMaterial( {
- side: THREE.DoubleSide,
- uniforms: {
- uSize1: {
- value: size1
- },
- uSize2: {
- value: size2
- },
- uColor: {
- value: color
- },
- uDistance: {
- value: distance
- }
- },
- transparent: true,
- vertexShader: `
-
- varying vec3 worldPosition;
-
- uniform float uDistance;
-
- void main() {
-
- vec3 pos = position.${axes} * uDistance;
- pos.${planeAxes} += cameraPosition.${planeAxes};
-
- worldPosition = pos;
-
- gl_Position = projectionMatrix * modelViewMatrix * vec4(pos, 1.0);
-
- }
- `,
- fragmentShader: `
-
- varying vec3 worldPosition;
-
- uniform float uSize1;
- uniform float uSize2;
- uniform vec3 uColor;
- uniform float uDistance;
-
-
-
- float getGrid(float size) {
-
- vec2 r = worldPosition.${planeAxes} / size;
-
-
- vec2 grid = abs(fract(r - 0.5) - 0.5) / fwidth(r);
- float line = min(grid.x, grid.y);
-
-
- return 1.0 - min(line, 1.0);
- }
-
- void main() {
-
-
- float d = 1.0 - min(distance(cameraPosition.${planeAxes}, worldPosition.${planeAxes}) / uDistance, 1.0);
-
- float g1 = getGrid(uSize1);
- float g2 = getGrid(uSize2);
-
-
- gl_FragColor = vec4(uColor.rgb, mix(g2, g1, g1) * pow(d, 3.0));
- gl_FragColor.a = mix(0.5 * gl_FragColor.a, gl_FragColor.a, g2);
-
- if ( gl_FragColor.a <= 0.0 ) discard;
-
-
- }
-
- `,
- extensions: {
- derivatives: true
- }
- } );
- super( geometry, material );
- this.frustumCulled = false;
- }
- }
-
- Object.assign( InfiniteGridHelper.prototype, THREE.InfiniteGridHelper.prototype );
- THREE.InfiniteGridHelper = InfiniteGridHelper;
- }
- module.exports.InfiniteGridHelper = THREE.InfiniteGridHelper
- // module.exports.InfiniteGridHelper =THREE.InfiniteGridHelper;
|