webgl_pmrem_test.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js PMREM directional light 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="http://threejs.org" target="_blank" rel="noopener">three.js</a> -
  13. PMREM directional light test <a href="https://github.com/elalish" target="_blank" rel="noopener">Emmett Lalish</a>
  14. </div>
  15. </div>
  16. <script type="module">
  17. import * as THREE from '../build/three.module.js';
  18. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  19. import { RGBELoader } from './jsm/loaders/RGBELoader.js';
  20. import { PMREMGenerator } from './jsm/pmrem/PMREMGenerator.js';
  21. var scene, camera, controls, renderer;
  22. function init() {
  23. var width = window.innerWidth;
  24. var height = window.innerHeight;
  25. var aspect = width / height;
  26. // renderer
  27. renderer = new THREE.WebGLRenderer( { antialias: true } );
  28. renderer.setSize( width, height );
  29. renderer.setPixelRatio( window.devicePixelRatio );
  30. renderer.gammaOutput = true;
  31. document.body.appendChild( renderer.domElement );
  32. window.addEventListener( 'resize', onResize, false );
  33. // scene
  34. scene = new THREE.Scene();
  35. // camera
  36. camera = new THREE.PerspectiveCamera( 40, aspect, 1, 30 );
  37. camera.position.set( 0, 0, 18 );
  38. // controls
  39. controls = new OrbitControls( camera, renderer.domElement );
  40. controls.update();
  41. // light
  42. var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
  43. var x = 597;
  44. var y = 213;
  45. var theta = (x + 0.5) * Math.PI / 512;
  46. var phi = (y + 0.5) * Math.PI / 512;
  47. directionalLight.position.set(
  48. -Math.sin(phi) * Math.cos(theta),
  49. Math.cos(phi),
  50. -Math.sin(phi) * Math.sin(theta));
  51. scene.add( directionalLight );
  52. document.body.addEventListener( 'mouseover', function () {
  53. scene.traverse( function ( child ) {
  54. if ( child.isMesh ) {
  55. child.material.envMapIntensity = 1;
  56. directionalLight.intensity = 0;
  57. }
  58. } );
  59. } );
  60. document.body.addEventListener( 'mouseout', function () {
  61. scene.traverse( function ( child ) {
  62. if ( child.isMesh ) {
  63. child.material.envMapIntensity = 0;
  64. directionalLight.intensity = 1;
  65. }
  66. } );
  67. } );
  68. }
  69. function createObjects() {
  70. var radianceMap = null;
  71. new RGBELoader()
  72. .setDataType( THREE.UnsignedByteType )
  73. .setPath( 'textures/equirectangular/' )
  74. .load( 'pedestrian_overpass_1k.hdr', function ( texture ) {
  75. var pmremGenerator = new PMREMGenerator( renderer );
  76. radianceMap = pmremGenerator.fromEquirectangular( texture ).texture;
  77. pmremGenerator.dispose();
  78. scene.background = radianceMap;
  79. var geometry = new THREE.SphereBufferGeometry( 0.4, 32, 32 );
  80. for ( var x = 0; x <= 10; x ++ ) {
  81. for ( var y = 0; y <= 2; y ++ ) {
  82. var material = new THREE.MeshPhysicalMaterial( {
  83. roughness: x / 10,
  84. metalness: y < 1 ? 1 : 0,
  85. color: y < 2 ? 0xffffff : 0x000000,
  86. envMap: radianceMap,
  87. envMapIntensity: 1
  88. } );
  89. var mesh = new THREE.Mesh( geometry, material );
  90. mesh.position.x = x - 5;
  91. mesh.position.y = 1 - y;
  92. scene.add( mesh );
  93. }
  94. }
  95. } );
  96. }
  97. function onResize() {
  98. var width = window.innerWidth;
  99. var height = window.innerHeight;
  100. camera.aspect = width / height;
  101. camera.updateProjectionMatrix();
  102. renderer.setSize( width, height );
  103. }
  104. function animate() {
  105. renderer.setAnimationLoop( render );
  106. }
  107. function render() {
  108. renderer.render( scene, camera );
  109. }
  110. Promise.resolve()
  111. .then( init )
  112. .then( createObjects )
  113. .then( animate );
  114. </script>
  115. </body>
  116. </html>