webgl_shadowmesh.html 10 KB

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