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