shadows-fake.html 5.8 KB

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