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