webgl_materials_reflectivity.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>threejs webgl - materials - 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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="container"></div>
  11. <div id="info">
  12. <a href="https://threejs.org" target="_blank" rel="noopener">threejs</a> - Standard Material Reflectivity (reflectance at F0)<br/>
  13. example by <a href="http://clara.io/" target="_blank" rel="noopener">Ben Houston</a>.
  14. </div>
  15. <script type="module">
  16. import * as THREE from '../build/three.module.js';
  17. import Stats from './jsm/libs/stats.module.js';
  18. import { GUI } from './jsm/libs/dat.gui.module.js';
  19. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  20. import { OBJLoader } from './jsm/loaders/OBJLoader.js';
  21. import { RGBELoader } from './jsm/loaders/RGBELoader.js';
  22. var container, stats;
  23. var params = {
  24. projection: 'normal',
  25. autoRotate: true,
  26. reflectivity: 1.0,
  27. background: false,
  28. exposure: 1.0,
  29. gemColor: 'Green'
  30. };
  31. var camera, scene, renderer, objects = [];
  32. var gemBackMaterial, gemFrontMaterial;
  33. var hdrCubeRenderTarget;
  34. init();
  35. animate();
  36. function init() {
  37. container = document.createElement( 'div' );
  38. document.body.appendChild( container );
  39. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2000 );
  40. camera.position.set( 0.0, - 10, 20 * 3.5 );
  41. scene = new THREE.Scene();
  42. scene.background = new THREE.Color( 0x000000 );
  43. renderer = new THREE.WebGLRenderer( { antialias: true } );
  44. gemBackMaterial = new THREE.MeshPhysicalMaterial( {
  45. map: null,
  46. color: 0x0000ff,
  47. metalness: 1,
  48. roughness: 0,
  49. opacity: 0.5,
  50. side: THREE.BackSide,
  51. transparent: true,
  52. envMapIntensity: 5,
  53. premultipliedAlpha: true
  54. // TODO: Add custom blend mode that modulates background color by this materials color.
  55. } );
  56. gemFrontMaterial = new THREE.MeshPhysicalMaterial( {
  57. map: null,
  58. color: 0x0000ff,
  59. metalness: 0,
  60. roughness: 0,
  61. opacity: 0.25,
  62. side: THREE.FrontSide,
  63. transparent: true,
  64. envMapIntensity: 10,
  65. premultipliedAlpha: true
  66. } );
  67. var manager = new THREE.LoadingManager();
  68. manager.onProgress = function ( item, loaded, total ) {
  69. console.log( item, loaded, total );
  70. };
  71. var loader = new OBJLoader( manager );
  72. loader.load( 'models/obj/emerald.obj', function ( object ) {
  73. object.traverse( function ( child ) {
  74. if ( child instanceof THREE.Mesh ) {
  75. child.material = gemBackMaterial;
  76. var second = child.clone();
  77. second.material = gemFrontMaterial;
  78. var parent = new THREE.Group();
  79. parent.add( second );
  80. parent.add( child );
  81. scene.add( parent );
  82. objects.push( parent );
  83. }
  84. } );
  85. } );
  86. new RGBELoader()
  87. .setDataType( THREE.UnsignedByteType )
  88. .setPath( 'textures/equirectangular/' )
  89. .load( 'royal_esplanade_1k.hdr', function ( hdrEquirect ) {
  90. hdrCubeRenderTarget = pmremGenerator.fromEquirectangular( hdrEquirect );
  91. pmremGenerator.dispose();
  92. gemFrontMaterial.envMap = gemBackMaterial.envMap = hdrCubeRenderTarget.texture;
  93. gemFrontMaterial.needsUpdate = gemBackMaterial.needsUpdate = true;
  94. hdrEquirect.dispose();
  95. } );
  96. var pmremGenerator = new THREE.PMREMGenerator( renderer );
  97. pmremGenerator.compileEquirectangularShader();
  98. // Lights
  99. scene.add( new THREE.AmbientLight( 0x222222 ) );
  100. var pointLight1 = new THREE.PointLight( 0xffffff );
  101. pointLight1.position.set( 150, 10, 0 );
  102. pointLight1.castShadow = false;
  103. scene.add( pointLight1 );
  104. var pointLight2 = new THREE.PointLight( 0xffffff );
  105. pointLight2.position.set( - 150, 0, 0 );
  106. scene.add( pointLight2 );
  107. var pointLight3 = new THREE.PointLight( 0xffffff );
  108. pointLight3.position.set( 0, - 10, - 150 );
  109. scene.add( pointLight3 );
  110. var pointLight4 = new THREE.PointLight( 0xffffff );
  111. pointLight4.position.set( 0, 0, 150 );
  112. scene.add( pointLight4 );
  113. renderer.setPixelRatio( window.devicePixelRatio );
  114. renderer.setSize( window.innerWidth, window.innerHeight );
  115. renderer.shadowMap.enabled = true;
  116. container.appendChild( renderer.domElement );
  117. renderer.outputEncoding = THREE.sRGBEncoding;
  118. stats = new Stats();
  119. container.appendChild( stats.dom );
  120. var controls = new OrbitControls( camera, renderer.domElement );
  121. window.addEventListener( 'resize', onWindowResize, false );
  122. var gui = new GUI();
  123. gui.add( params, 'reflectivity', 0, 1 );
  124. gui.add( params, 'exposure', 0.1, 2 );
  125. gui.add( params, 'autoRotate' );
  126. gui.add( params, 'gemColor', [ 'Blue', 'Green', 'Red', 'White', 'Black' ] );
  127. gui.open();
  128. }
  129. function onWindowResize() {
  130. var width = window.innerWidth;
  131. var height = window.innerHeight;
  132. camera.aspect = width / height;
  133. camera.updateProjectionMatrix();
  134. renderer.setSize( width, height );
  135. }
  136. //
  137. function animate() {
  138. requestAnimationFrame( animate );
  139. stats.begin();
  140. render();
  141. stats.end();
  142. }
  143. function render() {
  144. if ( gemBackMaterial !== undefined && gemFrontMaterial !== undefined ) {
  145. gemFrontMaterial.reflectivity = gemBackMaterial.reflectivity = params.reflectivity;
  146. var newColor = gemBackMaterial.color;
  147. switch ( params.gemColor ) {
  148. case 'Blue': newColor = new THREE.Color( 0x000088 ); break;
  149. case 'Red': newColor = new THREE.Color( 0x880000 ); break;
  150. case 'Green': newColor = new THREE.Color( 0x008800 ); break;
  151. case 'White': newColor = new THREE.Color( 0x888888 ); break;
  152. case 'Black': newColor = new THREE.Color( 0x0f0f0f ); break;
  153. }
  154. gemBackMaterial.color = gemFrontMaterial.color = newColor;
  155. }
  156. renderer.toneMappingExposure = params.exposure;
  157. camera.lookAt( scene.position );
  158. if ( params.autoRotate ) {
  159. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  160. var object = objects[ i ];
  161. object.rotation.y += 0.005;
  162. }
  163. }
  164. renderer.render( scene, camera );
  165. }
  166. </script>
  167. </body>
  168. </html>