webgl_shadowmesh.html 9.7 KB

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