webgl_shadowmesh.html 9.8 KB

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