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