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