webgpu_custom_fog.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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="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>Custom Fog</span>
  14. </div>
  15. <small>
  16. Custom Fog via 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 { color, fog, float, positionWorld, triNoise3D, positionView, normalWorld, uniform } 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( 30, 15, 30 );
  40. scene = new THREE.Scene();
  41. // custom fog
  42. const skyColorValue = 0xf0f5f5;
  43. const groundColorValue = 0xd0dee7;
  44. const skyColor = color( skyColorValue );
  45. const groundColor = color( groundColorValue );
  46. const fogNoiseDistance = positionView.z.negate().smoothstep( 0, camera.far - 300 );
  47. const distance = fogNoiseDistance.mul( 20 ).max( 4 );
  48. const alpha = .98;
  49. const groundFogArea = float( distance ).sub( positionWorld.y ).div( distance ).pow( 3 ).saturate().mul( alpha );
  50. // a alternative way to create a TimerNode
  51. const timer = uniform( 0 ).onFrameUpdate( ( frame ) => frame.time );
  52. const fogNoiseA = triNoise3D( positionWorld.mul( .005 ), 0.2, timer );
  53. const fogNoiseB = triNoise3D( positionWorld.mul( .01 ), 0.2, timer.mul( 1.2 ) );
  54. const fogNoise = fogNoiseA.add( fogNoiseB ).mul( groundColor );
  55. // apply custom fog
  56. scene.fogNode = fog( fogNoiseDistance.oneMinus().mix( groundColor, fogNoise ), groundFogArea );
  57. scene.backgroundNode = normalWorld.y.max( 0 ).mix( groundColor, skyColor );
  58. // builds
  59. const buildWindows = positionWorld.y.mul( 10 ).floor().mod( 4 ).sign().mix( color( 0x000066 ).add( fogNoiseDistance ), color( 0xffffff ) );
  60. const buildGeometry = new THREE.BoxGeometry( 1, 1, 1 );
  61. const buildMaterial = new THREE.MeshPhongNodeMaterial( {
  62. colorNode: buildWindows
  63. } );
  64. const buildMesh = new THREE.InstancedMesh( buildGeometry, buildMaterial, 4000 );
  65. scene.add( buildMesh );
  66. const dummy = new THREE.Object3D();
  67. const center = new THREE.Vector3();
  68. for ( let i = 0; i < buildMesh.count; i ++ ) {
  69. const scaleY = Math.random() * 7 + .5;
  70. dummy.position.x = Math.random() * 600 - 300;
  71. dummy.position.z = Math.random() * 600 - 300;
  72. const distance = Math.max( dummy.position.distanceTo( center ) * .012, 1 );
  73. dummy.position.y = .5 * scaleY * distance;
  74. dummy.scale.x = dummy.scale.z = Math.random() * 3 + .5;
  75. dummy.scale.y = scaleY * distance;
  76. dummy.updateMatrix();
  77. buildMesh.setMatrixAt( i, dummy.matrix );
  78. }
  79. // lights
  80. scene.add( new THREE.HemisphereLight( skyColorValue, groundColorValue, 0.5 ) );
  81. // geometry
  82. const planeGeometry = new THREE.PlaneGeometry( 200, 200 );
  83. const planeMaterial = new THREE.MeshPhongMaterial( {
  84. color: 0x999999
  85. } );
  86. const ground = new THREE.Mesh( planeGeometry, planeMaterial );
  87. ground.rotation.x = - Math.PI / 2;
  88. ground.scale.multiplyScalar( 3 );
  89. ground.castShadow = true;
  90. ground.receiveShadow = true;
  91. scene.add( ground );
  92. // renderer
  93. renderer = new THREE.WebGPURenderer( { antialias: true } );
  94. renderer.setPixelRatio( window.devicePixelRatio );
  95. renderer.setSize( window.innerWidth, window.innerHeight );
  96. renderer.setAnimationLoop( animate );
  97. renderer.inspector = new Inspector();
  98. document.body.appendChild( renderer.domElement );
  99. // controls
  100. controls = new OrbitControls( camera, renderer.domElement );
  101. controls.target.set( 0, 2, 0 );
  102. controls.minDistance = 7;
  103. controls.maxDistance = 100;
  104. controls.maxPolarAngle = Math.PI / 2;
  105. controls.autoRotate = true;
  106. controls.autoRotateSpeed = .1;
  107. controls.update();
  108. window.addEventListener( 'resize', resize );
  109. }
  110. function resize() {
  111. camera.aspect = window.innerWidth / window.innerHeight;
  112. camera.updateProjectionMatrix();
  113. renderer.setSize( window.innerWidth, window.innerHeight );
  114. }
  115. function animate() {
  116. controls.update();
  117. renderer.render( scene, camera );
  118. }
  119. </script>
  120. </body>
  121. </html>
粤ICP备19079148号