webgpu_custom_fog.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - custom 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="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgpu - custom fog
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.webgpu.js",
  17. "three/webgpu": "../build/three.webgpu.js",
  18. "three/tsl": "../build/three.tsl.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import { color, fog, float, positionWorld, triNoise3D, positionView, normalWorld, uniform } from 'three/tsl';
  26. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  27. let camera, scene, renderer;
  28. let controls;
  29. init();
  30. function init() {
  31. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 600 );
  32. camera.position.set( 30, 15, 30 );
  33. scene = new THREE.Scene();
  34. // custom fog
  35. const skyColor = color( 0xf0f5f5 );
  36. const groundColor = color( 0xd0dee7 );
  37. const fogNoiseDistance = positionView.z.negate().smoothstep( 0, camera.far - 300 );
  38. const distance = fogNoiseDistance.mul( 20 ).max( 4 );
  39. const alpha = .98;
  40. const groundFogArea = float( distance ).sub( positionWorld.y ).div( distance ).pow( 3 ).saturate().mul( alpha );
  41. // a alternative way to create a TimerNode
  42. const timer = uniform( 0 ).onFrameUpdate( ( frame ) => frame.time );
  43. const fogNoiseA = triNoise3D( positionWorld.mul( .005 ), 0.2, timer );
  44. const fogNoiseB = triNoise3D( positionWorld.mul( .01 ), 0.2, timer.mul( 1.2 ) );
  45. const fogNoise = fogNoiseA.add( fogNoiseB ).mul( groundColor );
  46. // apply custom fog
  47. scene.fogNode = fog( fogNoiseDistance.oneMinus().mix( groundColor, fogNoise ), groundFogArea );
  48. scene.backgroundNode = normalWorld.y.max( 0 ).mix( groundColor, skyColor );
  49. // builds
  50. const buildWindows = positionWorld.y.mul( 10 ).floor().mod( 4 ).sign().mix( color( 0x000066 ).add( fogNoiseDistance ), color( 0xffffff ) );
  51. const buildGeometry = new THREE.BoxGeometry( 1, 1, 1 );
  52. const buildMaterial = new THREE.MeshPhongNodeMaterial( {
  53. colorNode: buildWindows
  54. } );
  55. const buildMesh = new THREE.InstancedMesh( buildGeometry, buildMaterial, 4000 );
  56. scene.add( buildMesh );
  57. const dummy = new THREE.Object3D();
  58. const center = new THREE.Vector3();
  59. for ( let i = 0; i < buildMesh.count; i ++ ) {
  60. const scaleY = Math.random() * 7 + .5;
  61. dummy.position.x = Math.random() * 600 - 300;
  62. dummy.position.z = Math.random() * 600 - 300;
  63. const distance = Math.max( dummy.position.distanceTo( center ) * .012, 1 );
  64. dummy.position.y = .5 * scaleY * distance;
  65. dummy.scale.x = dummy.scale.z = Math.random() * 3 + .5;
  66. dummy.scale.y = scaleY * distance;
  67. dummy.updateMatrix();
  68. buildMesh.setMatrixAt( i, dummy.matrix );
  69. }
  70. // lights
  71. scene.add( new THREE.HemisphereLight( skyColor.value, groundColor.value, 0.5 ) );
  72. // geometry
  73. const planeGeometry = new THREE.PlaneGeometry( 200, 200 );
  74. const planeMaterial = new THREE.MeshPhongMaterial( {
  75. color: 0x999999
  76. } );
  77. const ground = new THREE.Mesh( planeGeometry, planeMaterial );
  78. ground.rotation.x = - Math.PI / 2;
  79. ground.scale.multiplyScalar( 3 );
  80. ground.castShadow = true;
  81. ground.receiveShadow = true;
  82. scene.add( ground );
  83. // renderer
  84. renderer = new THREE.WebGPURenderer( { antialias: true } );
  85. renderer.setPixelRatio( window.devicePixelRatio );
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. renderer.setAnimationLoop( animate );
  88. document.body.appendChild( renderer.domElement );
  89. // controls
  90. controls = new OrbitControls( camera, renderer.domElement );
  91. controls.target.set( 0, 2, 0 );
  92. controls.minDistance = 7;
  93. controls.maxDistance = 100;
  94. controls.maxPolarAngle = Math.PI / 2;
  95. controls.autoRotate = true;
  96. controls.autoRotateSpeed = .1;
  97. controls.update();
  98. window.addEventListener( 'resize', resize );
  99. }
  100. function resize() {
  101. camera.aspect = window.innerWidth / window.innerHeight;
  102. camera.updateProjectionMatrix();
  103. renderer.setSize( window.innerWidth, window.innerHeight );
  104. }
  105. function animate() {
  106. controls.update();
  107. renderer.render( scene, camera );
  108. }
  109. </script>
  110. </body>
  111. </html>
粤ICP备19079148号