webgl_materials_bumpmap_skin.html 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - bump map - skin [Lee Perry-Smith]</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. background:#000;
  10. color:#fff;
  11. padding:0;
  12. margin:0;
  13. font-weight: bold;
  14. overflow:hidden;
  15. }
  16. a { color: #ffffff; }
  17. #info {
  18. position: absolute;
  19. top: 0px; width: 100%;
  20. color: #ffffff;
  21. padding: 5px;
  22. font-family:Monospace;
  23. font-size:13px;
  24. text-align:center;
  25. z-index:1000;
  26. }
  27. #oldie {
  28. background:rgb(200,100,0) !important;
  29. color:#fff;
  30. }
  31. #stats { position: absolute; top:0; left: 0 }
  32. #stats #fps { background: transparent !important }
  33. #stats #fps #fpsText { color: #aaa !important }
  34. #stats #fps #fpsGraph { display: none }
  35. </style>
  36. </head>
  37. <body>
  38. <div id="info">
  39. <a href="http://threejs.org" target="_blank">three.js</a> - webgl simple single-pass skin material with <a href="http://mmikkelsen3d.blogspot.sk/2011/07/derivative-maps.html">tangent-less bump mapping</a> -
  40. <a href="http://www.ir-ltd.net/infinite-3d-head-scan-released/" target="_blank">Lee Perry-Smith</a> head
  41. </div>
  42. <script src="../build/three.min.js"></script>
  43. <script src="js/ShaderSkin.js"></script>
  44. <script src="js/shaders/CopyShader.js"></script>
  45. <script src="js/postprocessing/EffectComposer.js"></script>
  46. <script src="js/postprocessing/RenderPass.js"></script>
  47. <script src="js/postprocessing/ShaderPass.js"></script>
  48. <script src="js/postprocessing/MaskPass.js"></script>
  49. <script src="js/Detector.js"></script>
  50. <script src="js/libs/stats.min.js"></script>
  51. <script>
  52. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  53. var statsEnabled = true;
  54. var container, stats, loader;
  55. var camera, scene, renderer;
  56. var mesh, mesh2;
  57. var directionalLight, directionalLight2, pointLight, ambientLight, spotLight;
  58. var mouseX = 0;
  59. var mouseY = 0;
  60. var targetX = 0, targetY = 0;
  61. var windowHalfX = window.innerWidth / 2;
  62. var windowHalfY = window.innerHeight / 2;
  63. var mapColor, mapHeight, mapSpecular;
  64. var firstPass = true;
  65. var composer, composerBeckmann;
  66. init();
  67. animate();
  68. function init() {
  69. container = document.createElement( 'div' );
  70. document.body.appendChild( container );
  71. //
  72. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 10000 );
  73. camera.position.z = 1200;
  74. scene = new THREE.Scene();
  75. // LIGHTS
  76. ambientLight = new THREE.AmbientLight( 0x555555 );
  77. scene.add( ambientLight );
  78. //
  79. pointLight = new THREE.PointLight( 0xffffff, 1.5, 1000 );
  80. pointLight.position.set( 0, 0, 600 );
  81. scene.add( pointLight );
  82. // shadow for PointLight
  83. spotLight = new THREE.SpotLight( 0xffffff, 1 );
  84. spotLight.position.set( 0.05, 0.05, 1 );
  85. scene.add( spotLight );
  86. spotLight.position.multiplyScalar( 700 );
  87. spotLight.castShadow = true;
  88. spotLight.onlyShadow = true;
  89. //spotLight.shadowCameraVisible = true;
  90. spotLight.shadowMapWidth = 2048;
  91. spotLight.shadowMapHeight = 2048;
  92. spotLight.shadowCameraNear = 200;
  93. spotLight.shadowCameraFar = 1500;
  94. spotLight.shadowCameraFov = 40;
  95. spotLight.shadowBias = -0.005;
  96. spotLight.shadowDarkness = 0.15;
  97. //
  98. directionalLight = new THREE.DirectionalLight( 0xffffff, 0.85 );
  99. directionalLight.position.set( 1, -0.5, 1 );
  100. directionalLight.color.setHSL( 0.6, 1, 0.85 );
  101. scene.add( directionalLight );
  102. directionalLight.position.multiplyScalar( 500 );
  103. directionalLight.castShadow = true;
  104. //directionalLight.shadowCameraVisible = true;
  105. directionalLight.shadowMapWidth = 2048;
  106. directionalLight.shadowMapHeight = 2048;
  107. directionalLight.shadowCameraNear = 200;
  108. directionalLight.shadowCameraFar = 1500;
  109. directionalLight.shadowCameraLeft = -500;
  110. directionalLight.shadowCameraRight = 500;
  111. directionalLight.shadowCameraTop = 500;
  112. directionalLight.shadowCameraBottom = -500;
  113. directionalLight.shadowBias = -0.005;
  114. directionalLight.shadowDarkness = 0.15;
  115. //
  116. directionalLight2 = new THREE.DirectionalLight( 0xffffff, 0.85 );
  117. directionalLight2.position.set( 1, -0.5, -1 );
  118. scene.add( directionalLight2 );
  119. //
  120. loader = new THREE.JSONLoader( true );
  121. document.body.appendChild( loader.statusDomElement );
  122. loader.load( "obj/leeperrysmith/LeePerrySmith.js", function( geometry ) { createScene( geometry, 100 ) } );
  123. //
  124. renderer = new THREE.WebGLRenderer( { antialias: false } );
  125. renderer.setClearColor( 0x060708, 1 );
  126. renderer.setSize( window.innerWidth, window.innerHeight );
  127. container.appendChild( renderer.domElement );
  128. var color = new THREE.Color();
  129. color.setHSL( 0.6, 0.1, 0.3 );
  130. renderer.setClearColor( color, 1 );
  131. renderer.shadowMapEnabled = true;
  132. renderer.shadowMapCullFace = THREE.CullFaceBack;
  133. renderer.autoClear = false;
  134. //
  135. renderer.gammaInput = true;
  136. renderer.gammaOutput = true;
  137. //
  138. if ( statsEnabled ) {
  139. stats = new Stats();
  140. container.appendChild( stats.domElement );
  141. }
  142. // COMPOSER
  143. renderer.autoClear = false;
  144. // BECKMANN
  145. var effectBeckmann = new THREE.ShaderPass( THREE.ShaderSkin[ "beckmann" ] );
  146. var effectCopy = new THREE.ShaderPass( THREE.CopyShader );
  147. effectCopy.renderToScreen = true;
  148. var pars = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat, stencilBufer: false };
  149. var rtwidth = 512, rtheight = 512;
  150. composerBeckmann = new THREE.EffectComposer( renderer, new THREE.WebGLRenderTarget( rtwidth, rtheight, pars ) );
  151. composerBeckmann.addPass( effectBeckmann );
  152. composerBeckmann.addPass( effectCopy );
  153. // EVENTS
  154. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  155. window.addEventListener( 'resize', onWindowResize, false );
  156. }
  157. function createScene( geometry, scale ) {
  158. var mapHeight = THREE.ImageUtils.loadTexture( "obj/leeperrysmith/Infinite-Level_02_Disp_NoSmoothUV-4096.jpg" );
  159. mapHeight.anisotropy = 4;
  160. mapHeight.repeat.set( 0.998, 0.998 );
  161. mapHeight.offset.set( 0.001, 0.001 )
  162. mapHeight.wrapS = mapHeight.wrapT = THREE.RepeatWrapping;
  163. mapHeight.format = THREE.RGBFormat;
  164. var mapSpecular = THREE.ImageUtils.loadTexture( "obj/leeperrysmith/Map-SPEC.jpg" );
  165. mapSpecular.anisotropy = 4;
  166. mapSpecular.repeat.set( 0.998, 0.998 );
  167. mapSpecular.offset.set( 0.001, 0.001 )
  168. mapSpecular.wrapS = mapSpecular.wrapT = THREE.RepeatWrapping;
  169. mapSpecular.format = THREE.RGBFormat;
  170. var mapColor = THREE.ImageUtils.loadTexture( "obj/leeperrysmith/Map-COL.jpg" );
  171. mapColor.anisotropy = 4;
  172. mapColor.repeat.set( 0.998, 0.998 );
  173. mapColor.offset.set( 0.001, 0.001 )
  174. mapColor.wrapS = mapColor.wrapT = THREE.RepeatWrapping;
  175. mapColor.format = THREE.RGBFormat;
  176. var shader = THREE.ShaderSkin[ "skinSimple" ];
  177. var fragmentShader = shader.fragmentShader;
  178. var vertexShader = shader.vertexShader;
  179. var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  180. uniforms[ "enableBump" ].value = true;
  181. uniforms[ "enableSpecular" ].value = true;
  182. uniforms[ "tBeckmann" ].value = composerBeckmann.renderTarget1;
  183. uniforms[ "tDiffuse" ].value = mapColor;
  184. uniforms[ "bumpMap" ].value = mapHeight;
  185. uniforms[ "specularMap" ].value = mapSpecular;
  186. uniforms[ "ambient" ].value.setHex( 0xa0a0a0 );
  187. uniforms[ "diffuse" ].value.setHex( 0xa0a0a0 );
  188. uniforms[ "specular" ].value.setHex( 0xa0a0a0 );
  189. uniforms[ "uRoughness" ].value = 0.145;
  190. uniforms[ "uSpecularBrightness" ].value = 0.75;
  191. uniforms[ "bumpScale" ].value = 16;
  192. uniforms[ "offsetRepeat" ].value.set( 0.001, 0.001, 0.998, 0.998 );
  193. var material = new THREE.ShaderMaterial( { fragmentShader: fragmentShader, vertexShader: vertexShader, uniforms: uniforms, lights: true } );
  194. mesh = new THREE.Mesh( geometry, material );
  195. mesh.position.y = - 50;
  196. mesh.scale.set( scale, scale, scale );
  197. mesh.castShadow = true;
  198. mesh.receiveShadow = true;
  199. scene.add( mesh );
  200. loader.statusDomElement.style.display = "none";
  201. }
  202. //
  203. function onWindowResize( event ) {
  204. SCREEN_WIDTH = window.innerWidth;
  205. SCREEN_HEIGHT = window.innerHeight;
  206. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  207. camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  208. camera.updateProjectionMatrix();
  209. }
  210. function onDocumentMouseMove( event ) {
  211. mouseX = ( event.clientX - windowHalfX ) * 1;
  212. mouseY = ( event.clientY - windowHalfY ) * 1;
  213. }
  214. //
  215. function animate() {
  216. requestAnimationFrame( animate );
  217. render();
  218. if ( statsEnabled ) stats.update();
  219. }
  220. function render() {
  221. targetX = mouseX * .001;
  222. targetY = mouseY * .001;
  223. if ( mesh ) {
  224. mesh.rotation.y += 0.05 * ( targetX - mesh.rotation.y );
  225. mesh.rotation.x += 0.05 * ( targetY - mesh.rotation.x );
  226. }
  227. if ( firstPass ) {
  228. composerBeckmann.render();
  229. firstPass = false;
  230. }
  231. renderer.clear();
  232. renderer.render( scene, camera );
  233. }
  234. </script>
  235. </body>
  236. </html>