webgl_materials_channels.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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" rel="noopener">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" rel="noopener">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/WebGL.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 ( WEBGL.isWebGLAvailable() === false ) {
  44. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  45. }
  46. var stats;
  47. var camera, scene, renderer;
  48. var params = {
  49. material: 'normal',
  50. camera: 'perspective',
  51. side: 'double'
  52. };
  53. var sides = {
  54. 'front': THREE.FrontSide,
  55. 'back': THREE.BackSide,
  56. 'double': THREE.DoubleSide
  57. };
  58. var cameraOrtho, cameraPerspective;
  59. var controlsOrtho, controlsPerspective;
  60. var mesh, materialStandard, materialDepthBasic, materialDepthRGBA, materialNormal;
  61. var height = 500; // of camera frustum
  62. var SCALE = 2.436143; // from original model
  63. var BIAS = - 0.428408; // from original model
  64. init();
  65. animate();
  66. initGui();
  67. // Init gui
  68. function initGui() {
  69. var gui = new dat.GUI();
  70. gui.add( params, 'material', [ 'standard', 'normal', 'depthBasic', 'depthRGBA' ] );
  71. gui.add( params, 'camera', [ 'perspective', 'ortho' ] );
  72. gui.add( params, 'side', [ 'front', 'back', 'double' ] );
  73. }
  74. function init() {
  75. var container = document.createElement( 'div' );
  76. document.body.appendChild( container );
  77. renderer = new THREE.WebGLRenderer();
  78. renderer.setPixelRatio( window.devicePixelRatio );
  79. renderer.setSize( window.innerWidth, window.innerHeight );
  80. container.appendChild( renderer.domElement );
  81. //
  82. scene = new THREE.Scene();
  83. var aspect = window.innerWidth / window.innerHeight;
  84. cameraPerspective = new THREE.PerspectiveCamera( 45, aspect, 1000, 2500 );
  85. cameraPerspective.position.z = 1500;
  86. scene.add( cameraPerspective );
  87. cameraOrtho = new THREE.OrthographicCamera( - height * aspect, height * aspect, height, - height, 1000, 2500 );
  88. cameraOrtho.position.z = 1500;
  89. scene.add( cameraOrtho );
  90. camera = cameraPerspective;
  91. controlsPerspective = new THREE.OrbitControls( cameraPerspective, renderer.domElement );
  92. controlsPerspective.minDistance = 1000;
  93. controlsPerspective.maxDistance = 2500;
  94. controlsPerspective.enablePan = false;
  95. controlsPerspective.enableDamping = true;
  96. controlsOrtho = new THREE.OrbitControls( cameraOrtho, renderer.domElement );
  97. controlsOrtho.minZoom = 0.5;
  98. controlsOrtho.maxZoom = 2;
  99. controlsOrtho.enablePan = false;
  100. controlsOrtho.enableDamping = true;
  101. // lights
  102. var ambientLight = new THREE.AmbientLight( 0xffffff, 0.1 );
  103. scene.add( ambientLight );
  104. var pointLight = new THREE.PointLight( 0xff0000, 0.5 );
  105. pointLight.position.z = 2500;
  106. scene.add( pointLight );
  107. var pointLight2 = new THREE.PointLight( 0xff6666, 1 );
  108. camera.add( pointLight2 );
  109. var pointLight3 = new THREE.PointLight( 0x0000ff, 0.5 );
  110. pointLight3.position.x = - 1000;
  111. pointLight3.position.z = 1000;
  112. scene.add( pointLight3 );
  113. // textures
  114. var textureLoader = new THREE.TextureLoader();
  115. var normalMap = textureLoader.load( "models/obj/ninja/normal.jpg" );
  116. var aoMap = textureLoader.load( "models/obj/ninja/ao.jpg" );
  117. var displacementMap = textureLoader.load( "models/obj/ninja/displacement.jpg" );
  118. // material
  119. materialStandard = new THREE.MeshStandardMaterial( {
  120. color: 0xffffff,
  121. metalness: 0.5,
  122. roughness: 0.6,
  123. displacementMap: displacementMap,
  124. displacementScale: SCALE,
  125. displacementBias: BIAS,
  126. aoMap: aoMap,
  127. normalMap: normalMap,
  128. normalScale: new THREE.Vector2( 1, - 1 ),
  129. //flatShading: true,
  130. side: THREE.DoubleSide
  131. } );
  132. materialDepthBasic = new THREE.MeshDepthMaterial( {
  133. depthPacking: THREE.BasicDepthPacking,
  134. displacementMap: displacementMap,
  135. displacementScale: SCALE,
  136. displacementBias: BIAS,
  137. side: THREE.DoubleSide
  138. } );
  139. materialDepthRGBA = new THREE.MeshDepthMaterial( {
  140. depthPacking: THREE.RGBADepthPacking,
  141. displacementMap: displacementMap,
  142. displacementScale: SCALE,
  143. displacementBias: BIAS,
  144. side: THREE.DoubleSide
  145. } );
  146. materialNormal = new THREE.MeshNormalMaterial( {
  147. displacementMap: displacementMap,
  148. displacementScale: SCALE,
  149. displacementBias: BIAS,
  150. normalMap: normalMap,
  151. normalScale: new THREE.Vector2( 1, - 1 ),
  152. //flatShading: true,
  153. side: THREE.DoubleSide
  154. } );
  155. //
  156. var loader = new THREE.OBJLoader();
  157. loader.load( "models/obj/ninja/ninjaHead_Low.obj", function ( group ) {
  158. var geometry = group.children[ 0 ].geometry;
  159. geometry.attributes.uv2 = geometry.attributes.uv;
  160. geometry.center();
  161. mesh = new THREE.Mesh( geometry, materialNormal );
  162. mesh.scale.multiplyScalar( 25 );
  163. scene.add( mesh );
  164. } );
  165. //
  166. stats = new Stats();
  167. container.appendChild( stats.dom );
  168. //
  169. window.addEventListener( 'resize', onWindowResize, false );
  170. }
  171. function onWindowResize() {
  172. var width = window.innerWidth;
  173. var height = window.innerHeight;
  174. var aspect = window.innerWidth / window.innerHeight;
  175. camera.aspect = aspect;
  176. camera.left = - height * aspect;
  177. camera.right = height * aspect;
  178. camera.top = height;
  179. camera.bottom = - height;
  180. camera.updateProjectionMatrix();
  181. renderer.setSize( width, height );
  182. }
  183. //
  184. function animate() {
  185. requestAnimationFrame( animate );
  186. stats.begin();
  187. render();
  188. stats.end();
  189. }
  190. function render() {
  191. if ( mesh ) {
  192. var material = mesh.material;
  193. switch ( params.material ) {
  194. case 'standard': material = materialStandard; break;
  195. case 'depthBasic': material = materialDepthBasic; break;
  196. case 'depthRGBA': material = materialDepthRGBA; break;
  197. case 'normal': material = materialNormal; break;
  198. }
  199. if ( sides[ params.side ] !== material.side ) {
  200. switch ( params.side ) {
  201. case 'front': material.side = THREE.FrontSide; break;
  202. case 'back': material.side = THREE.BackSide; break;
  203. case 'double': material.side = THREE.DoubleSide; break;
  204. }
  205. material.needsUpdate = true;
  206. }
  207. mesh.material = material;
  208. }
  209. switch ( params.camera ) {
  210. case 'perspective': camera = cameraPerspective; break;
  211. case 'ortho': camera = cameraOrtho; break;
  212. }
  213. renderer.render( scene, camera );
  214. }
  215. </script>
  216. </body>
  217. </html>