2
0

webgl_materials_envmaps_pmrem_nodes.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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/dat.gui.module.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. .setDataType( THREE.UnsignedByteType )
  85. .load( hdrUrls, function () {
  86. hdrCubeRenderTarget = pmremGenerator.fromCubemap( hdrCubeMap );
  87. pmremGenerator.dispose();
  88. nodeTexture.value = hdrCubeRenderTarget.texture;
  89. hdrCubeMap.magFilter = THREE.LinearFilter;
  90. hdrCubeMap.needsUpdate = true;
  91. planeMesh.material.map = hdrCubeRenderTarget.texture;
  92. planeMesh.material.needsUpdate = true;
  93. nodeMaterial.visible = true;
  94. } );
  95. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  96. pmremGenerator.compileCubemapShader();
  97. renderer.setPixelRatio( window.devicePixelRatio );
  98. renderer.setSize( window.innerWidth, window.innerHeight );
  99. container.appendChild( renderer.domElement );
  100. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  101. renderer.outputEncoding = THREE.sRGBEncoding;
  102. stats = new Stats();
  103. container.appendChild( stats.dom );
  104. controls = new OrbitControls( camera, renderer.domElement );
  105. controls.minDistance = 50;
  106. controls.maxDistance = 300;
  107. window.addEventListener( 'resize', onWindowResize );
  108. const gui = new GUI();
  109. gui.add( params, 'roughness', 0, 1, 0.01 );
  110. gui.add( params, 'metalness', 0, 1, 0.01 );
  111. gui.add( params, 'exposure', 0, 2, 0.01 );
  112. gui.add( params, 'intensity', 0, 2, 0.01 );
  113. gui.add( params, 'animate', true );
  114. gui.add( params, 'debug', false );
  115. gui.open();
  116. }
  117. function onWindowResize() {
  118. const width = window.innerWidth;
  119. const height = window.innerHeight;
  120. camera.aspect = width / height;
  121. camera.updateProjectionMatrix();
  122. renderer.setSize( width, height );
  123. }
  124. function animate() {
  125. requestAnimationFrame( animate );
  126. stats.begin();
  127. render();
  128. stats.end();
  129. }
  130. function render() {
  131. torusMesh.material.roughness.value = params.roughness;
  132. torusMesh.material.metalness.value = params.metalness;
  133. nodeTextureIntensity.value = params.intensity;
  134. if ( params.animate ) {
  135. torusMesh.rotation.y += 0.005;
  136. }
  137. planeMesh.visible = params.debug;
  138. scene.background = hdrCubeMap;
  139. renderer.toneMappingExposure = params.exposure;
  140. renderer.render( scene, camera );
  141. }
  142. </script>
  143. </body>
  144. </html>