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. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import Stats from 'three/addons/libs/stats.module.js';
  29. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  30. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  31. import { OBJLoader } from 'three/addons/loaders/OBJLoader.js';
  32. import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
  33. let container, stats;
  34. const params = {
  35. projection: 'normal',
  36. autoRotate: true,
  37. reflectivity: 1,
  38. background: false,
  39. exposure: 1,
  40. gemColor: 'Green'
  41. };
  42. let camera, scene, renderer;
  43. let gemBackMaterial, gemFrontMaterial;
  44. const objects = [];
  45. init();
  46. animate();
  47. function init() {
  48. container = document.createElement( 'div' );
  49. document.body.appendChild( container );
  50. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  51. camera.position.set( 0.0, - 10, 20 * 3.5 );
  52. scene = new THREE.Scene();
  53. scene.background = new THREE.Color( 0x000000 );
  54. renderer = new THREE.WebGLRenderer( { antialias: true } );
  55. gemBackMaterial = new THREE.MeshPhysicalMaterial( {
  56. map: null,
  57. color: 0x0000ff,
  58. metalness: 1,
  59. roughness: 0,
  60. opacity: 0.5,
  61. side: THREE.BackSide,
  62. transparent: true,
  63. envMapIntensity: 5,
  64. premultipliedAlpha: true
  65. // TODO: Add custom blend mode that modulates background color by this materials color.
  66. } );
  67. gemFrontMaterial = new THREE.MeshPhysicalMaterial( {
  68. map: null,
  69. color: 0x0000ff,
  70. metalness: 0,
  71. roughness: 0,
  72. opacity: 0.25,
  73. side: THREE.FrontSide,
  74. transparent: true,
  75. envMapIntensity: 10,
  76. premultipliedAlpha: true
  77. } );
  78. const loader = new OBJLoader();
  79. loader.load( 'models/obj/emerald.obj', function ( object ) {
  80. object.traverse( function ( child ) {
  81. if ( child instanceof THREE.Mesh ) {
  82. child.material = gemBackMaterial;
  83. const second = child.clone();
  84. second.material = gemFrontMaterial;
  85. const parent = new THREE.Group();
  86. parent.add( second );
  87. parent.add( child );
  88. scene.add( parent );
  89. objects.push( parent );
  90. }
  91. } );
  92. } );
  93. new RGBELoader()
  94. .setPath( 'textures/equirectangular/' )
  95. .load( 'royal_esplanade_1k.hdr', function ( texture ) {
  96. texture.mapping = THREE.EquirectangularReflectionMapping;
  97. gemFrontMaterial.envMap = gemBackMaterial.envMap = texture;
  98. gemFrontMaterial.needsUpdate = gemBackMaterial.needsUpdate = true;
  99. } );
  100. // Lights
  101. scene.add( new THREE.AmbientLight( 0x222222 ) );
  102. const pointLight1 = new THREE.PointLight( 0xffffff );
  103. pointLight1.position.set( 150, 10, 0 );
  104. pointLight1.castShadow = false;
  105. scene.add( pointLight1 );
  106. const pointLight2 = new THREE.PointLight( 0xffffff );
  107. pointLight2.position.set( - 150, 0, 0 );
  108. scene.add( pointLight2 );
  109. const pointLight3 = new THREE.PointLight( 0xffffff );
  110. pointLight3.position.set( 0, - 10, - 150 );
  111. scene.add( pointLight3 );
  112. const pointLight4 = new THREE.PointLight( 0xffffff );
  113. pointLight4.position.set( 0, 0, 150 );
  114. scene.add( pointLight4 );
  115. renderer.setPixelRatio( window.devicePixelRatio );
  116. renderer.setSize( window.innerWidth, window.innerHeight );
  117. renderer.shadowMap.enabled = true;
  118. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  119. container.appendChild( renderer.domElement );
  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>