webgl_materials_matcap.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - matcap</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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl materials matcap<br />
  12. Drag-and-drop JPG, PNG, or EXR MatCap image files<br/>
  13. </div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import { GUI } from './jsm/libs/dat.gui.module.js';
  17. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  18. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  19. import { EXRLoader } from './jsm/loaders/EXRLoader.js';
  20. var mesh, renderer, scene, camera;
  21. var API = {
  22. color: 0xffffff,
  23. exposure: 1.0
  24. };
  25. init();
  26. function init() {
  27. // renderer
  28. renderer = new THREE.WebGLRenderer( { antialias: true } );
  29. renderer.setPixelRatio( window.devicePixelRatio );
  30. renderer.setSize( window.innerWidth, window.innerHeight );
  31. document.body.appendChild( renderer.domElement );
  32. // tone mapping
  33. renderer.toneMapping = THREE.LinearToneMapping;
  34. renderer.toneMappingExposure = API.exposure;
  35. renderer.outputEncoding = THREE.sRGBEncoding;
  36. // scene
  37. scene = new THREE.Scene();
  38. // camera
  39. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 100 );
  40. camera.position.set( 0, 0, 13 );
  41. // controls
  42. var controls = new OrbitControls( camera, renderer.domElement );
  43. controls.addEventListener( 'change', render );
  44. controls.enableZoom = false;
  45. controls.enablePan = false;
  46. // manager
  47. var manager = new THREE.LoadingManager( render );
  48. // matcap
  49. var loader = new EXRLoader( manager );
  50. var matcap = loader.load( 'textures/matcaps/040full.exr' );
  51. // normalmap
  52. var loader = new THREE.TextureLoader( manager );
  53. var normalmap = loader.load( 'models/gltf/LeePerrySmith/Infinite-Level_02_Tangent_SmoothUV.jpg' );
  54. // model
  55. new GLTFLoader( manager ).load( 'models/gltf/LeePerrySmith/LeePerrySmith.glb', function ( gltf ) {
  56. mesh = gltf.scene.children[ 0 ];
  57. mesh.position.y = - 0.25;
  58. mesh.material = new THREE.MeshMatcapMaterial( {
  59. color: API.color,
  60. matcap: matcap,
  61. normalMap: normalmap
  62. } );
  63. scene.add( mesh );
  64. } );
  65. // gui
  66. var gui = new GUI();
  67. gui.addColor( API, 'color' )
  68. .listen()
  69. .onChange( function () {
  70. mesh.material.color.set( API.color ); render();
  71. } );
  72. gui.add( API, 'exposure', 0, 2 )
  73. .onChange( function () {
  74. renderer.toneMappingExposure = API.exposure; render();
  75. } );
  76. gui.domElement.style.webkitUserSelect = 'none';
  77. // drag 'n drop
  78. initDragAndDrop();
  79. window.addEventListener( 'resize', onWindowResize, false );
  80. }
  81. function onWindowResize() {
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. camera.aspect = window.innerWidth / window.innerHeight;
  84. camera.updateProjectionMatrix();
  85. render();
  86. }
  87. function render() {
  88. renderer.render( scene, camera );
  89. }
  90. //
  91. // drag and drop anywhere in document
  92. //
  93. function updateMatcap( texture ) {
  94. if ( mesh.material.matcap ) {
  95. mesh.material.matcap.dispose();
  96. }
  97. mesh.material.matcap = texture;
  98. texture.needsUpdate = true;
  99. mesh.material.needsUpdate = true; // because the encoding can change
  100. render();
  101. }
  102. function handleJPG( event ) { // PNG, too
  103. function imgCallback( event ) {
  104. var texture = new THREE.Texture( event.target );
  105. texture.encoding = THREE.sRGBEncoding;
  106. updateMatcap( texture );
  107. }
  108. var img = new Image();
  109. img.onload = imgCallback;
  110. img.src = event.target.result;
  111. }
  112. function handleEXR( event ) {
  113. var contents = event.target.result;
  114. var loader = new EXRLoader();
  115. var texData = loader.parse( contents );
  116. var texture = new THREE.DataTexture();
  117. texture.image.width = texData.width;
  118. texture.image.height = texData.height;
  119. texture.image.data = texData.data;
  120. texture.format = texData.format;
  121. texture.type = texData.type;
  122. texture.generateMipmaps = false;
  123. texture.magFilter = THREE.LinearFilter;
  124. texture.minFilter = THREE.LinearFilter;
  125. updateMatcap( texture );
  126. }
  127. function loadFile( file ) {
  128. var filename = file.name;
  129. var extension = filename.split( '.' ).pop().toLowerCase();
  130. if ( extension === 'exr' ) {
  131. var reader = new FileReader();
  132. reader.addEventListener( 'load', function ( event ) {
  133. handleEXR( event );
  134. }, false );
  135. reader.readAsArrayBuffer( file );
  136. } else { // 'jpg', 'png'
  137. var reader = new FileReader();
  138. reader.addEventListener( 'load', function ( event ) {
  139. handleJPG( event );
  140. }, false );
  141. reader.readAsDataURL( file );
  142. }
  143. }
  144. function initDragAndDrop() {
  145. document.addEventListener( 'dragover', function ( event ) {
  146. event.preventDefault();
  147. event.dataTransfer.dropEffect = 'copy';
  148. }, false );
  149. document.addEventListener( 'drop', function ( event ) {
  150. event.preventDefault();
  151. loadFile( event.dataTransfer.files[ 0 ] );
  152. }, false );
  153. }
  154. </script>
  155. </body>
  156. </html>