webgl_shadowmesh.html 10 KB

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