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