webgl_materials_curvature.html 9.3 KB

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