threejs-shadows-fake.html 5.6 KB

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