123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <!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';
- var camera, scene, material, renderer, stats;
- var effectController = {
- 'mode': 'relief',
- 'scale': 0.005,
- 'minLayers': 20,
- 'maxLayers': 25
- };
- init();
- initGUI();
- animate();
- function init() {
- var 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();
- //
- var shader = ParallaxShader;
- var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
- var parameters = {
- fragmentShader: shader.fragmentShader,
- vertexShader: shader.vertexShader,
- uniforms: uniforms
- };
- //
- var 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;
- //
- var geometry = new THREE.BoxBufferGeometry( 1.0, 1.0, 1.0 );
- var 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;
- //
- var controls = new OrbitControls( camera, renderer.domElement );
- //
- stats = new Stats();
- container.appendChild( stats.dom );
- //
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function guiChanged() {
- var 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() {
- var 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>
|