webgpu_shadowmap_array.html 12 KB

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