webgl_materials_curvature.html 9.6 KB

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