webgpu_shadowmap_pointlight.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - point light shadow-map</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 webgpu - PointLight ShadowMap">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_shadowmap_pointlight.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_shadowmap_pointlight.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  16. <div class="title-wrapper">
  17. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>PointLight ShadowMap</span>
  18. </div>
  19. <small>Animated point light shadows.</small>
  20. </div>
  21. <script type="importmap">
  22. {
  23. "imports": {
  24. "three": "../build/three.webgpu.js",
  25. "three/webgpu": "../build/three.webgpu.js",
  26. "three/tsl": "../build/three.tsl.js",
  27. "three/addons/": "./jsm/"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three';
  33. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  34. import { Inspector } from 'three/addons/inspector/Inspector.js';
  35. let camera, scene, renderer;
  36. let pointLight, pointLight2;
  37. init();
  38. async function init() {
  39. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  40. camera.position.set( 0, 10, 40 );
  41. scene = new THREE.Scene();
  42. scene.add( new THREE.AmbientLight( 0x111122, 3 ) );
  43. // lights
  44. function createLight( color ) {
  45. const intensity = 200;
  46. const light = new THREE.PointLight( color, intensity, 20 );
  47. light.castShadow = true;
  48. light.shadow.bias = - 0.005; // reduces self-shadowing on double-sided objects
  49. light.shadow.mapSize.width = 128;
  50. light.shadow.radius = 10;
  51. let geometry = new THREE.SphereGeometry( 0.3, 12, 6 );
  52. let material = new THREE.MeshBasicMaterial( { color: color } );
  53. material.color.multiplyScalar( intensity );
  54. let sphere = new THREE.Mesh( geometry, material );
  55. light.add( sphere );
  56. const texture = new THREE.CanvasTexture( generateTexture() );
  57. texture.magFilter = THREE.NearestFilter;
  58. texture.wrapT = THREE.RepeatWrapping;
  59. texture.wrapS = THREE.RepeatWrapping;
  60. texture.repeat.set( 1, 4.5 );
  61. geometry = new THREE.SphereGeometry( 2, 32, 8 );
  62. material = new THREE.MeshPhongNodeMaterial( {
  63. side: THREE.DoubleSide,
  64. alphaMap: texture,
  65. alphaTest: 0.5
  66. } );
  67. sphere = new THREE.Mesh( geometry, material );
  68. sphere.castShadow = true;
  69. sphere.receiveShadow = true;
  70. light.add( sphere );
  71. return light;
  72. }
  73. pointLight = createLight( 0x0088ff );
  74. scene.add( pointLight );
  75. pointLight2 = createLight( 0xff8888 );
  76. scene.add( pointLight2 );
  77. //
  78. const geometry = new THREE.BoxGeometry( 30, 30, 30 );
  79. const material = new THREE.MeshPhongNodeMaterial( {
  80. color: 0xa0adaf,
  81. shininess: 10,
  82. specular: 0x111111,
  83. side: THREE.BackSide
  84. } );
  85. const mesh = new THREE.Mesh( geometry, material );
  86. mesh.position.y = 10;
  87. mesh.receiveShadow = true;
  88. scene.add( mesh );
  89. //
  90. renderer = new THREE.WebGPURenderer( { antialias: true } );
  91. renderer.setPixelRatio( window.devicePixelRatio );
  92. renderer.setSize( window.innerWidth, window.innerHeight );
  93. renderer.setAnimationLoop( animate );
  94. renderer.shadowMap.enabled = true;
  95. // renderer.shadowMap.type = THREE.BasicShadowMap;
  96. renderer.inspector = new Inspector();
  97. document.body.appendChild( renderer.domElement );
  98. await renderer.init();
  99. const controls = new OrbitControls( camera, renderer.domElement );
  100. controls.target.set( 0, 10, 0 );
  101. controls.update();
  102. //
  103. window.addEventListener( 'resize', onWindowResize );
  104. }
  105. function onWindowResize() {
  106. camera.aspect = window.innerWidth / window.innerHeight;
  107. camera.updateProjectionMatrix();
  108. renderer.setSize( window.innerWidth, window.innerHeight );
  109. }
  110. function generateTexture() {
  111. const canvas = document.createElement( 'canvas' );
  112. canvas.width = 2;
  113. canvas.height = 2;
  114. const context = canvas.getContext( '2d' );
  115. context.fillStyle = 'white';
  116. context.fillRect( 0, 1, 2, 1 );
  117. return canvas;
  118. }
  119. function animate() {
  120. let time = performance.now() * 0.001;
  121. pointLight.position.x = Math.sin( time * 0.6 ) * 9;
  122. pointLight.position.y = Math.sin( time * 0.7 ) * 9 + 6;
  123. pointLight.position.z = Math.sin( time * 0.8 ) * 9;
  124. pointLight.rotation.x = time;
  125. pointLight.rotation.z = time;
  126. time += 10000;
  127. pointLight2.position.x = Math.sin( time * 0.6 ) * 9;
  128. pointLight2.position.y = Math.sin( time * 0.7 ) * 9 + 6;
  129. pointLight2.position.z = Math.sin( time * 0.8 ) * 9;
  130. pointLight2.rotation.x = time;
  131. pointLight2.rotation.z = time;
  132. renderer.render( scene, camera );
  133. }
  134. </script>
  135. </body>
  136. </html>
粤ICP备19079148号