webgl_shadowmesh.html 9.9 KB

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