1
0

webgpu_shadowmap_array.html 12 KB

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