Spaces:
Running
Running
File size: 4,297 Bytes
6cd9596 |
1 2 3 4 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
/**
* SEA3D - Google Draco
* @author Sunag / http://www.sunag.com.br/
*/
'use strict';
//
// Lossy Compression
//
SEA3D.GeometryDraco = function ( name, data, sea3d ) {
this.name = name;
this.data = data;
this.sea3d = sea3d;
var attrib = data.readUShort(),
i;
this.isBig = ( attrib & 1 ) !== 0;
data.readVInt = this.isBig ? data.readUInt : data.readUShort;
this.groups = [];
if ( attrib & 32 ) {
this.uv = [];
this.uv.length = data.readUByte();
}
if ( attrib & 1024 ) {
var numGroups = data.readUByte(),
groupOffset = 0;
for ( i = 0; i < numGroups; i ++ ) {
var groupLength = data.readVInt() * 3;
this.groups.push( {
start: groupOffset,
count: groupLength,
} );
groupOffset += groupLength;
}
}
var module = SEA3D.GeometryDraco.getModule(),
dracoData = new Int8Array( data.concat( data.position, data.bytesAvailable ).buffer );
var decoder = new module.Decoder();
var buffer = new module.DecoderBuffer();
buffer.Init( dracoData, dracoData.length );
var mesh = new module.Mesh();
var decodingStatus = decoder.DecodeBufferToMesh( buffer, mesh );
if ( ! decodingStatus.ok() ) {
data.position += 5; // jump "DRACO" magic string
var version = data.readUByte() + '.' + data.readUByte(); // draco version
console.error( "SEA3D Draco", version, "decoding failed:", decodingStatus.error_msg(), "You may need update 'draco_decoder.js'." );
// use an empty geometry
this.vertex = new Float32Array();
return;
}
var index = 0;
this.vertex = this.readFloat32Array( module, decoder, mesh, index ++ );
if ( attrib & 4 ) this.normal = this.readFloat32Array( module, decoder, mesh, index ++ );
if ( attrib & 32 ) {
for ( i = 0; i < this.uv.length; i ++ ) {
this.uv[ i ] = this.readFloat32Array( module, decoder, mesh, index ++ );
}
}
if ( attrib & 64 ) {
this.jointPerVertex = decoder.GetAttribute( mesh, index ).num_components();
this.joint = this.readUint16Array( module, decoder, mesh, index ++ );
this.weight = this.readFloat32Array( module, decoder, mesh, index ++ );
}
this.indexes = this.readIndices( module, decoder, mesh );
module.destroy( mesh );
module.destroy( buffer );
module.destroy( decoder );
};
SEA3D.GeometryDraco.getModule = function () {
if ( ! this.module ) {
this.module = DracoDecoderModule();
}
return this.module;
};
SEA3D.GeometryDraco.prototype.type = "sdrc";
SEA3D.GeometryDraco.prototype.readIndices = function ( module, decoder, mesh ) {
var numFaces = mesh.num_faces(),
numIndices = numFaces * 3,
indices = new ( numIndices >= 0xFFFE ? Uint32Array : Uint16Array )( numIndices );
var ia = new module.DracoInt32Array();
for ( var i = 0; i < numFaces; ++ i ) {
decoder.GetFaceFromMesh( mesh, i, ia );
var index = i * 3;
indices[ index ] = ia.GetValue( 0 );
indices[ index + 1 ] = ia.GetValue( 1 );
indices[ index + 2 ] = ia.GetValue( 2 );
}
module.destroy( ia );
return indices;
};
SEA3D.GeometryDraco.prototype.readFloat32Array = function ( module, decoder, mesh, attrib ) {
var attribute = decoder.GetAttribute( mesh, attrib ),
numPoints = mesh.num_points();
var dracoArray = new module.DracoFloat32Array();
decoder.GetAttributeFloatForAllPoints( mesh, attribute, dracoArray );
var size = numPoints * attribute.num_components(),
output = new Float32Array( size );
for ( var i = 0; i < size; ++ i ) {
output[ i ] = dracoArray.GetValue( i );
}
module.destroy( dracoArray );
return output;
};
SEA3D.GeometryDraco.prototype.readUint16Array = function ( module, decoder, mesh, attrib, type ) {
var attribute = decoder.GetAttribute( mesh, attrib ),
numPoints = mesh.num_points();
var dracoArray = new module.DracoUInt16Array();
decoder.GetAttributeUInt16ForAllPoints( mesh, attribute, dracoArray );
var size = numPoints * attribute.num_components(),
output = new Uint16Array( size );
for ( var i = 0; i < size; ++ i ) {
output[ i ] = dracoArray.GetValue( i );
}
module.destroy( dracoArray );
return output;
};
//
// Extension
//
THREE.SEA3D.EXTENSIONS_LOADER.push( {
setTypeRead: function () {
this.file.addClass( SEA3D.GeometryDraco, true );
this.file.typeRead[ SEA3D.GeometryDraco.prototype.type ] = this.readGeometryBuffer;
}
} );
|