Files
apt-nl-map/static/Magic4/js/three.js-dev/examples/webgl_materials_parallaxmap.html

159 lines
4.1 KiB
HTML
Raw Normal View History

2024-12-04 10:21:04 +08:00
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - parallax mapping</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">Three.js</a> parallax mapping <br />
Oryginal shaders authors:
<a href="http://sunandblackcat.com/tipFullView.php?topicid=28">Igor Dyhta</a>,
<a href="http://mmikkelsen3d.blogspot.sk/2012/02/parallaxpoc-mapping-and-no-tangent.html">Morten S. Mikkelsen</a><br />
Texture by <a href="http://agf81.deviantart.com/">AGF81</a>
</div>
<script type="module">
import * as THREE from '../build/three.module.js';
import Stats from './jsm/libs/stats.module.js';
import { GUI } from './jsm/libs/dat.gui.module.js';
import { OrbitControls } from './jsm/controls/OrbitControls.js';
import { ParallaxShader } from './jsm/shaders/ParallaxShader.js';
let camera, scene, material, renderer, stats;
const effectController = {
'mode': 'relief',
'scale': 0.005,
'minLayers': 20,
'maxLayers': 25
};
init();
initGUI();
animate();
function init() {
const container = document.createElement( 'div' );
document.body.appendChild( container );
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.01, 10 );
camera.position.z = 2;
scene = new THREE.Scene();
//
const shader = ParallaxShader;
const uniforms = THREE.UniformsUtils.clone( shader.uniforms );
const parameters = {
fragmentShader: shader.fragmentShader,
vertexShader: shader.vertexShader,
uniforms: uniforms
};
//
const textureLoader = new THREE.TextureLoader();
material = new THREE.ShaderMaterial( parameters );
material.map = textureLoader.load( 'textures/brick_diffuse.jpg' );
material.bumpMap = textureLoader.load( 'textures/brick_bump.jpg' );
material.map.anisotropy = 4;
material.bumpMap.anisotropy = 4;
uniforms[ 'map' ].value = material.map;
uniforms[ 'bumpMap' ].value = material.bumpMap;
//
const geometry = new THREE.BoxGeometry( 1.0, 1.0, 1.0 );
const mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
//
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
renderer.outputEncoding = THREE.sRGBEncoding;
//
const controls = new OrbitControls( camera, renderer.domElement );
controls.minDistance = 1;
controls.maxDistance = 5;
//
stats = new Stats();
container.appendChild( stats.dom );
//
window.addEventListener( 'resize', onWindowResize );
}
function guiChanged() {
const uniforms = material.uniforms;
uniforms[ 'parallaxScale' ].value = - 1.0 * effectController.scale;
uniforms[ 'parallaxMinLayers' ].value = effectController.minLayers;
uniforms[ 'parallaxMaxLayers' ].value = effectController.maxLayers;
material.defines = {};
material.defines[ ParallaxShader.modes[ effectController.mode ] ] = '';
material.needsUpdate = true;
}
function initGUI() {
const gui = new GUI();
gui.add( effectController, 'mode', Object.keys( ParallaxShader.modes ) ).onChange( guiChanged );
gui.add( effectController, 'scale', 0.0, 0.01, 0.001 ).onChange( guiChanged );
gui.add( effectController, 'minLayers', 1.0, 30, 1 ).onChange( guiChanged );
gui.add( effectController, 'maxLayers', 1.0, 30, 1 ).onChange( guiChanged );
guiChanged();
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
requestAnimationFrame( animate );
render();
stats.update();
}
function render() {
renderer.render( scene, camera );
}
</script>
</body>
</html>