123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - materials - clearcoat normal</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="http://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl - materials - clearcoat normal
- demo.<br />
- </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 { HDRCubeTextureLoader } from './jsm/loaders/HDRCubeTextureLoader.js';
- import { PMREMGenerator } from './jsm/pmrem/PMREMGenerator.js';
- import { PMREMCubeUVPacker } from './jsm/pmrem/PMREMCubeUVPacker.js';
- var container, stats;
- var camera, scene, renderer;
- var guiParams = {
- metalness: 0.0,
- roughness: 1.0,
- clearCoat: 1.0,
- clearCoatRoughness: 0.0,
- reflectivity: 0.0,
- };
- var guiNormalMapNames = [
- "face",
- "tentacle"
- ];
- var guiMatParams = {
- normalMap: "face",
- normalScale: 1.0,
- clearCoatNormalMap: "tentacle",
- clearCoatNormalScale: 1.0,
- };
- var particleLight;
- var meshes = [];
- var mats = [];
- var normalMaps = {};
- const sphereSize = 80;
- const sphereSpacing = 1.25 * sphereSize * 2;
- const variationsX = [
- ( mat, scale, map ) => { },
- ( mat, scale, map ) => {
- mat.clearCoatNormalScale = new THREE.Vector2( scale, scale );
- if ( mat.clearCoatNormalMap !== map ) {
- mat.clearCoatNormalMap = map;
- mat.needsUpdate = true;
- }
- },
- ];
- const variationsY = [
- ( mat, scale, map ) => { },
- ( mat, scale, map ) => {
- mat.normalScale = new THREE.Vector2( scale, scale );
- if ( mat.normalMap !== map ) {
- mat.normalMap = map;
- mat.needsUpdate = true;
- }
- },
- ];
- const maxI = variationsX.length;
- const maxJ = variationsY.length;
- new THREE.FontLoader()
- .load( 'fonts/gentilis_regular.typeface.json',
- function ( font ) {
- init( font );
- animate();
- }
- );
- function init( font ) {
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 10000 );
- camera.position.z = 1500;
- var genCubeUrls = function ( prefix, postfix ) {
- return [
- prefix + 'px' + postfix, prefix + 'nx' + postfix,
- prefix + 'py' + postfix, prefix + 'ny' + postfix,
- prefix + 'pz' + postfix, prefix + 'nz' + postfix
- ];
- };
- scene = new THREE.Scene();
- var hdrUrls = genCubeUrls( './textures/cube/pisaHDR/', '.hdr' );
- new HDRCubeTextureLoader()
- .setType( THREE.UnsignedByteType )
- .load( hdrUrls,
- function ( hdrCubeMap ) {
- var pmremGenerator = new PMREMGenerator( hdrCubeMap );
- pmremGenerator.update( renderer );
- var pmremCubeUVPacker = new PMREMCubeUVPacker( pmremGenerator.cubeLods );
- pmremCubeUVPacker.update( renderer );
- var hdrCubeRenderTarget = pmremCubeUVPacker.CubeUVRenderTarget;
- var geometry = new THREE.SphereBufferGeometry( 80, 64, 32 );
- var textureLoader = new THREE.TextureLoader();
- normalMaps.tentacle = textureLoader
- .load( "textures/nvidia_tentacle/tentacle_tangent_space.png" );
- normalMaps.face = textureLoader
- .load( "models/gltf/LeePerrySmith/Infinite-Level_02_Tangent_SmoothUV.jpg" );
- var matParams = {
- envMap: hdrCubeRenderTarget.texture,
- };
- for ( var ii = 0; ii < maxI; ii ++ ) {
- if ( ! mats[ ii ] ) {
- mats[ ii ] = [];
- }
- for ( var jj = 0; jj < maxJ; jj ++ ) {
- var mat = mats[ ii ][ jj ] = new THREE.MeshPhysicalMaterial( matParams );
- variationsX[ ii ]( mat, 1, normalMaps.tentacle );
- variationsY[ jj ]( mat, 1, normalMaps.face );
- var mesh = new THREE.Mesh( geometry, mat );
- meshes.push( mesh );
- mesh.position.x = ( ii - ( maxI - 1 ) / 2 ) * sphereSpacing;
- mesh.position.y = ( jj - ( maxJ - 1 ) / 2 ) * sphereSpacing;
- scene.add( mesh );
- }
- }
- hdrCubeMap.magFilter = THREE.LinearFilter;
- hdrCubeMap.needsUpdate = true;
- scene.background = hdrCubeMap;
- pmremGenerator.dispose();
- pmremCubeUVPacker.dispose();
- }
- );
- function addLabel( name, location, fontSize ) {
- fontSize = fontSize | 20;
- var textGeo = new THREE.TextBufferGeometry( name,
- {
- font: font,
- size: fontSize,
- height: 1,
- curveSegments: 1
- }
- );
- var textMaterial = new THREE.MeshBasicMaterial( { color: 0xffffff } );
- var textMesh = new THREE.Mesh( textGeo, textMaterial );
- // Center the bounding box on the specified position w/ translation of -half diagonal
- textGeo.computeBoundingBox();
- var bb = textGeo.boundingBox.clone();
- var displace = bb.max.sub( bb.min ).divide( new THREE.Vector3( 2, 2, 2 ) );
- textMesh.position.copy( location.sub( displace ) );
- scene.add( textMesh );
- }
- addLabel( "Normal Map", new THREE.Vector3( -350, 0, 0 ), 30 );
- addLabel( "None", new THREE.Vector3( -250, -100, 0 ) );
- addLabel( "Map", new THREE.Vector3( -300, 100, 0 ) );
- addLabel( "Clearcoat Normal Map", new THREE.Vector3( 0, 260, 0 ), 30 );
- addLabel( "None", new THREE.Vector3( -100, 200, 0 ) );
- addLabel( "Map", new THREE.Vector3( 100, 200, 0 ) );
- // LIGHTS
- particleLight = new THREE.Mesh( new THREE.SphereBufferGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
- scene.add( particleLight );
- {
- var ambientLight = new THREE.AmbientLight( 0x444444 );
- scene.add( ambientLight );
- }
- {
- var pointLight = new THREE.PointLight( 0xffffff, 1.25, 1000 );
- particleLight.add( pointLight );
- }
- {
- var directionalLight = new THREE.DirectionalLight( 0xffffff );
- directionalLight.position.set( 1, - 0.5, - 1 );
- scene.add( directionalLight );
- }
- renderer = new THREE.WebGLRenderer();
- renderer.setSize( window.innerWidth, window.innerHeight );
- container.appendChild( renderer.domElement );
- //
- renderer.gammaInput = true;
- renderer.gammaOutput = true;
- //
- stats = new Stats();
- container.appendChild( stats.dom );
- // EVENTS
- var controls = new OrbitControls( camera, renderer.domElement );
- window.addEventListener( 'resize', onWindowResize, false );
- var gui = new GUI();
- {
- var materialGui = gui.addFolder( 'Physical Material' );
- materialGui.add( guiParams, 'metalness', 0, 1, 0.01 );
- materialGui.add( guiParams, 'roughness', 0, 1, 0.01 );
- materialGui.add( guiParams, 'reflectivity', 0, 1, 0.01 );
- materialGui.open();
- }
- {
- var clearCoatGui = gui.addFolder( 'ClearCoat' );
- clearCoatGui.add( guiParams, 'clearCoat', 0, 1, 0.01 );
- clearCoatGui.add( guiParams, 'clearCoatRoughness', 0, 1, 0.01 );
- clearCoatGui.open();
- }
- {
- var normalGui = gui.addFolder( 'Normal Maps' );
- normalGui.add( guiMatParams, 'normalMap', guiNormalMapNames );
- normalGui.add( guiMatParams, 'normalScale', 0, 1, 0.01 );
- normalGui.add( guiMatParams, 'clearCoatNormalMap', guiNormalMapNames );
- normalGui.add( guiMatParams, 'clearCoatNormalScale', 0, 1, 0.01 );
- normalGui.open();
- }
- gui.open();
- }
- //
- function onWindowResize() {
- var width = window.innerWidth;
- var height = window.innerHeight;
- camera.aspect = width / height;
- camera.updateProjectionMatrix();
- renderer.setSize( width, height );
- }
- //
- function animate() {
- requestAnimationFrame( animate );
- render();
- stats.update();
- }
- function render() {
- var matColor = new THREE.Color().setHSL( 0.0, 0.5, 0.25 );
- for ( var ii = 0; ii < maxI; ii ++ ) {
- if ( mats[ ii ] ) { // avoid exceptions on first render before materials are set. TODO: lauch animation only after setup
- for ( var jj = 0; jj < maxJ; jj ++ ) {
- var mat = mats[ ii ][ jj ];
- mat.color = matColor;
- for ( var matParam in guiParams ) {
- mat[ matParam ] = guiParams[ matParam ];
- }
- var normalMap = normalMaps[ guiMatParams.normalMap ];
- var clearCoatNormalMap = normalMaps[ guiMatParams.clearCoatNormalMap ];
- variationsX[ ii ]( mat, guiMatParams.clearCoatNormalScale, clearCoatNormalMap );
- variationsY[ jj ]( mat, guiMatParams.normalScale, normalMap );
- }
- }
- }
- var timer = Date.now() * 0.00025;
- particleLight.position.x = Math.sin( timer * 7 ) * 300;
- particleLight.position.y = Math.cos( timer * 5 ) * 400;
- particleLight.position.z = Math.cos( timer * 3 ) * 300;
- for ( var mesh of meshes ) {
- mesh.rotation.y += 0.01;
- }
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|