webgl_materials_curvature.html 9.7 KB

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