webgl_materials_channels.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. #stats { position: absolute; top:0; left: 0 }
  29. #stats #fps { background: transparent !important }
  30. #stats #fps #fpsText { color: #aaa !important }
  31. #stats #fps #fpsGraph { display: none }
  32. </style>
  33. </head>
  34. <body>
  35. <div id="info">
  36. <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 />
  37. 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>
  38. <div id="vt">displacement mapping requires vertex textures</div>
  39. </div>
  40. <script src="../build/three.min.js"></script>
  41. <script src="js/controls/OrbitControls.js"></script>
  42. <script src="js/loaders/BinaryLoader.js"></script>
  43. <script src="js/Detector.js"></script>
  44. <script src="js/libs/stats.min.js"></script>
  45. <script src='js/libs/dat.gui.min.js'></script>
  46. <script>
  47. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  48. var stats, loader;
  49. var camera, scene, renderer, controls;
  50. var params = {
  51. material: 'normal',
  52. camera: 'perspective'
  53. };
  54. var cameraOrtho, cameraPerspective;
  55. var controlsOrtho, controlsPerspective;
  56. var mesh, materialStandard, materialDepthAuto, materialDepthAutoRGBA, materialDepthLinearClipZ, materialDepthLinearClipZRGBA, materialDepthInvClipZ, materialDepthInvClipZRGBA, materialNormal;
  57. var pointLight, ambientLight;
  58. var height = 500; // of camera frustum
  59. init();
  60. animate();
  61. initGui();
  62. // Init gui
  63. function initGui() {
  64. var gui = new dat.GUI();
  65. gui.add( params, 'material', [ 'standard', 'normal', 'depthAuto', 'depthAutoRGBA', 'depthLinearClipZ', 'depthLinearClipZRGBA', 'depthInvClipZ', 'depthInvClipZRGBA' ] );
  66. gui.add( params, 'camera', [ 'perspective', 'ortho' ] );
  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( "textures/normal/ninja/normal.jpg" );
  110. var aoMap = textureLoader.load( "textures/normal/ninja/ao.jpg" );
  111. var displacementMap = textureLoader.load( "textures/normal/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: 2.436143, // from original model
  119. displacementBias: - 0.428408, // from original model
  120. aoMap: aoMap,
  121. normalMap: normalMap,
  122. normalScale: new THREE.Vector2( 1, - 1 ),
  123. side: THREE.DoubleSide
  124. } );
  125. materialDepthAuto = new THREE.MeshDepthMaterial( {
  126. depthFormat: THREE.AutoDepthFormat,
  127. depthPacking: THREE.LinearDepthPacking
  128. } );
  129. materialDepthAutoRGBA = new THREE.MeshDepthMaterial( {
  130. depthFormat: THREE.AutoDepthFormat,
  131. depthPacking: THREE.RGBADepthPacking
  132. } );
  133. materialDepthLinearClipZ = new THREE.MeshDepthMaterial( {
  134. depthFormat: THREE.LinearClipZDepthFormat,
  135. depthPacking: THREE.LinearDepthPacking
  136. } );
  137. materialDepthLinearClipZRGBA = new THREE.MeshDepthMaterial( {
  138. depthFormat: THREE.LinearClipZDepthFormat,
  139. depthPacking: THREE.RGBADepthPacking
  140. } );
  141. materialDepthInvClipZ = new THREE.MeshDepthMaterial( {
  142. depthFormat: THREE.InvClipZDepthFormat,
  143. depthPacking: THREE.LinearDepthPacking
  144. } );
  145. materialDepthInvClipZRGBA = new THREE.MeshDepthMaterial( {
  146. depthFormat: THREE.InvClipZDepthFormat,
  147. depthPacking: THREE.RGBADepthPacking
  148. } );
  149. materialNormal = new THREE.MeshNormalMaterial();
  150. //
  151. loader = new THREE.BinaryLoader();
  152. loader.load( "obj/ninja/NinjaLo_bin.js", function( geometry ) {
  153. geometry.faceVertexUvs[ 1 ] = geometry.faceVertexUvs[ 0 ]; // 2nd set of UVs required for aoMap
  154. mesh = new THREE.Mesh( geometry, materialNormal );
  155. mesh.scale.multiplyScalar( 25 );
  156. scene.add( mesh );
  157. } );
  158. //
  159. stats = new Stats();
  160. container.appendChild( stats.domElement );
  161. //
  162. window.addEventListener( 'resize', onWindowResize, false );
  163. }
  164. function onWindowResize() {
  165. var width = window.innerWidth;
  166. var height = window.innerHeight;
  167. var aspect = window.innerWidth / window.innerHeight;
  168. camera.aspect = aspect;
  169. camera.left = - height * aspect;
  170. camera.right = height * aspect;
  171. camera.top = height;
  172. camera.bottom = - height;
  173. camera.updateProjectionMatrix();
  174. renderer.setSize( width, height );
  175. }
  176. //
  177. function animate() {
  178. requestAnimationFrame( animate );
  179. controlsOrtho.update();
  180. controlsPerspective.update();
  181. stats.begin();
  182. render();
  183. stats.end();
  184. }
  185. function render() {
  186. if ( mesh ) {
  187. var material = mesh.material;
  188. switch ( params.material ) {
  189. case 'standard': material = materialStandard; break;
  190. case 'depthAuto': material = materialDepthAuto; break;
  191. case 'depthAutoRGBA': material = materialDepthAutoRGBA; break;
  192. case 'depthLinearClipZ': material = materialDepthLinearClipZ; break;
  193. case 'depthLinearClipZRGBA': material = materialDepthLinearClipZRGBA; break;
  194. case 'depthInvClipZ': material = materialDepthInvClipZ; break;
  195. case 'depthInvClipZRGBA': material = materialDepthInvClipZRGBA; break;
  196. case 'normal': material = materialNormal; break;
  197. }
  198. mesh.material = material;
  199. }
  200. switch ( params.camera ) {
  201. case 'perspective': camera = cameraPerspective; break;
  202. case 'ortho': camera = cameraOrtho; break;
  203. }
  204. renderer.render( scene, camera );
  205. }
  206. </script>
  207. </body>
  208. </html>
粤ICP备19079148号