123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - materials - normal map [Lee Perry-Smith]</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">
- <style>
- #vt { display:none }
- #vt, #vt a { color:orange; }
- </style>
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl normalmap demo.<br/>
- <a href="http://graphics.cs.williams.edu/data/meshes.xml#14" target="_blank" rel="noopener">Lee Perry-Smith</a> head.
- <div id="vt">displacement mapping needs vertex textures (GPU with Shader Model 3.0)<br/>
- on Windows use <span class="code">Chrome --use-gl=desktop</span> or Firefox 4<br/>
- please star this <a href="http://code.google.com/p/chromium/issues/detail?id=52497">Chrome issue</a> to get ANGLE support
- </div>
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
- import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
- import { RenderPass } from './jsm/postprocessing/RenderPass.js';
- import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
- import { BleachBypassShader } from './jsm/shaders/BleachBypassShader.js';
- import { ColorCorrectionShader } from './jsm/shaders/ColorCorrectionShader.js';
- import { FXAAShader } from './jsm/shaders/FXAAShader.js';
- import { GammaCorrectionShader } from './jsm/shaders/GammaCorrectionShader.js';
- var container, stats, loader;
- var camera, scene, renderer;
- var mesh;
- var directionalLight, pointLight, ambientLight;
- var mouseX = 0;
- var mouseY = 0;
- var targetX = 0;
- var targetY = 0;
- var windowHalfX = window.innerWidth / 2;
- var windowHalfY = window.innerHeight / 2;
- var composer, effectFXAA;
- init();
- animate();
- function init() {
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 10000 );
- camera.position.z = 1200;
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0x111111 );
- // LIGHTS
- ambientLight = new THREE.AmbientLight( 0x444444 );
- scene.add( ambientLight );
- pointLight = new THREE.PointLight( 0xffffff, 1.25, 1000 );
- pointLight.position.set( 0, 0, 600 );
- scene.add( pointLight );
- directionalLight = new THREE.DirectionalLight( 0xffffff );
- directionalLight.position.set( 1, - 0.5, - 1 );
- scene.add( directionalLight );
- var textureLoader = new THREE.TextureLoader();
- var diffuseMap = textureLoader.load( "models/gltf/LeePerrySmith/Map-COL.jpg" );
- diffuseMap.encoding = THREE.sRGBEncoding;
- var specularMap = textureLoader.load( "models/gltf/LeePerrySmith/Map-SPEC.jpg" );
- specularMap.encoding = THREE.sRGBEncoding;
- var normalMap = textureLoader.load( "models/gltf/LeePerrySmith/Infinite-Level_02_Tangent_SmoothUV.jpg" );
- var material = new THREE.MeshPhongMaterial( {
- color: 0xdddddd,
- specular: 0x222222,
- shininess: 35,
- map: diffuseMap,
- specularMap: specularMap,
- normalMap: normalMap,
- normalScale: new THREE.Vector2( 0.8, 0.8 )
- } );
- loader = new GLTFLoader();
- loader.load( "models/gltf/LeePerrySmith/LeePerrySmith.glb", function ( gltf ) {
- createScene( gltf.scene.children[ 0 ].geometry, 100, material );
- } );
- renderer = new THREE.WebGLRenderer();
- renderer.setSize( window.innerWidth, window.innerHeight );
- container.appendChild( renderer.domElement );
- //
- stats = new Stats();
- container.appendChild( stats.dom );
- // COMPOSER
- renderer.autoClear = false;
- var renderModel = new RenderPass( scene, camera );
- var effectBleach = new ShaderPass( BleachBypassShader );
- var effectColor = new ShaderPass( ColorCorrectionShader );
- effectFXAA = new ShaderPass( FXAAShader );
- var gammaCorrection = new ShaderPass( GammaCorrectionShader );
- effectFXAA.uniforms[ 'resolution' ].value.set( 1 / window.innerWidth, 1 / window.innerHeight );
- effectBleach.uniforms[ 'opacity' ].value = 0.2;
- effectColor.uniforms[ 'powRGB' ].value.set( 1.4, 1.45, 1.45 );
- effectColor.uniforms[ 'mulRGB' ].value.set( 1.1, 1.1, 1.1 );
- composer = new EffectComposer( renderer );
- composer.addPass( renderModel );
- composer.addPass( effectFXAA );
- composer.addPass( effectBleach );
- composer.addPass( effectColor );
- composer.addPass( gammaCorrection );
- // EVENTS
- document.addEventListener( 'mousemove', onDocumentMouseMove, false );
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function createScene( geometry, scale, material ) {
- mesh = new THREE.Mesh( geometry, material );
- mesh.position.y = - 50;
- mesh.scale.x = mesh.scale.y = mesh.scale.z = scale;
- scene.add( mesh );
- }
- //
- function onWindowResize() {
- var width = window.innerWidth;
- var height = window.innerHeight;
- camera.aspect = width / height;
- camera.updateProjectionMatrix();
- renderer.setSize( width, height );
- composer.setSize( width, height );
- effectFXAA.uniforms[ 'resolution' ].value.set( 1 / width, 1 / height );
- }
- function onDocumentMouseMove( event ) {
- mouseX = ( event.clientX - windowHalfX );
- mouseY = ( event.clientY - windowHalfY );
- }
- //
- function animate() {
- requestAnimationFrame( animate );
- render();
- stats.update();
- }
- function render() {
- targetX = mouseX * .001;
- targetY = mouseY * .001;
- if ( mesh ) {
- mesh.rotation.y += 0.05 * ( targetX - mesh.rotation.y );
- mesh.rotation.x += 0.05 * ( targetY - mesh.rotation.x );
- }
- composer.render();
- }
- </script>
- </body>
- </html>
|