webgl_materials_curvature.html 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - shader - curvature [ninja]</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="container"></div>
  11. <div id="info">
  12. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - curvature estimation of a geometry<br/>
  13. by <a href="http://codercat.club" target="_blank" rel="noopener">CoderCat</a>
  14. </div>
  15. <script src="../build/three.js"></script>
  16. <script src="js/WebGL.js"></script>
  17. <script src="js/controls/OrbitControls.js"></script>
  18. <script src="js/loaders/OBJLoader.js"></script>
  19. <script src="js/libs/dat.gui.min.js"></script>
  20. <script id="vertexShaderRaw" type="x-shader/x-vertex">
  21. attribute float curvature;
  22. varying float vCurvature;
  23. void main() {
  24. vec3 p = position;
  25. vec4 modelViewPosition = modelViewMatrix * vec4( p , 1.0 );
  26. gl_Position = projectionMatrix * modelViewPosition;
  27. vCurvature = curvature;
  28. }
  29. </script>
  30. <script id="fragmentShaderRaw" type="x-shader/x-fragment">
  31. varying vec3 vViewPosition;
  32. varying float vCurvature;
  33. void main() {
  34. gl_FragColor = vec4( vCurvature * 2.0, 0.0, 0.0, 0.0 );
  35. }
  36. </script>
  37. <script>
  38. if ( WEBGL.isWebGLAvailable() === false ) {
  39. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  40. }
  41. var camera, scene, renderer;
  42. var ninjaMeshRaw, curvatureAttribute, bufferGeo;
  43. init();
  44. animate();
  45. //returns average of elements in a dictionary
  46. function average( dict ) {
  47. var sum = 0;
  48. var length = 0;
  49. Object.keys( dict ).forEach( function ( key ) {
  50. sum += dict[ key ];
  51. length ++;
  52. } );
  53. return sum / length;
  54. }
  55. //clamp a number between min and max
  56. function clamp( number, min, max ) {
  57. return Math.max( min, Math.min( number, max ) );
  58. }
  59. //filter the curvature array to only show concave values
  60. function filterConcave( curvature ) {
  61. for ( var i = 0; i < curvature.length; i ++ ) {
  62. curvature[ i ] = Math.abs( clamp( curvature[ i ], - 1, 0 ) );
  63. }
  64. }
  65. //filter the curvature array to only show convex values
  66. function filterConvex( curvature ) {
  67. for ( var i = 0; i < curvature.length; i ++ ) {
  68. curvature[ i ] = clamp( curvature[ i ], 0, 1 );
  69. }
  70. }
  71. //filter the curvature array to show both the concave and convex values
  72. function filterBoth( curvature ) {
  73. for ( var i = 0; i < curvature.length; i ++ ) {
  74. curvature[ i ] = Math.abs( curvature[ i ] );
  75. }
  76. }
  77. //initialize the scene
  78. function init() {
  79. scene = new THREE.Scene();
  80. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
  81. camera.position.x = - 23;
  82. camera.position.y = 2;
  83. camera.position.z = 24;
  84. renderer = new THREE.WebGLRenderer();
  85. renderer.setSize( window.innerWidth, window.innerHeight );
  86. renderer.autoClear = false;
  87. document.body.appendChild( renderer.domElement );
  88. var controls = new THREE.OrbitControls( camera, renderer.domElement );
  89. var loader = new THREE.OBJLoader();
  90. //load the obj
  91. loader.load( 'models/obj/ninja/ninjaHead_Low.obj', function ( object ) {
  92. object.traverse( function ( child ) {
  93. if ( child.isMesh ) {
  94. bufferGeo = child.geometry;
  95. bufferGeo.center();
  96. var dict = {};
  97. for ( var i = 0; i < bufferGeo.attributes.position.count; i += 3 ) {
  98. //create a dictionary of every position, and its neighboring positions
  99. var array = bufferGeo.attributes.position.array;
  100. var normArray = bufferGeo.attributes.normal.array;
  101. var posA = new THREE.Vector3( array[ 3 * i ], array[ 3 * i + 1 ], array[ 3 * i + 2 ] );
  102. var posB = new THREE.Vector3( array[ 3 * ( i + 1 ) ], array[ 3 * ( i + 1 ) + 1 ], array[ 3 * ( i + 1 ) + 2 ] );
  103. var posC = new THREE.Vector3( array[ 3 * ( i + 2 ) ], array[ 3 * ( i + 2 ) + 1 ], array[ 3 * ( i + 2 ) + 2 ] );
  104. var normA = new THREE.Vector3( normArray[ 3 * i ], normArray[ 3 * i + 1 ], normArray[ 3 * i + 2 ] ).normalize();
  105. var normB = new THREE.Vector3( normArray[ 3 * ( i + 1 ) ], normArray[ 3 * ( i + 1 ) + 1 ], normArray[ 3 * ( i + 1 ) + 2 ] ).normalize();
  106. var normC = new THREE.Vector3( normArray[ 3 * ( i + 2 ) ], normArray[ 3 * ( i + 2 ) + 1 ], normArray[ 3 * ( i + 2 ) + 2 ] ).normalize();
  107. var strA = posA.toArray().toString();
  108. var strB = posB.toArray().toString();
  109. var strC = posC.toArray().toString();
  110. var posB_A = new THREE.Vector3().subVectors( posB, posA );
  111. var posB_C = new THREE.Vector3().subVectors( posB, posC );
  112. var posC_A = new THREE.Vector3().subVectors( posC, posA );
  113. var b2a = normB.dot( posB_A.normalize() );
  114. var b2c = normB.dot( posB_C.normalize() );
  115. var c2a = normC.dot( posC_A.normalize() );
  116. var a2b = - normA.dot( posB_A.normalize() );
  117. var c2b = - normC.dot( posB_C.normalize() );
  118. var a2c = - normA.dot( posC_A.normalize() );
  119. if ( dict[ strA ] === undefined ) {
  120. dict[ strA ] = {};
  121. }
  122. if ( dict[ strB ] === undefined ) {
  123. dict[ strB ] = {};
  124. }
  125. if ( dict[ strC ] === undefined ) {
  126. dict[ strC ] = {};
  127. }
  128. dict[ strA ][ strB ] = a2b;
  129. dict[ strA ][ strC ] = a2c;
  130. dict[ strB ][ strA ] = b2a;
  131. dict[ strB ][ strC ] = b2c;
  132. dict[ strC ][ strA ] = c2a;
  133. dict[ strC ][ strB ] = c2b;
  134. }
  135. var curvatureDict = {};
  136. var min = 10, max = 0;
  137. Object.keys( dict ).forEach( function ( key ) {
  138. curvatureDict[ key ] = average( dict[ key ] );
  139. } );
  140. //smoothing
  141. var smoothCurvatureDict = Object.create( curvatureDict );
  142. Object.keys( dict ).forEach( function ( key ) {
  143. var count = 0;
  144. var sum = 0;
  145. Object.keys( dict[ key ] ).forEach( function ( key2 ) {
  146. sum += smoothCurvatureDict[ key2 ];
  147. count ++;
  148. } );
  149. smoothCurvatureDict[ key ] = sum / count;
  150. } );
  151. curvatureDict = smoothCurvatureDict;
  152. // fit values to 0 and 1
  153. Object.keys( curvatureDict ).forEach( function ( key ) {
  154. var val = Math.abs( curvatureDict[ key ] );
  155. if ( val < min ) min = val;
  156. if ( val > max ) max = val;
  157. } );
  158. var range = ( max - min );
  159. Object.keys( curvatureDict ).forEach( function ( key ) {
  160. var val = Math.abs( curvatureDict[ key ] );
  161. if ( curvatureDict[ key ] < 0 ) {
  162. curvatureDict[ key ] = ( min - val ) / range;
  163. } else {
  164. curvatureDict[ key ] = ( val - min ) / range;
  165. }
  166. } );
  167. curvatureAttribute = new Float32Array( bufferGeo.attributes.position.count );
  168. for ( var i = 0; i < bufferGeo.attributes.position.count; i ++ ) {
  169. array = bufferGeo.attributes.position.array;
  170. var pos = new THREE.Vector3( array[ 3 * i ], array[ 3 * i + 1 ], array[ 3 * i + 2 ] );
  171. var str = pos.toArray().toString();
  172. curvatureAttribute[ i ] = curvatureDict[ str ];
  173. }
  174. bufferGeo.addAttribute( 'curvature', new THREE.BufferAttribute( curvatureAttribute, 1 ) );
  175. //starting filter is to show both concave and convex
  176. var curvatureFiltered = new Float32Array( curvatureAttribute );
  177. filterBoth( curvatureFiltered );
  178. var materialRaw = new THREE.ShaderMaterial( {
  179. vertexShader: document.getElementById( 'vertexShaderRaw' ).textContent,
  180. fragmentShader: document.getElementById( 'fragmentShaderRaw' ).textContent
  181. } );
  182. ninjaMeshRaw = new THREE.Mesh( bufferGeo, materialRaw );
  183. }
  184. } );
  185. scene.add( ninjaMeshRaw );
  186. } );
  187. //init GUI
  188. var params = {
  189. filterConvex: function () {
  190. var curvatureFiltered = new Float32Array( curvatureAttribute );
  191. filterConvex( curvatureFiltered );
  192. bufferGeo.attributes.curvature.array = curvatureFiltered;
  193. bufferGeo.attributes.curvature.needsUpdate = true;
  194. },
  195. filterConcave: function () {
  196. var curvatureFiltered = new Float32Array( curvatureAttribute );
  197. filterConcave( curvatureFiltered );
  198. bufferGeo.attributes.curvature.array = curvatureFiltered;
  199. bufferGeo.attributes.curvature.needsUpdate = true;
  200. },
  201. filterBoth: function () {
  202. var curvatureFiltered = new Float32Array( curvatureAttribute );
  203. filterBoth( curvatureFiltered );
  204. bufferGeo.attributes.curvature.array = curvatureFiltered;
  205. bufferGeo.attributes.curvature.needsUpdate = true;
  206. }
  207. };
  208. var gui = new dat.GUI();
  209. var topologyFolder = gui.addFolder( 'Topology' );
  210. topologyFolder.add( params, 'filterConvex' );
  211. topologyFolder.add( params, 'filterConcave' );
  212. topologyFolder.add( params, 'filterBoth' );
  213. topologyFolder.open();
  214. onWindowResize();
  215. window.addEventListener( 'resize', onWindowResize, false );
  216. }
  217. function onWindowResize() {
  218. renderer.setSize( window.innerWidth, window.innerHeight );
  219. camera.aspect = window.innerWidth / window.innerHeight;
  220. camera.updateProjectionMatrix();
  221. }
  222. function animate() {
  223. requestAnimationFrame( animate );
  224. render();
  225. }
  226. function render() {
  227. renderer.render( scene, camera );
  228. }
  229. </script>
  230. </body>
  231. </html>