webgpu_shadowmap_array.html 12 KB

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