webgl_shadowmesh.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title> three.js webgl - ShadowMesh </title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - shadow mesh<br />
  12. <input id="lightButton" type="button" value="Switch to PointLight">
  13. </div>
  14. <div id="container"></div>
  15. <!-- Import maps polyfill -->
  16. <!-- Remove this when import maps will be widely supported -->
  17. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.module.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import { ShadowMesh } from 'three/addons/objects/ShadowMesh.js';
  29. let SCREEN_WIDTH = window.innerWidth;
  30. let SCREEN_HEIGHT = window.innerHeight;
  31. const scene = new THREE.Scene();
  32. const camera = new THREE.PerspectiveCamera( 55, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 3000 );
  33. const clock = new THREE.Clock();
  34. const renderer = new THREE.WebGLRenderer();
  35. const sunLight = new THREE.DirectionalLight( 'rgb(255,255,255)', 3 );
  36. let useDirectionalLight = true;
  37. let arrowHelper1, arrowHelper2, arrowHelper3;
  38. const arrowDirection = new THREE.Vector3();
  39. const arrowPosition1 = new THREE.Vector3();
  40. const arrowPosition2 = new THREE.Vector3();
  41. const arrowPosition3 = new THREE.Vector3();
  42. let groundMesh;
  43. let lightSphere, lightHolder;
  44. let pyramid, pyramidShadow;
  45. let sphere, sphereShadow;
  46. let cube, cubeShadow;
  47. let cylinder, cylinderShadow;
  48. let torus, torusShadow;
  49. const normalVector = new THREE.Vector3( 0, 1, 0 );
  50. const planeConstant = 0.01; // this value must be slightly higher than the groundMesh's y position of 0.0
  51. const groundPlane = new THREE.Plane( normalVector, planeConstant );
  52. const lightPosition4D = new THREE.Vector4();
  53. let verticalAngle = 0;
  54. let horizontalAngle = 0;
  55. let frameTime = 0;
  56. const TWO_PI = Math.PI * 2;
  57. init();
  58. animate();
  59. function init() {
  60. scene.background = new THREE.Color( 0x0096ff );
  61. renderer.useLegacyLights = false;
  62. renderer.setPixelRatio( window.devicePixelRatio );
  63. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  64. document.getElementById( 'container' ).appendChild( renderer.domElement );
  65. window.addEventListener( 'resize', onWindowResize );
  66. camera.position.set( 0, 2.5, 10 );
  67. scene.add( camera );
  68. onWindowResize();
  69. sunLight.position.set( 5, 7, - 1 );
  70. sunLight.lookAt( scene.position );
  71. scene.add( sunLight );
  72. lightPosition4D.x = sunLight.position.x;
  73. lightPosition4D.y = sunLight.position.y;
  74. lightPosition4D.z = sunLight.position.z;
  75. // amount of light-ray divergence. Ranging from:
  76. // 0.001 = sunlight(min divergence) to 1.0 = pointlight(max divergence)
  77. lightPosition4D.w = 0.001; // must be slightly greater than 0, due to 0 causing matrixInverse errors
  78. // YELLOW ARROW HELPERS
  79. arrowDirection.subVectors( scene.position, sunLight.position ).normalize();
  80. arrowPosition1.copy( sunLight.position );
  81. arrowHelper1 = new THREE.ArrowHelper( arrowDirection, arrowPosition1, 0.9, 0xffff00, 0.25, 0.08 );
  82. scene.add( arrowHelper1 );
  83. arrowPosition2.copy( sunLight.position ).add( new THREE.Vector3( 0, 0.2, 0 ) );
  84. arrowHelper2 = new THREE.ArrowHelper( arrowDirection, arrowPosition2, 0.9, 0xffff00, 0.25, 0.08 );
  85. scene.add( arrowHelper2 );
  86. arrowPosition3.copy( sunLight.position ).add( new THREE.Vector3( 0, - 0.2, 0 ) );
  87. arrowHelper3 = new THREE.ArrowHelper( arrowDirection, arrowPosition3, 0.9, 0xffff00, 0.25, 0.08 );
  88. scene.add( arrowHelper3 );
  89. // LIGHTBULB
  90. const lightSphereGeometry = new THREE.SphereGeometry( 0.09 );
  91. const lightSphereMaterial = new THREE.MeshBasicMaterial( { color: 'rgb(255,255,255)' } );
  92. lightSphere = new THREE.Mesh( lightSphereGeometry, lightSphereMaterial );
  93. scene.add( lightSphere );
  94. lightSphere.visible = false;
  95. const lightHolderGeometry = new THREE.CylinderGeometry( 0.05, 0.05, 0.13 );
  96. const lightHolderMaterial = new THREE.MeshBasicMaterial( { color: 'rgb(75,75,75)' } );
  97. lightHolder = new THREE.Mesh( lightHolderGeometry, lightHolderMaterial );
  98. scene.add( lightHolder );
  99. lightHolder.visible = false;
  100. // GROUND
  101. const groundGeometry = new THREE.BoxGeometry( 30, 0.01, 40 );
  102. const groundMaterial = new THREE.MeshLambertMaterial( { color: 'rgb(0,130,0)' } );
  103. groundMesh = new THREE.Mesh( groundGeometry, groundMaterial );
  104. groundMesh.position.y = 0.0; //this value must be slightly lower than the planeConstant (0.01) parameter above
  105. scene.add( groundMesh );
  106. // RED CUBE and CUBE's SHADOW
  107. const cubeGeometry = new THREE.BoxGeometry( 1, 1, 1 );
  108. const cubeMaterial = new THREE.MeshLambertMaterial( { color: 'rgb(255,0,0)', emissive: 0x200000 } );
  109. cube = new THREE.Mesh( cubeGeometry, cubeMaterial );
  110. cube.position.z = - 1;
  111. scene.add( cube );
  112. cubeShadow = new ShadowMesh( cube );
  113. scene.add( cubeShadow );
  114. // BLUE CYLINDER and CYLINDER's SHADOW
  115. const cylinderGeometry = new THREE.CylinderGeometry( 0.3, 0.3, 2 );
  116. const cylinderMaterial = new THREE.MeshPhongMaterial( { color: 'rgb(0,0,255)', emissive: 0x000020 } );
  117. cylinder = new THREE.Mesh( cylinderGeometry, cylinderMaterial );
  118. cylinder.position.z = - 2.5;
  119. scene.add( cylinder );
  120. cylinderShadow = new ShadowMesh( cylinder );
  121. scene.add( cylinderShadow );
  122. // MAGENTA TORUS and TORUS' SHADOW
  123. const torusGeometry = new THREE.TorusGeometry( 1, 0.2, 10, 16, TWO_PI );
  124. const torusMaterial = new THREE.MeshPhongMaterial( { color: 'rgb(255,0,255)', emissive: 0x200020 } );
  125. torus = new THREE.Mesh( torusGeometry, torusMaterial );
  126. torus.position.z = - 6;
  127. scene.add( torus );
  128. torusShadow = new ShadowMesh( torus );
  129. scene.add( torusShadow );
  130. // WHITE SPHERE and SPHERE'S SHADOW
  131. const sphereGeometry = new THREE.SphereGeometry( 0.5, 20, 10 );
  132. const sphereMaterial = new THREE.MeshPhongMaterial( { color: 'rgb(255,255,255)', emissive: 0x222222 } );
  133. sphere = new THREE.Mesh( sphereGeometry, sphereMaterial );
  134. sphere.position.set( 4, 0.5, 2 );
  135. scene.add( sphere );
  136. sphereShadow = new ShadowMesh( sphere );
  137. scene.add( sphereShadow );
  138. // YELLOW PYRAMID and PYRAMID'S SHADOW
  139. const pyramidGeometry = new THREE.CylinderGeometry( 0, 0.5, 2, 4 );
  140. const pyramidMaterial = new THREE.MeshPhongMaterial( { color: 'rgb(255,255,0)', emissive: 0x440000, flatShading: true, shininess: 0 } );
  141. pyramid = new THREE.Mesh( pyramidGeometry, pyramidMaterial );
  142. pyramid.position.set( - 4, 1, 2 );
  143. scene.add( pyramid );
  144. pyramidShadow = new ShadowMesh( pyramid );
  145. scene.add( pyramidShadow );
  146. document.getElementById( 'lightButton' ).addEventListener( 'click', lightButtonHandler );
  147. }
  148. function animate() {
  149. requestAnimationFrame( animate );
  150. frameTime = clock.getDelta();
  151. cube.rotation.x += 1.0 * frameTime;
  152. cube.rotation.y += 1.0 * frameTime;
  153. cylinder.rotation.y += 1.0 * frameTime;
  154. cylinder.rotation.z -= 1.0 * frameTime;
  155. torus.rotation.x -= 1.0 * frameTime;
  156. torus.rotation.y -= 1.0 * frameTime;
  157. pyramid.rotation.y += 0.5 * frameTime;
  158. horizontalAngle += 0.5 * frameTime;
  159. if ( horizontalAngle > TWO_PI )
  160. horizontalAngle -= TWO_PI;
  161. cube.position.x = Math.sin( horizontalAngle ) * 4;
  162. cylinder.position.x = Math.sin( horizontalAngle ) * - 4;
  163. torus.position.x = Math.cos( horizontalAngle ) * 4;
  164. verticalAngle += 1.5 * frameTime;
  165. if ( verticalAngle > TWO_PI )
  166. verticalAngle -= TWO_PI;
  167. cube.position.y = Math.sin( verticalAngle ) * 2 + 2.9;
  168. cylinder.position.y = Math.sin( verticalAngle ) * 2 + 3.1;
  169. torus.position.y = Math.cos( verticalAngle ) * 2 + 3.3;
  170. // update the ShadowMeshes to follow their shadow-casting objects
  171. cubeShadow.update( groundPlane, lightPosition4D );
  172. cylinderShadow.update( groundPlane, lightPosition4D );
  173. torusShadow.update( groundPlane, lightPosition4D );
  174. sphereShadow.update( groundPlane, lightPosition4D );
  175. pyramidShadow.update( groundPlane, lightPosition4D );
  176. renderer.render( scene, camera );
  177. }
  178. function onWindowResize() {
  179. SCREEN_WIDTH = window.innerWidth;
  180. SCREEN_HEIGHT = window.innerHeight;
  181. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  182. camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  183. camera.updateProjectionMatrix();
  184. }
  185. function lightButtonHandler() {
  186. useDirectionalLight = ! useDirectionalLight;
  187. if ( useDirectionalLight ) {
  188. scene.background.setHex( 0x0096ff );
  189. groundMesh.material.color.setHex( 0x008200 );
  190. sunLight.position.set( 5, 7, - 1 );
  191. sunLight.lookAt( scene.position );
  192. lightPosition4D.x = sunLight.position.x;
  193. lightPosition4D.y = sunLight.position.y;
  194. lightPosition4D.z = sunLight.position.z;
  195. lightPosition4D.w = 0.001; // more of a directional Light value
  196. arrowHelper1.visible = true;
  197. arrowHelper2.visible = true;
  198. arrowHelper3.visible = true;
  199. lightSphere.visible = false;
  200. lightHolder.visible = false;
  201. document.getElementById( 'lightButton' ).value = 'Switch to PointLight';
  202. } else {
  203. scene.background.setHex( 0x000000 );
  204. groundMesh.material.color.setHex( 0x969696 );
  205. sunLight.position.set( 0, 6, - 2 );
  206. sunLight.lookAt( scene.position );
  207. lightSphere.position.copy( sunLight.position );
  208. lightHolder.position.copy( lightSphere.position );
  209. lightHolder.position.y += 0.12;
  210. lightPosition4D.x = sunLight.position.x;
  211. lightPosition4D.y = sunLight.position.y;
  212. lightPosition4D.z = sunLight.position.z;
  213. lightPosition4D.w = 0.9; // more of a point Light value
  214. arrowHelper1.visible = false;
  215. arrowHelper2.visible = false;
  216. arrowHelper3.visible = false;
  217. lightSphere.visible = true;
  218. lightHolder.visible = true;
  219. document.getElementById( 'lightButton' ).value = 'Switch to THREE.DirectionalLight';
  220. }
  221. }
  222. </script>
  223. </body>
  224. </html>