123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - materials - clearcoat</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> webgl - materials - clearcoat
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- import { HDRCubeTextureLoader } from './jsm/loaders/HDRCubeTextureLoader.js';
- var container, stats;
- var camera, scene, renderer;
- var particleLight;
- var group;
- 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 = 1000;
- scene = new THREE.Scene();
- group = new THREE.Group();
- scene.add( group );
- new HDRCubeTextureLoader()
- .setDataType( THREE.UnsignedByteType )
- .setPath( 'textures/cube/pisaHDR/' )
- .load( [ 'px.hdr', 'nx.hdr', 'py.hdr', 'ny.hdr', 'pz.hdr', 'nz.hdr' ],
- function ( hdrCubeMap ) {
- var hdrCubeRenderTarget = pmremGenerator.fromCubemap( hdrCubeMap );
- hdrCubeMap.dispose();
- pmremGenerator.dispose();
- var geometry = new THREE.SphereBufferGeometry( 80, 64, 32 );
- var textureLoader = new THREE.TextureLoader();
- var diffuse = textureLoader.load( "textures/carbon/Carbon.png" );
- diffuse.encoding = THREE.sRGBEncoding;
- diffuse.wrapS = THREE.RepeatWrapping;
- diffuse.wrapT = THREE.RepeatWrapping;
- diffuse.repeat.x = 10;
- diffuse.repeat.y = 10;
- var normalMap = textureLoader.load( "textures/carbon/Carbon_Normal.png" );
- normalMap.wrapS = THREE.RepeatWrapping;
- normalMap.wrapT = THREE.RepeatWrapping;
- var normalMap2 = textureLoader.load( "textures/water/Water_1_M_Normal.jpg" );
- var normalMap3 = textureLoader.load( "textures/flakes.png" );
- normalMap3.wrapS = THREE.RepeatWrapping;
- normalMap3.wrapT = THREE.RepeatWrapping;
- normalMap3.repeat.x = 10;
- normalMap3.repeat.y = 10;
- normalMap3.anisotropy = 16;
- var normalMap4 = textureLoader.load( "textures/golfball.jpg" );
- var clearcoatNormaMap = textureLoader.load( "textures/pbr/Scratched_gold/Scratched_gold_01_1K_Normal.png" );
- // car paint
- var material = new THREE.MeshPhysicalMaterial( {
- clearcoat: 1.0,
- clearcoatRoughness: 0.1,
- metalness: 0.9,
- roughness: 0.5,
- color: 0x0000ff,
- normalMap: normalMap3,
- normalScale: new THREE.Vector2( 0.15, 0.15 )
- } );
- var mesh = new THREE.Mesh( geometry, material );
- mesh.position.x = - 100;
- mesh.position.y = 100;
- group.add( mesh );
- // fibers
- var material = new THREE.MeshPhysicalMaterial( {
- roughness: 0.5,
- clearcoat: 1.0,
- clearcoatRoughness: 0.1,
- map: diffuse,
- normalMap: normalMap
- } );
- var mesh = new THREE.Mesh( geometry, material );
- mesh.position.x = 100;
- mesh.position.y = 100;
- group.add( mesh );
- // golf
- var material = new THREE.MeshPhysicalMaterial( {
- metalness: 0.0,
- roughness: 0.1,
- clearcoat: 1.0,
- normalMap: normalMap4,
- clearcoatNormalMap: clearcoatNormaMap,
- clearcoatNormalScale: new THREE.Vector2( 2.0, 2.0 )
- } );
- var mesh = new THREE.Mesh( geometry, material );
- mesh.position.x = - 100;
- mesh.position.y = - 100;
- group.add( mesh );
- // clearcoat + normalmap
- var material = new THREE.MeshPhysicalMaterial( {
- clearcoat: 1.0,
- metalness: 1.0,
- color: 0xff0000,
- normalMap: normalMap2,
- normalScale: new THREE.Vector2( 0.15, 0.15 ),
- clearcoatNormalMap: clearcoatNormaMap,
- clearcoatNormalScale: new THREE.Vector2( 2.0, 2.0 )
- } );
- var mesh = new THREE.Mesh( geometry, material );
- mesh.position.x = 100;
- mesh.position.y = - 100;
- group.add( mesh );
- //
- scene.background = hdrCubeRenderTarget.texture;
- scene.environment = hdrCubeRenderTarget.texture;
- }
- );
- // LIGHTS
- particleLight = new THREE.Mesh(
- new THREE.SphereBufferGeometry( 4, 8, 8 ),
- new THREE.MeshBasicMaterial( { color: 0xffffff } )
- );
- scene.add( particleLight );
- particleLight.add( new THREE.PointLight( 0xffffff, 1 ) );
- renderer = new THREE.WebGLRenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- container.appendChild( renderer.domElement );
- //
- renderer.toneMapping = THREE.ACESFilmicToneMapping;
- renderer.toneMappingExposure = 1;
- //
- renderer.outputEncoding = THREE.sRGBEncoding;
- //
- var pmremGenerator = new THREE.PMREMGenerator( renderer );
- pmremGenerator.compileEquirectangularShader();
- //
- stats = new Stats();
- container.appendChild( stats.dom );
- // EVENTS
- new OrbitControls( camera, renderer.domElement );
- window.addEventListener( 'resize', onWindowResize, false );
- }
- //
- 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 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 i = 0; i < group.children.length; i ++ ) {
- var child = group.children[ i ];
- child.rotation.y += 0.005;
- }
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|