webgl_shading_physical.html 10 KB

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