Commit dd33a5e6 authored by alteredq's avatar alteredq

Removed old UTF8 format things.

Doing this in multiple steps not to confuse git.
parent a06b2dcc
/**
* Loader for UTF8 encoded models generated by:
* http://code.google.com/p/webgl-loader/
*
* Limitations:
* - number of vertices < 65536 (this is after optimizations in compressor, input OBJ may have even less)
* - models must have normals and texture coordinates
* - texture coordinates must be only from <0,1>
* - no materials support yet
* - models are scaled and offset (copy numbers from compressor and use them as parameters in UTF8Loader.load() )
*
* @author alteredq / http://alteredqualia.com/
* @author won3d / http://twitter.com/won3d
*/
THREE.UTF8Loader = function () {};
THREE.UTF8Loader.prototype.load = function ( url, callback, metadata ) {
var xhr = new XMLHttpRequest();
var callbackProgress = null;
var length = 0;
xhr.onreadystatechange = function() {
if ( xhr.readyState == 4 ) {
if ( xhr.status == 200 || xhr.status == 0 ) {
var geometry = THREE.UTF8Loader.prototype.parse( xhr.responseText, metadata );
callback( geometry );
} else {
console.error( "THREE.UTF8Loader: Couldn't load [" + url + "] [" + xhr.status + "]" );
}
} else if ( xhr.readyState == 3 ) {
if ( callbackProgress ) {
if ( length == 0 ) {
length = xhr.getResponseHeader( "Content-Length" );
}
callbackProgress( { total: length, loaded: xhr.responseText.length } );
}
} else if ( xhr.readyState == 2 ) {
length = xhr.getResponseHeader( "Content-Length" );
}
}
xhr.open( "GET", url, true );
xhr.send( null );
};
// UTF-8 decoder from webgl-loader
// http://code.google.com/p/webgl-loader/
// Copyright 2011 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you
// may not use this file except in compliance with the License. You
// may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
THREE.UTF8Loader.prototype.decompressMesh = function ( str ) {
var num_verts = str.charCodeAt( 0 );
if ( num_verts >= 0xE000 ) {
num_verts -= 0x0800;
}
num_verts ++;
var attribs_out = new Float32Array( 8 * num_verts );
var offset = 1;
for ( var i = 0; i < 8; i ++ ) {
var prev_attrib = 0;
for ( var j = 0; j < num_verts; ++ j ) {
var code = str.charCodeAt( j + offset );
prev_attrib += ( code >> 1 ) ^ ( - ( code & 1 ) );
attribs_out[ 8 * j + i ] = prev_attrib;
}
offset += num_verts;
}
var num_indices = str.length - offset;
var indices_out = new Uint16Array( num_indices );
var index_high_water_mark = 0;
for ( var i = 0; i < num_indices; i ++ ) {
var code = str.charCodeAt( i + offset );
indices_out[ i ] = index_high_water_mark - code;
if ( code == 0 ) {
index_high_water_mark ++;
}
}
return [ attribs_out, indices_out ];
};
THREE.UTF8Loader.prototype.parse = function ( data, metadata ) {
if ( metadata === undefined ) metadata = {};
var scale = metadata.scale !== undefined ? metadata.scale : 1;
var offsetX = metadata.offsetX !== undefined ? metadata.offsetX : 0;
var offsetY = metadata.offsetY !== undefined ? metadata.offsetY : 0;
var offsetZ = metadata.offsetZ !== undefined ? metadata.offsetZ : 0;
var Model = function ( texture_path ) {
//var s = (new Date).getTime();
var scope = this;
scope.materials = [];
THREE.Geometry.call( this );
var buffers = THREE.UTF8Loader.prototype.decompressMesh( data );
var normals = [],
uvs = [];
init_vertices( buffers[ 0 ], 8, 0 );
init_uvs( buffers[ 0 ], 8, 3 );
init_normals( buffers[ 0 ], 8, 5 );
init_faces( buffers[ 1 ] );
this.computeCentroids();
this.computeFaceNormals();
//this.computeTangents();
//var e = (new Date).getTime();
//console.log( "utf8 data parse time: " + (e-s) + " ms" );
function init_vertices( data, stride, offset ) {
var i, x, y, z,
end = data.length;
for( i = offset; i < end; i += stride ) {
x = data[ i ];
y = data[ i + 1 ];
z = data[ i + 2 ];
// fix scale and offsets
x = ( x / 16383 ) * scale;
y = ( y / 16383 ) * scale;
z = ( z / 16383 ) * scale;
x += offsetX;
y += offsetY;
z += offsetZ;
vertex( scope, x, y, z );
}
};
function init_normals( data, stride, offset ) {
var i, x, y, z,
end = data.length;
for( i = offset; i < end; i += stride ) {
x = data[ i ];
y = data[ i + 1 ];
z = data[ i + 2 ];
// normalize to <-1,1>
x = ( x - 512 ) / 511;
y = ( y - 512 ) / 511;
z = ( z - 512 ) / 511;
normals.push( x, y, z );
}
};
function init_uvs( data, stride, offset ) {
var i, u, v,
end = data.length;
for( i = offset; i < end; i += stride ) {
u = data[ i ];
v = data[ i + 1 ];
// normalize to <0,1>
u /= 1023;
v /= 1023;
uvs.push( u, v );
}
};
function init_faces( indices ) {
var i,
a, b, c,
u1, v1, u2, v2, u3, v3,
m,
end = indices.length;
m = 0; // all faces defaulting to material 0
for( i = 0; i < end; i += 3 ) {
a = indices[ i ];
b = indices[ i + 1 ];
c = indices[ i + 2 ];
f3n( scope, normals, a, b, c, m, a, b, c );
u1 = uvs[ a * 2 ];
v1 = uvs[ a * 2 + 1 ];
u2 = uvs[ b * 2 ];
v2 = uvs[ b * 2 + 1 ];
u3 = uvs[ c * 2 ];
v3 = uvs[ c * 2 + 1 ];
uv3( scope.faceVertexUvs[ 0 ], u1, v1, u2, v2, u3, v3 );
}
}
};
function vertex ( scope, x, y, z ) {
scope.vertices.push( new THREE.Vector3( x, y, z ) );
};
function f3n ( scope, normals, a, b, c, mi, nai, nbi, nci ) {
var nax = normals[ nai * 3 ],
nay = normals[ nai * 3 + 1 ],
naz = normals[ nai * 3 + 2 ],
nbx = normals[ nbi * 3 ],
nby = normals[ nbi * 3 + 1 ],
nbz = normals[ nbi * 3 + 2 ],
ncx = normals[ nci * 3 ],
ncy = normals[ nci * 3 + 1 ],
ncz = normals[ nci * 3 + 2 ];
var na = new THREE.Vector3( nax, nay, naz ),
nb = new THREE.Vector3( nbx, nby, nbz ),
nc = new THREE.Vector3( ncx, ncy, ncz );
scope.faces.push( new THREE.Face3( a, b, c, [ na, nb, nc ], null, mi ) );
};
function uv3 ( where, u1, v1, u2, v2, u3, v3 ) {
var uv = [];
uv.push( new THREE.UV( u1, v1 ) );
uv.push( new THREE.UV( u2, v2 ) );
uv.push( new THREE.UV( u3, v3 ) );
where.push( uv );
};
Model.prototype = Object.create( THREE.Geometry.prototype );
return new Model();
};
File deleted
File deleted
......@@ -152,7 +152,6 @@
<script src="js/loaders/ctm/lzma.js"></script>
<script src="js/loaders/ctm/ctm.js"></script>
<script src="js/loaders/ctm/CTMLoader.js"></script>
<script src="js/loaders/UTF8Loader.js"></script>
<script src="js/Detector.js"></script>
<script src="js/Stats.js"></script>
......@@ -334,7 +333,6 @@
var loader = new THREE.SceneLoader();
loader.addGeometryHandler( "ctm", THREE.CTMLoader );
loader.addGeometryHandler( "utf8", THREE.UTF8Loader );
loader.callbackSync = callbackSync;
loader.callbackProgress = callbackProgress;
......
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - io - UTF8 loader</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
font-family: Monospace;
background-color: #000;
color: #fff;
margin: 0px;
overflow: hidden;
}
#info {
color: #fff;
position: absolute;
top: 10px;
width: 100%;
text-align: center;
z-index: 100;
display:block;
}
#info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
</style>
</head>
<body>
<div id="info">
<a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> -
<a href="http://code.google.com/p/webgl-loader/" target="_blank">UTF8 format</a> loader test -
models from <a href="http://www.sci.utah.edu/~wald/animrep/" target="_blank">The Utah 3D Animation Repository</a>
</div>
<script src="../build/three.min.js"></script>
<script src="js/loaders/UTF8Loader.js"></script>
<script src="js/Detector.js"></script>
<script src="js/Stats.js"></script>
<script>
var SCREEN_WIDTH = window.innerWidth;
var SCREEN_HEIGHT = window.innerHeight;
var FLOOR = -150;
var container, stats;
var camera, scene, renderer;
var mesh, zmesh, geometry;
var mouseX = 0, mouseY = 0;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
document.addEventListener('mousemove', onDocumentMouseMove, false);
init();
animate();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
camera = new THREE.PerspectiveCamera( 20, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 2000 );
camera.position.z = 800;
scene = new THREE.Scene();
scene.fog = new THREE.Fog( 0x000000, 800, 2000 );
var path = "textures/cube/SwedishRoyalCastle/";
var format = '.jpg';
var urls = [
path + 'px' + format, path + 'nx' + format,
path + 'py' + format, path + 'ny' + format,
path + 'pz' + format, path + 'nz' + format
];
reflectionCube = THREE.ImageUtils.loadTextureCube( urls );
// LIGHTS
var ambient = new THREE.AmbientLight( 0x221100 );
scene.add( ambient );
var directionalLight = new THREE.DirectionalLight( 0xffeedd );
directionalLight.position.set( 0, 0, 1 ).normalize();
scene.add( directionalLight );
// RENDERER
renderer = new THREE.WebGLRenderer();
renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
renderer.setClearColor( scene.fog.color, 1 );
renderer.domElement.style.position = "relative";
container.appendChild( renderer.domElement );
// STATS
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
stats.domElement.style.zIndex = 100;
container.appendChild( stats.domElement );
var loader = new THREE.UTF8Loader();
loader.load( "models/utf8/hand.utf8", function ( geometry ) {
callbackModel( geometry, 400, 0xffffff, 125, FLOOR, 0 );
}, { scale: 0.815141, offsetX: -0.371823, offsetY: -0.011920, offsetZ: -0.416061 } );
loader.load( "models/utf8/ben.utf8", function ( geometry ) {
callbackModel( geometry, 400, 0xffaa00, -125, FLOOR, 0 );
}, { scale: 0.707192, offsetX: -0.109362, offsetY: -0.006435, offsetZ: -0.268751 } );
//
window.addEventListener( 'resize', onWindowResize, false );
}
function onWindowResize() {
windowHalfX = window.innerWidth / 2;
windowHalfY = window.innerHeight / 2;
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function callbackModel( geometry, s, color, x, y, z ) {
var material = new THREE.MeshLambertMaterial( {
color: color,
map: THREE.ImageUtils.loadTexture( "textures/ash_uvgrid01.jpg" ),
envMap: reflectionCube,
combine: THREE.MixOperation,
reflectivity: 0.3
} );
//material.shading = THREE.FlatShading;
var mesh = new THREE.Mesh( geometry, material );
mesh.position.set( x, y, z );
mesh.scale.set( s, s, s );
scene.add( mesh );
}
function onDocumentMouseMove( event ) {
mouseX = ( event.clientX - windowHalfX );
mouseY = ( event.clientY - windowHalfY );
}
//
function animate() {
requestAnimationFrame( animate );
render();
stats.update();
}
function render() {
camera.position.x += ( mouseX - camera.position.x ) * .05;
camera.position.y += ( - mouseY - camera.position.y ) * .05;
camera.lookAt( scene.position );
renderer.render( scene, camera );
}
</script>
</body>
</html>
-------------------------
---- three.js README ----
-------------------------
Slightly modified "mesh.h" so that compressor:
- compiles with latest MinGW on Windows 64-bit
(had to replace %z size_t formatting option)
- dumps to stderr scaling and offset parameters
Also for convenience added pre-build executable for Windows.
Latest version of compressor can be downloaded at webgl-loader repository:
http://code.google.com/p/webgl-loader/
--------------------------------------------
---- Original README from webgl-loader -----
--------------------------------------------
Usage: ./objcompress in.obj [out.utf8]
If 'out' is specified, then attempt to write out a compressed,
UTF-8 version to 'out.'
If not, write a JSON version to STDOUT.
Building:
Since there are no external dependences outside of the C/C++ standard
libraries, you can pretty much build this however you please. I've
included a cheeky way to do this on POSIX-like systems by including a
build shell script at the top of the file itself. You can build by
making the .cc file executable, and running it on the command line.
\ No newline at end of file
g++ objcompress.cc -O2 -Wall -o objcompress
\ No newline at end of file
This diff is collapsed.
File deleted
#if 0 // A cute trick to making this .cc self-building from shell.
g++ $0 -O2 -Wall -Werror -o `basename $0 .cc`;
exit;
#endif
// Copyright 2011 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you
// may not use this file except in compliance with the License. You
// may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
#include "mesh.h"
const bool kQuantize = true;
const bool kOptimize = true;
int main(int argc, char* argv[]) {
if (argc < 2 || argc > 3) {
fprintf(stderr, "Usage: %s in.obj [out.utf8]\n\n"
"\tIf 'out' is specified, then attempt to write out a compressed,\n"
"\tUTF-8 version to 'out.'\n\n"
"\tIf not, write a JSON version to STDOUT.\n\n",
argv[0]);
return -1;
}
FILE* fp = fopen(argv[1], "r");
WavefrontObjFile obj(fp);
fclose(fp);
std::vector<DrawMesh> meshes;
obj.CreateDrawMeshes(&meshes);
if (kQuantize) {
QuantizedAttribList attribs;
AttribsToQuantizedAttribs(meshes[0].interleaved_attribs, &attribs);
if (kOptimize) {
QuantizedAttribList optimized_attribs;
IndexList optimized_indices;
VertexOptimizer vertex_optimizer(attribs, meshes[0].triangle_indices);
vertex_optimizer.GetOptimizedMesh(&optimized_attribs, &optimized_indices);
if (argc == 3) {
CompressMeshToFile(optimized_attribs, optimized_indices, argv[2]);
} else {
DumpJsonFromQuantizedAttribs(optimized_attribs);
DumpJsonFromIndices(optimized_indices);
}
return 0;
} else {
DumpJsonFromQuantizedAttribs(attribs);
}
} else {
DumpJsonFromInterleavedAttribs(meshes[0].interleaved_attribs);
}
DumpJsonFromIndices(meshes[0].triangle_indices);
return 0;
}
File deleted
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment