webgl_furnace_test.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. </div>
  15. </div>
  16. <script type="module">
  17. import * as THREE from '../build/three.module.js';
  18. var scene, camera, renderer, radianceMap;
  19. function init() {
  20. var width = window.innerWidth;
  21. var height = window.innerHeight;
  22. var aspect = width / height;
  23. // renderer
  24. renderer = new THREE.WebGLRenderer( { antialias: true } );
  25. renderer.setSize( width, height );
  26. renderer.setPixelRatio( window.devicePixelRatio );
  27. document.body.appendChild( renderer.domElement );
  28. window.addEventListener( 'resize', onResize, false );
  29. document.body.addEventListener( 'mouseover', function () {
  30. scene.traverse( function ( child ) {
  31. if ( child.isMesh ) child.material.color.setHex( 0xaaaaff );
  32. } );
  33. } );
  34. document.body.addEventListener( 'mouseout', function () {
  35. scene.traverse( function ( child ) {
  36. if ( child.isMesh ) child.material.color.setHex( 0xffffff );
  37. } );
  38. } );
  39. // scene
  40. scene = new THREE.Scene();
  41. // camera
  42. camera = new THREE.PerspectiveCamera( 40, aspect, 1, 30 );
  43. camera.position.set( 0, 0, 18 );
  44. }
  45. function createObjects() {
  46. var geometry = new THREE.SphereBufferGeometry( 0.4, 32, 32 );
  47. for ( var x = 0; x <= 10; x ++ ) {
  48. for ( var y = 0; y <= 10; y ++ ) {
  49. var material = new THREE.MeshPhysicalMaterial( {
  50. roughness: x / 10,
  51. metalness: y / 10,
  52. color: 0xffffff,
  53. envMap: radianceMap,
  54. envMapIntensity: 1,
  55. reflectivity: 1
  56. } );
  57. var mesh = new THREE.Mesh( geometry, material );
  58. mesh.position.x = x - 5;
  59. mesh.position.y = 5 - y;
  60. scene.add( mesh );
  61. }
  62. }
  63. }
  64. function createEnvironment() {
  65. return new Promise( function ( resolve ) {
  66. var envScene = new THREE.Scene();
  67. envScene.background = new THREE.Color( 0xcccccc );
  68. var pmremGenerator = new THREE.PMREMGenerator( renderer );
  69. radianceMap = pmremGenerator.fromScene( envScene ).texture;
  70. pmremGenerator.dispose();
  71. scene.background = radianceMap;
  72. resolve();
  73. } );
  74. }
  75. function onResize() {
  76. var width = window.innerWidth;
  77. var height = window.innerHeight;
  78. camera.aspect = width / height;
  79. camera.updateProjectionMatrix();
  80. renderer.setSize( width, height );
  81. }
  82. function animate() {
  83. renderer.setAnimationLoop( render );
  84. }
  85. function render() {
  86. renderer.render( scene, camera );
  87. }
  88. Promise.resolve()
  89. .then( init )
  90. .then( createEnvironment )
  91. .then( createObjects )
  92. .then( animate );
  93. </script>
  94. </body>
  95. </html>