123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - physically based shading</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 physically based shading testbed
- </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 { TrackballControls } from './jsm/controls/TrackballControls.js';
- import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
- var container, stats;
- var camera, scene, renderer;
- var mesh;
- var controls;
- var cubeCamera;
- var sunLight, pointLight, ambientLight;
- var mixer;
- var clock = new THREE.Clock();
- var gui, shadowCameraHelper, shadowConfig = {
- shadowCameraVisible: false,
- shadowCameraNear: 750,
- shadowCameraFar: 4000,
- shadowBias: - 0.0002
- };
- init();
- animate();
- function init() {
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- // CAMERA
- camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 2, 10000 );
- camera.position.set( 500, 400, 1200 );
- // SCENE
- scene = new THREE.Scene();
- scene.fog = new THREE.Fog( 0, 1000, 10000 );
- // CUBE CAMERA
- cubeCamera = new THREE.CubeCamera( 1, 10000, 128, { encoding: THREE.sRGBEncoding } );
- // TEXTURES
- var textureLoader = new THREE.TextureLoader();
- var textureSquares = textureLoader.load( "textures/patterns/bright_squares256.png" );
- textureSquares.repeat.set( 50, 50 );
- textureSquares.wrapS = textureSquares.wrapT = THREE.RepeatWrapping;
- textureSquares.magFilter = THREE.NearestFilter;
- textureSquares.format = THREE.RGBFormat;
- textureSquares.encoding = THREE.sRGBEncoding;
- var textureNoiseColor = textureLoader.load( "textures/disturb.jpg" );
- textureNoiseColor.repeat.set( 1, 1 );
- textureNoiseColor.wrapS = textureNoiseColor.wrapT = THREE.RepeatWrapping;
- textureNoiseColor.format = THREE.RGBFormat;
- textureNoiseColor.encoding = THREE.sRGBEncoding;
- var textureLava = textureLoader.load( "textures/lava/lavatile.jpg" );
- textureLava.repeat.set( 6, 2 );
- textureLava.wrapS = textureLava.wrapT = THREE.RepeatWrapping;
- textureLava.format = THREE.RGBFormat;
- textureLava.encoding = THREE.sRGBEncoding;
- // GROUND
- var groundMaterial = new THREE.MeshPhongMaterial( {
- shininess: 80,
- color: 0xffffff,
- specular: 0xffffff,
- map: textureSquares
- } );
- var planeGeometry = new THREE.PlaneBufferGeometry( 100, 100 );
- var ground = new THREE.Mesh( planeGeometry, groundMaterial );
- ground.position.set( 0, 0, 0 );
- ground.rotation.x = - Math.PI / 2;
- ground.scale.set( 1000, 1000, 1000 );
- ground.receiveShadow = true;
- scene.add( ground );
- // MATERIALS
- var materialLambert = new THREE.MeshPhongMaterial( { shininess: 50, color: 0xffffff, map: textureNoiseColor } );
- var materialPhong = new THREE.MeshPhongMaterial( { shininess: 50, color: 0xffffff, specular: 0x999999, map: textureLava } );
- var materialPhongCube = new THREE.MeshPhongMaterial( { shininess: 50, color: 0xffffff, specular: 0x999999, envMap: cubeCamera.renderTarget.texture } );
- // OBJECTS
- var sphereGeometry = new THREE.SphereBufferGeometry( 100, 64, 32 );
- var torusGeometry = new THREE.TorusBufferGeometry( 240, 60, 32, 64 );
- var cubeGeometry = new THREE.BoxBufferGeometry( 150, 150, 150 );
- addObject( torusGeometry, materialPhong, 0, 100, 0, 0 );
- addObject( cubeGeometry, materialLambert, 350, 75, 300, 0 );
- mesh = addObject( sphereGeometry, materialPhongCube, 350, 100, - 350, 0 );
- mesh.add( cubeCamera );
- function addObjectColor( geometry, color, x, y, z, ry ) {
- var material = new THREE.MeshPhongMaterial( { color: 0xffffff } );
- return addObject( geometry, material, x, y, z, ry );
- }
- function addObject( geometry, material, x, y, z, ry ) {
- var tmpMesh = new THREE.Mesh( geometry, material );
- tmpMesh.material.color.offsetHSL( 0.1, - 0.1, 0 );
- tmpMesh.position.set( x, y, z );
- tmpMesh.rotation.y = ry;
- tmpMesh.castShadow = true;
- tmpMesh.receiveShadow = true;
- scene.add( tmpMesh );
- return tmpMesh;
- }
- var bigCube = new THREE.BoxBufferGeometry( 50, 500, 50 );
- var midCube = new THREE.BoxBufferGeometry( 50, 200, 50 );
- var smallCube = new THREE.BoxBufferGeometry( 100, 100, 100 );
- addObjectColor( bigCube, 0xff0000, - 500, 250, 0, 0 );
- addObjectColor( smallCube, 0xff0000, - 500, 50, - 150, 0 );
- addObjectColor( midCube, 0x00ff00, 500, 100, 0, 0 );
- addObjectColor( smallCube, 0x00ff00, 500, 50, - 150, 0 );
- addObjectColor( midCube, 0x0000ff, 0, 100, - 500, 0 );
- addObjectColor( smallCube, 0x0000ff, - 150, 50, - 500, 0 );
- addObjectColor( midCube, 0xff00ff, 0, 100, 500, 0 );
- addObjectColor( smallCube, 0xff00ff, - 150, 50, 500, 0 );
- addObjectColor( new THREE.BoxBufferGeometry( 500, 10, 10 ), 0xffff00, 0, 600, 0, Math.PI / 4 );
- addObjectColor( new THREE.BoxBufferGeometry( 250, 10, 10 ), 0xffff00, 0, 600, 0, 0 );
- addObjectColor( new THREE.SphereBufferGeometry( 100, 32, 26 ), 0xffffff, - 300, 100, 300, 0 );
- // MORPHS
- var loader = new GLTFLoader();
- loader.load( "models/gltf/SittingBox.glb", function ( gltf ) {
- var mesh = gltf.scene.children[ 0 ];
- mixer = new THREE.AnimationMixer( mesh );
- mixer.clipAction( gltf.animations[ 0 ] ).setDuration( 10 ).play();
- var s = 200;
- mesh.scale.set( s, s, s );
- mesh.castShadow = true;
- mesh.receiveShadow = true;
- scene.add( mesh );
- } );
- // LIGHTS
- ambientLight = new THREE.AmbientLight( 0x3f2806 );
- scene.add( ambientLight );
- pointLight = new THREE.PointLight( 0xffaa00, 1, 5000 );
- scene.add( pointLight );
- sunLight = new THREE.DirectionalLight( 0xffffff, 0.3 );
- sunLight.position.set( 1000, 2000, 1000 );
- sunLight.castShadow = true;
- sunLight.shadow.camera.top = 750;
- sunLight.shadow.camera.bottom = - 750;
- sunLight.shadow.camera.left = - 750;
- sunLight.shadow.camera.right = 750;
- sunLight.shadow.camera.near = shadowConfig.shadowCameraNear;
- sunLight.shadow.camera.far = shadowConfig.shadowCameraFar;
- sunLight.shadow.mapSize.set( 1024, 1024 );
- sunLight.shadow.bias = shadowConfig.shadowBias;
- scene.add( sunLight );
- // SHADOW CAMERA HELPER
- shadowCameraHelper = new THREE.CameraHelper( sunLight.shadow.camera );
- shadowCameraHelper.visible = shadowConfig.shadowCameraVisible;
- scene.add( shadowCameraHelper );
- // RENDERER
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- container.appendChild( renderer.domElement );
- //
- renderer.shadowMap.enabled = true;
- renderer.shadowMap.type = THREE.PCFSoftShadowMap;
- renderer.outputEncoding = THREE.sRGBEncoding;
- //
- controls = new TrackballControls( camera, renderer.domElement );
- controls.target.set( 0, 120, 0 );
- controls.rotateSpeed = 1.0;
- controls.zoomSpeed = 1.2;
- controls.panSpeed = 0.8;
- controls.staticMoving = true;
- controls.keys = [ 65, 83, 68 ];
- // STATS
- stats = new Stats();
- container.appendChild( stats.dom );
- // EVENTS
- window.addEventListener( 'resize', onWindowResize, false );
- // GUI
- gui = new GUI( { width: 400 } );
- // SHADOW
- var shadowGUI = gui.addFolder( "Shadow" );
- shadowGUI.add( shadowConfig, 'shadowCameraVisible' ).onChange( function () {
- shadowCameraHelper.visible = shadowConfig.shadowCameraVisible;
- } );
- shadowGUI.add( shadowConfig, 'shadowCameraNear', 1, 1500 ).onChange( function () {
- sunLight.shadow.camera.near = shadowConfig.shadowCameraNear;
- sunLight.shadow.camera.updateProjectionMatrix();
- shadowCameraHelper.update();
- } );
- shadowGUI.add( shadowConfig, 'shadowCameraFar', 1501, 5000 ).onChange( function () {
- sunLight.shadow.camera.far = shadowConfig.shadowCameraFar;
- sunLight.shadow.camera.updateProjectionMatrix();
- shadowCameraHelper.update();
- } );
- shadowGUI.add( shadowConfig, 'shadowBias', - 0.01, 0.01 ).onChange( function () {
- sunLight.shadow.bias = shadowConfig.shadowBias;
- } );
- shadowGUI.open();
- }
- //
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- controls.handleResize();
- }
- //
- function animate() {
- requestAnimationFrame( animate );
- stats.begin();
- render();
- stats.end();
- }
- function render() {
- // update
- var delta = clock.getDelta();
- controls.update();
- if ( mixer ) {
- mixer.update( delta );
- }
- // render cube map
- mesh.visible = false;
- cubeCamera.update( renderer, scene );
- mesh.visible = true;
- // render scene
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|