GroundProjectedEnv.js 4.4 KB

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