webgl_shadowmesh.html 9.7 KB

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