1
0

webgl_postprocessing_material_ao.html 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - postprocessing - GTAO</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. <style>
  9. body {
  10. background-color: #ffffff;
  11. color: #000;
  12. }
  13. a {
  14. color: #2983ff;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="info">
  20. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Mesh Post Processing Material by <a href="https://github.com/Rabbid76" target="_blank" rel="noopener">Rabbid76</a><br/>
  21. <p>Improved application of the AO passes by using the AO directly in the material shader instead of simply blending with the whole scene</p>
  22. </div>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../build/three.module.js",
  27. "three/addons/": "./jsm/"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three';
  33. import Stats from 'three/addons/libs/stats.module.js';
  34. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  35. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  36. import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
  37. import { PLYLoader } from 'three/addons/loaders/PLYLoader.js';
  38. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  39. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  40. import { GTAOPass } from 'three/addons/postprocessing/GTAOPass.js';
  41. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  42. import { MeshPostProcessingMaterial } from 'three/addons/materials/MeshPostProcessingMaterial.js';
  43. let renderer, camera, scene, composer, controls, stats;
  44. const sceneParameters = {
  45. output: 0,
  46. envMapIntensity: 1.0,
  47. ambientLightIntensity: 0.0,
  48. lightIntensity: 50,
  49. shadow: true,
  50. };
  51. const aoParameters = {
  52. radius: 0.5,
  53. distanceExponent: 2.,
  54. thickness: 10.,
  55. scale: 1.,
  56. samples: 16,
  57. distanceFallOff: 1.,
  58. };
  59. init();
  60. function init() {
  61. const container = document.createElement( 'div' );
  62. document.body.appendChild( container );
  63. stats = new Stats();
  64. container.appendChild( stats.dom );
  65. renderer = new THREE.WebGLRenderer();
  66. renderer.setSize( window.innerWidth, window.innerHeight );
  67. renderer.setAnimationLoop( animate );
  68. document.body.appendChild( renderer.domElement );
  69. renderer.shadowMap.enabled = sceneParameters.shadow;
  70. const plyLoader = new PLYLoader();
  71. const rgbeloader = new RGBELoader();
  72. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 50 );
  73. camera.position.set( 0, 3, 5 );
  74. controls = new OrbitControls( camera, renderer.domElement );
  75. controls.target.set( 0, 1, 0 );
  76. controls.update();
  77. controls.enablePan = false;
  78. controls.enableDamping = true;
  79. const width = window.innerWidth;
  80. const height = window.innerHeight;
  81. scene = new THREE.Scene();
  82. composer = new EffectComposer( renderer );
  83. const gtaoPass = new GTAOPass( scene, camera, width, height );
  84. gtaoPass.output = GTAOPass.OUTPUT.Off;
  85. const renderPasse = new RenderPass( scene, camera );
  86. const outputPass = new OutputPass();
  87. composer.addPass( gtaoPass );
  88. composer.addPass( renderPasse );
  89. composer.addPass( outputPass );
  90. rgbeloader.load( 'textures/equirectangular/royal_esplanade_1k.hdr', function ( texture ) {
  91. texture.mapping = THREE.EquirectangularReflectionMapping;
  92. scene.environment = texture;
  93. } );
  94. const groundMaterial = new MeshPostProcessingMaterial( { color: 0x7f7f7f, envMapIntensity: sceneParameters.envMapIntensity, aoPassMap: gtaoPass.gtaoMap } );
  95. const objectMaterial = new MeshPostProcessingMaterial( { color: 0xffffff, roughness: 0.5, metalness: 0.5, envMapIntensity: sceneParameters.envMapIntensity, aoPassMap: gtaoPass.gtaoMap } );
  96. const emissiveMaterial = new MeshPostProcessingMaterial( { color: 0, emissive: 0xffffff, aoPassMap: gtaoPass.gtaoMap } );
  97. plyLoader.load( 'models/ply/binary/Lucy100k.ply', ( geometry ) => {
  98. geometry.computeVertexNormals();
  99. const lucy = new THREE.Mesh( geometry, objectMaterial );
  100. lucy.receiveShadow = true;
  101. lucy.castShadow = true;
  102. lucy.scale.setScalar( 0.001 );
  103. lucy.rotation.set( 0, Math.PI, 0 );
  104. lucy.position.set( 0.04, 1.8, 0.02 );
  105. scene.add( lucy );
  106. } );
  107. const ambientLight = new THREE.AmbientLight( 0xffffff, sceneParameters.ambientLightIntensity );
  108. const lightGroup = new THREE.Group();
  109. const planeGeometry = new THREE.PlaneGeometry( 6, 6 );
  110. const cylinderGeometry = new THREE.CylinderGeometry( 0.5, 0.5, 1, 64 );
  111. const sphereGeometry = new THREE.SphereGeometry( 0.5, 32, 32 );
  112. const lightSphereGeometry = new THREE.SphereGeometry( 0.1, 32, 32 );
  113. scene.background = new THREE.Color( 0xbfe3dd );
  114. scene.add( ambientLight );
  115. scene.add( lightGroup );
  116. const targetObject = new THREE.Object3D();
  117. targetObject.position.set( 0, 1, 0 );
  118. scene.add( targetObject );
  119. const lightColors = [ 0xff4040, 0x40ff40, 0x4040ff ];
  120. for ( let j = 0; j < 3; ++ j ) {
  121. const light = new THREE.SpotLight( lightColors[ j ], sceneParameters.lightIntensity, 0, Math.PI / 9 );
  122. light.castShadow = true;
  123. light.shadow.camera.far = 15;
  124. light.position.set( 5 * Math.cos( Math.PI * j * 2 / 3 ), 2.5, 5 * Math.sin( Math.PI * j * 2 / 3 ) );
  125. light.target = targetObject;
  126. lightGroup.add( light );
  127. }
  128. const groundPlane = new THREE.Mesh( planeGeometry, groundMaterial );
  129. groundPlane.rotation.x = - Math.PI / 2;
  130. groundPlane.position.set( 0, 0, 0 );
  131. groundPlane.receiveShadow = true;
  132. scene.add( groundPlane );
  133. const pedestal = new THREE.Mesh( cylinderGeometry, groundMaterial );
  134. pedestal.position.set( 0, 0.5, 0 );
  135. pedestal.receiveShadow = true;
  136. pedestal.castShadow = true;
  137. scene.add( pedestal );
  138. const sphereMesh = new THREE.InstancedMesh( sphereGeometry, objectMaterial, 6 );
  139. sphereMesh.receiveShadow = true;
  140. sphereMesh.castShadow = true;
  141. scene.add( sphereMesh );
  142. [ ...Array( 6 ).keys() ].forEach( ( i ) => sphereMesh.setMatrixAt( i, new THREE.Matrix4().makeTranslation( Math.cos( Math.PI * i / 3 ), 0.5, Math.sin( Math.PI * i / 3 ) ) ) );
  143. const lightSphereMesh = new THREE.InstancedMesh( lightSphereGeometry, emissiveMaterial, 4 );
  144. scene.add( lightSphereMesh );
  145. [ ...Array( 4 ).keys() ].forEach( ( i ) => lightSphereMesh.setMatrixAt( i, new THREE.Matrix4().makeTranslation( 0.4 * Math.cos( Math.PI * ( i + 0.5 ) / 2 ), 1.1, 0.45 * Math.sin( Math.PI * ( i + 0.5 ) / 2 ) ) ) );
  146. const updateGtaoMaterial = () => gtaoPass.updateGtaoMaterial( aoParameters );
  147. const updateOutput = () => {
  148. composer.removePass( gtaoPass );
  149. composer.insertPass( gtaoPass, sceneParameters.output == 1 ? 1 : 0 );
  150. switch ( sceneParameters.output ) {
  151. default:
  152. case 0:
  153. gtaoPass.output = GTAOPass.OUTPUT.Off;
  154. gtaoPass.enabled = true;
  155. renderPasse.enabled = true;
  156. break;
  157. case 1:
  158. gtaoPass.output = GTAOPass.OUTPUT.Default;
  159. gtaoPass.enabled = true;
  160. renderPasse.enabled = true;
  161. break;
  162. case 2:
  163. gtaoPass.output = GTAOPass.OUTPUT.Diffuse;
  164. gtaoPass.enabled = false;
  165. renderPasse.enabled = true;
  166. break;
  167. case 3:
  168. gtaoPass.output = GTAOPass.OUTPUT.Denoise;
  169. gtaoPass.enabled = true;
  170. renderPasse.enabled = false;
  171. break;
  172. }
  173. groundMaterial.aoPassMap = sceneParameters.output === 0 ? gtaoPass.gtaoMap : null;
  174. objectMaterial.aoPassMap = sceneParameters.output === 0 ? gtaoPass.gtaoMap : null;
  175. };
  176. updateOutput();
  177. updateGtaoMaterial();
  178. const gui = new GUI();
  179. gui.add( sceneParameters, 'output', {
  180. 'material AO': 0,
  181. 'post blended AO': 1,
  182. 'only diffuse': 2,
  183. 'only AO': 3,
  184. } ).onChange( () => updateOutput() );
  185. gui.add( sceneParameters, 'envMapIntensity' ).min( 0 ).max( 1 ).step( 0.01 ).onChange( () => {
  186. groundMaterial.envMapIntensity = sceneParameters.envMapIntensity;
  187. objectMaterial.envMapIntensity = sceneParameters.envMapIntensity;
  188. } );
  189. gui.add( sceneParameters, 'ambientLightIntensity' ).min( 0.0 ).max( 1.0 ).step( 0.01 ).onChange( () => {
  190. ambientLight.intensity = sceneParameters.ambientLightIntensity;
  191. } );
  192. gui.add( sceneParameters, 'lightIntensity' ).min( 0 ).max( 100 ).step( 1 ).onChange( () => {
  193. lightGroup.children.forEach( light => light.intensity = sceneParameters.lightIntensity );
  194. } );
  195. gui.add( sceneParameters, 'shadow' ).onChange( ( value ) => {
  196. renderer.shadowMap.enabled = value;
  197. lightGroup.children.forEach( light => light.castShadow = value );
  198. } );
  199. gui.add( aoParameters, 'radius' ).min( 0.01 ).max( 2 ).step( 0.01 ).onChange( () => updateGtaoMaterial() );
  200. gui.add( aoParameters, 'distanceExponent' ).min( 1 ).max( 4 ).step( 0.01 ).onChange( () => updateGtaoMaterial() );
  201. gui.add( aoParameters, 'thickness' ).min( 0.01 ).max( 10 ).step( 0.01 ).onChange( () => updateGtaoMaterial() );
  202. gui.add( aoParameters, 'distanceFallOff' ).min( 0 ).max( 1 ).step( 0.01 ).onChange( () => updateGtaoMaterial() );
  203. gui.add( aoParameters, 'scale' ).min( 0.01 ).max( 2.0 ).step( 0.01 ).onChange( () => updateGtaoMaterial() );
  204. gui.add( aoParameters, 'samples' ).min( 2 ).max( 32 ).step( 1 ).onChange( () => updateGtaoMaterial() );
  205. window.addEventListener( 'resize', onWindowResize );
  206. }
  207. function onWindowResize() {
  208. const width = window.innerWidth;
  209. const height = window.innerHeight;
  210. camera.aspect = width / height;
  211. camera.updateProjectionMatrix();
  212. renderer.setSize( width, height );
  213. composer.setSize( width, height );
  214. }
  215. function animate() {
  216. controls.update();
  217. stats.begin();
  218. composer.render();
  219. stats.end();
  220. }
  221. </script>
  222. </body>
  223. </html>