webgl_materials_channels.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - channels</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. }
  26. #vt { display:none }
  27. #vt, #vt a { color:orange; }
  28. </style>
  29. </head>
  30. <body>
  31. <div id="info">
  32. <a href="http://threejs.org" target="_blank">three.js</a> - <span id="description">Normal, Depth, DepthRGBA, DepthRGBAUnpacked, Materials</span> by <a href="https://Clara.io">Ben Houston</a>.<br />
  33. ninja head from <a href="http://developer.amd.com/tools-and-sdks/archive/legacy-cpu-gpu-tools/amd-gpu-meshmapper/" target="_blank">AMD GPU MeshMapper</a>
  34. <div id="vt">displacement mapping requires vertex textures</div>
  35. </div>
  36. <script src="../build/three.js"></script>
  37. <script src="js/controls/OrbitControls.js"></script>
  38. <script src="js/loaders/OBJLoader.js"></script>
  39. <script src="js/Detector.js"></script>
  40. <script src="js/libs/stats.min.js"></script>
  41. <script src='js/libs/dat.gui.min.js'></script>
  42. <script>
  43. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  44. var stats;
  45. var camera, scene, renderer, controls;
  46. var params = {
  47. material: 'normal',
  48. camera: 'perspective',
  49. side: 'double'
  50. };
  51. var cameraOrtho, cameraPerspective;
  52. var controlsOrtho, controlsPerspective;
  53. var mesh, materialStandard, materialDepthBasic, materialDepthRGBA, materialNormal;
  54. var pointLight, ambientLight;
  55. var height = 500; // of camera frustum
  56. var SCALE = 2.436143; // from original model
  57. var BIAS = - 0.428408; // from original model
  58. init();
  59. animate();
  60. initGui();
  61. // Init gui
  62. function initGui() {
  63. var gui = new dat.GUI();
  64. gui.add( params, 'material', [ 'standard', 'normal', 'depthBasic', 'depthRGBA' ] );
  65. gui.add( params, 'camera', [ 'perspective', 'ortho' ] );
  66. gui.add( params, 'side', [ 'front', 'back', 'double' ] );
  67. }
  68. function init() {
  69. var container = document.createElement( 'div' );
  70. document.body.appendChild( container );
  71. renderer = new THREE.WebGLRenderer();
  72. renderer.setPixelRatio( window.devicePixelRatio );
  73. renderer.setSize( window.innerWidth, window.innerHeight );
  74. container.appendChild( renderer.domElement );
  75. //
  76. scene = new THREE.Scene();
  77. var aspect = window.innerWidth / window.innerHeight;
  78. cameraPerspective = new THREE.PerspectiveCamera( 45, aspect, 1000, 2500 );
  79. cameraPerspective.position.z = 1500;
  80. scene.add( cameraPerspective );
  81. cameraOrtho = new THREE.OrthographicCamera( - height * aspect, height * aspect, height, - height, 1000, 2500 );
  82. cameraOrtho.position.z = 1500;
  83. scene.add( cameraOrtho );
  84. camera = cameraPerspective;
  85. controlsPerspective = new THREE.OrbitControls( cameraPerspective, renderer.domElement );
  86. controlsPerspective.minDistance = 1000;
  87. controlsPerspective.maxDistance = 2500;
  88. controlsPerspective.enablePan = false;
  89. controlsPerspective.enableDamping = true;
  90. controlsOrtho = new THREE.OrbitControls( cameraOrtho, renderer.domElement );
  91. controlsOrtho.minZoom = 0.5;
  92. controlsOrtho.maxZoom = 2;
  93. controlsOrtho.enablePan = false;
  94. controlsOrtho.enableDamping = true;
  95. // lights
  96. var ambientLight = new THREE.AmbientLight( 0xffffff, 0.1 );
  97. scene.add( ambientLight );
  98. var pointLight = new THREE.PointLight( 0xff0000, 0.5 );
  99. pointLight.position.z = 2500;
  100. scene.add( pointLight );
  101. var pointLight2 = new THREE.PointLight( 0xff6666, 1 );
  102. camera.add( pointLight2 );
  103. var pointLight3 = new THREE.PointLight( 0x0000ff, 0.5 );
  104. pointLight3.position.x = - 1000;
  105. pointLight3.position.z = 1000;
  106. scene.add( pointLight3 );
  107. // textures
  108. var textureLoader = new THREE.TextureLoader();
  109. var normalMap = textureLoader.load( "models/obj/ninja/normal.jpg" );
  110. var aoMap = textureLoader.load( "models/obj/ninja/ao.jpg" );
  111. var displacementMap = textureLoader.load( "models/obj/ninja/displacement.jpg" );
  112. // material
  113. materialStandard = new THREE.MeshStandardMaterial( {
  114. color: 0xffffff,
  115. metalness: 0.5,
  116. roughness: 0.6,
  117. displacementMap: displacementMap,
  118. displacementScale: SCALE,
  119. displacementBias: BIAS,
  120. aoMap: aoMap,
  121. normalMap: normalMap,
  122. normalScale: new THREE.Vector2( 1, - 1 ),
  123. //shading: THREE.FlatShading,
  124. side: THREE.DoubleSide
  125. } );
  126. materialDepthBasic = new THREE.MeshDepthMaterial( {
  127. depthPacking: THREE.BasicDepthPacking,
  128. displacementMap: displacementMap,
  129. displacementScale: SCALE,
  130. displacementBias: BIAS,
  131. side: THREE.DoubleSide
  132. } );
  133. materialDepthRGBA = new THREE.MeshDepthMaterial( {
  134. depthPacking: THREE.RGBADepthPacking,
  135. displacementMap: displacementMap,
  136. displacementScale: SCALE,
  137. displacementBias: BIAS,
  138. side: THREE.DoubleSide
  139. } );
  140. materialNormal = new THREE.MeshNormalMaterial( {
  141. displacementMap: displacementMap,
  142. displacementScale: SCALE,
  143. displacementBias: BIAS,
  144. normalMap: normalMap,
  145. normalScale: new THREE.Vector2( 1, - 1 ),
  146. //shading: THREE.FlatShading,
  147. side: THREE.DoubleSide
  148. } );
  149. //
  150. var loader = new THREE.OBJLoader();
  151. loader.load( "models/obj/ninja/ninjaHead_Low.obj", function ( group ) {
  152. geometry = group.children[ 0 ].geometry;
  153. geometry.attributes.uv2 = geometry.attributes.uv;
  154. geometry.center();
  155. mesh = new THREE.Mesh( geometry, materialNormal );
  156. mesh.scale.multiplyScalar( 25 );
  157. scene.add( mesh );
  158. } );
  159. //
  160. stats = new Stats();
  161. container.appendChild( stats.dom );
  162. //
  163. window.addEventListener( 'resize', onWindowResize, false );
  164. }
  165. function onWindowResize() {
  166. var width = window.innerWidth;
  167. var height = window.innerHeight;
  168. var aspect = window.innerWidth / window.innerHeight;
  169. camera.aspect = aspect;
  170. camera.left = - height * aspect;
  171. camera.right = height * aspect;
  172. camera.top = height;
  173. camera.bottom = - height;
  174. camera.updateProjectionMatrix();
  175. renderer.setSize( width, height );
  176. }
  177. //
  178. function animate() {
  179. requestAnimationFrame( animate );
  180. controlsOrtho.update();
  181. controlsPerspective.update();
  182. stats.begin();
  183. render();
  184. stats.end();
  185. }
  186. function render() {
  187. if ( mesh ) {
  188. var material = mesh.material;
  189. switch ( params.material ) {
  190. case 'standard': material = materialStandard; break;
  191. case 'depthBasic': material = materialDepthBasic; break;
  192. case 'depthRGBA': material = materialDepthRGBA; break;
  193. case 'normal': material = materialNormal; break;
  194. }
  195. if ( params.side !== material.side ) {
  196. switch ( params.side ) {
  197. case 'front': material.side = THREE.FrontSide; break;
  198. case 'back': material.side = THREE.BackSide; break;
  199. case 'double': material.side = THREE.DoubleSide; break;
  200. }
  201. material.needsUpdate = true;
  202. }
  203. mesh.material = material;
  204. }
  205. switch ( params.camera ) {
  206. case 'perspective': camera = cameraPerspective; break;
  207. case 'ortho': camera = cameraOrtho; break;
  208. }
  209. renderer.render( scene, camera );
  210. }
  211. </script>
  212. </body>
  213. </html>