webgl_shadowmesh.html 9.7 KB

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