2
0

webgl_materials_physical_reflectivity.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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,
  27. background: false,
  28. exposure: 1,
  29. gemColor: 'Green'
  30. };
  31. let camera, scene, renderer;
  32. let gemBackMaterial, gemFrontMaterial;
  33. const objects = [];
  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, 1000 );
  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. const manager = new THREE.LoadingManager();
  68. manager.onProgress = function ( item, loaded, total ) {
  69. console.log( item, loaded, total );
  70. };
  71. const 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. const second = child.clone();
  77. second.material = gemFrontMaterial;
  78. const 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. .setPath( 'textures/equirectangular/' )
  88. .load( 'royal_esplanade_1k.hdr', function ( texture ) {
  89. texture.mapping = THREE.EquirectangularReflectionMapping;
  90. gemFrontMaterial.envMap = gemBackMaterial.envMap = texture;
  91. gemFrontMaterial.needsUpdate = gemBackMaterial.needsUpdate = true;
  92. } );
  93. // Lights
  94. scene.add( new THREE.AmbientLight( 0x222222 ) );
  95. const pointLight1 = new THREE.PointLight( 0xffffff );
  96. pointLight1.position.set( 150, 10, 0 );
  97. pointLight1.castShadow = false;
  98. scene.add( pointLight1 );
  99. const pointLight2 = new THREE.PointLight( 0xffffff );
  100. pointLight2.position.set( - 150, 0, 0 );
  101. scene.add( pointLight2 );
  102. const pointLight3 = new THREE.PointLight( 0xffffff );
  103. pointLight3.position.set( 0, - 10, - 150 );
  104. scene.add( pointLight3 );
  105. const pointLight4 = new THREE.PointLight( 0xffffff );
  106. pointLight4.position.set( 0, 0, 150 );
  107. scene.add( pointLight4 );
  108. renderer.setPixelRatio( window.devicePixelRatio );
  109. renderer.setSize( window.innerWidth, window.innerHeight );
  110. renderer.shadowMap.enabled = true;
  111. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  112. container.appendChild( renderer.domElement );
  113. renderer.outputEncoding = THREE.sRGBEncoding;
  114. stats = new Stats();
  115. container.appendChild( stats.dom );
  116. const controls = new OrbitControls( camera, renderer.domElement );
  117. controls.minDistance = 20;
  118. controls.maxDistance = 200;
  119. window.addEventListener( 'resize', onWindowResize );
  120. const gui = new GUI();
  121. gui.add( params, 'reflectivity', 0, 1 );
  122. gui.add( params, 'exposure', 0, 2 );
  123. gui.add( params, 'autoRotate' );
  124. gui.add( params, 'gemColor', [ 'Blue', 'Green', 'Red', 'White', 'Black' ] );
  125. gui.open();
  126. }
  127. function onWindowResize() {
  128. const width = window.innerWidth;
  129. const height = window.innerHeight;
  130. camera.aspect = width / height;
  131. camera.updateProjectionMatrix();
  132. renderer.setSize( width, height );
  133. }
  134. //
  135. function animate() {
  136. requestAnimationFrame( animate );
  137. stats.begin();
  138. render();
  139. stats.end();
  140. }
  141. function render() {
  142. if ( gemBackMaterial !== undefined && gemFrontMaterial !== undefined ) {
  143. gemFrontMaterial.reflectivity = gemBackMaterial.reflectivity = params.reflectivity;
  144. let newColor = gemBackMaterial.color;
  145. switch ( params.gemColor ) {
  146. case 'Blue': newColor = new THREE.Color( 0x000088 ); break;
  147. case 'Red': newColor = new THREE.Color( 0x880000 ); break;
  148. case 'Green': newColor = new THREE.Color( 0x008800 ); break;
  149. case 'White': newColor = new THREE.Color( 0x888888 ); break;
  150. case 'Black': newColor = new THREE.Color( 0x0f0f0f ); break;
  151. }
  152. gemBackMaterial.color = gemFrontMaterial.color = newColor;
  153. }
  154. renderer.toneMappingExposure = params.exposure;
  155. camera.lookAt( scene.position );
  156. if ( params.autoRotate ) {
  157. for ( let i = 0, l = objects.length; i < l; i ++ ) {
  158. const object = objects[ i ];
  159. object.rotation.y += 0.005;
  160. }
  161. }
  162. renderer.render( scene, camera );
  163. }
  164. </script>
  165. </body>
  166. </html>