webgl_materials_curvature.html 9.7 KB
Newer Older
1 2 3
<!DOCTYPE html>
<html lang="en">
	<head>
4
		<title>three.js webgl - shader - curvature [ninja]</title>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				color: #ffffff;
				font-family:Monospace;
				font-size:13px;
				text-align:center;
				font-weight: bold;

				background-color: #000000;
				margin: 0px;
				overflow: hidden;
			}

			#info {
				position: absolute;
				top: 0px; width: 100%;
				padding: 5px;
			}

			a {

				color: #ffffff;
			}

			#oldie a { color:#da0 }
		</style>
	</head>
	<body>

		<div id="container"></div>
		<div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - curvature estimation of a geometry by <a href="http://codercat.club" target="_blank" rel="noopener">CoderCat</a></div>
38

39 40 41 42 43 44 45
		<script src="../build/three.js"></script>
		<script src="js/Detector.js"></script>

		<script src="js/controls/OrbitControls.js"></script>
		<script src="js/loaders/OBJLoader.js"></script>
		<script src="js/libs/dat.gui.min.js"></script>

46 47 48 49 50 51 52 53 54 55 56 57
		<script id="vertexShaderRaw" type="x-shader/x-vertex">

		attribute float curvature;

		varying float vCurvature;

		void main() {

			vec3 p = position;
			vec4 modelViewPosition = modelViewMatrix * vec4( p , 1.0 );
			gl_Position = projectionMatrix * modelViewPosition;
			vCurvature = curvature;
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

		}

		</script>

		<script id="fragmentShaderRaw" type="x-shader/x-fragment">

		varying vec3 vViewPosition;
		varying float vCurvature;

		void main() {
				gl_FragColor = vec4( vCurvature * 2.0, 0.0, 0.0, 0.0 );
		}

		</script>

		<script>

			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

			var container;

80
			var camera, scene, renderer;
81

82
			var ninjaMeshRaw, curvatureAttribute;
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

			init();
			animate();

			//returns average of elements in a dictionary
			function average( dict ) {

				var sum = 0;
				var length = 0;

				Object.keys( dict ).forEach( function( key ) {

					sum += dict[ key ];
					length ++;

				});

				return sum / length;

			}

			//clamp a number between min and max
			function clamp( number, min, max ) {

			  return Math.max( min, Math.min( number, max ) );

			}

			//filter the curvature array to only show concave values
			function filterConcave( curvature ) {

				for ( var i = 0; i < curvature.length; i++ ) {

					curvature[ i ] = Math.abs( clamp( curvature[ i ], -1, 0 ) );

				}

			}

			//filter the curvature array to only show convex values
			function filterConvex( curvature ) {

				for ( var i = 0; i < curvature.length; i++ ) {

					curvature[ i ] = clamp( curvature[ i ], 0, 1 );

				}

			}

			//filter the curvature array to show both the concave and convex values
			function filterBoth( curvature ) {

				for ( var i = 0; i < curvature.length; i++ ) {

					curvature[ i ] = Math.abs( curvature[ i ] );

				}

			}

			//initialize the scene
			function init() {

147
				scene = new THREE.Scene();
148

149
				camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
150 151 152 153

				camera.position.x = -23;
				camera.position.y = 2;
				camera.position.z = 24;
154 155 156 157 158 159 160 161 162 163 164

				controls = new THREE.OrbitControls( camera );

				renderer = new THREE.WebGLRenderer();
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.autoClear = false;

				document.body.appendChild( renderer.domElement );

				var loader = new THREE.OBJLoader();
				//load the obj
165
				loader.load( 'models/obj/ninja/ninjaHead_Low.obj', function ( object ) {
166 167 168 169
					object.traverse( function ( child ) {
						if ( child instanceof THREE.Mesh ) {

							bufferGeo = child.geometry;
170
							bufferGeo.center();
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
							var dict= {};

							for ( var i = 0; i < bufferGeo.attributes.position.count; i+=3 ) {
								//create a dictionary of every position, and its neighboring positions
								var array = bufferGeo.attributes.position.array;
								var normArray = bufferGeo.attributes.normal.array;

								var posA = new THREE.Vector3(array[ 3 * i ], array[ 3 * i + 1 ], array[ 3 * i + 2 ]);
								var posB = new THREE.Vector3(array[ 3 * ( i + 1 ) ], array[ 3 * ( i + 1 ) + 1 ], array[ 3 * ( i + 1 ) + 2 ]);
								var posC = new THREE.Vector3(array[ 3 * ( i + 2 ) ], array[ 3 * ( i + 2 ) + 1 ], array[ 3 * ( i + 2 ) + 2 ]);

								var normA = new THREE.Vector3(normArray[ 3 * i ], normArray[ 3 * i + 1 ], normArray[ 3 * i + 2 ]).normalize();
								var normB = new THREE.Vector3(normArray[ 3 * ( i + 1 ) ], normArray[ 3 * ( i + 1 ) + 1 ], normArray[ 3 * ( i + 1 ) + 2 ]).normalize();
								var normC = new THREE.Vector3(normArray[ 3 * ( i + 2 ) ], normArray[ 3 * ( i + 2 ) + 1 ], normArray[ 3 * ( i + 2 ) + 2 ]).normalize();

								var strA = posA.toArray().toString();
								var strB = posB.toArray().toString();
								var strC = posC.toArray().toString();

								var posB_A = new THREE.Vector3().subVectors( posB, posA );
								var posB_C = new THREE.Vector3().subVectors( posB, posC );
								var posC_A = new THREE.Vector3().subVectors( posC, posA );

								var normB_A = new THREE.Vector3().subVectors( normB, normA );
								var normB_C = new THREE.Vector3().subVectors( normB, normC );
								var normC_A = new THREE.Vector3().subVectors( normC, normA );

198 199 200
								var b2a = normB.dot( posB_A.normalize() );
								var b2c = normB.dot( posB_C.normalize() );
								var c2a = normC.dot( posC_A.normalize() );
201

202 203 204
								var a2b = -normA.dot( posB_A.normalize() );
								var c2b = -normC.dot( posB_C.normalize() );
								var a2c = -normA.dot( posC_A.normalize() );
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

								if (dict[ strA ] === undefined ) {
									dict[ strA ] = {};
								}
								if (dict[ strB ] === undefined ) {
									dict[ strB ] = {};
								}
								if (dict[ strC ] === undefined ) {
									dict[ strC ] = {};
								}

								dict[ strA ][ strB ] = a2b;
								dict[ strA ][ strC ] = a2c;
								dict[ strB ][ strA ] = b2a;
								dict[ strB ][ strC ] = b2c;
								dict[ strC ][ strA ] = c2a;
								dict[ strC ][ strB ] = c2b;

							}

							curvatureDict = {};
							var min = 10, max = 0;

							Object.keys( dict ).forEach( function( key ) {

230
								curvatureDict[ key ] = average( dict[ key ] );
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292

							});

							//smoothing
							var smoothCurvatureDict  = Object.create(curvatureDict);

							Object.keys( dict ).forEach( function( key ) {

								var count = 0;
								var sum = 0;
								Object.keys( dict[ key ] ).forEach( function( key2 ) {

									sum += smoothCurvatureDict[ key2 ];
									count ++;

								});
								smoothCurvatureDict[key] = sum / count;

							});

							curvatureDict = smoothCurvatureDict;

							// fit values to 0 and 1
							Object.keys( curvatureDict ).forEach( function( key ) {

								val = Math.abs( curvatureDict[ key ] );
								if ( val < min ) min = val;
								if ( val > max ) max = val;

							});

							var range = ( max - min );

							Object.keys( curvatureDict ).forEach( function( key ) {

								val = Math.abs( curvatureDict[ key ] );
								if ( curvatureDict[ key ] < 0 ) {
									curvatureDict[ key ] = (min - val) / range
								} else {
									curvatureDict[ key ] = (val - min) / range;
								}

							});

							curvatureAttribute = new Float32Array( bufferGeo.attributes.position.count );

							for ( var i = 0; i < bufferGeo.attributes.position.count; i++ ) {

								array = bufferGeo.attributes.position.array;
								var pos = new THREE.Vector3( array[ 3 * i ], array[ 3 * i + 1 ], array[ 3 * i + 2 ] );
								var str = pos.toArray().toString();
								curvatureAttribute[i] = curvatureDict[ str ];
							}

							bufferGeo.addAttribute( 'curvature', new THREE.BufferAttribute( curvatureAttribute, 1 ) );

							//starting filter is to show both concave and convex
							var curvatureFiltered = new Float32Array( curvatureAttribute );
							filterBoth(curvatureFiltered);

							var materialRaw = new THREE.ShaderMaterial ({

293
								vertexShader: document.getElementById( 'vertexShaderRaw' ).textContent,
294 295 296 297
								fragmentShader: document.getElementById( 'fragmentShaderRaw' ).textContent

							} );

298
							ninjaMeshRaw = new THREE.Mesh( bufferGeo, materialRaw );
299 300 301

						}
					} );
302 303

					scene.add( ninjaMeshRaw );
304 305 306 307 308 309

				} );


				//init GUI
				var params = {
310

311 312 313 314
					filterConvex: function () {

						var curvatureFiltered = new Float32Array( curvatureAttribute );
						filterConvex(curvatureFiltered);
315 316 317
						bufferGeo.attributes.curvature.array = curvatureFiltered;
						bufferGeo.attributes.curvature.needsUpdate = true;

318 319 320 321 322 323

					},
					filterConcave: function () {

						var curvatureFiltered = new Float32Array( curvatureAttribute );
						filterConcave(curvatureFiltered);
324 325 326
						bufferGeo.attributes.curvature.array = curvatureFiltered;
						bufferGeo.attributes.curvature.needsUpdate = true;

327 328 329 330 331 332

					},
					filterBoth: function () {

						var curvatureFiltered = new Float32Array( curvatureAttribute );
						filterBoth(curvatureFiltered);
333 334
						bufferGeo.attributes.curvature.array = curvatureFiltered;
						bufferGeo.attributes.curvature.needsUpdate = true;
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355

					}
				};

				var gui = new dat.GUI();

				topologyFolder = gui.addFolder( 'Topology' );
				topologyFolder.add( params, 'filterConvex' );
				topologyFolder.add( params, 'filterConcave' );
				topologyFolder.add( params, 'filterBoth' );
				topologyFolder.open()

				onWindowResize();

				window.addEventListener( 'resize', onWindowResize, false );

			}

			function onWindowResize( event ) {

				renderer.setSize( window.innerWidth, window.innerHeight );
356
				camera.aspect = window.innerWidth / window.innerHeight;
357 358 359 360 361 362 363 364 365 366 367 368 369 370
				camera.updateProjectionMatrix();

			}

			function animate() {

				requestAnimationFrame( animate );

				render();

			}

			function render() {

371
				renderer.render(scene, camera);
372 373 374 375 376 377 378

			}

		</script>

	</body>
</html>