shadows-fake.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - Shadows - Fake</title>
  8. <style>
  9. html, body {
  10. margin: 0;
  11. height: 100%;
  12. }
  13. #c {
  14. width: 100%;
  15. height: 100%;
  16. display: block;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <canvas id="c"></canvas>
  22. </body>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../../build/three.module.js"
  27. }
  28. }
  29. </script>
  30. <script type="module">
  31. import * as THREE from 'three';
  32. function main() {
  33. const canvas = document.querySelector( '#c' );
  34. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas } );
  35. const fov = 45;
  36. const aspect = 2; // the canvas default
  37. const near = 0.1;
  38. const far = 100;
  39. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  40. camera.position.set( 0, 10, 20 );
  41. camera.lookAt( 0, 0, 0 );
  42. const scene = new THREE.Scene();
  43. scene.background = new THREE.Color( 'white' );
  44. const loader = new THREE.TextureLoader();
  45. {
  46. const planeSize = 40;
  47. const texture = loader.load( 'resources/images/checker.png' );
  48. texture.wrapS = THREE.RepeatWrapping;
  49. texture.wrapT = THREE.RepeatWrapping;
  50. texture.magFilter = THREE.NearestFilter;
  51. texture.colorSpace = THREE.SRGBColorSpace;
  52. const repeats = planeSize / 2;
  53. texture.repeat.set( repeats, repeats );
  54. const planeGeo = new THREE.PlaneGeometry( planeSize, planeSize );
  55. const planeMat = new THREE.MeshBasicMaterial( {
  56. map: texture,
  57. side: THREE.DoubleSide,
  58. } );
  59. planeMat.color.setRGB( 1.5, 1.5, 1.5 );
  60. const mesh = new THREE.Mesh( planeGeo, planeMat );
  61. mesh.rotation.x = Math.PI * - .5;
  62. scene.add( mesh );
  63. }
  64. const shadowTexture = loader.load( 'resources/images/roundshadow.png' );
  65. const sphereShadowBases = [];
  66. {
  67. const sphereRadius = 1;
  68. const sphereWidthDivisions = 32;
  69. const sphereHeightDivisions = 16;
  70. const sphereGeo = new THREE.SphereGeometry( sphereRadius, sphereWidthDivisions, sphereHeightDivisions );
  71. const planeSize = 1;
  72. const shadowGeo = new THREE.PlaneGeometry( planeSize, planeSize );
  73. const numSpheres = 15;
  74. for ( let i = 0; i < numSpheres; ++ i ) {
  75. // make a base for the shadow and the sphere.
  76. // so they move together.
  77. const base = new THREE.Object3D();
  78. scene.add( base );
  79. // add the shadow to the base
  80. // note: we make a new material for each sphere
  81. // so we can set that sphere's material transparency
  82. // separately.
  83. const shadowMat = new THREE.MeshBasicMaterial( {
  84. map: shadowTexture,
  85. transparent: true, // so we can see the ground
  86. depthWrite: false, // so we don't have to sort
  87. } );
  88. const shadowMesh = new THREE.Mesh( shadowGeo, shadowMat );
  89. shadowMesh.position.y = 0.001; // so we're above the ground slightly
  90. shadowMesh.rotation.x = Math.PI * - .5;
  91. const shadowSize = sphereRadius * 4;
  92. shadowMesh.scale.set( shadowSize, shadowSize, shadowSize );
  93. base.add( shadowMesh );
  94. // add the sphere to the base
  95. const u = i / numSpheres;
  96. const sphereMat = new THREE.MeshPhongMaterial();
  97. sphereMat.color.setHSL( u, 1, .75 );
  98. const sphereMesh = new THREE.Mesh( sphereGeo, sphereMat );
  99. sphereMesh.position.set( 0, sphereRadius + 2, 0 );
  100. base.add( sphereMesh );
  101. // remember all 3 plus the y position
  102. sphereShadowBases.push( { base, sphereMesh, shadowMesh, y: sphereMesh.position.y } );
  103. }
  104. }
  105. {
  106. const skyColor = 0xB1E1FF; // light blue
  107. const groundColor = 0xB97A20; // brownish orange
  108. const intensity = 0.75;
  109. const light = new THREE.HemisphereLight( skyColor, groundColor, intensity );
  110. scene.add( light );
  111. }
  112. {
  113. const color = 0xFFFFFF;
  114. const intensity = 2.5;
  115. const light = new THREE.DirectionalLight( color, intensity );
  116. light.position.set( 0, 10, 5 );
  117. light.target.position.set( - 5, 0, 0 );
  118. scene.add( light );
  119. scene.add( light.target );
  120. }
  121. function resizeRendererToDisplaySize( renderer ) {
  122. const canvas = renderer.domElement;
  123. const width = canvas.clientWidth;
  124. const height = canvas.clientHeight;
  125. const needResize = canvas.width !== width || canvas.height !== height;
  126. if ( needResize ) {
  127. renderer.setSize( width, height, false );
  128. }
  129. return needResize;
  130. }
  131. function render( time ) {
  132. time *= 0.001; // convert to seconds
  133. resizeRendererToDisplaySize( renderer );
  134. {
  135. const canvas = renderer.domElement;
  136. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  137. camera.updateProjectionMatrix();
  138. }
  139. sphereShadowBases.forEach( ( sphereShadowBase, ndx ) => {
  140. const { base, sphereMesh, shadowMesh, y } = sphereShadowBase;
  141. // u is a value that goes from 0 to 1 as we iterate the spheres
  142. const u = ndx / sphereShadowBases.length;
  143. // compute a position for there base. This will move
  144. // both the sphere and its shadow
  145. const speed = time * .2;
  146. const angle = speed + u * Math.PI * 2 * ( ndx % 1 ? 1 : - 1 );
  147. const radius = Math.sin( speed - ndx ) * 10;
  148. base.position.set( Math.cos( angle ) * radius, 0, Math.sin( angle ) * radius );
  149. // yOff is a value that goes from 0 to 1
  150. const yOff = Math.abs( Math.sin( time * 2 + ndx ) );
  151. // move the sphere up and down
  152. sphereMesh.position.y = y + THREE.MathUtils.lerp( - 2, 2, yOff );
  153. // fade the shadow as the sphere goes up
  154. shadowMesh.material.opacity = THREE.MathUtils.lerp( 1, .25, yOff );
  155. } );
  156. renderer.render( scene, camera );
  157. requestAnimationFrame( render );
  158. }
  159. requestAnimationFrame( render );
  160. }
  161. main();
  162. </script>
  163. </html>