webgl_shadowmesh.html 10 KB

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