123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - materials - cube reflection / refraction [Walt]</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="container"></div>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - cube mapping demo.<br />
- Texture by <a href="http://www.humus.name/index.php?page=Textures" target="_blank" rel="noopener">Humus</a>, Walt Disney head by <a href="http://davidoreilly.com/post/18087489343/disneyhead" target="_blank" rel="noopener">David OReilly</a>
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- import { OBJLoader } from './jsm/loaders/OBJLoader.js';
- var container, stats;
- var camera, scene, renderer;
- var pointLight;
- init();
- animate();
- function init() {
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 5000 );
- camera.position.z = 2000;
- //cubemap
- var path = 'textures/cube/SwedishRoyalCastle/';
- var format = '.jpg';
- var urls = [
- path + 'px' + format, path + 'nx' + format,
- path + 'py' + format, path + 'ny' + format,
- path + 'pz' + format, path + 'nz' + format
- ];
- var reflectionCube = new THREE.CubeTextureLoader().load( urls );
- reflectionCube.format = THREE.RGBFormat;
- var refractionCube = new THREE.CubeTextureLoader().load( urls );
- refractionCube.mapping = THREE.CubeRefractionMapping;
- refractionCube.format = THREE.RGBFormat;
- scene = new THREE.Scene();
- scene.background = reflectionCube;
- //lights
- var ambient = new THREE.AmbientLight( 0xffffff );
- scene.add( ambient );
- pointLight = new THREE.PointLight( 0xffffff, 2 );
- scene.add( pointLight );
- //materials
- var cubeMaterial3 = new THREE.MeshLambertMaterial( { color: 0xff6600, envMap: reflectionCube, combine: THREE.MixOperation, reflectivity: 0.3 } );
- var cubeMaterial2 = new THREE.MeshLambertMaterial( { color: 0xffee00, envMap: refractionCube, refractionRatio: 0.95 } );
- var cubeMaterial1 = new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: reflectionCube } );
- //models
- var objLoader = new OBJLoader();
- objLoader.setPath( 'models/obj/walt/' );
- objLoader.load( 'WaltHead.obj', function ( object ) {
- var head = object.children[ 0 ];
- head.scale.multiplyScalar( 15 );
- head.position.y = - 500;
- head.material = cubeMaterial1;
- var head2 = head.clone();
- head2.position.x = - 900;
- head2.material = cubeMaterial2;
- var head3 = head.clone();
- head3.position.x = 900;
- head3.material = cubeMaterial3;
- scene.add( head, head2, head3 );
- } );
- //renderer
- renderer = new THREE.WebGLRenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- container.appendChild( renderer.domElement );
- //controls
- var controls = new OrbitControls( camera, renderer.domElement );
- controls.enableZoom = false;
- controls.enablePan = false;
- controls.minPolarAngle = Math.PI / 4;
- controls.maxPolarAngle = Math.PI / 1.5;
- //stats
- stats = new Stats();
- container.appendChild( stats.dom );
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate() {
- requestAnimationFrame( animate );
- render();
- }
- function render() {
- renderer.render( scene, camera );
- stats.update();
- }
- </script>
- </body>
- </html>
|