webgl_materials_curvature.html 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 controls;
  59. var ninjaMeshRaw, curvatureAttribute, bufferGeo;
  60. init();
  61. animate();
  62. //returns average of elements in a dictionary
  63. function average( dict ) {
  64. var sum = 0;
  65. var length = 0;
  66. Object.keys( dict ).forEach( function( key ) {
  67. sum += dict[ key ];
  68. length ++;
  69. });
  70. return sum / length;
  71. }
  72. //clamp a number between min and max
  73. function clamp( number, min, max ) {
  74. return Math.max( min, Math.min( number, max ) );
  75. }
  76. //filter the curvature array to only show concave values
  77. function filterConcave( curvature ) {
  78. for ( var i = 0; i < curvature.length; i++ ) {
  79. curvature[ i ] = Math.abs( clamp( curvature[ i ], -1, 0 ) );
  80. }
  81. }
  82. //filter the curvature array to only show convex values
  83. function filterConvex( curvature ) {
  84. for ( var i = 0; i < curvature.length; i++ ) {
  85. curvature[ i ] = clamp( curvature[ i ], 0, 1 );
  86. }
  87. }
  88. //filter the curvature array to show both the concave and convex values
  89. function filterBoth( curvature ) {
  90. for ( var i = 0; i < curvature.length; i++ ) {
  91. curvature[ i ] = Math.abs( curvature[ i ] );
  92. }
  93. }
  94. //initialize the scene
  95. function init() {
  96. scene = new THREE.Scene();
  97. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
  98. camera.position.x = -23;
  99. camera.position.y = 2;
  100. camera.position.z = 24;
  101. controls = new THREE.OrbitControls( camera );
  102. renderer = new THREE.WebGLRenderer();
  103. renderer.setSize( window.innerWidth, window.innerHeight );
  104. renderer.autoClear = false;
  105. document.body.appendChild( renderer.domElement );
  106. var loader = new THREE.OBJLoader();
  107. //load the obj
  108. loader.load( 'models/obj/ninja/ninjaHead_Low.obj', function ( object ) {
  109. object.traverse( function ( child ) {
  110. if ( child.isMesh ) {
  111. bufferGeo = child.geometry;
  112. bufferGeo.center();
  113. var dict= {};
  114. for ( var i = 0; i < bufferGeo.attributes.position.count; i+=3 ) {
  115. //create a dictionary of every position, and its neighboring positions
  116. var array = bufferGeo.attributes.position.array;
  117. var normArray = bufferGeo.attributes.normal.array;
  118. var posA = new THREE.Vector3(array[ 3 * i ], array[ 3 * i + 1 ], array[ 3 * i + 2 ]);
  119. var posB = new THREE.Vector3(array[ 3 * ( i + 1 ) ], array[ 3 * ( i + 1 ) + 1 ], array[ 3 * ( i + 1 ) + 2 ]);
  120. var posC = new THREE.Vector3(array[ 3 * ( i + 2 ) ], array[ 3 * ( i + 2 ) + 1 ], array[ 3 * ( i + 2 ) + 2 ]);
  121. var normA = new THREE.Vector3(normArray[ 3 * i ], normArray[ 3 * i + 1 ], normArray[ 3 * i + 2 ]).normalize();
  122. var normB = new THREE.Vector3(normArray[ 3 * ( i + 1 ) ], normArray[ 3 * ( i + 1 ) + 1 ], normArray[ 3 * ( i + 1 ) + 2 ]).normalize();
  123. var normC = new THREE.Vector3(normArray[ 3 * ( i + 2 ) ], normArray[ 3 * ( i + 2 ) + 1 ], normArray[ 3 * ( i + 2 ) + 2 ]).normalize();
  124. var strA = posA.toArray().toString();
  125. var strB = posB.toArray().toString();
  126. var strC = posC.toArray().toString();
  127. var posB_A = new THREE.Vector3().subVectors( posB, posA );
  128. var posB_C = new THREE.Vector3().subVectors( posB, posC );
  129. var posC_A = new THREE.Vector3().subVectors( posC, posA );
  130. var b2a = normB.dot( posB_A.normalize() );
  131. var b2c = normB.dot( posB_C.normalize() );
  132. var c2a = normC.dot( posC_A.normalize() );
  133. var a2b = -normA.dot( posB_A.normalize() );
  134. var c2b = -normC.dot( posB_C.normalize() );
  135. var a2c = -normA.dot( posC_A.normalize() );
  136. if (dict[ strA ] === undefined ) {
  137. dict[ strA ] = {};
  138. }
  139. if (dict[ strB ] === undefined ) {
  140. dict[ strB ] = {};
  141. }
  142. if (dict[ strC ] === undefined ) {
  143. dict[ strC ] = {};
  144. }
  145. dict[ strA ][ strB ] = a2b;
  146. dict[ strA ][ strC ] = a2c;
  147. dict[ strB ][ strA ] = b2a;
  148. dict[ strB ][ strC ] = b2c;
  149. dict[ strC ][ strA ] = c2a;
  150. dict[ strC ][ strB ] = c2b;
  151. }
  152. var curvatureDict = {};
  153. var min = 10, max = 0;
  154. Object.keys( dict ).forEach( function( key ) {
  155. curvatureDict[ key ] = average( dict[ key ] );
  156. });
  157. //smoothing
  158. var smoothCurvatureDict = Object.create(curvatureDict);
  159. Object.keys( dict ).forEach( function( key ) {
  160. var count = 0;
  161. var sum = 0;
  162. Object.keys( dict[ key ] ).forEach( function( key2 ) {
  163. sum += smoothCurvatureDict[ key2 ];
  164. count ++;
  165. });
  166. smoothCurvatureDict[key] = sum / count;
  167. });
  168. curvatureDict = smoothCurvatureDict;
  169. // fit values to 0 and 1
  170. Object.keys( curvatureDict ).forEach( function( key ) {
  171. var val = Math.abs( curvatureDict[ key ] );
  172. if ( val < min ) min = val;
  173. if ( val > max ) max = val;
  174. });
  175. var range = ( max - min );
  176. Object.keys( curvatureDict ).forEach( function( key ) {
  177. var val = Math.abs( curvatureDict[ key ] );
  178. if ( curvatureDict[ key ] < 0 ) {
  179. curvatureDict[ key ] = (min - val) / range
  180. } else {
  181. curvatureDict[ key ] = (val - min) / range;
  182. }
  183. });
  184. curvatureAttribute = new Float32Array( bufferGeo.attributes.position.count );
  185. for ( var i = 0; i < bufferGeo.attributes.position.count; i++ ) {
  186. array = bufferGeo.attributes.position.array;
  187. var pos = new THREE.Vector3( array[ 3 * i ], array[ 3 * i + 1 ], array[ 3 * i + 2 ] );
  188. var str = pos.toArray().toString();
  189. curvatureAttribute[i] = curvatureDict[ str ];
  190. }
  191. bufferGeo.addAttribute( 'curvature', new THREE.BufferAttribute( curvatureAttribute, 1 ) );
  192. //starting filter is to show both concave and convex
  193. var curvatureFiltered = new Float32Array( curvatureAttribute );
  194. filterBoth(curvatureFiltered);
  195. var materialRaw = new THREE.ShaderMaterial ({
  196. vertexShader: document.getElementById( 'vertexShaderRaw' ).textContent,
  197. fragmentShader: document.getElementById( 'fragmentShaderRaw' ).textContent
  198. } );
  199. ninjaMeshRaw = new THREE.Mesh( bufferGeo, materialRaw );
  200. }
  201. } );
  202. scene.add( ninjaMeshRaw );
  203. } );
  204. //init GUI
  205. var params = {
  206. filterConvex: function () {
  207. var curvatureFiltered = new Float32Array( curvatureAttribute );
  208. filterConvex(curvatureFiltered);
  209. bufferGeo.attributes.curvature.array = curvatureFiltered;
  210. bufferGeo.attributes.curvature.needsUpdate = true;
  211. },
  212. filterConcave: function () {
  213. var curvatureFiltered = new Float32Array( curvatureAttribute );
  214. filterConcave(curvatureFiltered);
  215. bufferGeo.attributes.curvature.array = curvatureFiltered;
  216. bufferGeo.attributes.curvature.needsUpdate = true;
  217. },
  218. filterBoth: function () {
  219. var curvatureFiltered = new Float32Array( curvatureAttribute );
  220. filterBoth(curvatureFiltered);
  221. bufferGeo.attributes.curvature.array = curvatureFiltered;
  222. bufferGeo.attributes.curvature.needsUpdate = true;
  223. }
  224. };
  225. var gui = new dat.GUI();
  226. var topologyFolder = gui.addFolder( 'Topology' );
  227. topologyFolder.add( params, 'filterConvex' );
  228. topologyFolder.add( params, 'filterConcave' );
  229. topologyFolder.add( params, 'filterBoth' );
  230. topologyFolder.open();
  231. onWindowResize();
  232. window.addEventListener( 'resize', onWindowResize, false );
  233. }
  234. function onWindowResize( event ) {
  235. renderer.setSize( window.innerWidth, window.innerHeight );
  236. camera.aspect = window.innerWidth / window.innerHeight;
  237. camera.updateProjectionMatrix();
  238. }
  239. function animate() {
  240. requestAnimationFrame( animate );
  241. render();
  242. }
  243. function render() {
  244. renderer.render(scene, camera);
  245. }
  246. </script>
  247. </body>
  248. </html>