123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - materials - environment maps</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl environment mapping example<br/>
- Equirectangular Map by <a href="http://gl.ict.usc.edu/Data/HighResProbes/">University of Southern California</a><br/>
- Spherical Map by <a href="http://www.pauldebevec.com/Probes/">Paul Debevec</a>
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import { GUI } from './jsm/libs/dat.gui.module.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- var controls, camera, scene, renderer;
- var cameraCube, sceneCube;
- var textureEquirec, textureCube, textureSphere;
- var cubeMesh, sphereMesh;
- var sphereMaterial;
- init();
- animate();
- function init() {
- // CAMERAS
- camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 100000 );
- camera.position.set( 0, 0, 1000 );
- cameraCube = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 100000 );
- // SCENE
- scene = new THREE.Scene();
- sceneCube = new THREE.Scene();
- // Lights
- var ambient = new THREE.AmbientLight( 0xffffff );
- scene.add( ambient );
- // Textures
- var r = "textures/cube/Bridge2/";
- var urls = [ r + "posx.jpg", r + "negx.jpg",
- r + "posy.jpg", r + "negy.jpg",
- r + "posz.jpg", r + "negz.jpg" ];
- textureCube = new THREE.CubeTextureLoader().load( urls );
- textureCube.format = THREE.RGBFormat;
- textureCube.mapping = THREE.CubeReflectionMapping;
- textureCube.encoding = THREE.sRGBEncoding;
- var textureLoader = new THREE.TextureLoader();
- textureEquirec = textureLoader.load( "textures/2294472375_24a3b8ef46_o.jpg" );
- textureEquirec.mapping = THREE.EquirectangularReflectionMapping;
- textureEquirec.magFilter = THREE.LinearFilter;
- textureEquirec.minFilter = THREE.LinearMipmapLinearFilter;
- textureEquirec.encoding = THREE.sRGBEncoding;
- textureSphere = textureLoader.load( "textures/metal.jpg" );
- textureSphere.mapping = THREE.SphericalReflectionMapping;
- textureSphere.encoding = THREE.sRGBEncoding;
- // Materials
- var equirectShader = THREE.ShaderLib[ "equirect" ];
- var equirectMaterial = new THREE.ShaderMaterial( {
- fragmentShader: equirectShader.fragmentShader,
- vertexShader: equirectShader.vertexShader,
- uniforms: equirectShader.uniforms,
- depthWrite: false,
- side: THREE.BackSide
- } );
- equirectMaterial.uniforms[ "tEquirect" ].value = textureEquirec;
- // enable code injection for non-built-in material
- Object.defineProperty( equirectMaterial, 'map', {
- get: function () {
- return this.uniforms.tEquirect.value;
- }
- } );
- var cubeShader = THREE.ShaderLib[ "cube" ];
- var cubeMaterial = new THREE.ShaderMaterial( {
- fragmentShader: cubeShader.fragmentShader,
- vertexShader: cubeShader.vertexShader,
- uniforms: cubeShader.uniforms,
- depthWrite: false,
- side: THREE.BackSide
- } );
- cubeMaterial.envMap = textureCube;
- // Skybox
- cubeMesh = new THREE.Mesh( new THREE.BoxBufferGeometry( 100, 100, 100 ), cubeMaterial );
- sceneCube.add( cubeMesh );
- //
- var geometry = new THREE.SphereBufferGeometry( 400.0, 48, 24 );
- sphereMaterial = new THREE.MeshLambertMaterial( { envMap: textureCube } );
- sphereMesh = new THREE.Mesh( geometry, sphereMaterial );
- scene.add( sphereMesh );
- //
- renderer = new THREE.WebGLRenderer();
- renderer.autoClear = false;
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- document.body.appendChild( renderer.domElement );
- renderer.outputEncoding = THREE.sRGBEncoding;
- //
- controls = new OrbitControls( camera, renderer.domElement );
- controls.minDistance = 500;
- controls.maxDistance = 2500;
- //
- var params = {
- Cube: function () {
- cubeMesh.material = cubeMaterial;
- cubeMesh.visible = true;
- sphereMaterial.envMap = textureCube;
- sphereMaterial.needsUpdate = true;
- },
- Equirectangular: function () {
- cubeMesh.material = equirectMaterial;
- cubeMesh.visible = true;
- sphereMaterial.envMap = textureEquirec;
- sphereMaterial.needsUpdate = true;
- },
- Spherical: function () {
- cubeMesh.visible = false;
- sphereMaterial.envMap = textureSphere;
- sphereMaterial.needsUpdate = true;
- },
- Refraction: false
- };
- var gui = new GUI();
- gui.add( params, 'Cube' );
- gui.add( params, 'Equirectangular' );
- gui.add( params, 'Spherical' );
- gui.add( params, 'Refraction' ).onChange( function ( value ) {
- if ( value ) {
- textureEquirec.mapping = THREE.EquirectangularRefractionMapping;
- textureCube.mapping = THREE.CubeRefractionMapping;
- } else {
- textureEquirec.mapping = THREE.EquirectangularReflectionMapping;
- textureCube.mapping = THREE.CubeReflectionMapping;
- }
- sphereMaterial.needsUpdate = true;
- } );
- gui.open();
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- cameraCube.aspect = window.innerWidth / window.innerHeight;
- cameraCube.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- //
- function animate() {
- requestAnimationFrame( animate );
- render();
- }
- function render() {
- camera.lookAt( scene.position );
- cameraCube.rotation.copy( camera.rotation );
- renderer.render( sceneCube, cameraCube );
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|