webgl_shading_physical.html 9.3 KB

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