webgl_materials_physical_reflectivity.html 6.3 KB

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