webgl_shading_physical.html 9.7 KB

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