webgl_furnace_test.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. window.addEventListener( 'resize', onWindowResize );
  43. document.body.addEventListener( 'mouseover', function () {
  44. scene.traverse( function ( child ) {
  45. if ( child.isMesh ) child.material.color.setHex( 0xffffff );
  46. } );
  47. render();
  48. } );
  49. document.body.addEventListener( 'mouseout', function () {
  50. scene.traverse( function ( child ) {
  51. if ( child.isMesh ) child.material.color.setHex( 0xccccff ); // tinted for visibility
  52. } );
  53. render();
  54. } );
  55. // scene
  56. scene = new THREE.Scene();
  57. // camera
  58. camera = new THREE.PerspectiveCamera( 40, aspect, 1, 30 );
  59. camera.position.set( 0, 0, 18 );
  60. }
  61. function createObjects() {
  62. const geometry = new THREE.SphereGeometry( 0.4, 32, 16 );
  63. for ( let x = 0; x <= 10; x ++ ) {
  64. for ( let y = 0; y <= 10; y ++ ) {
  65. const material = new THREE.MeshPhysicalMaterial( {
  66. roughness: x / 10,
  67. metalness: y / 10,
  68. color: 0xffffff,
  69. envMap: radianceMap,
  70. envMapIntensity: 1,
  71. transmission: 0,
  72. ior: 1.5
  73. } );
  74. const mesh = new THREE.Mesh( geometry, material );
  75. mesh.position.x = x - 5;
  76. mesh.position.y = 5 - y;
  77. scene.add( mesh );
  78. }
  79. }
  80. }
  81. function createEnvironment() {
  82. const envScene = new THREE.Scene();
  83. envScene.background = new THREE.Color( COLOR );
  84. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  85. radianceMap = pmremGenerator.fromScene( envScene ).texture;
  86. pmremGenerator.dispose();
  87. scene.background = envScene.background;
  88. }
  89. function onWindowResize() {
  90. const width = window.innerWidth;
  91. const height = window.innerHeight;
  92. camera.aspect = width / height;
  93. camera.updateProjectionMatrix();
  94. renderer.setSize( width, height );
  95. render();
  96. }
  97. function render() {
  98. renderer.render( scene, camera );
  99. }
  100. Promise.resolve()
  101. .then( init )
  102. .then( createEnvironment )
  103. .then( createObjects )
  104. .then( render );
  105. </script>
  106. </body>
  107. </html>