2
0

GroundProjectedEnv.js 3.9 KB

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