webgl_materials_envmaps.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - environment maps</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="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl environment mapping example<br/>
  12. Equirectangular Map by <a href="http://gl.ict.usc.edu/Data/HighResProbes/">University of Southern California</a><br/>
  13. Spherical Map by <a href="http://www.pauldebevec.com/Probes/">Paul Debevec</a>
  14. </div>
  15. <script type="module">
  16. import * as THREE from '../build/three.module.js';
  17. import { GUI } from './jsm/libs/dat.gui.module.js';
  18. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  19. let controls, camera, scene, renderer;
  20. let textureEquirec, textureCube;
  21. let sphereMesh, sphereMaterial;
  22. init();
  23. animate();
  24. function init() {
  25. // CAMERAS
  26. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 100000 );
  27. camera.position.set( 0, 0, 1000 );
  28. // SCENE
  29. scene = new THREE.Scene();
  30. // Lights
  31. const ambient = new THREE.AmbientLight( 0xffffff );
  32. scene.add( ambient );
  33. // Textures
  34. const loader = new THREE.CubeTextureLoader();
  35. loader.setPath( 'textures/cube/Bridge2/' );
  36. textureCube = loader.load( [ 'posx.jpg', 'negx.jpg', 'posy.jpg', 'negy.jpg', 'posz.jpg', 'negz.jpg' ] );
  37. textureCube.encoding = THREE.sRGBEncoding;
  38. const textureLoader = new THREE.TextureLoader();
  39. textureEquirec = textureLoader.load( 'textures/2294472375_24a3b8ef46_o.jpg' );
  40. textureEquirec.mapping = THREE.EquirectangularReflectionMapping;
  41. textureEquirec.encoding = THREE.sRGBEncoding;
  42. scene.background = textureCube;
  43. //
  44. const geometry = new THREE.IcosahedronGeometry( 400, 15 );
  45. sphereMaterial = new THREE.MeshLambertMaterial( { envMap: textureCube } );
  46. sphereMesh = new THREE.Mesh( geometry, sphereMaterial );
  47. scene.add( sphereMesh );
  48. //
  49. renderer = new THREE.WebGLRenderer();
  50. renderer.setPixelRatio( window.devicePixelRatio );
  51. renderer.setSize( window.innerWidth, window.innerHeight );
  52. renderer.outputEncoding = THREE.sRGBEncoding;
  53. document.body.appendChild( renderer.domElement );
  54. //
  55. controls = new OrbitControls( camera, renderer.domElement );
  56. controls.minDistance = 500;
  57. controls.maxDistance = 2500;
  58. //
  59. const params = {
  60. Cube: function () {
  61. scene.background = textureCube;
  62. sphereMaterial.envMap = textureCube;
  63. sphereMaterial.needsUpdate = true;
  64. },
  65. Equirectangular: function () {
  66. scene.background = textureEquirec;
  67. sphereMaterial.envMap = textureEquirec;
  68. sphereMaterial.needsUpdate = true;
  69. },
  70. Refraction: false
  71. };
  72. const gui = new GUI();
  73. gui.add( params, 'Cube' );
  74. gui.add( params, 'Equirectangular' );
  75. gui.add( params, 'Refraction' ).onChange( function ( value ) {
  76. if ( value ) {
  77. textureEquirec.mapping = THREE.EquirectangularRefractionMapping;
  78. textureCube.mapping = THREE.CubeRefractionMapping;
  79. } else {
  80. textureEquirec.mapping = THREE.EquirectangularReflectionMapping;
  81. textureCube.mapping = THREE.CubeReflectionMapping;
  82. }
  83. sphereMaterial.needsUpdate = true;
  84. } );
  85. gui.open();
  86. window.addEventListener( 'resize', onWindowResize );
  87. }
  88. function onWindowResize() {
  89. camera.aspect = window.innerWidth / window.innerHeight;
  90. camera.updateProjectionMatrix();
  91. renderer.setSize( window.innerWidth, window.innerHeight );
  92. }
  93. //
  94. function animate() {
  95. requestAnimationFrame( animate );
  96. render();
  97. }
  98. function render() {
  99. camera.lookAt( scene.position );
  100. renderer.render( scene, camera );
  101. }
  102. </script>
  103. </body>
  104. </html>