webgl_materials_envmaps_pmrem_nodes.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>threejs webgl - node material - custom size hdr environment mapping</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. <style>
  8. body {
  9. color: #fff;
  10. font-family:Monospace;
  11. font-size:13px;
  12. text-align:center;
  13. background-color: #000;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. a { color: #00f }
  18. #info {
  19. position: absolute;
  20. top: 0px; width: 100%;
  21. padding: 5px;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div id="container"></div>
  27. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">threejs</a> - High dynamic range (RGBE) Image-based Lighting (IBL)<br />using run-time generated pre-filtered roughness mipmaps (PMREM)<br/>
  28. Created by Prashant Sharma and <a href="http://clara.io/" target="_blank" rel="noopener">Ben Houston</a>.</div>
  29. <script type="module">
  30. import * as THREE from '../build/three.module.js';
  31. import Stats from './jsm/libs/stats.module.js';
  32. import { GUI } from './jsm/libs/lil-gui.module.min.js';
  33. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  34. import { HDRCubeTextureLoader } from './jsm/loaders/HDRCubeTextureLoader.js';
  35. import {
  36. StandardNodeMaterial,
  37. FloatNode,
  38. OperatorNode,
  39. TextureNode,
  40. TextureCubeNode
  41. } from './jsm/nodes/Nodes.js';
  42. const params = {
  43. roughness: 0.0,
  44. metalness: 0.0,
  45. exposure: 1.0,
  46. intensity: 1.0,
  47. animate: true,
  48. debug: false
  49. };
  50. let container, stats;
  51. let camera, scene, renderer, controls;
  52. let nodeMaterial, nodeTexture, nodeTextureIntensity, torusMesh, planeMesh;
  53. let hdrCubeRenderTarget;
  54. let hdrCubeMap;
  55. init();
  56. animate();
  57. function init() {
  58. container = document.createElement( 'div' );
  59. document.body.appendChild( container );
  60. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  61. camera.position.set( 0, 0, 120 );
  62. scene = new THREE.Scene();
  63. scene.background = new THREE.Color( 0x000000 );
  64. renderer = new THREE.WebGLRenderer();
  65. //
  66. const geometry = new THREE.TorusKnotGeometry( 18, 8, 150, 20 );
  67. nodeMaterial = new StandardNodeMaterial();
  68. nodeMaterial.color.value = new THREE.Color( 0xffffff );
  69. nodeMaterial.roughness.value = params.metalness;
  70. nodeMaterial.metalness.value = params.roguhness;
  71. nodeMaterial.visible = false;
  72. nodeTexture = new TextureNode();
  73. nodeTextureIntensity = new FloatNode( 1 );
  74. nodeMaterial.environment = new OperatorNode( new TextureCubeNode( nodeTexture ), nodeTextureIntensity, OperatorNode.MUL );
  75. torusMesh = new THREE.Mesh( geometry, nodeMaterial );
  76. scene.add( torusMesh );
  77. planeMesh = new THREE.Mesh( new THREE.PlaneGeometry( 200, 200 ), new THREE.MeshBasicMaterial() );
  78. planeMesh.position.y = - 50;
  79. planeMesh.rotation.x = - Math.PI * 0.5;
  80. scene.add( planeMesh );
  81. const hdrUrls = [ 'px.hdr', 'nx.hdr', 'py.hdr', 'ny.hdr', 'pz.hdr', 'nz.hdr' ];
  82. hdrCubeMap = new HDRCubeTextureLoader()
  83. .setPath( './textures/cube/pisaHDR/' )
  84. .load( hdrUrls, function () {
  85. hdrCubeRenderTarget = pmremGenerator.fromCubemap( hdrCubeMap );
  86. pmremGenerator.dispose();
  87. nodeTexture.value = hdrCubeRenderTarget.texture;
  88. hdrCubeMap.magFilter = THREE.LinearFilter;
  89. hdrCubeMap.needsUpdate = true;
  90. planeMesh.material.map = hdrCubeRenderTarget.texture;
  91. planeMesh.material.needsUpdate = true;
  92. nodeMaterial.visible = true;
  93. } );
  94. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  95. pmremGenerator.compileCubemapShader();
  96. renderer.setPixelRatio( window.devicePixelRatio );
  97. renderer.setSize( window.innerWidth, window.innerHeight );
  98. container.appendChild( renderer.domElement );
  99. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  100. renderer.outputEncoding = THREE.sRGBEncoding;
  101. stats = new Stats();
  102. container.appendChild( stats.dom );
  103. controls = new OrbitControls( camera, renderer.domElement );
  104. controls.minDistance = 50;
  105. controls.maxDistance = 300;
  106. window.addEventListener( 'resize', onWindowResize );
  107. const gui = new GUI();
  108. gui.add( params, 'roughness', 0, 1, 0.01 );
  109. gui.add( params, 'metalness', 0, 1, 0.01 );
  110. gui.add( params, 'exposure', 0, 2, 0.01 );
  111. gui.add( params, 'intensity', 0, 2, 0.01 );
  112. gui.add( params, 'animate', true );
  113. gui.add( params, 'debug', false );
  114. gui.open();
  115. }
  116. function onWindowResize() {
  117. const width = window.innerWidth;
  118. const height = window.innerHeight;
  119. camera.aspect = width / height;
  120. camera.updateProjectionMatrix();
  121. renderer.setSize( width, height );
  122. }
  123. function animate() {
  124. requestAnimationFrame( animate );
  125. stats.begin();
  126. render();
  127. stats.end();
  128. }
  129. function render() {
  130. torusMesh.material.roughness.value = params.roughness;
  131. torusMesh.material.metalness.value = params.metalness;
  132. nodeTextureIntensity.value = params.intensity;
  133. if ( params.animate ) {
  134. torusMesh.rotation.y += 0.005;
  135. }
  136. planeMesh.visible = params.debug;
  137. scene.background = hdrCubeMap;
  138. renderer.toneMappingExposure = params.exposure;
  139. renderer.render( scene, camera );
  140. }
  141. </script>
  142. </body>
  143. </html>