webgl_furnace_test.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js white furnace test</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">
  11. <div id="info">
  12. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> -
  13. <a href="https://google.github.io/filament/Filament.md.html#toc4.7.2" target="_blank" rel="noopener">White Furnace</a> energy conservation test by <a href="https://jsantell.com/" target="_blank" rel="noopener">Jordan Santell</a>
  14. <br>Rougnness increases left to right
  15. <br>Metalness increases top to bottom
  16. </div>
  17. </div>
  18. <script type="module">
  19. import * as THREE from '../build/three.module.js';
  20. let scene, camera, renderer, radianceMap;
  21. const frustumSize = 14;
  22. const COLOR = 0xcccccc;
  23. function init() {
  24. const width = window.innerWidth;
  25. const height = window.innerHeight;
  26. const aspect = width / height;
  27. // renderer
  28. renderer = new THREE.WebGLRenderer( { antialias: true } );
  29. renderer.setSize( width, height );
  30. renderer.setPixelRatio( window.devicePixelRatio );
  31. document.body.appendChild( renderer.domElement );
  32. //renderer.outputEncoding = THREE.sRGBEncoding; // optional
  33. window.addEventListener( 'resize', onWindowResize );
  34. document.body.addEventListener( 'mouseover', function () {
  35. scene.traverse( function ( child ) {
  36. if ( child.isMesh ) child.material.color.setHex( 0xccccff ); // tinted for visibility
  37. render();
  38. } );
  39. } );
  40. document.body.addEventListener( 'mouseout', function () {
  41. scene.traverse( function ( child ) {
  42. if ( child.isMesh ) child.material.color.setHex( 0xffffff );
  43. render();
  44. } );
  45. } );
  46. // scene
  47. scene = new THREE.Scene();
  48. // camera
  49. camera = new THREE.OrthographicCamera( frustumSize * aspect / - 2, frustumSize * aspect / 2, frustumSize / 2, frustumSize / - 2, 1, 30 );
  50. camera.position.set( 0, 0, 10 );
  51. }
  52. function createObjects() {
  53. const geometry = new THREE.SphereGeometry( 0.4, 32, 16 );
  54. for ( let x = 0; x <= 10; x ++ ) {
  55. for ( let y = 0; y <= 10; y ++ ) {
  56. const material = new THREE.MeshPhysicalMaterial( {
  57. roughness: x / 10,
  58. metalness: y / 10,
  59. color: 0xffffff,
  60. envMap: radianceMap,
  61. envMapIntensity: 1,
  62. reflectivity: 0.5
  63. } );
  64. const mesh = new THREE.Mesh( geometry, material );
  65. mesh.position.x = x - 5;
  66. mesh.position.y = 5 - y;
  67. scene.add( mesh );
  68. }
  69. }
  70. }
  71. function createEnvironment() {
  72. return new Promise( function ( resolve ) {
  73. const envScene = new THREE.Scene();
  74. envScene.background = new THREE.Color( COLOR ); // sRGB colorspace assumed, apparently
  75. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  76. radianceMap = pmremGenerator.fromScene( envScene ).texture;
  77. pmremGenerator.dispose();
  78. scene.background = new THREE.Color( COLOR );
  79. if ( renderer.outputEncoding !== THREE.sRGBEncoding ) scene.background.convertSRGBToLinear();
  80. resolve();
  81. } );
  82. }
  83. function onWindowResize() {
  84. const aspect = window.innerWidth / window.innerHeight;
  85. camera.left = - frustumSize * aspect / 2;
  86. camera.right = frustumSize * aspect / 2;
  87. camera.top = frustumSize / 2;
  88. camera.bottom = - frustumSize / 2;
  89. camera.updateProjectionMatrix();
  90. renderer.setSize( window.innerWidth, window.innerHeight );
  91. render();
  92. }
  93. function render() {
  94. renderer.render( scene, camera );
  95. }
  96. Promise.resolve()
  97. .then( init )
  98. .then( createEnvironment )
  99. .then( createObjects )
  100. .then( render );
  101. </script>
  102. </body>
  103. </html>