123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - materials - channels</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" target="_blank" rel="noopener">three.js</a> - <span id="description">Normal, Depth, DepthRGBA, DepthRGBAUnpacked, Materials</span><br/>
- by <a href="https://Clara.io">Ben Houston</a>. 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>
- <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 { OBJLoader } from './jsm/loaders/OBJLoader.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- var stats;
- var camera, scene, renderer;
- var params = {
- material: 'normal',
- camera: 'perspective',
- side: 'double'
- };
- var sides = {
- 'front': THREE.FrontSide,
- 'back': THREE.BackSide,
- 'double': THREE.DoubleSide
- };
- var cameraOrtho, cameraPerspective;
- var controlsOrtho, controlsPerspective;
- var mesh, materialStandard, materialDepthBasic, materialDepthRGBA, materialNormal;
- var height = 500; // of camera frustum
- var SCALE = 2.436143; // from original model
- var BIAS = - 0.428408; // from original model
- init();
- animate();
- initGui();
- // Init gui
- function initGui() {
- var gui = new GUI();
- gui.add( params, 'material', [ 'standard', 'normal', 'depthBasic', 'depthRGBA' ] );
- gui.add( params, 'camera', [ 'perspective', 'ortho' ] );
- gui.add( params, 'side', [ 'front', 'back', 'double' ] );
- }
- 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 );
- //
- scene = new THREE.Scene();
- var aspect = window.innerWidth / window.innerHeight;
- cameraPerspective = new THREE.PerspectiveCamera( 45, aspect, 500, 3000 );
- cameraPerspective.position.z = 1500;
- scene.add( cameraPerspective );
- cameraOrtho = new THREE.OrthographicCamera( - height * aspect, height * aspect, height, - height, 1000, 2500 );
- cameraOrtho.position.z = 1500;
- scene.add( cameraOrtho );
- camera = cameraPerspective;
- controlsPerspective = new OrbitControls( cameraPerspective, renderer.domElement );
- controlsPerspective.minDistance = 1000;
- controlsPerspective.maxDistance = 2400;
- controlsPerspective.enablePan = false;
- controlsPerspective.enableDamping = true;
- controlsOrtho = new OrbitControls( cameraOrtho, renderer.domElement );
- controlsOrtho.minZoom = 0.5;
- controlsOrtho.maxZoom = 1.5;
- controlsOrtho.enablePan = false;
- controlsOrtho.enableDamping = true;
- // lights
- var ambientLight = new THREE.AmbientLight( 0xffffff, 0.1 );
- scene.add( ambientLight );
- var 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 );
- // 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
- materialStandard = new THREE.MeshStandardMaterial( {
- color: 0xffffff,
- metalness: 0.5,
- roughness: 0.6,
- displacementMap: displacementMap,
- displacementScale: SCALE,
- displacementBias: BIAS,
- aoMap: aoMap,
- normalMap: normalMap,
- normalScale: new THREE.Vector2( 1, - 1 ),
- //flatShading: true,
- side: THREE.DoubleSide
- } );
- materialDepthBasic = new THREE.MeshDepthMaterial( {
- depthPacking: THREE.BasicDepthPacking,
- displacementMap: displacementMap,
- displacementScale: SCALE,
- displacementBias: BIAS,
- side: THREE.DoubleSide
- } );
- materialDepthRGBA = new THREE.MeshDepthMaterial( {
- depthPacking: THREE.RGBADepthPacking,
- displacementMap: displacementMap,
- displacementScale: SCALE,
- displacementBias: BIAS,
- side: THREE.DoubleSide
- } );
- materialNormal = new THREE.MeshNormalMaterial( {
- displacementMap: displacementMap,
- displacementScale: SCALE,
- displacementBias: BIAS,
- normalMap: normalMap,
- normalScale: new THREE.Vector2( 1, - 1 ),
- //flatShading: true,
- 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, materialNormal );
- mesh.scale.multiplyScalar( 25 );
- scene.add( mesh );
- } );
- //
- stats = new Stats();
- container.appendChild( stats.dom );
- //
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- var width = window.innerWidth;
- var height = window.innerHeight;
- var aspect = window.innerWidth / window.innerHeight;
- camera.aspect = aspect;
- camera.left = - height * aspect;
- camera.right = height * aspect;
- camera.top = height;
- camera.bottom = - height;
- camera.updateProjectionMatrix();
- renderer.setSize( width, height );
- }
- //
- function animate() {
- requestAnimationFrame( animate );
- stats.begin();
- render();
- stats.end();
- }
- function render() {
- if ( mesh ) {
- var material = mesh.material;
- switch ( params.material ) {
- case 'standard': material = materialStandard; break;
- case 'depthBasic': material = materialDepthBasic; break;
- case 'depthRGBA': material = materialDepthRGBA; break;
- case 'normal': material = materialNormal; break;
- }
- if ( sides[ params.side ] !== material.side ) {
- switch ( params.side ) {
- case 'front': material.side = THREE.FrontSide; break;
- case 'back': material.side = THREE.BackSide; break;
- case 'double': material.side = THREE.DoubleSide; break;
- }
- material.needsUpdate = true;
- }
- mesh.material = material;
- }
- switch ( params.camera ) {
- case 'perspective':
- camera = cameraPerspective;
- break;
- case 'ortho':
- camera = cameraOrtho;
- break;
- }
- controlsPerspective.update();
- controlsOrtho.update(); // must update both controls for damping to complete
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|