webgpu_generator_building.html 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - building generator</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 - building generator">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_generator_building.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_generator_building.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>Building Generator</span>
  18. </div>
  19. <small>
  20. A single procedurally generated Neo-Gothic terracotta skyscraper at sunset. </small>
  21. </div>
  22. <script type="importmap">
  23. {
  24. "imports": {
  25. "three": "../build/three.webgpu.js",
  26. "three/webgpu": "../build/three.webgpu.js",
  27. "three/tsl": "../build/three.tsl.js",
  28. "three/addons/": "./jsm/"
  29. }
  30. }
  31. </script>
  32. <script type="module">
  33. import * as THREE from 'three/webgpu';
  34. import { uniform } from 'three/tsl';
  35. import { Inspector } from 'three/addons/inspector/Inspector.js';
  36. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  37. import { SkyMesh } from 'three/addons/objects/SkyMesh.js';
  38. import { SkyscraperGenerator, createSkyscraperMaterial, pickBuildingColor } from 'three/addons/generators/city/SkyscraperGenerator.js';
  39. let camera, scene, renderer, controls, generator, building, material;
  40. let sky, sunLight, pmremGenerator, envScene;
  41. // reused by updateSun(): the current sun direction and the key-light
  42. // colours it blends between as the sun nears the horizon or climbs high
  43. const sun = new THREE.Vector3();
  44. const sunHorizonColor = new THREE.Color( 0xffb072 );
  45. const sunMiddayColor = new THREE.Color( 0xfff4e8 );
  46. // every building takes one flat masonry colour; a uniform lets the seed
  47. // repick it without recompiling the shared material
  48. const baseColor = uniform( new THREE.Color( 0xc6c0b2 ) );
  49. const parameters = {
  50. seed: 7,
  51. height: 100,
  52. width: 34,
  53. depth: 28,
  54. floorHeight: 4,
  55. bayWidth: 2.6,
  56. chamfer: 5,
  57. setback: 1.5,
  58. timeOfDay: 17 // hours: 6 sunrise, 12 noon, 18 sunset
  59. };
  60. init();
  61. async function init() {
  62. renderer = new THREE.WebGPURenderer( { antialias: true } );
  63. renderer.setPixelRatio( window.devicePixelRatio );
  64. renderer.setSize( window.innerWidth, window.innerHeight );
  65. renderer.setAnimationLoop( animate );
  66. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  67. renderer.toneMappingExposure = 0.25;
  68. renderer.shadowMap.enabled = true;
  69. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  70. renderer.inspector = new Inspector();
  71. document.body.appendChild( renderer.domElement );
  72. await renderer.init();
  73. pmremGenerator = new THREE.PMREMGenerator( renderer );
  74. scene = new THREE.Scene();
  75. scene.environmentIntensity = 0.25; // sky as a soft fill; the sun is the key
  76. // a physical sky drives both the backdrop and the image-based lighting;
  77. // updateSun() positions the sun and bakes it into the IBL environment
  78. sky = new SkyMesh();
  79. sky.scale.setScalar( 10000 );
  80. sky.turbidity.value = 8;
  81. sky.rayleigh.value = 3;
  82. sky.mieCoefficient.value = 0.008;
  83. sky.mieDirectionalG.value = 0.88;
  84. // the sky is baked into the environment map from this scene, then moved
  85. // to the main scene as the backdrop ( see updateSun )
  86. envScene = new THREE.Scene();
  87. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 20000 );
  88. camera.position.set( 120, 95, 155 );
  89. controls = new OrbitControls( camera, renderer.domElement );
  90. controls.enableDamping = true;
  91. controls.target.set( 0, 58, 0 );
  92. controls.maxPolarAngle = Math.PI * 0.495;
  93. controls.minDistance = 30;
  94. controls.maxDistance = 600;
  95. controls.autoRotate = true;
  96. controls.autoRotateSpeed = 0.3;
  97. controls.update();
  98. // a single directional key aligned with the sky's sun, for the crisp
  99. // relief shadows the IBL alone can't give. updateSun() and
  100. // fitShadowCamera() drive its colour, intensity and shadow frustum
  101. sunLight = new THREE.DirectionalLight();
  102. sunLight.castShadow = true;
  103. sunLight.shadow.mapSize.set( 2048, 2048 );
  104. sunLight.shadow.bias = - 0.0004;
  105. scene.add( sunLight );
  106. scene.add( sunLight.target );
  107. // place the sun ( sky, IBL, key light and shadow ) for the current time
  108. updateSun();
  109. // an invisible ground that only catches the tower's shadow, large
  110. // enough to hold the long shadow the low sun casts
  111. const ground = new THREE.Mesh(
  112. new THREE.PlaneGeometry( 2000, 2000 ).rotateX( - Math.PI / 2 ),
  113. new THREE.ShadowMaterial( { color: 0x000000, opacity: 0.35 } )
  114. );
  115. ground.receiveShadow = true;
  116. scene.add( ground );
  117. material = createSkyscraperMaterial( baseColor );
  118. generate();
  119. const gui = renderer.inspector.createParameters( 'Building' );
  120. gui.add( parameters, 'seed', 0, 100, 1 ).onChange( generate );
  121. gui.add( parameters, 'height', 60, 200, 1 ).onChange( generate );
  122. gui.add( parameters, 'width', 18, 60, 1 ).onChange( generate );
  123. gui.add( parameters, 'depth', 16, 50, 1 ).onChange( generate );
  124. gui.add( parameters, 'floorHeight', 3, 6, 0.1 ).onChange( generate );
  125. gui.add( parameters, 'bayWidth', 1.8, 4.5, 0.1 ).onChange( generate );
  126. gui.add( parameters, 'chamfer', 0, 10, 0.5 ).onChange( generate );
  127. gui.add( parameters, 'setback', 0, 4, 0.1 ).onChange( generate );
  128. gui.add( parameters, 'timeOfDay', 6, 18, 0.1 ).name( 'time of day' ).onChange( updateSun );
  129. window.addEventListener( 'resize', onWindowResize );
  130. }
  131. function generate() {
  132. if ( building ) {
  133. scene.remove( building );
  134. building.geometry.dispose();
  135. }
  136. baseColor.value.setHex( pickBuildingColor( parameters.seed ) );
  137. generator = new SkyscraperGenerator( {
  138. seed: parameters.seed,
  139. totalHeight: parameters.height,
  140. footprint: { width: parameters.width, depth: parameters.depth },
  141. floorHeight: parameters.floorHeight,
  142. bayWidth: parameters.bayWidth,
  143. chamferWidth: parameters.chamfer,
  144. setbackDepth: parameters.setback
  145. }, material );
  146. building = generator.build();
  147. building.castShadow = building.receiveShadow = true;
  148. scene.add( building );
  149. fitShadowCamera();
  150. }
  151. function updateSun() {
  152. // map the time of day to a sun arc: low and warm at dawn / dusk, high and
  153. // bright at noon, sweeping east → west across the day
  154. const t = parameters.timeOfDay;
  155. const u = ( t - 12 ) / 6; // -1 at sunrise, 0 at noon, +1 at sunset
  156. const elevation = Math.max( 0, 1 - u * u ) * 72; // degrees above the horizon, peaks at noon
  157. const azimuth = 90 - u * 55; // the sun swings east → west across the day
  158. sun.setFromSphericalCoords( 1, THREE.MathUtils.degToRad( 90 - elevation ), THREE.MathUtils.degToRad( azimuth ) );
  159. sky.sunPosition.value.copy( sun );
  160. // physically-motivated key light: real sunlight is far brighter than the
  161. // sky, so the sun stays the dominant source and cast shadows read clearly
  162. // against the soft sky fill. the longer air path near the horizon dims and
  163. // warms it ( the fill dims with it, as the environment is re-baked below ).
  164. const sinElevation = Math.sin( THREE.MathUtils.degToRad( elevation ) );
  165. const transmittance = Math.sqrt( Math.max( sinElevation, 0 ) ); // 0 at the horizon → 1 at the zenith
  166. sunLight.color.copy( sunHorizonColor ).lerp( sunMiddayColor, transmittance );
  167. sunLight.intensity = 6 * transmittance; // illuminance perpendicular to the rays; the renderer applies N·L per surface
  168. // re-bake the sky ( without the sun disc ) into the environment map for IBL
  169. sky.showSunDisc.value = false;
  170. envScene.add( sky );
  171. const env = pmremGenerator.fromScene( envScene ).texture;
  172. if ( scene.environment ) scene.environment.dispose();
  173. scene.environment = env;
  174. sky.showSunDisc.value = true;
  175. scene.add( sky );
  176. fitShadowCamera();
  177. }
  178. function fitShadowCamera() {
  179. // fit the directional light's shadow frustum to the tower and the full
  180. // ground shadow it casts, so a low sun's long shadow isn't clipped
  181. const height = parameters.height;
  182. const tipDistance = height / Math.max( sun.y, 0.05 ); // shadow tip on the ground
  183. const centerX = - sun.x * tipDistance * 0.5;
  184. const centerZ = - sun.z * tipDistance * 0.5;
  185. const radius = Math.hypot( parameters.width, parameters.depth ) * 0.5;
  186. const half = Math.hypot( centerX, centerZ ) + radius + 20;
  187. const distance = half + height; // place the light clear of the whole scene
  188. sunLight.target.position.set( centerX, 0, centerZ );
  189. sunLight.target.updateMatrixWorld();
  190. sunLight.position.set( centerX + sun.x * distance, sun.y * distance, centerZ + sun.z * distance );
  191. const shadowCamera = sunLight.shadow.camera;
  192. shadowCamera.left = - half;
  193. shadowCamera.right = half;
  194. shadowCamera.top = half;
  195. shadowCamera.bottom = - half;
  196. shadowCamera.near = 1;
  197. shadowCamera.far = distance * 2;
  198. shadowCamera.updateProjectionMatrix();
  199. }
  200. function onWindowResize() {
  201. camera.aspect = window.innerWidth / window.innerHeight;
  202. camera.updateProjectionMatrix();
  203. renderer.setSize( window.innerWidth, window.innerHeight );
  204. }
  205. function animate() {
  206. controls.update();
  207. renderer.render( scene, camera );
  208. }
  209. </script>
  210. </body>
  211. </html>
粤ICP备19079148号