123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- /**
- * @author mrdoob / http://mrdoob.com/
- * @author alteredq / http://alteredqualia.com/
- */
- THREE.JSONLoader = function ( showStatus ) {
- THREE.Loader.call( this, showStatus );
-
- };
- THREE.JSONLoader.prototype = new THREE.Loader();
- THREE.JSONLoader.prototype.constructor = THREE.JSONLoader;
- THREE.JSONLoader.prototype.supr = THREE.Loader.prototype;
- THREE.JSONLoader.prototype = {
- // Load models generated by slim OBJ converter with ASCII option (converter_obj_three_slim.py -t ascii)
- // - parameters
- // - model (required)
- // - callback (required)
- // - texture_path (optional: if not specified, textures will be assumed to be in the same folder as JS model file)
- load: function ( parameters ) {
-
- var url = parameters.model,
- callback = parameters.callback,
- texture_path = parameters.texture_path ? parameters.texture_path : THREE.Loader.prototype.extractUrlbase( url ),
-
- s = (new Date).getTime(),
- worker = new Worker( url );
-
- worker.onmessage = function( event ) {
-
- THREE.JSONLoader.prototype.createModel( event.data, callback, texture_path );
- };
- worker.postMessage( s );
- },
-
- createModel: function ( json, callback, texture_path ) {
- var Model = function ( texture_path ) {
- var scope = this;
- THREE.Geometry.call( this );
- THREE.Loader.prototype.init_materials( scope, json.materials, texture_path );
-
- parse();
- init_skin();
- init_morphing();
- this.computeCentroids();
- this.computeFaceNormals();
-
- function parse() {
- if ( json.version === undefined || json.version != 2 ) {
- console.error( 'Deprecated file format.' );
- return;
- }
-
- function isBitSet( value, position ) {
-
- return value & ( 1 << position );
- };
-
- var i, j,
-
- offset, zLength,
- type,
- isQuad,
- hasMaterial,
- hasFaceUv, hasFaceVertexUv,
- hasFaceNormal, hasFaceVertexNormal,
- hasFaceColor, hasFaceVertexColor,
- vertex, face,
-
- faces = json.faces,
- vertices = json.vertices,
- normals = json.normals,
- colors = json.colors,
- nUvLayers = 0;
-
- // disregard empty arrays
-
- for ( i = 0; i < json.uvs.length; i++ ) {
-
- if ( json.uvs[ i ].length ) nUvLayers ++;
- }
-
- for ( i = 0; i < nUvLayers; i++ ) {
- scope.faceUvs[ i ] = [];
- scope.faceVertexUvs[ i ] = [];
- }
- offset = 0;
- zLength = vertices.length;
-
- while ( offset < zLength ) {
- vertex = new THREE.Vertex();
-
- vertex.position.x = vertices[ offset ++ ];
- vertex.position.y = vertices[ offset ++ ];
- vertex.position.z = vertices[ offset ++ ];
- scope.vertices.push( vertex );
- }
-
- offset = 0;
- zLength = faces.length;
- while ( offset < zLength ) {
- type = faces[ offset ++ ];
- isQuad = isBitSet( type, 0 );
- hasMaterial = isBitSet( type, 1 );
- hasFaceUv = isBitSet( type, 2 );
- hasFaceVertexUv = isBitSet( type, 3 );
- hasFaceNormal = isBitSet( type, 4 );
- hasFaceVertexNormal = isBitSet( type, 5 );
- hasFaceColor = isBitSet( type, 6 );
- hasFaceVertexColor = isBitSet( type, 7 );
- //console.log("type", type, "bits", isQuad, hasMaterial, hasFaceUv, hasFaceVertexUv, hasFaceNormal, hasFaceVertexNormal, hasFaceColor, hasFaceVertexColor);
- if ( isQuad ) {
- face = new THREE.Face4();
-
- face.a = faces[ offset ++ ];
- face.b = faces[ offset ++ ];
- face.c = faces[ offset ++ ];
- face.d = faces[ offset ++ ];
- nVertices = 4;
- } else {
- face = new THREE.Face3();
-
- face.a = faces[ offset ++ ];
- face.b = faces[ offset ++ ];
- face.c = faces[ offset ++ ];
- nVertices = 3;
- }
-
- if ( hasMaterial ) {
- materialIndex = faces[ offset ++ ];
- face.materials = scope.materials[ materialIndex ];
- }
- if ( hasFaceUv ) {
- for ( i = 0; i < nUvLayers; i++ ) {
- uvLayer = json.uvs[ i ];
- uvIndex = faces[ offset ++ ];
- u = uvLayer[ uvIndex * 2 ];
- v = uvLayer[ uvIndex * 2 + 1 ];
- scope.faceUvs[ i ].push( new THREE.UV( u, v ) );
- }
- }
- if ( hasFaceVertexUv ) {
- for ( i = 0; i < nUvLayers; i++ ) {
-
- uvLayer = json.uvs[ i ];
- uvs = [];
- for ( j = 0; j < nVertices; j ++ ) {
- uvIndex = faces[ offset ++ ];
- u = uvLayer[ uvIndex * 2 ];
- v = uvLayer[ uvIndex * 2 + 1 ];
- uvs[ j ] = new THREE.UV( u, v );
- }
- scope.faceVertexUvs[ i ].push( uvs );
- }
- }
- if ( hasFaceNormal ) {
- normalIndex = faces[ offset ++ ] * 3;
- normal = new THREE.Vector3();
-
- normal.x = normals[ normalIndex ++ ];
- normal.y = normals[ normalIndex ++ ];
- normal.z = normals[ normalIndex ];
- face.normal = normal;
- }
- if ( hasFaceVertexNormal ) {
- for ( i = 0; i < nVertices; i++ ) {
- normalIndex = faces[ offset ++ ] * 3;
- normal = new THREE.Vector3();
-
- normal.x = normals[ normalIndex ++ ];
- normal.y = normals[ normalIndex ++ ];
- normal.z = normals[ normalIndex ];
- face.vertexNormals.push( normal );
- }
- }
-
- if ( hasFaceColor ) {
- color = new THREE.Color( faces[ offset ++ ] );
- face.color = color;
- }
-
- if ( hasFaceVertexColor ) {
- for ( i = 0; i < nVertices; i++ ) {
- colorIndex = faces[ offset ++ ];
-
- color = new THREE.Color( colors[ colorIndex ] );
- face.vertexColors.push( color );
-
- }
- }
- scope.faces.push( face );
- }
- };
-
- function init_skin() {
-
- var i, l, x, y, z, w, a, b, c, d;
- if ( json.skinWeights ) {
-
- for( i = 0, l = json.skinWeights.length; i < l; i += 2 ) {
- x = json.skinWeights[ i ];
- y = json.skinWeights[ i + 1 ];
- z = 0;
- w = 0;
-
- scope.skinWeights.push( new THREE.Vector4( x, y, z, w ) );
- }
-
- }
-
- if ( json.skinIndices ) {
-
- for( i = 0, l = json.skinIndices.length; i < l; i += 2 ) {
- a = json.skinIndices[ i ];
- b = json.skinIndices[ i + 1 ];
- c = 0;
- d = 0;
- scope.skinIndices.push( new THREE.Vector4( a, b, c, d ) );
- }
-
- }
-
- scope.bones = json.bones;
- scope.animation = json.animation;
-
- };
-
- function init_morphing() {
- if( json.morphTargets !== undefined ) {
-
- var i, l, v, vl;
-
- for( i = 0, l = json.morphTargets.length; i < l; i++ ) {
-
- scope.morphTargets[ i ] = {};
- scope.morphTargets[ i ].name = json.morphTargets[ i ].name;
- scope.morphTargets[ i ].vertices = [];
-
- dstVertices = scope.morphTargets[ i ].vertices;
- srcVertices = json.morphTargets [ i ].vertices;
- for( v = 0, vl = srcVertices.length; v < vl; v += 3 ) {
- dstVertices.push( new THREE.Vertex( new THREE.Vector3( srcVertices[ v ], srcVertices[ v + 1 ], srcVertices[ v + 2 ] ) ) );
- }
-
- }
-
- }
- };
-
- };
- Model.prototype = new THREE.Geometry();
- Model.prototype.constructor = Model;
- callback( new Model( texture_path ) );
- }
- };
|