2
0

webgl_materials_envmaps_pmrem_nodes.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. <!-- Import maps polyfill -->
  30. <!-- Remove this when import maps will be widely supported -->
  31. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  32. <script type="importmap">
  33. {
  34. "imports": {
  35. "three": "../build/three.module.js"
  36. }
  37. }
  38. </script>
  39. <script type="module">
  40. import * as THREE from 'three';
  41. import Stats from './jsm/libs/stats.module.js';
  42. import { GUI } from './jsm/libs/lil-gui.module.min.js';
  43. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  44. import { HDRCubeTextureLoader } from './jsm/loaders/HDRCubeTextureLoader.js';
  45. import {
  46. StandardNodeMaterial,
  47. FloatNode,
  48. OperatorNode,
  49. TextureNode,
  50. TextureCubeNode
  51. } from './jsm/nodes/Nodes.js';
  52. const params = {
  53. roughness: 0.0,
  54. metalness: 0.0,
  55. exposure: 1.0,
  56. intensity: 1.0,
  57. animate: true,
  58. debug: false
  59. };
  60. let container, stats;
  61. let camera, scene, renderer, controls;
  62. let nodeMaterial, nodeTexture, nodeTextureIntensity, torusMesh, planeMesh;
  63. let hdrCubeRenderTarget;
  64. let hdrCubeMap;
  65. init();
  66. animate();
  67. function init() {
  68. container = document.createElement( 'div' );
  69. document.body.appendChild( container );
  70. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  71. camera.position.set( 0, 0, 120 );
  72. scene = new THREE.Scene();
  73. scene.background = new THREE.Color( 0x000000 );
  74. renderer = new THREE.WebGLRenderer();
  75. //
  76. const geometry = new THREE.TorusKnotGeometry( 18, 8, 150, 20 );
  77. nodeMaterial = new StandardNodeMaterial();
  78. nodeMaterial.color.value = new THREE.Color( 0xffffff );
  79. nodeMaterial.roughness.value = params.metalness;
  80. nodeMaterial.metalness.value = params.roguhness;
  81. nodeMaterial.visible = false;
  82. nodeTexture = new TextureNode();
  83. nodeTextureIntensity = new FloatNode( 1 );
  84. nodeMaterial.environment = new OperatorNode( new TextureCubeNode( nodeTexture ), nodeTextureIntensity, OperatorNode.MUL );
  85. torusMesh = new THREE.Mesh( geometry, nodeMaterial );
  86. scene.add( torusMesh );
  87. planeMesh = new THREE.Mesh( new THREE.PlaneGeometry( 200, 200 ), new THREE.MeshBasicMaterial() );
  88. planeMesh.position.y = - 50;
  89. planeMesh.rotation.x = - Math.PI * 0.5;
  90. scene.add( planeMesh );
  91. const hdrUrls = [ 'px.hdr', 'nx.hdr', 'py.hdr', 'ny.hdr', 'pz.hdr', 'nz.hdr' ];
  92. hdrCubeMap = new HDRCubeTextureLoader()
  93. .setPath( './textures/cube/pisaHDR/' )
  94. .load( hdrUrls, function () {
  95. hdrCubeRenderTarget = pmremGenerator.fromCubemap( hdrCubeMap );
  96. pmremGenerator.dispose();
  97. nodeTexture.value = hdrCubeRenderTarget.texture;
  98. hdrCubeMap.magFilter = THREE.LinearFilter;
  99. hdrCubeMap.needsUpdate = true;
  100. planeMesh.material.map = hdrCubeRenderTarget.texture;
  101. planeMesh.material.needsUpdate = true;
  102. nodeMaterial.visible = true;
  103. } );
  104. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  105. pmremGenerator.compileCubemapShader();
  106. renderer.setPixelRatio( window.devicePixelRatio );
  107. renderer.setSize( window.innerWidth, window.innerHeight );
  108. container.appendChild( renderer.domElement );
  109. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  110. renderer.outputEncoding = THREE.sRGBEncoding;
  111. stats = new Stats();
  112. container.appendChild( stats.dom );
  113. controls = new OrbitControls( camera, renderer.domElement );
  114. controls.minDistance = 50;
  115. controls.maxDistance = 300;
  116. window.addEventListener( 'resize', onWindowResize );
  117. const gui = new GUI();
  118. gui.add( params, 'roughness', 0, 1, 0.01 );
  119. gui.add( params, 'metalness', 0, 1, 0.01 );
  120. gui.add( params, 'exposure', 0, 2, 0.01 );
  121. gui.add( params, 'intensity', 0, 2, 0.01 );
  122. gui.add( params, 'animate', true );
  123. gui.add( params, 'debug', false );
  124. gui.open();
  125. }
  126. function onWindowResize() {
  127. const width = window.innerWidth;
  128. const height = window.innerHeight;
  129. camera.aspect = width / height;
  130. camera.updateProjectionMatrix();
  131. renderer.setSize( width, height );
  132. }
  133. function animate() {
  134. requestAnimationFrame( animate );
  135. stats.begin();
  136. render();
  137. stats.end();
  138. }
  139. function render() {
  140. torusMesh.material.roughness.value = params.roughness;
  141. torusMesh.material.metalness.value = params.metalness;
  142. nodeTextureIntensity.value = params.intensity;
  143. if ( params.animate ) {
  144. torusMesh.rotation.y += 0.005;
  145. }
  146. planeMesh.visible = params.debug;
  147. scene.background = hdrCubeMap;
  148. renderer.toneMappingExposure = params.exposure;
  149. renderer.render( scene, camera );
  150. }
  151. </script>
  152. </body>
  153. </html>