webgpu_fog_height.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - height fog</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 - height fog">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_fog_height.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_fog_height.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>Height Fog</span>
  18. </div>
  19. <small>
  20. Exponential Height Fog with TSL.
  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 { exponentialHeightFogFactor, uniform, fog, color } from 'three/tsl';
  36. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  37. import { Inspector } from 'three/addons/inspector/Inspector.js';
  38. let camera, scene, renderer;
  39. let controls;
  40. init();
  41. function init() {
  42. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 600 );
  43. camera.position.set( 20, 10, 25 );
  44. scene = new THREE.Scene();
  45. // height fog
  46. const density = uniform( 0.04 );
  47. const height = uniform( 2 );
  48. const fogFactor = exponentialHeightFogFactor( density, height );
  49. scene.fogNode = fog( color( 0xffdfc1 ), fogFactor );
  50. scene.backgroundNode = color( 0xffdfc1 );
  51. // meshes
  52. const geometry = new THREE.BoxGeometry( 1, 25, 1 );
  53. const material = new THREE.MeshPhongNodeMaterial( { color: 0xcd959a } );
  54. const mesh = new THREE.InstancedMesh( geometry, material, 100 );
  55. mesh.position.y = - 10;
  56. scene.add( mesh );
  57. const dummy = new THREE.Object3D();
  58. let index = 0;
  59. for ( let i = 0; i < 10; i ++ ) {
  60. for ( let j = 0; j < 10; j ++ ) {
  61. dummy.position.x = - 18 + ( i * 4 );
  62. dummy.position.z = - 18 + ( j * 4 );
  63. dummy.updateMatrix();
  64. mesh.setMatrixAt( index ++, dummy.matrix );
  65. }
  66. }
  67. // lights
  68. const directionalLight = new THREE.DirectionalLight( 0xffc0cb, 2 );
  69. directionalLight.position.set( - 10, 10, 10 );
  70. scene.add( directionalLight );
  71. const ambientLight = new THREE.AmbientLight( 0xcccccc );
  72. scene.add( ambientLight );
  73. // renderer
  74. renderer = new THREE.WebGPURenderer( { antialias: true } );
  75. renderer.setPixelRatio( window.devicePixelRatio );
  76. renderer.setSize( window.innerWidth, window.innerHeight );
  77. renderer.setAnimationLoop( animate );
  78. renderer.inspector = new Inspector();
  79. document.body.appendChild( renderer.domElement );
  80. // gui
  81. const gui = renderer.inspector.createParameters( 'Fog Settings' );
  82. gui.add( density, 'value', 0.001, 0.1 ).step( 0.0001 ).name( 'Density' );
  83. gui.add( height, 'value', - 5, 5 ).name( 'Height' );
  84. // controls
  85. controls = new OrbitControls( camera, renderer.domElement );
  86. controls.minDistance = 7;
  87. controls.maxDistance = 100;
  88. controls.maxPolarAngle = Math.PI / 2;
  89. controls.enableDamping = true;
  90. window.addEventListener( 'resize', resize );
  91. }
  92. function resize() {
  93. camera.aspect = window.innerWidth / window.innerHeight;
  94. camera.updateProjectionMatrix();
  95. renderer.setSize( window.innerWidth, window.innerHeight );
  96. }
  97. function animate() {
  98. controls.update();
  99. renderer.render( scene, camera );
  100. }
  101. </script>
  102. </body>
  103. </html>
粤ICP备19079148号