webgl_materials_envmaps_pmrem_nodes.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. var 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. var container, stats;
  51. var camera, scene, renderer, controls;
  52. var nodeMaterial, nodeTexture, nodeTextureIntensity, torusMesh, planeMesh;
  53. var hdrCubeRenderTarget;
  54. var 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. renderer.toneMapping = THREE.LinearToneMapping;
  66. //
  67. var geometry = new THREE.TorusKnotBufferGeometry( 18, 8, 150, 20 );
  68. nodeMaterial = new StandardNodeMaterial();
  69. nodeMaterial.color.value = new THREE.Color( 0xffffff );
  70. nodeMaterial.roughness.value = params.metalness;
  71. nodeMaterial.metalness.value = params.roguhness;
  72. nodeMaterial.visible = false;
  73. nodeTexture = new TextureNode();
  74. nodeTextureIntensity = new FloatNode( 1 );
  75. nodeMaterial.environment = new OperatorNode( new TextureCubeNode( nodeTexture ), nodeTextureIntensity, OperatorNode.MUL );
  76. torusMesh = new THREE.Mesh( geometry, nodeMaterial );
  77. scene.add( torusMesh );
  78. planeMesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 200, 200 ), new THREE.MeshBasicMaterial() );
  79. planeMesh.position.y = - 50;
  80. planeMesh.rotation.x = - Math.PI * 0.5;
  81. scene.add( planeMesh );
  82. var hdrUrls = [ 'px.hdr', 'nx.hdr', 'py.hdr', 'ny.hdr', 'pz.hdr', 'nz.hdr' ];
  83. hdrCubeMap = new HDRCubeTextureLoader()
  84. .setPath( './textures/cube/pisaHDR/' )
  85. .setDataType( THREE.UnsignedByteType )
  86. .load( hdrUrls, function () {
  87. hdrCubeRenderTarget = pmremGenerator.fromCubemap( hdrCubeMap );
  88. pmremGenerator.dispose();
  89. nodeTexture.value = hdrCubeRenderTarget.texture;
  90. hdrCubeMap.magFilter = THREE.LinearFilter;
  91. hdrCubeMap.needsUpdate = true;
  92. planeMesh.material.map = hdrCubeRenderTarget.texture;
  93. planeMesh.material.needsUpdate = true;
  94. nodeMaterial.visible = true;
  95. } );
  96. var pmremGenerator = new THREE.PMREMGenerator( renderer );
  97. pmremGenerator.compileCubemapShader();
  98. renderer.setPixelRatio( window.devicePixelRatio );
  99. renderer.setSize( window.innerWidth, window.innerHeight );
  100. container.appendChild( renderer.domElement );
  101. //renderer.toneMapping = THREE.ReinhardToneMapping;
  102. renderer.outputEncoding = THREE.sRGBEncoding;
  103. stats = new Stats();
  104. container.appendChild( stats.dom );
  105. controls = new OrbitControls( camera, renderer.domElement );
  106. controls.minDistance = 50;
  107. controls.maxDistance = 300;
  108. window.addEventListener( 'resize', onWindowResize, false );
  109. var gui = new GUI();
  110. gui.add( params, 'roughness', 0, 1, 0.01 );
  111. gui.add( params, 'metalness', 0, 1, 0.01 );
  112. gui.add( params, 'exposure', 0, 2, 0.01 );
  113. gui.add( params, 'intensity', 0, 2, 0.01 );
  114. gui.add( params, 'animate', true );
  115. gui.add( params, 'debug', false );
  116. gui.open();
  117. }
  118. function onWindowResize() {
  119. var width = window.innerWidth;
  120. var height = window.innerHeight;
  121. camera.aspect = width / height;
  122. camera.updateProjectionMatrix();
  123. renderer.setSize( width, height );
  124. }
  125. function animate() {
  126. requestAnimationFrame( animate );
  127. stats.begin();
  128. render();
  129. stats.end();
  130. }
  131. function render() {
  132. torusMesh.material.roughness.value = params.roughness;
  133. torusMesh.material.metalness.value = params.metalness;
  134. nodeTextureIntensity.value = params.intensity;
  135. if ( params.animate ) {
  136. torusMesh.rotation.y += 0.005;
  137. }
  138. planeMesh.visible = params.debug;
  139. scene.background = hdrCubeMap;
  140. renderer.toneMappingExposure = params.exposure;
  141. renderer.render( scene, camera );
  142. }
  143. </script>
  144. </body>
  145. </html>