webgpu_custom_fog.html 4.9 KB

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