webgl_materials_channels.html 7.6 KB

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