webgl_shading_physical.html 9.3 KB

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