4 <title>three.js webgl - materials</title>
6 <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
10 font-family:Monospace;
14 background-color: #000;
21 top: 0px; width: 100%;
28 <div id="container"></div>
29 <div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - Physical Material Variations by <a href="http://clara.io/" target="_blank" rel="noopener">Ben Houston</a>.<br/><br/>
30 Note: Every second sphere has an IBL environment map on it.</div>
32 <script src="../build/three.js"></script>
33 <script src="js/controls/OrbitControls.js"></script>
34 <script src="js/loaders/RGBELoader.js"></script>
35 <script src="js/loaders/HDRCubeTextureLoader.js"></script>
37 <script src="js/pmrem/PMREMGenerator.js"></script>
38 <script src="js/pmrem/PMREMCubeUVPacker.js"></script>
40 <script src="js/Detector.js"></script>
41 <script src="js/libs/stats.min.js"></script>
42 <script src="js/libs/dat.gui.min.js"></script>
46 if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
50 var camera, scene, renderer, controls;
53 var loader = new THREE.FontLoader();
54 loader.load( 'fonts/gentilis_regular.typeface.json', function ( font ) {
61 function init( font ) {
63 container = document.createElement( 'div' );
64 document.body.appendChild( container );
66 camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2000 );
67 camera.position.set( 0.0, 400, 400 * 3.5 );
71 var genCubeUrls = function( prefix, postfix ) {
73 prefix + 'px' + postfix, prefix + 'nx' + postfix,
74 prefix + 'py' + postfix, prefix + 'ny' + postfix,
75 prefix + 'pz' + postfix, prefix + 'nz' + postfix
80 var textureCube = new THREE.CubeTextureLoader()
81 .setPath( 'textures/cube/pisa/' )
82 .load( [ 'px.png', 'nx.png', 'py.png', 'ny.png', 'pz.png', 'nz.png' ] );
84 scene = new THREE.Scene();
85 scene.background = textureCube;
87 var hdrUrls = genCubeUrls( './textures/cube/pisaHDR/', '.hdr' );
88 var hdrCubeRenderTarget = null;
90 new THREE.HDRCubeTextureLoader().load( THREE.UnsignedByteType, hdrUrls, function ( hdrCubeMap ) {
92 var pmremGenerator = new THREE.PMREMGenerator( hdrCubeMap );
93 pmremGenerator.update( renderer );
95 var pmremCubeUVPacker = new THREE.PMREMCubeUVPacker( pmremGenerator.cubeLods );
96 pmremCubeUVPacker.update( renderer );
98 hdrCubeRenderTarget = pmremCubeUVPacker.CubeUVRenderTarget;
103 var numberOfSphersPerSide = 5;
104 var sphereRadius = ( cubeWidth / numberOfSphersPerSide ) * 0.8 * 0.5;
105 var stepSize = 1.0 / numberOfSphersPerSide;
107 var geometry = new THREE.SphereBufferGeometry( sphereRadius, 32, 16 );
111 for ( var alpha = 0; alpha <= 1.0; alpha += stepSize ) {
113 for ( var beta = 0; beta <= 1.0; beta += stepSize ) {
115 for ( var gamma = 0; gamma <= 1.0; gamma += stepSize ) {
117 var diffuseColor = new THREE.Color().setHSL( alpha, 0.5, 0.25 );
119 var material = new THREE.MeshPhysicalMaterial( {
123 clearCoat: 1.0 - alpha,
124 clearCoatRoughness: 1.0 - beta,
125 reflectivity: 1.0 - gamma,
126 envMap: ( index % 2 ) == 1 ? hdrCubeRenderTarget.texture : null
131 var mesh = new THREE.Mesh( geometry, material );
133 mesh.position.x = alpha * 400 - 200;
134 mesh.position.y = beta * 400 - 200;
135 mesh.position.z = gamma * 400 - 200;
148 function addLabel( name, location ) {
150 var textGeo = new THREE.TextBufferGeometry( name, {
160 var textMaterial = new THREE.MeshBasicMaterial( { color: 0xffffff } );
161 var textMesh = new THREE.Mesh( textGeo, textMaterial );
162 textMesh.position.copy( location );
163 scene.add( textMesh );
167 addLabel( "+clearCoat", new THREE.Vector3( -350, 0, 0 ) );
168 addLabel( "-clearCoat", new THREE.Vector3( 350, 0, 0 ) );
170 addLabel( "+clearCoatRoughness", new THREE.Vector3( 0, -300, 0 ) );
171 addLabel( "-clearCoatRoughness", new THREE.Vector3( 0, 300, 0 ) );
173 addLabel( "+reflectivity", new THREE.Vector3( 0, 0, -300 ) );
174 addLabel( "-reflectivity", new THREE.Vector3( 0, 0, 300 ) );
176 particleLight = new THREE.Mesh( new THREE.SphereBufferGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
177 scene.add( particleLight );
181 scene.add( new THREE.AmbientLight( 0x222222 ) );
183 var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
184 directionalLight.position.set( 1, 1, 1 ).normalize();
185 scene.add( directionalLight );
187 var pointLight = new THREE.PointLight( 0xffffff, 2, 800 );
188 particleLight.add( pointLight );
192 renderer = new THREE.WebGLRenderer( { antialias: true } );
193 renderer.setPixelRatio( window.devicePixelRatio );
194 renderer.setSize( window.innerWidth, window.innerHeight );
195 container.appendChild( renderer.domElement );
197 renderer.gammaInput = true;
198 renderer.gammaOutput = true;
199 renderer.toneMapping = THREE.Uncharted2ToneMapping;
200 renderer.toneMappingExposure = 0.75;
205 container.appendChild( stats.dom );
207 controls = new THREE.OrbitControls( camera );
209 window.addEventListener( 'resize', onWindowResize, false );
213 function onWindowResize() {
215 camera.aspect = window.innerWidth / window.innerHeight;
216 camera.updateProjectionMatrix();
218 renderer.setSize( window.innerWidth, window.innerHeight );
226 requestAnimationFrame( animate );
235 var timer = Date.now() * 0.00025;
237 //camera.position.x = Math.cos( timer ) * 800;
238 //camera.position.z = Math.sin( timer ) * 800;
240 camera.lookAt( scene.position );
242 particleLight.position.x = Math.sin( timer * 7 ) * 300;
243 particleLight.position.y = Math.cos( timer * 5 ) * 400;
244 particleLight.position.z = Math.cos( timer * 3 ) * 300;
246 renderer.render( scene, camera );