webgl_materials_channels.html 7.5 KB

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