webgl_materials_physical_reflectivity.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. <!-- Import maps polyfill -->
  16. <!-- Remove this when import maps will be widely supported -->
  17. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.module.js"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import Stats from './jsm/libs/stats.module.js';
  28. import { GUI } from './jsm/libs/lil-gui.module.min.js';
  29. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  30. import { OBJLoader } from './jsm/loaders/OBJLoader.js';
  31. import { RGBELoader } from './jsm/loaders/RGBELoader.js';
  32. let container, stats;
  33. const params = {
  34. projection: 'normal',
  35. autoRotate: true,
  36. reflectivity: 1,
  37. background: false,
  38. exposure: 1,
  39. gemColor: 'Green'
  40. };
  41. let camera, scene, renderer;
  42. let gemBackMaterial, gemFrontMaterial;
  43. const objects = [];
  44. init();
  45. animate();
  46. function init() {
  47. container = document.createElement( 'div' );
  48. document.body.appendChild( container );
  49. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  50. camera.position.set( 0.0, - 10, 20 * 3.5 );
  51. scene = new THREE.Scene();
  52. scene.background = new THREE.Color( 0x000000 );
  53. renderer = new THREE.WebGLRenderer( { antialias: true } );
  54. gemBackMaterial = new THREE.MeshPhysicalMaterial( {
  55. map: null,
  56. color: 0x0000ff,
  57. metalness: 1,
  58. roughness: 0,
  59. opacity: 0.5,
  60. side: THREE.BackSide,
  61. transparent: true,
  62. envMapIntensity: 5,
  63. premultipliedAlpha: true
  64. // TODO: Add custom blend mode that modulates background color by this materials color.
  65. } );
  66. gemFrontMaterial = new THREE.MeshPhysicalMaterial( {
  67. map: null,
  68. color: 0x0000ff,
  69. metalness: 0,
  70. roughness: 0,
  71. opacity: 0.25,
  72. side: THREE.FrontSide,
  73. transparent: true,
  74. envMapIntensity: 10,
  75. premultipliedAlpha: true
  76. } );
  77. const loader = new OBJLoader();
  78. loader.load( 'models/obj/emerald.obj', function ( object ) {
  79. object.traverse( function ( child ) {
  80. if ( child instanceof THREE.Mesh ) {
  81. child.material = gemBackMaterial;
  82. const second = child.clone();
  83. second.material = gemFrontMaterial;
  84. const parent = new THREE.Group();
  85. parent.add( second );
  86. parent.add( child );
  87. scene.add( parent );
  88. objects.push( parent );
  89. }
  90. } );
  91. } );
  92. new RGBELoader()
  93. .setPath( 'textures/equirectangular/' )
  94. .load( 'royal_esplanade_1k.hdr', function ( texture ) {
  95. texture.mapping = THREE.EquirectangularReflectionMapping;
  96. gemFrontMaterial.envMap = gemBackMaterial.envMap = texture;
  97. gemFrontMaterial.needsUpdate = gemBackMaterial.needsUpdate = true;
  98. } );
  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. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  118. container.appendChild( renderer.domElement );
  119. renderer.outputEncoding = THREE.sRGBEncoding;
  120. stats = new Stats();
  121. container.appendChild( stats.dom );
  122. const controls = new OrbitControls( camera, renderer.domElement );
  123. controls.minDistance = 20;
  124. controls.maxDistance = 200;
  125. window.addEventListener( 'resize', onWindowResize );
  126. const gui = new GUI();
  127. gui.add( params, 'reflectivity', 0, 1 );
  128. gui.add( params, 'exposure', 0, 2 );
  129. gui.add( params, 'autoRotate' );
  130. gui.add( params, 'gemColor', [ 'Blue', 'Green', 'Red', 'White', 'Black' ] );
  131. gui.open();
  132. }
  133. function onWindowResize() {
  134. const width = window.innerWidth;
  135. const height = window.innerHeight;
  136. camera.aspect = width / height;
  137. camera.updateProjectionMatrix();
  138. renderer.setSize( width, height );
  139. }
  140. //
  141. function animate() {
  142. requestAnimationFrame( animate );
  143. stats.begin();
  144. render();
  145. stats.end();
  146. }
  147. function render() {
  148. if ( gemBackMaterial !== undefined && gemFrontMaterial !== undefined ) {
  149. gemFrontMaterial.reflectivity = gemBackMaterial.reflectivity = params.reflectivity;
  150. let newColor = gemBackMaterial.color;
  151. switch ( params.gemColor ) {
  152. case 'Blue': newColor = new THREE.Color( 0x000088 ); break;
  153. case 'Red': newColor = new THREE.Color( 0x880000 ); break;
  154. case 'Green': newColor = new THREE.Color( 0x008800 ); break;
  155. case 'White': newColor = new THREE.Color( 0x888888 ); break;
  156. case 'Black': newColor = new THREE.Color( 0x0f0f0f ); break;
  157. }
  158. gemBackMaterial.color = gemFrontMaterial.color = newColor;
  159. }
  160. renderer.toneMappingExposure = params.exposure;
  161. camera.lookAt( scene.position );
  162. if ( params.autoRotate ) {
  163. for ( let i = 0, l = objects.length; i < l; i ++ ) {
  164. const object = objects[ i ];
  165. object.rotation.y += 0.005;
  166. }
  167. }
  168. renderer.render( scene, camera );
  169. }
  170. </script>
  171. </body>
  172. </html>