123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - materials - displacement map</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> - (<span id="description">normal + ao + displacement + environment</span>) map demo.<br />
- ninja head from <a href="http://developer.amd.com/tools-and-sdks/archive/legacy-cpu-gpu-tools/amd-gpu-meshmapper/" target="_blank" rel="noopener">AMD GPU MeshMapper</a>
- <div id="vt">displacement mapping requires vertex textures</div>
- </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 { OBJLoader } from './jsm/loaders/OBJLoader.js';
- var stats;
- var camera, scene, renderer, controls;
- var settings = {
- metalness: 1.0,
- roughness: 0.4,
- ambientIntensity: 0.2,
- aoMapIntensity: 1.0,
- envMapIntensity: 1.0,
- displacementScale: 2.436143, // from original model
- normalScale: 1.0
- };
- var mesh, material;
- var pointLight, ambientLight;
- var height = 500; // of camera frustum
- var r = 0.0;
- init();
- animate();
- initGui();
- // Init gui
- function initGui() {
- var gui = new GUI();
- //var gui = gui.addFolder( "Material" );
- gui.add( settings, "metalness" ).min( 0 ).max( 1 ).onChange( function ( value ) {
- material.metalness = value;
- } );
- gui.add( settings, "roughness" ).min( 0 ).max( 1 ).onChange( function ( value ) {
- material.roughness = value;
- } );
- gui.add( settings, "aoMapIntensity" ).min( 0 ).max( 1 ).onChange( function ( value ) {
- material.aoMapIntensity = value;
- } );
- gui.add( settings, "ambientIntensity" ).min( 0 ).max( 1 ).onChange( function ( value ) {
- ambientLight.intensity = value;
- } );
- gui.add( settings, "envMapIntensity" ).min( 0 ).max( 3 ).onChange( function ( value ) {
- material.envMapIntensity = value;
- } );
- gui.add( settings, "displacementScale" ).min( 0 ).max( 3.0 ).onChange( function ( value ) {
- material.displacementScale = value;
- } );
- gui.add( settings, "normalScale" ).min( - 1 ).max( 1 ).onChange( function ( value ) {
- material.normalScale.set( 1, - 1 ).multiplyScalar( value );
- } );
- }
- function init() {
- var container = document.createElement( 'div' );
- document.body.appendChild( container );
- renderer = new THREE.WebGLRenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- container.appendChild( renderer.domElement );
- renderer.outputEncoding = THREE.sRGBEncoding;
- //
- scene = new THREE.Scene();
- var aspect = window.innerWidth / window.innerHeight;
- camera = new THREE.OrthographicCamera( - height * aspect, height * aspect, height, - height, 1, 10000 );
- camera.position.z = 1500;
- scene.add( camera );
- controls = new OrbitControls( camera, renderer.domElement );
- controls.enableZoom = false;
- controls.enableDamping = true;
- // lights
- ambientLight = new THREE.AmbientLight( 0xffffff, settings.ambientIntensity );
- scene.add( ambientLight );
- pointLight = new THREE.PointLight( 0xff0000, 0.5 );
- pointLight.position.z = 2500;
- scene.add( pointLight );
- var pointLight2 = new THREE.PointLight( 0xff6666, 1 );
- camera.add( pointLight2 );
- var pointLight3 = new THREE.PointLight( 0x0000ff, 0.5 );
- pointLight3.position.x = - 1000;
- pointLight3.position.z = 1000;
- scene.add( pointLight3 );
- // env map
- 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.encoding = THREE.sRGBEncoding;
- // textures
- var textureLoader = new THREE.TextureLoader();
- var normalMap = textureLoader.load( "models/obj/ninja/normal.jpg" );
- var aoMap = textureLoader.load( "models/obj/ninja/ao.jpg" );
- var displacementMap = textureLoader.load( "models/obj/ninja/displacement.jpg" );
- // material
- material = new THREE.MeshStandardMaterial( {
- color: 0x888888,
- roughness: settings.roughness,
- metalness: settings.metalness,
- normalMap: normalMap,
- normalScale: new THREE.Vector2( 1, - 1 ), // why does the normal map require negation in this case?
- aoMap: aoMap,
- aoMapIntensity: 1,
- displacementMap: displacementMap,
- displacementScale: settings.displacementScale,
- displacementBias: - 0.428408, // from original model
- envMap: reflectionCube,
- envMapIntensity: settings.envMapIntensity,
- side: THREE.DoubleSide
- } );
- //
- var loader = new OBJLoader();
- loader.load( "models/obj/ninja/ninjaHead_Low.obj", function ( group ) {
- var geometry = group.children[ 0 ].geometry;
- geometry.attributes.uv2 = geometry.attributes.uv;
- geometry.center();
- mesh = new THREE.Mesh( geometry, material );
- mesh.scale.multiplyScalar( 25 );
- scene.add( mesh );
- } );
- //
- var description = "normal + ao" + ( renderer.capabilities.vertexTextures ? " + displacement + environment" : " + <strike>displacement</strike>" );
- document.getElementById( "description" ).innerHTML = description;
- document.getElementById( "vt" ).style.display = renderer.capabilities.vertexTextures ? "none" : "block";
- //
- stats = new Stats();
- container.appendChild( stats.dom );
- //
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- var aspect = window.innerWidth / window.innerHeight;
- camera.left = - height * aspect;
- camera.right = height * aspect;
- camera.top = height;
- camera.bottom = - height;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- //
- function animate() {
- requestAnimationFrame( animate );
- controls.update();
- stats.begin();
- render();
- stats.end();
- }
- function render() {
- pointLight.position.x = 2500 * Math.cos( r );
- pointLight.position.z = 2500 * Math.sin( r );
- r += 0.01;
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|