GroundProjectedEnv.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import { Mesh, IcosahedronGeometry, ShaderMaterial, DoubleSide } from 'three';
  2. /**
  3. * Ground projected env map adapted from @react-three/drei.
  4. * https://github.com/pmndrs/drei/blob/master/src/core/Environment.tsx
  5. */
  6. export class GroundProjectedEnv extends Mesh {
  7. constructor( texture, options ) {
  8. const isCubeMap = texture.isCubeTexture;
  9. const w =
  10. ( isCubeMap ? texture.image[ 0 ]?.width : texture.image.width ) ?? 1024;
  11. const cubeSize = w / 4;
  12. const _lodMax = Math.floor( Math.log2( cubeSize ) );
  13. const _cubeSize = Math.pow( 2, _lodMax );
  14. const width = 3 * Math.max( _cubeSize, 16 * 7 );
  15. const height = 4 * _cubeSize;
  16. const defines = [
  17. isCubeMap ? '#define ENVMAP_TYPE_CUBE' : '',
  18. `#define CUBEUV_TEXEL_WIDTH ${1.0 / width}`,
  19. `#define CUBEUV_TEXEL_HEIGHT ${1.0 / height}`,
  20. `#define CUBEUV_MAX_MIP ${_lodMax}.0`,
  21. ];
  22. const vertexShader = /* glsl */ `
  23. varying vec3 vWorldPosition;
  24. void main()
  25. {
  26. vec4 worldPosition = ( modelMatrix * vec4( position, 1.0 ) );
  27. vWorldPosition = worldPosition.xyz;
  28. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  29. }
  30. `;
  31. const fragmentShader = defines.join( '\n' ) + /* glsl */ `
  32. #define ENVMAP_TYPE_CUBE_UV
  33. varying vec3 vWorldPosition;
  34. uniform float radius;
  35. uniform float height;
  36. uniform float angle;
  37. #ifdef ENVMAP_TYPE_CUBE
  38. uniform samplerCube map;
  39. #else
  40. uniform sampler2D map;
  41. #endif
  42. // From: https://www.shadertoy.com/view/4tsBD7
  43. float diskIntersectWithBackFaceCulling( vec3 ro, vec3 rd, vec3 c, vec3 n, float r )
  44. {
  45. float d = dot ( rd, n );
  46. if( d > 0.0 ) { return 1e6; }
  47. vec3 o = ro - c;
  48. float t = - dot( n, o ) / d;
  49. vec3 q = o + rd * t;
  50. return ( dot( q, q ) < r * r ) ? t : 1e6;
  51. }
  52. // From: https://www.iquilezles.org/www/articles/intersectors/intersectors.htm
  53. float sphereIntersect( vec3 ro, vec3 rd, vec3 ce, float ra )
  54. {
  55. vec3 oc = ro - ce;
  56. float b = dot( oc, rd );
  57. float c = dot( oc, oc ) - ra * ra;
  58. float h = b * b - c;
  59. if( h < 0.0 ) { return -1.0; }
  60. h = sqrt( h );
  61. return - b + h;
  62. }
  63. vec3 project()
  64. {
  65. vec3 p = normalize( vWorldPosition );
  66. vec3 camPos = cameraPosition;
  67. camPos.y -= height;
  68. float intersection = sphereIntersect( camPos, p, vec3( 0.0 ), radius );
  69. if( intersection > 0.0 ) {
  70. vec3 h = vec3( 0.0, - height, 0.0 );
  71. float intersection2 = diskIntersectWithBackFaceCulling( camPos, p, h, vec3( 0.0, 1.0, 0.0 ), radius );
  72. p = ( camPos + min( intersection, intersection2 ) * p ) / radius;
  73. } else {
  74. p = vec3( 0.0, 1.0, 0.0 );
  75. }
  76. return p;
  77. }
  78. #include <common>
  79. #include <cube_uv_reflection_fragment>
  80. void main()
  81. {
  82. vec3 projectedWorldPosition = project();
  83. #ifdef ENVMAP_TYPE_CUBE
  84. vec3 outcolor = textureCube( map, projectedWorldPosition ).rgb;
  85. #else
  86. vec3 direction = normalize( projectedWorldPosition );
  87. vec2 uv = equirectUv( direction );
  88. vec3 outcolor = texture2D( map, uv ).rgb;
  89. #endif
  90. gl_FragColor = vec4( outcolor, 1.0 );
  91. #include <tonemapping_fragment>
  92. #include <encodings_fragment>
  93. }
  94. `;
  95. const uniforms = {
  96. map: { value: texture },
  97. height: { value: options?.height || 15 },
  98. radius: { value: options?.radius || 100 },
  99. };
  100. const geometry = new IcosahedronGeometry( 1, 16 );
  101. const material = new ShaderMaterial( {
  102. uniforms,
  103. fragmentShader,
  104. vertexShader,
  105. side: DoubleSide,
  106. } );
  107. super( geometry, material );
  108. }
  109. set radius( radius ) {
  110. this.material.uniforms.radius.value = radius;
  111. }
  112. get radius() {
  113. return this.material.uniforms.radius.value;
  114. }
  115. set height( height ) {
  116. this.material.uniforms.height.value = height;
  117. }
  118. get height() {
  119. return this.material.uniforms.height.value;
  120. }
  121. }