webgl_furnace_test.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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>Roughness increases left to right
  15. <br>Metalness increases top to bottom
  16. </div>
  17. </div>
  18. <!-- Import maps polyfill -->
  19. <!-- Remove this when import maps will be widely supported -->
  20. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  21. <script type="importmap">
  22. {
  23. "imports": {
  24. "three": "../build/three.module.js",
  25. "three/addons/": "./jsm/"
  26. }
  27. }
  28. </script>
  29. <script type="module">
  30. import * as THREE from 'three';
  31. let scene, camera, renderer, radianceMap;
  32. const COLOR = 0xcccccc;
  33. function init() {
  34. const width = window.innerWidth;
  35. const height = window.innerHeight;
  36. const aspect = width / height;
  37. // renderer
  38. renderer = new THREE.WebGLRenderer( { antialias: true } );
  39. renderer.setSize( width, height );
  40. renderer.setPixelRatio( window.devicePixelRatio );
  41. document.body.appendChild( renderer.domElement );
  42. //renderer.outputEncoding = THREE.sRGBEncoding; // optional
  43. window.addEventListener( 'resize', onWindowResize );
  44. document.body.addEventListener( 'mouseover', function () {
  45. scene.traverse( function ( child ) {
  46. if ( child.isMesh ) child.material.color.setHex( 0xffffff );
  47. } );
  48. render();
  49. } );
  50. document.body.addEventListener( 'mouseout', function () {
  51. scene.traverse( function ( child ) {
  52. if ( child.isMesh ) child.material.color.setHex( 0xccccff ); // tinted for visibility
  53. } );
  54. render();
  55. } );
  56. // scene
  57. scene = new THREE.Scene();
  58. // camera
  59. camera = new THREE.PerspectiveCamera( 40, aspect, 1, 30 );
  60. camera.position.set( 0, 0, 18 );
  61. }
  62. function createObjects() {
  63. const geometry = new THREE.SphereGeometry( 0.4, 32, 16 );
  64. for ( let x = 0; x <= 10; x ++ ) {
  65. for ( let y = 0; y <= 10; y ++ ) {
  66. const material = new THREE.MeshPhysicalMaterial( {
  67. roughness: x / 10,
  68. metalness: y / 10,
  69. color: 0xffffff,
  70. envMap: radianceMap,
  71. envMapIntensity: 1,
  72. transmission: 0,
  73. ior: 1.5
  74. } );
  75. const mesh = new THREE.Mesh( geometry, material );
  76. mesh.position.x = x - 5;
  77. mesh.position.y = 5 - y;
  78. scene.add( mesh );
  79. }
  80. }
  81. }
  82. function createEnvironment() {
  83. const envScene = new THREE.Scene();
  84. envScene.background = new THREE.Color( COLOR );
  85. if ( renderer.outputEncoding === THREE.sRGBEncoding ) envScene.background.convertSRGBToLinear();
  86. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  87. radianceMap = pmremGenerator.fromScene( envScene ).texture;
  88. pmremGenerator.dispose();
  89. scene.background = radianceMap;
  90. }
  91. function onWindowResize() {
  92. const width = window.innerWidth;
  93. const height = window.innerHeight;
  94. camera.aspect = width / height;
  95. camera.updateProjectionMatrix();
  96. renderer.setSize( width, height );
  97. render();
  98. }
  99. function render() {
  100. renderer.render( scene, camera );
  101. }
  102. Promise.resolve()
  103. .then( init )
  104. .then( createEnvironment )
  105. .then( createObjects )
  106. .then( render );
  107. </script>
  108. </body>
  109. </html>