1
0

webgpu_shadowmap_array.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - shadow map array tile demo</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 - shadow map array tile demo">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_shadowmap_array.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_shadowmap_array.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="info" class="invert">
  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>Shadow Map Array</span>
  18. </div>
  19. <small>
  20. Tile shadow using shadow map array demonstration
  21. </small>
  22. </div>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../build/three.webgpu.js",
  27. "three/webgpu": "../build/three.webgpu.js",
  28. "three/tsl": "../build/three.tsl.js",
  29. "three/addons/": "./jsm/"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three/webgpu';
  35. import { mx_fractal_noise_vec3, positionWorld, Fn, color } from 'three/tsl';
  36. import { TileShadowNode } from 'three/addons/tsl/shadows/TileShadowNode.js';
  37. import { TileShadowNodeHelper } from 'three/addons/tsl/shadows/TileShadowNodeHelper.js';
  38. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  39. import { Inspector } from 'three/addons/inspector/Inspector.js';
  40. let camera, scene, renderer, timer;
  41. let dirLight;
  42. let torusKnot, dirGroup;
  43. let tsmHelper;
  44. init();
  45. async function init() {
  46. // Renderer setup
  47. renderer = new THREE.WebGPURenderer( { antialias: true } );
  48. renderer.setPixelRatio( window.devicePixelRatio );
  49. renderer.setSize( window.innerWidth, window.innerHeight );
  50. renderer.setAnimationLoop( animate );
  51. renderer.inspector = new Inspector();
  52. renderer.shadowMap.enabled = true;
  53. renderer.shadowMap.type = THREE.BasicShadowMap;
  54. // renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  55. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  56. renderer.toneMappingExposure = 1.2;
  57. document.body.appendChild( renderer.domElement );
  58. await renderer.init();
  59. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  60. camera.position.set( 45, 60, 100 );
  61. scene = new THREE.Scene();
  62. scene.backgroundNode = color( 0xCCCCFF ); // Brighter blue sky
  63. scene.fog = new THREE.Fog( 0xCCCCFF, 700, 1000 );
  64. // Enhanced lighting for a brighter scene
  65. scene.add( new THREE.AmbientLight( 0xCCCCFF, 3 ) );
  66. // Main directional light (sun)
  67. dirLight = new THREE.DirectionalLight( 0xFFFFAA, 5 );
  68. dirLight.position.set( 0, 80, 30 );
  69. dirLight.castShadow = true;
  70. dirLight.shadow.camera.near = 1;
  71. dirLight.shadow.camera.far = 200;
  72. dirLight.shadow.camera.right = 180;
  73. dirLight.shadow.camera.left = - 180;
  74. dirLight.shadow.camera.top = 180;
  75. dirLight.shadow.camera.bottom = - 160;
  76. dirLight.shadow.mapSize.width = 1024 * 4;
  77. dirLight.shadow.mapSize.height = 1024 * 4;
  78. dirLight.shadow.radius = 1;
  79. // Set up the tile shadow mapping
  80. const tsm = new TileShadowNode( dirLight, {
  81. tilesX: 2,
  82. tilesY: 2
  83. } );
  84. dirLight.shadow.shadowNode = tsm;
  85. scene.add( dirLight );
  86. tsmHelper = new TileShadowNodeHelper( tsm );
  87. scene.add( tsmHelper );
  88. dirGroup = new THREE.Group();
  89. dirGroup.add( dirLight );
  90. scene.add( dirGroup );
  91. // Create the ground with enhanced texture
  92. const planeGeometry = new THREE.PlaneGeometry( 1500, 1500, 2, 2 );
  93. const planeMaterial = new THREE.MeshPhongMaterial( {
  94. color: 0x88AA44,
  95. shininess: 5,
  96. specular: 0x222222
  97. } );
  98. planeMaterial.colorNode = Fn( () => {
  99. const noise = mx_fractal_noise_vec3( positionWorld.mul( 0.05 ) ).saturate();
  100. // Mix of greens and browns for a more natural ground
  101. const green = color( 0.4, 0.7, 0.3 );
  102. const brown = color( 0.6, 0.5, 0.3 );
  103. return noise.x.mix( green, brown );
  104. } )();
  105. const ground = new THREE.Mesh( planeGeometry, planeMaterial );
  106. ground.rotation.x = - Math.PI / 2;
  107. ground.receiveShadow = true;
  108. scene.add( ground );
  109. // Spread various objects across the scene
  110. createScenery();
  111. // Camera controls
  112. const controls = new OrbitControls( camera, renderer.domElement );
  113. controls.target.set( 0, 5, 0 );
  114. controls.minDistance = 0.01;
  115. controls.maxDistance = 400;
  116. controls.maxPolarAngle = Math.PI / 2 - 0.1; // Prevent camera from going below ground
  117. controls.update();
  118. timer = new THREE.Timer();
  119. timer.connect( document );
  120. window.addEventListener( 'resize', resize );
  121. }
  122. function createScenery() {
  123. // 1. Columns using instanced mesh
  124. const columnGeometry = new THREE.CylinderGeometry( 0.8, 1, 1, 16 );
  125. const columnMaterial = new THREE.MeshPhongMaterial( {
  126. color: 0xDDDDDD,
  127. shininess: 20
  128. } );
  129. const columnPositions = [];
  130. const columnScales = [];
  131. for ( let x = - 100; x <= 100; x += 40 ) {
  132. for ( let z = - 100; z <= 100; z += 40 ) {
  133. if ( Math.random() > 0.3 ) {
  134. const height = 5 + Math.random() * 10;
  135. const posX = x + ( Math.random() * 10 - 5 );
  136. const posY = height / 2;
  137. const posZ = z + ( Math.random() * 10 - 5 );
  138. columnPositions.push( posX, posY, posZ );
  139. columnScales.push( 1, height, 1 ); // Only scale Y to match height
  140. }
  141. }
  142. }
  143. const columnCount = columnPositions.length / 3;
  144. const columnInstancedMesh = new THREE.InstancedMesh(
  145. columnGeometry,
  146. columnMaterial,
  147. columnCount
  148. );
  149. const matrix = new THREE.Matrix4();
  150. for ( let i = 0; i < columnCount; i ++ ) {
  151. const x = columnPositions[ i * 3 ];
  152. const y = columnPositions[ i * 3 + 1 ];
  153. const z = columnPositions[ i * 3 + 2 ];
  154. const scaleY = columnScales[ i * 3 + 1 ];
  155. matrix.makeScale( 1, scaleY, 1 );
  156. matrix.setPosition( x, y, z );
  157. columnInstancedMesh.setMatrixAt( i, matrix );
  158. }
  159. columnInstancedMesh.castShadow = true;
  160. columnInstancedMesh.receiveShadow = true;
  161. scene.add( columnInstancedMesh );
  162. // 2. Add a central feature - the torus knot (kept as regular mesh for animation)
  163. const torusKnotGeometry = new THREE.TorusKnotGeometry( 25, 8, 100, 30 );
  164. const torusKnotMaterial = new THREE.MeshPhongNodeMaterial( {
  165. color: 0xFF6347, // Tomato color
  166. shininess: 30,
  167. } );
  168. torusKnot = new THREE.Mesh( torusKnotGeometry, torusKnotMaterial );
  169. torusKnot.scale.multiplyScalar( 1 / 18 );
  170. torusKnot.position.x = 5;
  171. torusKnot.position.y = 5;
  172. torusKnot.castShadow = true;
  173. torusKnot.receiveShadow = true;
  174. scene.add( torusKnot );
  175. // 3. Cubes using instanced mesh
  176. const cubeGeometry = new THREE.BoxGeometry( 3, 3, 3 );
  177. const cubeMaterials = [
  178. new THREE.MeshPhongMaterial( { color: 0x6699CC, shininess: 20 } ),
  179. new THREE.MeshPhongMaterial( { color: 0xCC6666, shininess: 20 } ),
  180. new THREE.MeshPhongMaterial( { color: 0xCCCC66, shininess: 20 } )
  181. ];
  182. const cubeCount = 10;
  183. const cubeInstances = cubeMaterials.map( material => {
  184. return new THREE.InstancedMesh( cubeGeometry, material, cubeCount );
  185. } );
  186. for ( let i = 0; i < 30; i ++ ) {
  187. const materialIndex = i % 3;
  188. const instanceIndex = Math.floor( i / 3 );
  189. const x = Math.random() * 300 - 150;
  190. const y = 1.5;
  191. const z = Math.random() * 300 - 150;
  192. const rotY = Math.random() * Math.PI * 2;
  193. matrix.makeRotationY( rotY );
  194. matrix.setPosition( x, y, z );
  195. cubeInstances[ materialIndex ].setMatrixAt( instanceIndex, matrix );
  196. }
  197. cubeInstances.forEach( instance => {
  198. instance.castShadow = true;
  199. instance.receiveShadow = true;
  200. scene.add( instance );
  201. } );
  202. // 4. Spheres using instanced mesh
  203. const sphereGeometry = new THREE.SphereGeometry( 2, 32, 32 );
  204. const sphereMaterial = new THREE.MeshPhongMaterial( {
  205. color: 0x88CCAA,
  206. shininess: 40
  207. } );
  208. const sphereCount = 25;
  209. const sphereInstancedMesh = new THREE.InstancedMesh(
  210. sphereGeometry,
  211. sphereMaterial,
  212. sphereCount
  213. );
  214. for ( let i = 0; i < sphereCount; i ++ ) {
  215. const x = Math.random() * 180 - 90;
  216. const y = 2;
  217. const z = Math.random() * 180 - 90;
  218. matrix.makeScale( 1, 1, 1 );
  219. matrix.setPosition( x, y, z );
  220. sphereInstancedMesh.setMatrixAt( i, matrix );
  221. }
  222. sphereInstancedMesh.castShadow = true;
  223. sphereInstancedMesh.receiveShadow = true;
  224. scene.add( sphereInstancedMesh );
  225. // 5. Trees using instanced mesh for trunks and tops separately
  226. const trunkGeometry = new THREE.CylinderGeometry( 0.5, 0.5, 2, 8 );
  227. const topGeometry = new THREE.ConeGeometry( 2, 8, 8 );
  228. const treeMaterial = new THREE.MeshPhongMaterial( {
  229. vertexColors: true,
  230. shininess: 5
  231. } );
  232. const treeCount = 40;
  233. const totalInstanceCount = treeCount * 2;
  234. const trunkVertexCount = trunkGeometry.attributes.position.count;
  235. const trunkIndexCount = trunkGeometry.index ? trunkGeometry.index.count : 0;
  236. const topVertexCount = topGeometry.attributes.position.count;
  237. const topIndexCount = topGeometry.index ? topGeometry.index.count : 0;
  238. const totalVertexCount = ( trunkVertexCount + topVertexCount ) * 2; // Multiple for safety
  239. const totalIndexCount = ( trunkIndexCount + topIndexCount ) * 2;
  240. const treeBatchedMesh = new THREE.BatchedMesh( totalInstanceCount, totalVertexCount, totalIndexCount, treeMaterial );
  241. treeBatchedMesh.castShadow = true;
  242. treeBatchedMesh.perObjectFrustumCulled = false;
  243. const trunkGeometryId = treeBatchedMesh.addGeometry( trunkGeometry );
  244. const topGeometryId = treeBatchedMesh.addGeometry( topGeometry );
  245. const trunkColor = new THREE.Color( 0x8B4513 );
  246. const topColor = new THREE.Color( 0x336633 );
  247. for ( let i = 0; i < treeCount; i ++ ) {
  248. const x = Math.random() * 300 - 150;
  249. const z = Math.random() * 300 - 150;
  250. const trunkId = treeBatchedMesh.addInstance( trunkGeometryId );
  251. matrix.makeScale( 1, 1, 1 );
  252. matrix.setPosition( x, 1, z );
  253. treeBatchedMesh.setMatrixAt( trunkId, matrix );
  254. treeBatchedMesh.setColorAt( trunkId, trunkColor );
  255. const topId = treeBatchedMesh.addInstance( topGeometryId );
  256. matrix.makeScale( 1, 1, 1 );
  257. matrix.setPosition( x, 6, z );
  258. treeBatchedMesh.setMatrixAt( topId, matrix );
  259. treeBatchedMesh.setColorAt( topId, topColor );
  260. }
  261. scene.add( treeBatchedMesh );
  262. // 6. Torus shapes using instanced mesh
  263. const torusGeometry = new THREE.TorusGeometry( 3, 1, 16, 50 );
  264. const torusMaterial = new THREE.MeshPhongMaterial( {
  265. color: 0xFF99CC,
  266. shininess: 30
  267. } );
  268. const torusCount = 15;
  269. const torusInstancedMesh = new THREE.InstancedMesh(
  270. torusGeometry,
  271. torusMaterial,
  272. torusCount
  273. );
  274. for ( let i = 0; i < torusCount; i ++ ) {
  275. const x = Math.random() * 320 - 160;
  276. const y = 2;
  277. const z = Math.random() * 320 - 160;
  278. const rotZ = Math.random() * Math.PI * 2;
  279. // Apply rotation (PI/2 on X-axis and random on Z-axis)
  280. matrix.makeRotationX( Math.PI / 2 );
  281. const rotMatrix = new THREE.Matrix4().makeRotationZ( rotZ );
  282. matrix.multiply( rotMatrix );
  283. matrix.setPosition( x, y, z );
  284. torusInstancedMesh.setMatrixAt( i, matrix );
  285. }
  286. torusInstancedMesh.castShadow = true;
  287. torusInstancedMesh.receiveShadow = true;
  288. scene.add( torusInstancedMesh );
  289. }
  290. function resize() {
  291. camera.aspect = window.innerWidth / window.innerHeight;
  292. camera.updateProjectionMatrix();
  293. renderer.setSize( window.innerWidth, window.innerHeight );
  294. }
  295. async function animate( time ) {
  296. timer.update();
  297. const delta = timer.getDelta();
  298. // Rotate the central torus knot
  299. torusKnot.rotation.x += 0.25 * delta;
  300. torusKnot.rotation.y += 0.5 * delta;
  301. torusKnot.rotation.z += 1 * delta;
  302. dirLight.position.x = Math.sin( time * 0.0001 ) * 30;
  303. dirLight.position.z = Math.cos( time * 0.0001 ) * 30;
  304. renderer.render( scene, camera );
  305. tsmHelper.update();
  306. }
  307. </script>
  308. </body>
  309. </html>
粤ICP备19079148号