shadows-fake.html 5.5 KB

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