webgpu_fog_height.html 3.6 KB

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