webgl_materials_envmaps.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. var controls, camera, scene, renderer;
  20. var cameraCube, sceneCube;
  21. var textureEquirec, textureCube, textureSphere;
  22. var cubeMesh, sphereMesh;
  23. var sphereMaterial;
  24. init();
  25. animate();
  26. function init() {
  27. // CAMERAS
  28. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 100000 );
  29. camera.position.set( 0, 0, 1000 );
  30. cameraCube = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 100000 );
  31. // SCENE
  32. scene = new THREE.Scene();
  33. sceneCube = new THREE.Scene();
  34. // Lights
  35. var ambient = new THREE.AmbientLight( 0xffffff );
  36. scene.add( ambient );
  37. // Textures
  38. var r = "textures/cube/Bridge2/";
  39. var urls = [ r + "posx.jpg", r + "negx.jpg",
  40. r + "posy.jpg", r + "negy.jpg",
  41. r + "posz.jpg", r + "negz.jpg" ];
  42. textureCube = new THREE.CubeTextureLoader().load( urls );
  43. textureCube.format = THREE.RGBFormat;
  44. textureCube.mapping = THREE.CubeReflectionMapping;
  45. textureCube.encoding = THREE.sRGBEncoding;
  46. var textureLoader = new THREE.TextureLoader();
  47. textureEquirec = textureLoader.load( "textures/2294472375_24a3b8ef46_o.jpg" );
  48. textureEquirec.mapping = THREE.EquirectangularReflectionMapping;
  49. textureEquirec.magFilter = THREE.LinearFilter;
  50. textureEquirec.minFilter = THREE.LinearMipmapLinearFilter;
  51. textureEquirec.encoding = THREE.sRGBEncoding;
  52. textureSphere = textureLoader.load( "textures/metal.jpg" );
  53. textureSphere.mapping = THREE.SphericalReflectionMapping;
  54. textureSphere.encoding = THREE.sRGBEncoding;
  55. // Materials
  56. var equirectShader = THREE.ShaderLib[ "equirect" ];
  57. var equirectMaterial = new THREE.ShaderMaterial( {
  58. fragmentShader: equirectShader.fragmentShader,
  59. vertexShader: equirectShader.vertexShader,
  60. uniforms: equirectShader.uniforms,
  61. depthWrite: false,
  62. side: THREE.BackSide
  63. } );
  64. equirectMaterial.uniforms[ "tEquirect" ].value = textureEquirec;
  65. // enable code injection for non-built-in material
  66. Object.defineProperty( equirectMaterial, 'map', {
  67. get: function () {
  68. return this.uniforms.tEquirect.value;
  69. }
  70. } );
  71. var cubeShader = THREE.ShaderLib[ "cube" ];
  72. var cubeMaterial = new THREE.ShaderMaterial( {
  73. fragmentShader: cubeShader.fragmentShader,
  74. vertexShader: cubeShader.vertexShader,
  75. uniforms: cubeShader.uniforms,
  76. depthWrite: false,
  77. side: THREE.BackSide
  78. } );
  79. cubeMaterial.envMap = textureCube;
  80. // Skybox
  81. cubeMesh = new THREE.Mesh( new THREE.BoxBufferGeometry( 100, 100, 100 ), cubeMaterial );
  82. sceneCube.add( cubeMesh );
  83. //
  84. var geometry = new THREE.SphereBufferGeometry( 400.0, 48, 24 );
  85. sphereMaterial = new THREE.MeshLambertMaterial( { envMap: textureCube } );
  86. sphereMesh = new THREE.Mesh( geometry, sphereMaterial );
  87. scene.add( sphereMesh );
  88. //
  89. renderer = new THREE.WebGLRenderer();
  90. renderer.autoClear = false;
  91. renderer.setPixelRatio( window.devicePixelRatio );
  92. renderer.setSize( window.innerWidth, window.innerHeight );
  93. document.body.appendChild( renderer.domElement );
  94. renderer.outputEncoding = THREE.sRGBEncoding;
  95. //
  96. controls = new OrbitControls( camera, renderer.domElement );
  97. controls.minDistance = 500;
  98. controls.maxDistance = 2500;
  99. //
  100. var params = {
  101. Cube: function () {
  102. cubeMesh.material = cubeMaterial;
  103. cubeMesh.visible = true;
  104. sphereMaterial.envMap = textureCube;
  105. sphereMaterial.needsUpdate = true;
  106. },
  107. Equirectangular: function () {
  108. cubeMesh.material = equirectMaterial;
  109. cubeMesh.visible = true;
  110. sphereMaterial.envMap = textureEquirec;
  111. sphereMaterial.needsUpdate = true;
  112. },
  113. Spherical: function () {
  114. cubeMesh.visible = false;
  115. sphereMaterial.envMap = textureSphere;
  116. sphereMaterial.needsUpdate = true;
  117. },
  118. Refraction: false
  119. };
  120. var gui = new GUI();
  121. gui.add( params, 'Cube' );
  122. gui.add( params, 'Equirectangular' );
  123. gui.add( params, 'Spherical' );
  124. gui.add( params, 'Refraction' ).onChange( function ( value ) {
  125. if ( value ) {
  126. textureEquirec.mapping = THREE.EquirectangularRefractionMapping;
  127. textureCube.mapping = THREE.CubeRefractionMapping;
  128. } else {
  129. textureEquirec.mapping = THREE.EquirectangularReflectionMapping;
  130. textureCube.mapping = THREE.CubeReflectionMapping;
  131. }
  132. sphereMaterial.needsUpdate = true;
  133. } );
  134. gui.open();
  135. window.addEventListener( 'resize', onWindowResize, false );
  136. }
  137. function onWindowResize() {
  138. camera.aspect = window.innerWidth / window.innerHeight;
  139. camera.updateProjectionMatrix();
  140. cameraCube.aspect = window.innerWidth / window.innerHeight;
  141. cameraCube.updateProjectionMatrix();
  142. renderer.setSize( window.innerWidth, window.innerHeight );
  143. }
  144. //
  145. function animate() {
  146. requestAnimationFrame( animate );
  147. render();
  148. }
  149. function render() {
  150. camera.lookAt( scene.position );
  151. cameraCube.rotation.copy( camera.rotation );
  152. renderer.render( sceneCube, cameraCube );
  153. renderer.render( scene, camera );
  154. }
  155. </script>
  156. </body>
  157. </html>