2
0

webgl_materials_envmaps.html 4.1 KB

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