webgl_shading_physical.html 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - physically based shading</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl physically based shading testbed
  12. </div>
  13. <script type="module">
  14. import * as THREE from '../build/three.module.js';
  15. import Stats from './jsm/libs/stats.module.js';
  16. import { GUI } from './jsm/libs/dat.gui.module.js';
  17. import { TrackballControls } from './jsm/controls/TrackballControls.js';
  18. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  19. var container, stats;
  20. var camera, scene, renderer;
  21. var mesh;
  22. var controls;
  23. var cubeCamera;
  24. var sunLight, pointLight, ambientLight;
  25. var mixer;
  26. var clock = new THREE.Clock();
  27. var gui, shadowCameraHelper, shadowConfig = {
  28. shadowCameraVisible: false,
  29. shadowCameraNear: 750,
  30. shadowCameraFar: 4000,
  31. shadowBias: - 0.0002
  32. };
  33. init();
  34. animate();
  35. function init() {
  36. container = document.createElement( 'div' );
  37. document.body.appendChild( container );
  38. // CAMERA
  39. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 2, 10000 );
  40. camera.position.set( 500, 400, 1200 );
  41. // SCENE
  42. scene = new THREE.Scene();
  43. scene.fog = new THREE.Fog( 0, 1000, 10000 );
  44. // CUBE CAMERA
  45. cubeCamera = new THREE.CubeCamera( 1, 10000, 128, { encoding: THREE.sRGBEncoding } );
  46. // TEXTURES
  47. var textureLoader = new THREE.TextureLoader();
  48. var textureSquares = textureLoader.load( "textures/patterns/bright_squares256.png" );
  49. textureSquares.repeat.set( 50, 50 );
  50. textureSquares.wrapS = textureSquares.wrapT = THREE.RepeatWrapping;
  51. textureSquares.magFilter = THREE.NearestFilter;
  52. textureSquares.encoding = THREE.sRGBEncoding;
  53. var textureNoiseColor = textureLoader.load( "textures/disturb.jpg" );
  54. textureNoiseColor.repeat.set( 1, 1 );
  55. textureNoiseColor.wrapS = textureNoiseColor.wrapT = THREE.RepeatWrapping;
  56. textureNoiseColor.encoding = THREE.sRGBEncoding;
  57. var textureLava = textureLoader.load( "textures/lava/lavatile.jpg" );
  58. textureLava.repeat.set( 6, 2 );
  59. textureLava.wrapS = textureLava.wrapT = THREE.RepeatWrapping;
  60. textureLava.encoding = THREE.sRGBEncoding;
  61. // GROUND
  62. var groundMaterial = new THREE.MeshPhongMaterial( {
  63. shininess: 80,
  64. color: 0xffffff,
  65. specular: 0xffffff,
  66. map: textureSquares
  67. } );
  68. var planeGeometry = new THREE.PlaneBufferGeometry( 100, 100 );
  69. var ground = new THREE.Mesh( planeGeometry, groundMaterial );
  70. ground.position.set( 0, 0, 0 );
  71. ground.rotation.x = - Math.PI / 2;
  72. ground.scale.set( 1000, 1000, 1000 );
  73. ground.receiveShadow = true;
  74. scene.add( ground );
  75. // MATERIALS
  76. var materialLambert = new THREE.MeshPhongMaterial( { shininess: 50, color: 0xffffff, map: textureNoiseColor } );
  77. var materialPhong = new THREE.MeshPhongMaterial( { shininess: 50, color: 0xffffff, specular: 0x999999, map: textureLava } );
  78. var materialPhongCube = new THREE.MeshPhongMaterial( { shininess: 50, color: 0xffffff, specular: 0x999999, envMap: cubeCamera.renderTarget.texture } );
  79. // OBJECTS
  80. var sphereGeometry = new THREE.SphereBufferGeometry( 100, 64, 32 );
  81. var torusGeometry = new THREE.TorusBufferGeometry( 240, 60, 32, 64 );
  82. var cubeGeometry = new THREE.BoxBufferGeometry( 150, 150, 150 );
  83. addObject( torusGeometry, materialPhong, 0, 100, 0, 0 );
  84. addObject( cubeGeometry, materialLambert, 350, 75, 300, 0 );
  85. mesh = addObject( sphereGeometry, materialPhongCube, 350, 100, - 350, 0 );
  86. mesh.add( cubeCamera );
  87. function addObjectColor( geometry, color, x, y, z, ry ) {
  88. var material = new THREE.MeshPhongMaterial( { color: 0xffffff } );
  89. return addObject( geometry, material, x, y, z, ry );
  90. }
  91. function addObject( geometry, material, x, y, z, ry ) {
  92. var tmpMesh = new THREE.Mesh( geometry, material );
  93. tmpMesh.material.color.offsetHSL( 0.1, - 0.1, 0 );
  94. tmpMesh.position.set( x, y, z );
  95. tmpMesh.rotation.y = ry;
  96. tmpMesh.castShadow = true;
  97. tmpMesh.receiveShadow = true;
  98. scene.add( tmpMesh );
  99. return tmpMesh;
  100. }
  101. var bigCube = new THREE.BoxBufferGeometry( 50, 500, 50 );
  102. var midCube = new THREE.BoxBufferGeometry( 50, 200, 50 );
  103. var smallCube = new THREE.BoxBufferGeometry( 100, 100, 100 );
  104. addObjectColor( bigCube, 0xff0000, - 500, 250, 0, 0 );
  105. addObjectColor( smallCube, 0xff0000, - 500, 50, - 150, 0 );
  106. addObjectColor( midCube, 0x00ff00, 500, 100, 0, 0 );
  107. addObjectColor( smallCube, 0x00ff00, 500, 50, - 150, 0 );
  108. addObjectColor( midCube, 0x0000ff, 0, 100, - 500, 0 );
  109. addObjectColor( smallCube, 0x0000ff, - 150, 50, - 500, 0 );
  110. addObjectColor( midCube, 0xff00ff, 0, 100, 500, 0 );
  111. addObjectColor( smallCube, 0xff00ff, - 150, 50, 500, 0 );
  112. addObjectColor( new THREE.BoxBufferGeometry( 500, 10, 10 ), 0xffff00, 0, 600, 0, Math.PI / 4 );
  113. addObjectColor( new THREE.BoxBufferGeometry( 250, 10, 10 ), 0xffff00, 0, 600, 0, 0 );
  114. addObjectColor( new THREE.SphereBufferGeometry( 100, 32, 26 ), 0xffffff, - 300, 100, 300, 0 );
  115. // MORPHS
  116. var loader = new GLTFLoader();
  117. loader.load( "models/gltf/SittingBox.glb", function ( gltf ) {
  118. var mesh = gltf.scene.children[ 0 ];
  119. mixer = new THREE.AnimationMixer( mesh );
  120. mixer.clipAction( gltf.animations[ 0 ] ).setDuration( 10 ).play();
  121. var s = 200;
  122. mesh.scale.set( s, s, s );
  123. mesh.castShadow = true;
  124. mesh.receiveShadow = true;
  125. scene.add( mesh );
  126. } );
  127. // LIGHTS
  128. ambientLight = new THREE.AmbientLight( 0x3f2806 );
  129. scene.add( ambientLight );
  130. pointLight = new THREE.PointLight( 0xffaa00, 1, 5000 );
  131. scene.add( pointLight );
  132. sunLight = new THREE.DirectionalLight( 0xffffff, 0.3 );
  133. sunLight.position.set( 1000, 2000, 1000 );
  134. sunLight.castShadow = true;
  135. sunLight.shadow.camera.top = 750;
  136. sunLight.shadow.camera.bottom = - 750;
  137. sunLight.shadow.camera.left = - 750;
  138. sunLight.shadow.camera.right = 750;
  139. sunLight.shadow.camera.near = shadowConfig.shadowCameraNear;
  140. sunLight.shadow.camera.far = shadowConfig.shadowCameraFar;
  141. sunLight.shadow.mapSize.set( 1024, 1024 );
  142. sunLight.shadow.bias = shadowConfig.shadowBias;
  143. scene.add( sunLight );
  144. // SHADOW CAMERA HELPER
  145. shadowCameraHelper = new THREE.CameraHelper( sunLight.shadow.camera );
  146. shadowCameraHelper.visible = shadowConfig.shadowCameraVisible;
  147. scene.add( shadowCameraHelper );
  148. // RENDERER
  149. renderer = new THREE.WebGLRenderer( { antialias: true } );
  150. renderer.setPixelRatio( window.devicePixelRatio );
  151. renderer.setSize( window.innerWidth, window.innerHeight );
  152. container.appendChild( renderer.domElement );
  153. //
  154. renderer.shadowMap.enabled = true;
  155. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  156. renderer.outputEncoding = THREE.sRGBEncoding;
  157. //
  158. controls = new TrackballControls( camera, renderer.domElement );
  159. controls.target.set( 0, 120, 0 );
  160. controls.rotateSpeed = 1.0;
  161. controls.zoomSpeed = 1.2;
  162. controls.panSpeed = 0.8;
  163. controls.staticMoving = true;
  164. controls.keys = [ 65, 83, 68 ];
  165. // STATS
  166. stats = new Stats();
  167. container.appendChild( stats.dom );
  168. // EVENTS
  169. window.addEventListener( 'resize', onWindowResize, false );
  170. // GUI
  171. gui = new GUI( { width: 400 } );
  172. // SHADOW
  173. var shadowGUI = gui.addFolder( "Shadow" );
  174. shadowGUI.add( shadowConfig, 'shadowCameraVisible' ).onChange( function () {
  175. shadowCameraHelper.visible = shadowConfig.shadowCameraVisible;
  176. } );
  177. shadowGUI.add( shadowConfig, 'shadowCameraNear', 1, 1500 ).onChange( function () {
  178. sunLight.shadow.camera.near = shadowConfig.shadowCameraNear;
  179. sunLight.shadow.camera.updateProjectionMatrix();
  180. shadowCameraHelper.update();
  181. } );
  182. shadowGUI.add( shadowConfig, 'shadowCameraFar', 1501, 5000 ).onChange( function () {
  183. sunLight.shadow.camera.far = shadowConfig.shadowCameraFar;
  184. sunLight.shadow.camera.updateProjectionMatrix();
  185. shadowCameraHelper.update();
  186. } );
  187. shadowGUI.add( shadowConfig, 'shadowBias', - 0.01, 0.01 ).onChange( function () {
  188. sunLight.shadow.bias = shadowConfig.shadowBias;
  189. } );
  190. shadowGUI.open();
  191. }
  192. //
  193. function onWindowResize() {
  194. camera.aspect = window.innerWidth / window.innerHeight;
  195. camera.updateProjectionMatrix();
  196. renderer.setSize( window.innerWidth, window.innerHeight );
  197. controls.handleResize();
  198. }
  199. //
  200. function animate() {
  201. requestAnimationFrame( animate );
  202. stats.begin();
  203. render();
  204. stats.end();
  205. }
  206. function render() {
  207. // update
  208. var delta = clock.getDelta();
  209. controls.update();
  210. if ( mixer ) {
  211. mixer.update( delta );
  212. }
  213. // render cube map
  214. mesh.visible = false;
  215. cubeCamera.update( renderer, scene );
  216. mesh.visible = true;
  217. // render scene
  218. renderer.render( scene, camera );
  219. }
  220. </script>
  221. </body>
  222. </html>