webgl_materials_envmaps.html 3.8 KB

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