1
0

webgl_materials_envmaps.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.module.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  25. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  26. let controls, camera, scene, renderer;
  27. let textureEquirec, textureCube;
  28. let sphereMesh, sphereMaterial, params;
  29. init();
  30. function init() {
  31. // CAMERAS
  32. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 100 );
  33. camera.position.set( 0, 0, 2.5 );
  34. // SCENE
  35. scene = new THREE.Scene();
  36. // Textures
  37. const loader = new THREE.CubeTextureLoader();
  38. loader.setPath( 'textures/cube/Bridge2/' );
  39. textureCube = loader.load( [ 'posx.jpg', 'negx.jpg', 'posy.jpg', 'negy.jpg', 'posz.jpg', 'negz.jpg' ] );
  40. const textureLoader = new THREE.TextureLoader();
  41. textureEquirec = textureLoader.load( 'textures/2294472375_24a3b8ef46_o.jpg' );
  42. textureEquirec.mapping = THREE.EquirectangularReflectionMapping;
  43. textureEquirec.colorSpace = THREE.SRGBColorSpace;
  44. scene.background = textureCube;
  45. //
  46. const geometry = new THREE.IcosahedronGeometry( 1, 15 );
  47. sphereMaterial = new THREE.MeshBasicMaterial( { envMap: textureCube } );
  48. sphereMesh = new THREE.Mesh( geometry, sphereMaterial );
  49. scene.add( sphereMesh );
  50. //
  51. renderer = new THREE.WebGLRenderer();
  52. renderer.setPixelRatio( window.devicePixelRatio );
  53. renderer.setSize( window.innerWidth, window.innerHeight );
  54. renderer.setAnimationLoop( animate );
  55. document.body.appendChild( renderer.domElement );
  56. //
  57. controls = new OrbitControls( camera, renderer.domElement );
  58. controls.minDistance = 1.5;
  59. controls.maxDistance = 6;
  60. //
  61. params = {
  62. Cube: function () {
  63. scene.background = textureCube;
  64. sphereMaterial.envMap = textureCube;
  65. sphereMaterial.needsUpdate = true;
  66. },
  67. Equirectangular: function () {
  68. scene.background = textureEquirec;
  69. sphereMaterial.envMap = textureEquirec;
  70. sphereMaterial.needsUpdate = true;
  71. },
  72. Refraction: false,
  73. backgroundRotationX: false,
  74. backgroundRotationY: false,
  75. backgroundRotationZ: false,
  76. syncMaterial: false
  77. };
  78. const gui = new GUI( { width: 300 } );
  79. gui.add( params, 'Cube' );
  80. gui.add( params, 'Equirectangular' );
  81. gui.add( params, 'Refraction' ).onChange( function ( value ) {
  82. if ( value ) {
  83. textureEquirec.mapping = THREE.EquirectangularRefractionMapping;
  84. textureCube.mapping = THREE.CubeRefractionMapping;
  85. } else {
  86. textureEquirec.mapping = THREE.EquirectangularReflectionMapping;
  87. textureCube.mapping = THREE.CubeReflectionMapping;
  88. }
  89. sphereMaterial.needsUpdate = true;
  90. } );
  91. gui.add( params, 'backgroundRotationX' );
  92. gui.add( params, 'backgroundRotationY' );
  93. gui.add( params, 'backgroundRotationZ' );
  94. gui.add( params, 'syncMaterial' );
  95. gui.open();
  96. window.addEventListener( 'resize', onWindowResize );
  97. }
  98. function onWindowResize() {
  99. camera.aspect = window.innerWidth / window.innerHeight;
  100. camera.updateProjectionMatrix();
  101. renderer.setSize( window.innerWidth, window.innerHeight );
  102. }
  103. //
  104. function animate() {
  105. if ( params.backgroundRotationX ) {
  106. scene.backgroundRotation.x += 0.001;
  107. }
  108. if ( params.backgroundRotationY ) {
  109. scene.backgroundRotation.y += 0.001;
  110. }
  111. if ( params.backgroundRotationZ ) {
  112. scene.backgroundRotation.z += 0.001;
  113. }
  114. if ( params.syncMaterial ) {
  115. sphereMesh.material.envMapRotation.copy( scene.backgroundRotation );
  116. }
  117. camera.lookAt( scene.position );
  118. renderer.render( scene, camera );
  119. }
  120. </script>
  121. </body>
  122. </html>