You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
156 lines
4.3 KiB
156 lines
4.3 KiB
2 years ago
|
<!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://web.archive.org/web/20120903131400/http://davidoreilly.com/post/18087489343/disneyhead" target="_blank" rel="noopener">David OReilly</a>
|
||
|
</div>
|
||
|
|
||
|
<!-- Import maps polyfill -->
|
||
|
<!-- Remove this when import maps will be widely supported -->
|
||
|
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
|
||
|
|
||
|
<script type="importmap">
|
||
|
{
|
||
|
"imports": {
|
||
|
"three": "../build/three.module.js",
|
||
|
"three/addons/": "./jsm/"
|
||
|
}
|
||
|
}
|
||
|
</script>
|
||
|
|
||
|
<script type="module">
|
||
|
|
||
|
import * as THREE from 'three';
|
||
|
|
||
|
import Stats from 'three/addons/libs/stats.module.js';
|
||
|
|
||
|
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
|
||
|
import { OBJLoader } from 'three/addons/loaders/OBJLoader.js';
|
||
|
|
||
|
let container, stats;
|
||
|
|
||
|
let camera, scene, renderer;
|
||
|
|
||
|
let 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
|
||
|
const path = 'textures/cube/SwedishRoyalCastle/';
|
||
|
const format = '.jpg';
|
||
|
const urls = [
|
||
|
path + 'px' + format, path + 'nx' + format,
|
||
|
path + 'py' + format, path + 'ny' + format,
|
||
|
path + 'pz' + format, path + 'nz' + format
|
||
|
];
|
||
|
|
||
|
const reflectionCube = new THREE.CubeTextureLoader().load( urls );
|
||
|
const refractionCube = new THREE.CubeTextureLoader().load( urls );
|
||
|
refractionCube.mapping = THREE.CubeRefractionMapping;
|
||
|
|
||
|
scene = new THREE.Scene();
|
||
|
scene.background = reflectionCube;
|
||
|
|
||
|
//lights
|
||
|
const ambient = new THREE.AmbientLight( 0xffffff );
|
||
|
scene.add( ambient );
|
||
|
|
||
|
pointLight = new THREE.PointLight( 0xffffff, 2 );
|
||
|
scene.add( pointLight );
|
||
|
|
||
|
//materials
|
||
|
const cubeMaterial3 = new THREE.MeshLambertMaterial( { color: 0xff6600, envMap: reflectionCube, combine: THREE.MixOperation, reflectivity: 0.3 } );
|
||
|
const cubeMaterial2 = new THREE.MeshLambertMaterial( { color: 0xffee00, envMap: refractionCube, refractionRatio: 0.95 } );
|
||
|
const cubeMaterial1 = new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: reflectionCube } );
|
||
|
|
||
|
//models
|
||
|
const objLoader = new OBJLoader();
|
||
|
|
||
|
objLoader.setPath( 'models/obj/walt/' );
|
||
|
objLoader.load( 'WaltHead.obj', function ( object ) {
|
||
|
|
||
|
const head = object.children[ 0 ];
|
||
|
|
||
|
head.scale.multiplyScalar( 15 );
|
||
|
head.position.y = - 500;
|
||
|
head.material = cubeMaterial1;
|
||
|
|
||
|
const head2 = head.clone();
|
||
|
head2.position.x = - 900;
|
||
|
head2.material = cubeMaterial2;
|
||
|
|
||
|
const 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
|
||
|
const 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 );
|
||
|
|
||
|
}
|
||
|
|
||
|
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>
|