1
0

webgpu_generator_building.html 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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.inspector = new Inspector();
  70. document.body.appendChild( renderer.domElement );
  71. await renderer.init();
  72. pmremGenerator = new THREE.PMREMGenerator( renderer );
  73. scene = new THREE.Scene();
  74. scene.environmentIntensity = 0.25; // sky as a soft fill; the sun is the key
  75. // a physical sky drives both the backdrop and the image-based lighting;
  76. // updateSun() positions the sun and bakes it into the IBL environment
  77. sky = new SkyMesh();
  78. sky.scale.setScalar( 10000 );
  79. sky.turbidity.value = 8;
  80. sky.rayleigh.value = 3;
  81. sky.mieCoefficient.value = 0.008;
  82. sky.mieDirectionalG.value = 0.88;
  83. // the sky is baked into the environment map from this scene, then moved
  84. // to the main scene as the backdrop ( see updateSun )
  85. envScene = new THREE.Scene();
  86. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 20000 );
  87. camera.position.set( 120, 95, 155 );
  88. controls = new OrbitControls( camera, renderer.domElement );
  89. controls.enableDamping = true;
  90. controls.target.set( 0, 58, 0 );
  91. controls.maxPolarAngle = Math.PI * 0.495;
  92. controls.minDistance = 30;
  93. controls.maxDistance = 600;
  94. controls.autoRotate = true;
  95. controls.autoRotateSpeed = 0.3;
  96. controls.update();
  97. // a single directional key aligned with the sky's sun, for the crisp
  98. // relief shadows the IBL alone can't give. updateSun() and
  99. // fitShadowCamera() drive its colour, intensity and shadow frustum
  100. sunLight = new THREE.DirectionalLight();
  101. sunLight.castShadow = true;
  102. sunLight.shadow.mapSize.set( 2048, 2048 );
  103. sunLight.shadow.bias = - 0.0004;
  104. scene.add( sunLight );
  105. scene.add( sunLight.target );
  106. // place the sun ( sky, IBL, key light and shadow ) for the current time
  107. updateSun();
  108. // an invisible ground that only catches the tower's shadow, large
  109. // enough to hold the long shadow the low sun casts
  110. const ground = new THREE.Mesh(
  111. new THREE.PlaneGeometry( 2000, 2000 ).rotateX( - Math.PI / 2 ),
  112. new THREE.ShadowMaterial( { color: 0x000000, opacity: 0.35 } )
  113. );
  114. ground.receiveShadow = true;
  115. scene.add( ground );
  116. material = createSkyscraperMaterial( baseColor );
  117. generate();
  118. const gui = renderer.inspector.createParameters( 'Building' );
  119. gui.add( parameters, 'seed', 0, 100, 1 ).onChange( generate );
  120. gui.add( parameters, 'height', 60, 200, 1 ).onChange( generate );
  121. gui.add( parameters, 'width', 18, 60, 1 ).onChange( generate );
  122. gui.add( parameters, 'depth', 16, 50, 1 ).onChange( generate );
  123. gui.add( parameters, 'floorHeight', 3, 6, 0.1 ).onChange( generate );
  124. gui.add( parameters, 'bayWidth', 1.8, 4.5, 0.1 ).onChange( generate );
  125. gui.add( parameters, 'chamfer', 0, 10, 0.5 ).onChange( generate );
  126. gui.add( parameters, 'setback', 0, 4, 0.1 ).onChange( generate );
  127. gui.add( parameters, 'timeOfDay', 6, 18, 0.1 ).name( 'time of day' ).onChange( updateSun );
  128. window.addEventListener( 'resize', onWindowResize );
  129. }
  130. function generate() {
  131. if ( building ) {
  132. scene.remove( building );
  133. building.geometry.dispose();
  134. }
  135. baseColor.value.setHex( pickBuildingColor( parameters.seed ) );
  136. generator = new SkyscraperGenerator( {
  137. seed: parameters.seed,
  138. totalHeight: parameters.height,
  139. footprint: { width: parameters.width, depth: parameters.depth },
  140. floorHeight: parameters.floorHeight,
  141. bayWidth: parameters.bayWidth,
  142. chamferWidth: parameters.chamfer,
  143. setbackDepth: parameters.setback
  144. }, material );
  145. building = generator.build();
  146. building.castShadow = building.receiveShadow = true;
  147. scene.add( building );
  148. fitShadowCamera();
  149. }
  150. function updateSun() {
  151. // map the time of day to a sun arc: low and warm at dawn / dusk, high and
  152. // bright at noon, sweeping east → west across the day
  153. const t = parameters.timeOfDay;
  154. const u = ( t - 12 ) / 6; // -1 at sunrise, 0 at noon, +1 at sunset
  155. const elevation = Math.max( 0, 1 - u * u ) * 72; // degrees above the horizon, peaks at noon
  156. const azimuth = 90 - u * 55; // the sun swings east → west across the day
  157. sun.setFromSphericalCoords( 1, THREE.MathUtils.degToRad( 90 - elevation ), THREE.MathUtils.degToRad( azimuth ) );
  158. sky.sunPosition.value.copy( sun );
  159. // physically-motivated key light: real sunlight is far brighter than the
  160. // sky, so the sun stays the dominant source and cast shadows read clearly
  161. // against the soft sky fill. the longer air path near the horizon dims and
  162. // warms it ( the fill dims with it, as the environment is re-baked below ).
  163. const sinElevation = Math.sin( THREE.MathUtils.degToRad( elevation ) );
  164. const transmittance = Math.sqrt( Math.max( sinElevation, 0 ) ); // 0 at the horizon → 1 at the zenith
  165. sunLight.color.copy( sunHorizonColor ).lerp( sunMiddayColor, transmittance );
  166. sunLight.intensity = 6 * transmittance; // illuminance perpendicular to the rays; the renderer applies N·L per surface
  167. // re-bake the sky ( without the sun disc ) into the environment map for IBL
  168. sky.showSunDisc.value = false;
  169. envScene.add( sky );
  170. const env = pmremGenerator.fromScene( envScene ).texture;
  171. if ( scene.environment ) scene.environment.dispose();
  172. scene.environment = env;
  173. sky.showSunDisc.value = true;
  174. scene.add( sky );
  175. fitShadowCamera();
  176. }
  177. function fitShadowCamera() {
  178. // fit the directional light's shadow frustum to the tower and the full
  179. // ground shadow it casts, so a low sun's long shadow isn't clipped
  180. const height = parameters.height;
  181. const tipDistance = height / Math.max( sun.y, 0.05 ); // shadow tip on the ground
  182. const centerX = - sun.x * tipDistance * 0.5;
  183. const centerZ = - sun.z * tipDistance * 0.5;
  184. const radius = Math.hypot( parameters.width, parameters.depth ) * 0.5;
  185. const half = Math.hypot( centerX, centerZ ) + radius + 20;
  186. const distance = half + height; // place the light clear of the whole scene
  187. sunLight.target.position.set( centerX, 0, centerZ );
  188. sunLight.target.updateMatrixWorld();
  189. sunLight.position.set( centerX + sun.x * distance, sun.y * distance, centerZ + sun.z * distance );
  190. const shadowCamera = sunLight.shadow.camera;
  191. shadowCamera.left = - half;
  192. shadowCamera.right = half;
  193. shadowCamera.top = half;
  194. shadowCamera.bottom = - half;
  195. shadowCamera.near = 1;
  196. shadowCamera.far = distance * 2;
  197. shadowCamera.updateProjectionMatrix();
  198. }
  199. function onWindowResize() {
  200. camera.aspect = window.innerWidth / window.innerHeight;
  201. camera.updateProjectionMatrix();
  202. renderer.setSize( window.innerWidth, window.innerHeight );
  203. }
  204. function animate() {
  205. controls.update();
  206. renderer.render( scene, camera );
  207. }
  208. </script>
  209. </body>
  210. </html>
粤ICP备19079148号