webgl_materials_curvature.html 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. var camera, scene, renderer;
  38. var ninjaMeshRaw, curvatureAttribute, bufferGeo;
  39. init();
  40. animate();
  41. //returns average of elements in a dictionary
  42. function average( dict ) {
  43. var sum = 0;
  44. var 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 ( var 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 ( var 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 ( var 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. var controls = new OrbitControls( camera, renderer.domElement );
  85. var loader = new OBJLoader();
  86. //load the obj
  87. loader.load( 'models/obj/ninja/ninjaHead_Low.obj', function ( object ) {
  88. object.traverse( function ( child ) {
  89. if ( child.isMesh ) {
  90. bufferGeo = child.geometry;
  91. bufferGeo.center();
  92. var dict = {};
  93. for ( var i = 0; i < bufferGeo.attributes.position.count; i += 3 ) {
  94. //create a dictionary of every position, and its neighboring positions
  95. var array = bufferGeo.attributes.position.array;
  96. var normArray = bufferGeo.attributes.normal.array;
  97. var posA = new THREE.Vector3( array[ 3 * i ], array[ 3 * i + 1 ], array[ 3 * i + 2 ] );
  98. var posB = new THREE.Vector3( array[ 3 * ( i + 1 ) ], array[ 3 * ( i + 1 ) + 1 ], array[ 3 * ( i + 1 ) + 2 ] );
  99. var posC = new THREE.Vector3( array[ 3 * ( i + 2 ) ], array[ 3 * ( i + 2 ) + 1 ], array[ 3 * ( i + 2 ) + 2 ] );
  100. var normA = new THREE.Vector3( normArray[ 3 * i ], normArray[ 3 * i + 1 ], normArray[ 3 * i + 2 ] ).normalize();
  101. var normB = new THREE.Vector3( normArray[ 3 * ( i + 1 ) ], normArray[ 3 * ( i + 1 ) + 1 ], normArray[ 3 * ( i + 1 ) + 2 ] ).normalize();
  102. var normC = new THREE.Vector3( normArray[ 3 * ( i + 2 ) ], normArray[ 3 * ( i + 2 ) + 1 ], normArray[ 3 * ( i + 2 ) + 2 ] ).normalize();
  103. var strA = posA.toArray().toString();
  104. var strB = posB.toArray().toString();
  105. var strC = posC.toArray().toString();
  106. var posB_A = new THREE.Vector3().subVectors( posB, posA );
  107. var posB_C = new THREE.Vector3().subVectors( posB, posC );
  108. var posC_A = new THREE.Vector3().subVectors( posC, posA );
  109. var b2a = normB.dot( posB_A.normalize() );
  110. var b2c = normB.dot( posB_C.normalize() );
  111. var c2a = normC.dot( posC_A.normalize() );
  112. var a2b = - normA.dot( posB_A.normalize() );
  113. var c2b = - normC.dot( posB_C.normalize() );
  114. var a2c = - normA.dot( posC_A.normalize() );
  115. if ( dict[ strA ] === undefined ) {
  116. dict[ strA ] = {};
  117. }
  118. if ( dict[ strB ] === undefined ) {
  119. dict[ strB ] = {};
  120. }
  121. if ( dict[ strC ] === undefined ) {
  122. dict[ strC ] = {};
  123. }
  124. dict[ strA ][ strB ] = a2b;
  125. dict[ strA ][ strC ] = a2c;
  126. dict[ strB ][ strA ] = b2a;
  127. dict[ strB ][ strC ] = b2c;
  128. dict[ strC ][ strA ] = c2a;
  129. dict[ strC ][ strB ] = c2b;
  130. }
  131. var curvatureDict = {};
  132. var min = 10, max = 0;
  133. Object.keys( dict ).forEach( function ( key ) {
  134. curvatureDict[ key ] = average( dict[ key ] );
  135. } );
  136. //smoothing
  137. var smoothCurvatureDict = Object.create( curvatureDict );
  138. Object.keys( dict ).forEach( function ( key ) {
  139. var count = 0;
  140. var sum = 0;
  141. Object.keys( dict[ key ] ).forEach( function ( key2 ) {
  142. sum += smoothCurvatureDict[ key2 ];
  143. count ++;
  144. } );
  145. smoothCurvatureDict[ key ] = sum / count;
  146. } );
  147. curvatureDict = smoothCurvatureDict;
  148. // fit values to 0 and 1
  149. Object.keys( curvatureDict ).forEach( function ( key ) {
  150. var val = Math.abs( curvatureDict[ key ] );
  151. if ( val < min ) min = val;
  152. if ( val > max ) max = val;
  153. } );
  154. var range = ( max - min );
  155. Object.keys( curvatureDict ).forEach( function ( key ) {
  156. var val = Math.abs( curvatureDict[ key ] );
  157. if ( curvatureDict[ key ] < 0 ) {
  158. curvatureDict[ key ] = ( min - val ) / range;
  159. } else {
  160. curvatureDict[ key ] = ( val - min ) / range;
  161. }
  162. } );
  163. curvatureAttribute = new Float32Array( bufferGeo.attributes.position.count );
  164. for ( var i = 0; i < bufferGeo.attributes.position.count; i ++ ) {
  165. array = bufferGeo.attributes.position.array;
  166. var pos = new THREE.Vector3( array[ 3 * i ], array[ 3 * i + 1 ], array[ 3 * i + 2 ] );
  167. var str = pos.toArray().toString();
  168. curvatureAttribute[ i ] = curvatureDict[ str ];
  169. }
  170. bufferGeo.setAttribute( 'curvature', new THREE.BufferAttribute( curvatureAttribute, 1 ) );
  171. //starting filter is to show both concave and convex
  172. var curvatureFiltered = new Float32Array( curvatureAttribute );
  173. filterBoth( curvatureFiltered );
  174. var materialRaw = new THREE.ShaderMaterial( {
  175. vertexShader: document.getElementById( 'vertexShaderRaw' ).textContent,
  176. fragmentShader: document.getElementById( 'fragmentShaderRaw' ).textContent
  177. } );
  178. ninjaMeshRaw = new THREE.Mesh( bufferGeo, materialRaw );
  179. }
  180. } );
  181. scene.add( ninjaMeshRaw );
  182. } );
  183. //init GUI
  184. var params = {
  185. filterConvex: function () {
  186. var curvatureFiltered = new Float32Array( curvatureAttribute );
  187. filterConvex( curvatureFiltered );
  188. bufferGeo.attributes.curvature.array = curvatureFiltered;
  189. bufferGeo.attributes.curvature.needsUpdate = true;
  190. },
  191. filterConcave: function () {
  192. var curvatureFiltered = new Float32Array( curvatureAttribute );
  193. filterConcave( curvatureFiltered );
  194. bufferGeo.attributes.curvature.array = curvatureFiltered;
  195. bufferGeo.attributes.curvature.needsUpdate = true;
  196. },
  197. filterBoth: function () {
  198. var curvatureFiltered = new Float32Array( curvatureAttribute );
  199. filterBoth( curvatureFiltered );
  200. bufferGeo.attributes.curvature.array = curvatureFiltered;
  201. bufferGeo.attributes.curvature.needsUpdate = true;
  202. }
  203. };
  204. var gui = new GUI();
  205. var topologyFolder = gui.addFolder( 'Topology' );
  206. topologyFolder.add( params, 'filterConvex' );
  207. topologyFolder.add( params, 'filterConcave' );
  208. topologyFolder.add( params, 'filterBoth' );
  209. topologyFolder.open();
  210. onWindowResize();
  211. window.addEventListener( 'resize', onWindowResize, false );
  212. }
  213. function onWindowResize() {
  214. renderer.setSize( window.innerWidth, window.innerHeight );
  215. camera.aspect = window.innerWidth / window.innerHeight;
  216. camera.updateProjectionMatrix();
  217. }
  218. function animate() {
  219. requestAnimationFrame( animate );
  220. render();
  221. }
  222. function render() {
  223. renderer.render( scene, camera );
  224. }
  225. </script>
  226. </body>
  227. </html>