webgl_materials_curvature.html 9.5 KB

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