webgl_shadowmesh.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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.setPixelRatio( window.devicePixelRatio );
  62. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  63. document.getElementById( 'container' ).appendChild( renderer.domElement );
  64. window.addEventListener( 'resize', onWindowResize );
  65. camera.position.set( 0, 2.5, 10 );
  66. scene.add( camera );
  67. onWindowResize();
  68. sunLight.position.set( 5, 7, - 1 );
  69. sunLight.lookAt( scene.position );
  70. scene.add( sunLight );
  71. lightPosition4D.x = sunLight.position.x;
  72. lightPosition4D.y = sunLight.position.y;
  73. lightPosition4D.z = sunLight.position.z;
  74. // amount of light-ray divergence. Ranging from:
  75. // 0.001 = sunlight(min divergence) to 1.0 = pointlight(max divergence)
  76. lightPosition4D.w = 0.001; // must be slightly greater than 0, due to 0 causing matrixInverse errors
  77. // YELLOW ARROW HELPERS
  78. arrowDirection.subVectors( scene.position, sunLight.position ).normalize();
  79. arrowPosition1.copy( sunLight.position );
  80. arrowHelper1 = new THREE.ArrowHelper( arrowDirection, arrowPosition1, 0.9, 0xffff00, 0.25, 0.08 );
  81. scene.add( arrowHelper1 );
  82. arrowPosition2.copy( sunLight.position ).add( new THREE.Vector3( 0, 0.2, 0 ) );
  83. arrowHelper2 = new THREE.ArrowHelper( arrowDirection, arrowPosition2, 0.9, 0xffff00, 0.25, 0.08 );
  84. scene.add( arrowHelper2 );
  85. arrowPosition3.copy( sunLight.position ).add( new THREE.Vector3( 0, - 0.2, 0 ) );
  86. arrowHelper3 = new THREE.ArrowHelper( arrowDirection, arrowPosition3, 0.9, 0xffff00, 0.25, 0.08 );
  87. scene.add( arrowHelper3 );
  88. // LIGHTBULB
  89. const lightSphereGeometry = new THREE.SphereGeometry( 0.09 );
  90. const lightSphereMaterial = new THREE.MeshBasicMaterial( { color: 'rgb(255,255,255)' } );
  91. lightSphere = new THREE.Mesh( lightSphereGeometry, lightSphereMaterial );
  92. scene.add( lightSphere );
  93. lightSphere.visible = false;
  94. const lightHolderGeometry = new THREE.CylinderGeometry( 0.05, 0.05, 0.13 );
  95. const lightHolderMaterial = new THREE.MeshBasicMaterial( { color: 'rgb(75,75,75)' } );
  96. lightHolder = new THREE.Mesh( lightHolderGeometry, lightHolderMaterial );
  97. scene.add( lightHolder );
  98. lightHolder.visible = false;
  99. // GROUND
  100. const groundGeometry = new THREE.BoxGeometry( 30, 0.01, 40 );
  101. const groundMaterial = new THREE.MeshLambertMaterial( { color: 'rgb(0,130,0)' } );
  102. groundMesh = new THREE.Mesh( groundGeometry, groundMaterial );
  103. groundMesh.position.y = 0.0; //this value must be slightly lower than the planeConstant (0.01) parameter above
  104. scene.add( groundMesh );
  105. // RED CUBE and CUBE's SHADOW
  106. const cubeGeometry = new THREE.BoxGeometry( 1, 1, 1 );
  107. const cubeMaterial = new THREE.MeshLambertMaterial( { color: 'rgb(255,0,0)', emissive: 0x200000 } );
  108. cube = new THREE.Mesh( cubeGeometry, cubeMaterial );
  109. cube.position.z = - 1;
  110. scene.add( cube );
  111. cubeShadow = new ShadowMesh( cube );
  112. scene.add( cubeShadow );
  113. // BLUE CYLINDER and CYLINDER's SHADOW
  114. const cylinderGeometry = new THREE.CylinderGeometry( 0.3, 0.3, 2 );
  115. const cylinderMaterial = new THREE.MeshPhongMaterial( { color: 'rgb(0,0,255)', emissive: 0x000020 } );
  116. cylinder = new THREE.Mesh( cylinderGeometry, cylinderMaterial );
  117. cylinder.position.z = - 2.5;
  118. scene.add( cylinder );
  119. cylinderShadow = new ShadowMesh( cylinder );
  120. scene.add( cylinderShadow );
  121. // MAGENTA TORUS and TORUS' SHADOW
  122. const torusGeometry = new THREE.TorusGeometry( 1, 0.2, 10, 16, TWO_PI );
  123. const torusMaterial = new THREE.MeshPhongMaterial( { color: 'rgb(255,0,255)', emissive: 0x200020 } );
  124. torus = new THREE.Mesh( torusGeometry, torusMaterial );
  125. torus.position.z = - 6;
  126. scene.add( torus );
  127. torusShadow = new ShadowMesh( torus );
  128. scene.add( torusShadow );
  129. // WHITE SPHERE and SPHERE'S SHADOW
  130. const sphereGeometry = new THREE.SphereGeometry( 0.5, 20, 10 );
  131. const sphereMaterial = new THREE.MeshPhongMaterial( { color: 'rgb(255,255,255)', emissive: 0x222222 } );
  132. sphere = new THREE.Mesh( sphereGeometry, sphereMaterial );
  133. sphere.position.set( 4, 0.5, 2 );
  134. scene.add( sphere );
  135. sphereShadow = new ShadowMesh( sphere );
  136. scene.add( sphereShadow );
  137. // YELLOW PYRAMID and PYRAMID'S SHADOW
  138. const pyramidGeometry = new THREE.CylinderGeometry( 0, 0.5, 2, 4 );
  139. const pyramidMaterial = new THREE.MeshPhongMaterial( { color: 'rgb(255,255,0)', emissive: 0x440000, flatShading: true, shininess: 0 } );
  140. pyramid = new THREE.Mesh( pyramidGeometry, pyramidMaterial );
  141. pyramid.position.set( - 4, 1, 2 );
  142. scene.add( pyramid );
  143. pyramidShadow = new ShadowMesh( pyramid );
  144. scene.add( pyramidShadow );
  145. document.getElementById( 'lightButton' ).addEventListener( 'click', lightButtonHandler );
  146. }
  147. function animate() {
  148. requestAnimationFrame( animate );
  149. frameTime = clock.getDelta();
  150. cube.rotation.x += 1.0 * frameTime;
  151. cube.rotation.y += 1.0 * frameTime;
  152. cylinder.rotation.y += 1.0 * frameTime;
  153. cylinder.rotation.z -= 1.0 * frameTime;
  154. torus.rotation.x -= 1.0 * frameTime;
  155. torus.rotation.y -= 1.0 * frameTime;
  156. pyramid.rotation.y += 0.5 * frameTime;
  157. horizontalAngle += 0.5 * frameTime;
  158. if ( horizontalAngle > TWO_PI )
  159. horizontalAngle -= TWO_PI;
  160. cube.position.x = Math.sin( horizontalAngle ) * 4;
  161. cylinder.position.x = Math.sin( horizontalAngle ) * - 4;
  162. torus.position.x = Math.cos( horizontalAngle ) * 4;
  163. verticalAngle += 1.5 * frameTime;
  164. if ( verticalAngle > TWO_PI )
  165. verticalAngle -= TWO_PI;
  166. cube.position.y = Math.sin( verticalAngle ) * 2 + 2.9;
  167. cylinder.position.y = Math.sin( verticalAngle ) * 2 + 3.1;
  168. torus.position.y = Math.cos( verticalAngle ) * 2 + 3.3;
  169. // update the ShadowMeshes to follow their shadow-casting objects
  170. cubeShadow.update( groundPlane, lightPosition4D );
  171. cylinderShadow.update( groundPlane, lightPosition4D );
  172. torusShadow.update( groundPlane, lightPosition4D );
  173. sphereShadow.update( groundPlane, lightPosition4D );
  174. pyramidShadow.update( groundPlane, lightPosition4D );
  175. renderer.render( scene, camera );
  176. }
  177. function onWindowResize() {
  178. SCREEN_WIDTH = window.innerWidth;
  179. SCREEN_HEIGHT = window.innerHeight;
  180. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  181. camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  182. camera.updateProjectionMatrix();
  183. }
  184. function lightButtonHandler() {
  185. useDirectionalLight = ! useDirectionalLight;
  186. if ( useDirectionalLight ) {
  187. scene.background.setHex( 0x0096ff );
  188. groundMesh.material.color.setHex( 0x008200 );
  189. sunLight.position.set( 5, 7, - 1 );
  190. sunLight.lookAt( scene.position );
  191. lightPosition4D.x = sunLight.position.x;
  192. lightPosition4D.y = sunLight.position.y;
  193. lightPosition4D.z = sunLight.position.z;
  194. lightPosition4D.w = 0.001; // more of a directional Light value
  195. arrowHelper1.visible = true;
  196. arrowHelper2.visible = true;
  197. arrowHelper3.visible = true;
  198. lightSphere.visible = false;
  199. lightHolder.visible = false;
  200. document.getElementById( 'lightButton' ).value = 'Switch to PointLight';
  201. } else {
  202. scene.background.setHex( 0x000000 );
  203. groundMesh.material.color.setHex( 0x969696 );
  204. sunLight.position.set( 0, 6, - 2 );
  205. sunLight.lookAt( scene.position );
  206. lightSphere.position.copy( sunLight.position );
  207. lightHolder.position.copy( lightSphere.position );
  208. lightHolder.position.y += 0.12;
  209. lightPosition4D.x = sunLight.position.x;
  210. lightPosition4D.y = sunLight.position.y;
  211. lightPosition4D.z = sunLight.position.z;
  212. lightPosition4D.w = 0.9; // more of a point Light value
  213. arrowHelper1.visible = false;
  214. arrowHelper2.visible = false;
  215. arrowHelper3.visible = false;
  216. lightSphere.visible = true;
  217. lightHolder.visible = true;
  218. document.getElementById( 'lightButton' ).value = 'Switch to THREE.DirectionalLight';
  219. }
  220. }
  221. </script>
  222. </body>
  223. </html>