webvr js meetup initial commit

This commit is contained in:
Senad Uka
2017-03-11 15:22:17 +01:00
commit 3f640b55db
761 changed files with 264174 additions and 0 deletions

3168
node_modules/three/examples/js/loaders/sea3d/SEA3D.js generated vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,887 @@
/*
* $Id: rawinflate.js,v 0.3 2013/04/09 14:25:38 dankogai Exp dankogai $
*
* GNU General Public License, version 2 (GPL-2.0)
* http://opensource.org/licenses/GPL-2.0
* original:
* http://www.onicos.com/staff/iz/amuse/javascript/expert/inflate.txt
*/
'use strict';
SEA3D.Deflate = function () {
/* Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp>
* Version: 1.0.0.1
* LastModified: Dec 25 1999
*/
/* Interface:
* data = zip_inflate(src);
*/
/* constant parameters */
var zip_WSIZE = 32768; // Sliding Window size
var zip_STORED_BLOCK = 0;
var zip_STATIC_TREES = 1;
var zip_DYN_TREES = 2;
/* for inflate */
var zip_lbits = 9; // bits in base literal/length lookup table
var zip_dbits = 6; // bits in base distance lookup table
var zip_INBUFSIZ = 32768; // Input buffer size
var zip_INBUF_EXTRA = 64; // Extra buffer
/* variables (inflate) */
var zip_slide;
var zip_wp; // current position in slide
var zip_fixed_tl = null; // inflate static
var zip_fixed_td; // inflate static
var zip_fixed_bl, fixed_bd, zip_fixed_bd; // inflate static
var zip_bit_buf; // bit buffer
var zip_bit_len; // bits in bit buffer
var zip_method;
var zip_eof;
var zip_copy_leng;
var zip_copy_dist;
var zip_tl, zip_td; // literal/length and distance decoder tables
var zip_bl, zip_bd; // number of bits decoded by tl and td
var zip_inflate_data;
var zip_inflate_pos;
/* constant tables (inflate) */
var zip_MASK_BITS = new Array(
0x0000,
0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff );
// Tables for deflate from PKZIP's appnote.txt.
var zip_cplens = new Array( // Copy lengths for literal codes 257..285
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 );
/* note: see note #13 above about the 258 in this list. */
var zip_cplext = new Array( // Extra bits for literal codes 257..285
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99 ); // 99==invalid
var zip_cpdist = new Array( // Copy offsets for distance codes 0..29
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
8193, 12289, 16385, 24577 );
var zip_cpdext = new Array( // Extra bits for distance codes
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
12, 12, 13, 13 );
var zip_border = new Array( // Order of the bit length code lengths
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 );
/* objects (inflate) */
var zip_HuftList = function () {
this.next = null;
this.list = null;
}
var zip_HuftNode = function () {
this.e = 0; // number of extra bits or operation
this.b = 0; // number of bits in this code or subcode
// union
this.n = 0; // literal, length base, or distance base
this.t = null; // (zip_HuftNode) pointer to next level of table
}
var zip_HuftBuild = function ( b, // code lengths in bits (all assumed <= BMAX)
n, // number of codes (assumed <= N_MAX)
s, // number of simple-valued codes (0..s-1)
d, // list of base values for non-simple codes
e, // list of extra bits for non-simple codes
mm // maximum lookup bits
) {
this.BMAX = 16; // maximum bit length of any code
this.N_MAX = 288; // maximum number of codes in any set
this.status = 0; // 0: success, 1: incomplete table, 2: bad input
this.root = null; // (zip_HuftList) starting table
this.m = 0; // maximum lookup bits, returns actual
/* Given a list of code lengths and a maximum table size, make a set of
tables to decode that set of codes. Return zero on success, one if
the given code set is incomplete (the tables are still built in this
case), two if the input is invalid (all zero length codes or an
oversubscribed set of lengths), and three if not enough memory.
The code with value 256 is special, and the tables are constructed
so that no bits beyond that code are fetched when that code is
decoded. */
{
var a; // counter for codes of length k
var c = new Array( this.BMAX + 1 ); // bit length count table
var el; // length of EOB code (value 256)
var f; // i repeats in table every f entries
var g; // maximum code length
var h; // table level
var i; // counter, current code
var j; // counter
var k; // number of bits in current code
var lx = new Array( this.BMAX + 1 ); // stack of bits per table
var p; // pointer into c[], b[], or v[]
var pidx; // index of p
var q; // (zip_HuftNode) points to current table
var r = new zip_HuftNode(); // table entry for structure assignment
var u = new Array( this.BMAX ); // zip_HuftNode[BMAX][] table stack
var v = new Array( this.N_MAX ); // values in order of bit length
var w;
var x = new Array( this.BMAX + 1 );// bit offsets, then code stack
var xp; // pointer into x or c
var y; // number of dummy codes added
var z; // number of entries in current table
var o;
var tail; // (zip_HuftList)
tail = this.root = null;
for ( i = 0; i < c.length; i ++ )
c[ i ] = 0;
for ( i = 0; i < lx.length; i ++ )
lx[ i ] = 0;
for ( i = 0; i < u.length; i ++ )
u[ i ] = null;
for ( i = 0; i < v.length; i ++ )
v[ i ] = 0;
for ( i = 0; i < x.length; i ++ )
x[ i ] = 0;
// Generate counts for each bit length
el = n > 256 ? b[ 256 ] : this.BMAX; // set length of EOB code, if any
p = b; pidx = 0;
i = n;
do {
c[ p[ pidx ]] ++; // assume all entries <= BMAX
pidx ++;
} while ( -- i > 0 );
if ( c[ 0 ] == n ) {
// null input--all zero length codes
this.root = null;
this.m = 0;
this.status = 0;
return;
}
// Find minimum and maximum length, bound *m by those
for ( j = 1; j <= this.BMAX; j ++ )
if ( c[ j ] != 0 )
break;
k = j; // minimum code length
if ( mm < j )
mm = j;
for ( i = this.BMAX; i != 0; i -- )
if ( c[ i ] != 0 )
break;
g = i; // maximum code length
if ( mm > i )
mm = i;
// Adjust last length count to fill out codes, if needed
for ( y = 1 << j; j < i; j ++, y <<= 1 )
if ( ( y -= c[ j ] ) < 0 ) {
this.status = 2; // bad input: more codes than bits
this.m = mm;
return;
}
if ( ( y -= c[ i ] ) < 0 ) {
this.status = 2;
this.m = mm;
return;
}
c[ i ] += y;
// Generate starting offsets into the value table for each length
x[ 1 ] = j = 0;
p = c;
pidx = 1;
xp = 2;
while ( -- i > 0 ) // note that i == g from above
x[ xp ++ ] = ( j += p[ pidx ++ ] );
// Make a table of values in order of bit lengths
p = b; pidx = 0;
i = 0;
do {
if ( ( j = p[ pidx ++ ] ) != 0 )
v[ x[ j ] ++ ] = i;
} while ( ++ i < n );
n = x[ g ]; // set n to length of v
// Generate the Huffman codes and for each, make the table entries
x[ 0 ] = i = 0; // first Huffman code is zero
p = v; pidx = 0; // grab values in bit order
h = - 1; // no tables yet--level -1
w = lx[ 0 ] = 0; // no bits decoded yet
q = null; // ditto
z = 0; // ditto
// go through the bit lengths (k already is bits in shortest code)
for ( ; k <= g; k ++ ) {
a = c[ k ];
while ( a -- > 0 ) {
// here i is the Huffman code of length k bits for value p[pidx]
// make tables up to required level
while ( k > w + lx[ 1 + h ] ) {
w += lx[ 1 + h ]; // add bits already decoded
h ++;
// compute minimum size table less than or equal to *m bits
z = ( z = g - w ) > mm ? mm : z; // upper limit
if ( ( f = 1 << ( j = k - w ) ) > a + 1 ) {
// try a k-w bit table
// too few codes for k-w bit table
f -= a + 1; // deduct codes from patterns left
xp = k;
while ( ++ j < z ) {
// try smaller tables up to z bits
if ( ( f <<= 1 ) <= c[ ++ xp ] )
break; // enough codes to use up j bits
f -= c[ xp ]; // else deduct codes from patterns
}
}
if ( w + j > el && w < el )
j = el - w; // make EOB code end at table
z = 1 << j; // table entries for j-bit table
lx[ 1 + h ] = j; // set table size in stack
// allocate and link in new table
q = new Array( z );
for ( o = 0; o < z; o ++ ) {
q[ o ] = new zip_HuftNode();
}
if ( tail == null )
tail = this.root = new zip_HuftList();
else
tail = tail.next = new zip_HuftList();
tail.next = null;
tail.list = q;
u[ h ] = q; // table starts after link
/* connect to last table, if there is one */
if ( h > 0 ) {
x[ h ] = i; // save pattern for backing up
r.b = lx[ h ]; // bits to dump before this table
r.e = 16 + j; // bits in this table
r.t = q; // pointer to this table
j = ( i & ( ( 1 << w ) - 1 ) ) >> ( w - lx[ h ] );
u[ h - 1 ][ j ].e = r.e;
u[ h - 1 ][ j ].b = r.b;
u[ h - 1 ][ j ].n = r.n;
u[ h - 1 ][ j ].t = r.t;
}
}
// set up table entry in r
r.b = k - w;
if ( pidx >= n )
r.e = 99; // out of values--invalid code
else if ( p[ pidx ] < s ) {
r.e = ( p[ pidx ] < 256 ? 16 : 15 ); // 256 is end-of-block code
r.n = p[ pidx ++ ]; // simple code is just the value
} else {
r.e = e[ p[ pidx ] - s ]; // non-simple--look up in lists
r.n = d[ p[ pidx ++ ] - s ];
}
// fill code-like entries with r //
f = 1 << ( k - w );
for ( j = i >> w; j < z; j += f ) {
q[ j ].e = r.e;
q[ j ].b = r.b;
q[ j ].n = r.n;
q[ j ].t = r.t;
}
// backwards increment the k-bit code i
for ( j = 1 << ( k - 1 ); ( i & j ) != 0; j >>= 1 )
i ^= j;
i ^= j;
// backup over finished tables
while ( ( i & ( ( 1 << w ) - 1 ) ) != x[ h ] ) {
w -= lx[ h ]; // don't need to update q
h --;
}
}
}
/* return actual size of base table */
this.m = lx[ 1 ];
/* Return true (1) if we were given an incomplete table */
this.status = ( ( y != 0 && g != 1 ) ? 1 : 0 );
} /* end of constructor */
}
/* routines (inflate) */
var zip_GET_BYTE = function () {
if ( zip_inflate_data.length == zip_inflate_pos )
return - 1;
return zip_inflate_data[ zip_inflate_pos ++ ];
}
var zip_NEEDBITS = function ( n ) {
while ( zip_bit_len < n ) {
zip_bit_buf |= zip_GET_BYTE() << zip_bit_len;
zip_bit_len += 8;
}
}
var zip_GETBITS = function ( n ) {
return zip_bit_buf & zip_MASK_BITS[ n ];
}
var zip_DUMPBITS = function ( n ) {
zip_bit_buf >>= n;
zip_bit_len -= n;
}
var zip_inflate_codes = function ( buff, off, size ) {
/* inflate (decompress) the codes in a deflated (compressed) block.
Return an error code or zero if it all goes ok. */
var e; // table entry flag/number of extra bits
var t; // (zip_HuftNode) pointer to table entry
var n;
if ( size == 0 )
return 0;
// inflate the coded data
n = 0;
for ( ;; ) {
// do until end of block
zip_NEEDBITS( zip_bl );
t = zip_tl.list[ zip_GETBITS( zip_bl ) ];
e = t.e;
while ( e > 16 ) {
if ( e == 99 )
return - 1;
zip_DUMPBITS( t.b );
e -= 16;
zip_NEEDBITS( e );
t = t.t[ zip_GETBITS( e ) ];
e = t.e;
}
zip_DUMPBITS( t.b );
if ( e == 16 ) {
// then it's a literal
zip_wp &= zip_WSIZE - 1;
buff[ off + n ++ ] = zip_slide[ zip_wp ++ ] = t.n;
if ( n == size )
return size;
continue;
}
// exit if end of block
if ( e == 15 )
break;
// it's an EOB or a length
// get length of block to copy
zip_NEEDBITS( e );
zip_copy_leng = t.n + zip_GETBITS( e );
zip_DUMPBITS( e );
// decode distance of block to copy
zip_NEEDBITS( zip_bd );
t = zip_td.list[ zip_GETBITS( zip_bd ) ];
e = t.e;
while ( e > 16 ) {
if ( e == 99 )
return - 1;
zip_DUMPBITS( t.b );
e -= 16;
zip_NEEDBITS( e );
t = t.t[ zip_GETBITS( e ) ];
e = t.e;
}
zip_DUMPBITS( t.b );
zip_NEEDBITS( e );
zip_copy_dist = zip_wp - t.n - zip_GETBITS( e );
zip_DUMPBITS( e );
// do the copy
while ( zip_copy_leng > 0 && n < size ) {
zip_copy_leng --;
zip_copy_dist &= zip_WSIZE - 1;
zip_wp &= zip_WSIZE - 1;
buff[ off + n ++ ] = zip_slide[ zip_wp ++ ]
= zip_slide[ zip_copy_dist ++ ];
}
if ( n == size )
return size;
}
zip_method = - 1; // done
return n;
}
var zip_inflate_stored = function ( buff, off, size ) {
/* "decompress" an inflated type 0 (stored) block. */
var n;
// go to byte boundary
n = zip_bit_len & 7;
zip_DUMPBITS( n );
// get the length and its complement
zip_NEEDBITS( 16 );
n = zip_GETBITS( 16 );
zip_DUMPBITS( 16 );
zip_NEEDBITS( 16 );
if ( n != ( ( ~ zip_bit_buf ) & 0xffff ) )
return - 1; // error in compressed data
zip_DUMPBITS( 16 );
// read and output the compressed data
zip_copy_leng = n;
n = 0;
while ( zip_copy_leng > 0 && n < size ) {
zip_copy_leng --;
zip_wp &= zip_WSIZE - 1;
zip_NEEDBITS( 8 );
buff[ off + n ++ ] = zip_slide[ zip_wp ++ ] =
zip_GETBITS( 8 );
zip_DUMPBITS( 8 );
}
if ( zip_copy_leng == 0 )
zip_method = - 1; // done
return n;
}
var zip_inflate_fixed = function ( buff, off, size ) {
/* decompress an inflated type 1 (fixed Huffman codes) block. We should
either replace this with a custom decoder, or at least precompute the
Huffman tables. */
// if first time, set up tables for fixed blocks
if ( zip_fixed_tl == null ) {
var i; // temporary variable
var l = new Array( 288 ); // length list for huft_build
var h; // zip_HuftBuild
// literal table
for ( i = 0; i < 144; i ++ )
l[ i ] = 8;
for ( ; i < 256; i ++ )
l[ i ] = 9;
for ( ; i < 280; i ++ )
l[ i ] = 7;
for ( ; i < 288; i ++ ) // make a complete, but wrong code set
l[ i ] = 8;
zip_fixed_bl = 7;
h = new zip_HuftBuild( l, 288, 257, zip_cplens, zip_cplext,
zip_fixed_bl );
if ( h.status != 0 ) {
alert( "HufBuild error: " + h.status );
return - 1;
}
zip_fixed_tl = h.root;
zip_fixed_bl = h.m;
// distance table
for ( i = 0; i < 30; i ++ ) // make an incomplete code set
l[ i ] = 5;
zip_fixed_bd = 5;
h = new zip_HuftBuild( l, 30, 0, zip_cpdist, zip_cpdext, zip_fixed_bd );
if ( h.status > 1 ) {
zip_fixed_tl = null;
alert( "HufBuild error: " + h.status );
return - 1;
}
zip_fixed_td = h.root;
zip_fixed_bd = h.m;
}
zip_tl = zip_fixed_tl;
zip_td = zip_fixed_td;
zip_bl = zip_fixed_bl;
zip_bd = zip_fixed_bd;
return zip_inflate_codes( buff, off, size );
}
var zip_inflate_dynamic = function ( buff, off, size ) {
// decompress an inflated type 2 (dynamic Huffman codes) block.
var i; // temporary variables
var j;
var l; // last length
var n; // number of lengths to get
var t; // (zip_HuftNode) literal/length code table
var nb; // number of bit length codes
var nl; // number of literal/length codes
var nd; // number of distance codes
var ll = new Array( 286 + 30 ); // literal/length and distance code lengths
var h; // (zip_HuftBuild)
for ( i = 0; i < ll.length; i ++ )
ll[ i ] = 0;
// read in table lengths
zip_NEEDBITS( 5 );
nl = 257 + zip_GETBITS( 5 ); // number of literal/length codes
zip_DUMPBITS( 5 );
zip_NEEDBITS( 5 );
nd = 1 + zip_GETBITS( 5 ); // number of distance codes
zip_DUMPBITS( 5 );
zip_NEEDBITS( 4 );
nb = 4 + zip_GETBITS( 4 ); // number of bit length codes
zip_DUMPBITS( 4 );
if ( nl > 286 || nd > 30 )
return - 1; // bad lengths
// read in bit-length-code lengths
for ( j = 0; j < nb; j ++ )
{
zip_NEEDBITS( 3 );
ll[ zip_border[ j ]] = zip_GETBITS( 3 );
zip_DUMPBITS( 3 );
}
for ( ; j < 19; j ++ )
ll[ zip_border[ j ]] = 0;
// build decoding table for trees--single level, 7 bit lookup
zip_bl = 7;
h = new zip_HuftBuild( ll, 19, 19, null, null, zip_bl );
if ( h.status != 0 )
return - 1; // incomplete code set
zip_tl = h.root;
zip_bl = h.m;
// read in literal and distance code lengths
n = nl + nd;
i = l = 0;
while ( i < n ) {
zip_NEEDBITS( zip_bl );
t = zip_tl.list[ zip_GETBITS( zip_bl ) ];
j = t.b;
zip_DUMPBITS( j );
j = t.n;
if ( j < 16 ) // length of code in bits (0..15)
ll[ i ++ ] = l = j; // save last length in l
else if ( j == 16 ) {
// repeat last length 3 to 6 times
zip_NEEDBITS( 2 );
j = 3 + zip_GETBITS( 2 );
zip_DUMPBITS( 2 );
if ( i + j > n )
return - 1;
while ( j -- > 0 )
ll[ i ++ ] = l;
} else if ( j == 17 ) {
// 3 to 10 zero length codes
zip_NEEDBITS( 3 );
j = 3 + zip_GETBITS( 3 );
zip_DUMPBITS( 3 );
if ( i + j > n )
return - 1;
while ( j -- > 0 )
ll[ i ++ ] = 0;
l = 0;
} else {
// j == 18: 11 to 138 zero length codes
zip_NEEDBITS( 7 );
j = 11 + zip_GETBITS( 7 );
zip_DUMPBITS( 7 );
if ( i + j > n )
return - 1;
while ( j -- > 0 )
ll[ i ++ ] = 0;
l = 0;
}
}
// build the decoding tables for literal/length and distance codes
zip_bl = zip_lbits;
h = new zip_HuftBuild( ll, nl, 257, zip_cplens, zip_cplext, zip_bl );
if ( zip_bl == 0 ) // no literals or lengths
h.status = 1;
if ( h.status != 0 ) {
/*if(h.status == 1)
;// **incomplete literal tree** */
return - 1; // incomplete code set
}
zip_tl = h.root;
zip_bl = h.m;
for ( i = 0; i < nd; i ++ )
ll[ i ] = ll[ i + nl ];
zip_bd = zip_dbits;
h = new zip_HuftBuild( ll, nd, 0, zip_cpdist, zip_cpdext, zip_bd );
zip_td = h.root;
zip_bd = h.m;
if ( zip_bd == 0 && nl > 257 ) {
// lengths but no distances
// **incomplete distance tree**
return - 1;
}
/*if(h.status == 1) {
;// **incomplete distance tree**
}*/
if ( h.status != 0 )
return - 1;
// decompress until an end-of-block code
return zip_inflate_codes( buff, off, size );
}
var zip_inflate_start = function () {
var i;
if ( zip_slide == null )
zip_slide = new Array( 2 * zip_WSIZE );
zip_wp = 0;
zip_bit_buf = 0;
zip_bit_len = 0;
zip_method = - 1;
zip_eof = false;
zip_copy_leng = zip_copy_dist = 0;
zip_tl = null;
}
var zip_inflate_internal = function ( buff, off, size ) {
// decompress an inflated entry
var n, i;
n = 0;
while ( n < size ) {
if ( zip_eof && zip_method == - 1 )
return n;
if ( zip_copy_leng > 0 ) {
if ( zip_method != zip_STORED_BLOCK ) {
// STATIC_TREES or DYN_TREES
while ( zip_copy_leng > 0 && n < size ) {
zip_copy_leng --;
zip_copy_dist &= zip_WSIZE - 1;
zip_wp &= zip_WSIZE - 1;
buff[ off + n ++ ] = zip_slide[ zip_wp ++ ] =
zip_slide[ zip_copy_dist ++ ];
}
} else {
while ( zip_copy_leng > 0 && n < size ) {
zip_copy_leng --;
zip_wp &= zip_WSIZE - 1;
zip_NEEDBITS( 8 );
buff[ off + n ++ ] = zip_slide[ zip_wp ++ ] = zip_GETBITS( 8 );
zip_DUMPBITS( 8 );
}
if ( zip_copy_leng == 0 )
zip_method = - 1; // done
}
if ( n == size )
return n;
}
if ( zip_method == - 1 ) {
if ( zip_eof )
break;
// read in last block bit
zip_NEEDBITS( 1 );
if ( zip_GETBITS( 1 ) != 0 )
zip_eof = true;
zip_DUMPBITS( 1 );
// read in block type
zip_NEEDBITS( 2 );
zip_method = zip_GETBITS( 2 );
zip_DUMPBITS( 2 );
zip_tl = null;
zip_copy_leng = 0;
}
switch ( zip_method ) {
case 0: // zip_STORED_BLOCK
i = zip_inflate_stored( buff, off + n, size - n );
break;
case 1: // zip_STATIC_TREES
if ( zip_tl != null )
i = zip_inflate_codes( buff, off + n, size - n );
else
i = zip_inflate_fixed( buff, off + n, size - n );
break;
case 2: // zip_DYN_TREES
if ( zip_tl != null )
i = zip_inflate_codes( buff, off + n, size - n );
else
i = zip_inflate_dynamic( buff, off + n, size - n );
break;
default: // error
i = - 1;
}
if ( i == - 1 ) {
if ( zip_eof )
return 0;
return - 1;
}
n += i;
}
return n;
}
var zip_inflate = function ( data ) {
var i, j, pos = 0;
zip_inflate_start();
zip_inflate_data = new Uint8Array( data );
zip_inflate_pos = 0;
var buff = new Uint8Array( 1024 );
var out = [];
while ( ( i = zip_inflate_internal( buff, 0, buff.length ) ) > 0 )
for ( j = 0; j < i; j ++ )
out[ pos ++ ] = buff[ j ];
zip_inflate_data = null; // G.C.
return new Uint8Array( out ).buffer;
}
return { inflate: zip_inflate };
}();
/**
* SEA3D Deflate
* @author Sunag / http://www.sunag.com.br/
*/
SEA3D.File.DeflateUncompress = function ( data ) {
return SEA3D.Deflate.inflate( data );
};
SEA3D.File.setDecompressionEngine( 1, "deflate", SEA3D.File.DeflateUncompress );

View File

@@ -0,0 +1,810 @@
/*
Copyright (c) 2011 Juan Mellado
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/*
References:
- "LZMA SDK" by Igor Pavlov
http://www.7-zip.org/sdk.html
*/
'use strict';
SEA3D.LZMA = function () {
var LZMA = LZMA || {};
LZMA.OutWindow = function () {
this._windowSize = 0;
};
LZMA.OutWindow.prototype.create = function ( windowSize ) {
if ( ( ! this._buffer ) || ( this._windowSize !== windowSize ) ) {
this._buffer = [];
}
this._windowSize = windowSize;
this._pos = 0;
this._streamPos = 0;
};
LZMA.OutWindow.prototype.flush = function () {
var size = this._pos - this._streamPos;
if ( size !== 0 ) {
while ( size -- ) {
this._stream.writeByte( this._buffer[ this._streamPos ++ ] );
}
if ( this._pos >= this._windowSize ) {
this._pos = 0;
}
this._streamPos = this._pos;
}
};
LZMA.OutWindow.prototype.releaseStream = function () {
this.flush();
this._stream = null;
};
LZMA.OutWindow.prototype.setStream = function ( stream ) {
this.releaseStream();
this._stream = stream;
};
LZMA.OutWindow.prototype.init = function ( solid ) {
if ( ! solid ) {
this._streamPos = 0;
this._pos = 0;
}
};
LZMA.OutWindow.prototype.copyBlock = function ( distance, len ) {
var pos = this._pos - distance - 1;
if ( pos < 0 ) {
pos += this._windowSize;
}
while ( len -- ) {
if ( pos >= this._windowSize ) {
pos = 0;
}
this._buffer[ this._pos ++ ] = this._buffer[ pos ++ ];
if ( this._pos >= this._windowSize ) {
this.flush();
}
}
};
LZMA.OutWindow.prototype.putByte = function ( b ) {
this._buffer[ this._pos ++ ] = b;
if ( this._pos >= this._windowSize ) {
this.flush();
}
};
LZMA.OutWindow.prototype.getByte = function ( distance ) {
var pos = this._pos - distance - 1;
if ( pos < 0 ) {
pos += this._windowSize;
}
return this._buffer[ pos ];
};
LZMA.RangeDecoder = function () {
};
LZMA.RangeDecoder.prototype.setStream = function ( stream ) {
this._stream = stream;
};
LZMA.RangeDecoder.prototype.releaseStream = function () {
this._stream = null;
};
LZMA.RangeDecoder.prototype.init = function () {
var i = 5;
this._code = 0;
this._range = - 1;
while ( i -- ) {
this._code = ( this._code << 8 ) | this._stream.readByte();
}
};
LZMA.RangeDecoder.prototype.decodeDirectBits = function ( numTotalBits ) {
var result = 0, i = numTotalBits, t;
while ( i -- ) {
this._range >>>= 1;
t = ( this._code - this._range ) >>> 31;
this._code -= this._range & ( t - 1 );
result = ( result << 1 ) | ( 1 - t );
if ( ( this._range & 0xff000000 ) === 0 ) {
this._code = ( this._code << 8 ) | this._stream.readByte();
this._range <<= 8;
}
}
return result;
};
LZMA.RangeDecoder.prototype.decodeBit = function ( probs, index ) {
var prob = probs[ index ],
newBound = ( this._range >>> 11 ) * prob;
if ( ( this._code ^ 0x80000000 ) < ( newBound ^ 0x80000000 ) ) {
this._range = newBound;
probs[ index ] += ( 2048 - prob ) >>> 5;
if ( ( this._range & 0xff000000 ) === 0 ) {
this._code = ( this._code << 8 ) | this._stream.readByte();
this._range <<= 8;
}
return 0;
}
this._range -= newBound;
this._code -= newBound;
probs[ index ] -= prob >>> 5;
if ( ( this._range & 0xff000000 ) === 0 ) {
this._code = ( this._code << 8 ) | this._stream.readByte();
this._range <<= 8;
}
return 1;
};
LZMA.initBitModels = function ( probs, len ) {
while ( len -- ) {
probs[ len ] = 1024;
}
};
LZMA.BitTreeDecoder = function ( numBitLevels ) {
this._models = [];
this._numBitLevels = numBitLevels;
};
LZMA.BitTreeDecoder.prototype.init = function () {
LZMA.initBitModels( this._models, 1 << this._numBitLevels );
};
LZMA.BitTreeDecoder.prototype.decode = function ( rangeDecoder ) {
var m = 1, i = this._numBitLevels;
while ( i -- ) {
m = ( m << 1 ) | rangeDecoder.decodeBit( this._models, m );
}
return m - ( 1 << this._numBitLevels );
};
LZMA.BitTreeDecoder.prototype.reverseDecode = function ( rangeDecoder ) {
var m = 1, symbol = 0, i = 0, bit;
for ( ; i < this._numBitLevels; ++ i ) {
bit = rangeDecoder.decodeBit( this._models, m );
m = ( m << 1 ) | bit;
symbol |= bit << i;
}
return symbol;
};
LZMA.reverseDecode2 = function ( models, startIndex, rangeDecoder, numBitLevels ) {
var m = 1, symbol = 0, i = 0, bit;
for ( ; i < numBitLevels; ++ i ) {
bit = rangeDecoder.decodeBit( models, startIndex + m );
m = ( m << 1 ) | bit;
symbol |= bit << i;
}
return symbol;
};
LZMA.LenDecoder = function () {
this._choice = [];
this._lowCoder = [];
this._midCoder = [];
this._highCoder = new LZMA.BitTreeDecoder( 8 );
this._numPosStates = 0;
};
LZMA.LenDecoder.prototype.create = function ( numPosStates ) {
for ( ; this._numPosStates < numPosStates; ++ this._numPosStates ) {
this._lowCoder[ this._numPosStates ] = new LZMA.BitTreeDecoder( 3 );
this._midCoder[ this._numPosStates ] = new LZMA.BitTreeDecoder( 3 );
}
};
LZMA.LenDecoder.prototype.init = function () {
var i = this._numPosStates;
LZMA.initBitModels( this._choice, 2 );
while ( i -- ) {
this._lowCoder[ i ].init();
this._midCoder[ i ].init();
}
this._highCoder.init();
};
LZMA.LenDecoder.prototype.decode = function ( rangeDecoder, posState ) {
if ( rangeDecoder.decodeBit( this._choice, 0 ) === 0 ) {
return this._lowCoder[ posState ].decode( rangeDecoder );
}
if ( rangeDecoder.decodeBit( this._choice, 1 ) === 0 ) {
return 8 + this._midCoder[ posState ].decode( rangeDecoder );
}
return 16 + this._highCoder.decode( rangeDecoder );
};
LZMA.Decoder2 = function () {
this._decoders = [];
};
LZMA.Decoder2.prototype.init = function () {
LZMA.initBitModels( this._decoders, 0x300 );
};
LZMA.Decoder2.prototype.decodeNormal = function ( rangeDecoder ) {
var symbol = 1;
do {
symbol = ( symbol << 1 ) | rangeDecoder.decodeBit( this._decoders, symbol );
}while ( symbol < 0x100 );
return symbol & 0xff;
};
LZMA.Decoder2.prototype.decodeWithMatchByte = function ( rangeDecoder, matchByte ) {
var symbol = 1, matchBit, bit;
do {
matchBit = ( matchByte >> 7 ) & 1;
matchByte <<= 1;
bit = rangeDecoder.decodeBit( this._decoders, ( ( 1 + matchBit ) << 8 ) + symbol );
symbol = ( symbol << 1 ) | bit;
if ( matchBit !== bit ) {
while ( symbol < 0x100 ) {
symbol = ( symbol << 1 ) | rangeDecoder.decodeBit( this._decoders, symbol );
}
break;
}
}while ( symbol < 0x100 );
return symbol & 0xff;
};
LZMA.LiteralDecoder = function () {
};
LZMA.LiteralDecoder.prototype.create = function ( numPosBits, numPrevBits ) {
var i;
if ( this._coders
&& ( this._numPrevBits === numPrevBits )
&& ( this._numPosBits === numPosBits ) ) {
return;
}
this._numPosBits = numPosBits;
this._posMask = ( 1 << numPosBits ) - 1;
this._numPrevBits = numPrevBits;
this._coders = [];
i = 1 << ( this._numPrevBits + this._numPosBits );
while ( i -- ) {
this._coders[ i ] = new LZMA.Decoder2();
}
};
LZMA.LiteralDecoder.prototype.init = function () {
var i = 1 << ( this._numPrevBits + this._numPosBits );
while ( i -- ) {
this._coders[ i ].init();
}
};
LZMA.LiteralDecoder.prototype.getDecoder = function ( pos, prevByte ) {
return this._coders[ ( ( pos & this._posMask ) << this._numPrevBits )
+ ( ( prevByte & 0xff ) >>> ( 8 - this._numPrevBits ) ) ];
};
LZMA.Decoder = function () {
this._outWindow = new LZMA.OutWindow();
this._rangeDecoder = new LZMA.RangeDecoder();
this._isMatchDecoders = [];
this._isRepDecoders = [];
this._isRepG0Decoders = [];
this._isRepG1Decoders = [];
this._isRepG2Decoders = [];
this._isRep0LongDecoders = [];
this._posSlotDecoder = [];
this._posDecoders = [];
this._posAlignDecoder = new LZMA.BitTreeDecoder( 4 );
this._lenDecoder = new LZMA.LenDecoder();
this._repLenDecoder = new LZMA.LenDecoder();
this._literalDecoder = new LZMA.LiteralDecoder();
this._dictionarySize = - 1;
this._dictionarySizeCheck = - 1;
this._posSlotDecoder[ 0 ] = new LZMA.BitTreeDecoder( 6 );
this._posSlotDecoder[ 1 ] = new LZMA.BitTreeDecoder( 6 );
this._posSlotDecoder[ 2 ] = new LZMA.BitTreeDecoder( 6 );
this._posSlotDecoder[ 3 ] = new LZMA.BitTreeDecoder( 6 );
};
LZMA.Decoder.prototype.setDictionarySize = function ( dictionarySize ) {
if ( dictionarySize < 0 ) {
return false;
}
if ( this._dictionarySize !== dictionarySize ) {
this._dictionarySize = dictionarySize;
this._dictionarySizeCheck = Math.max( this._dictionarySize, 1 );
this._outWindow.create( Math.max( this._dictionarySizeCheck, 4096 ) );
}
return true;
};
LZMA.Decoder.prototype.setLcLpPb = function ( lc, lp, pb ) {
var numPosStates = 1 << pb;
if ( lc > 8 || lp > 4 || pb > 4 ) {
return false;
}
this._literalDecoder.create( lp, lc );
this._lenDecoder.create( numPosStates );
this._repLenDecoder.create( numPosStates );
this._posStateMask = numPosStates - 1;
return true;
};
LZMA.Decoder.prototype.init = function () {
var i = 4;
this._outWindow.init( false );
LZMA.initBitModels( this._isMatchDecoders, 192 );
LZMA.initBitModels( this._isRep0LongDecoders, 192 );
LZMA.initBitModels( this._isRepDecoders, 12 );
LZMA.initBitModels( this._isRepG0Decoders, 12 );
LZMA.initBitModels( this._isRepG1Decoders, 12 );
LZMA.initBitModels( this._isRepG2Decoders, 12 );
LZMA.initBitModels( this._posDecoders, 114 );
this._literalDecoder.init();
while ( i -- ) {
this._posSlotDecoder[ i ].init();
}
this._lenDecoder.init();
this._repLenDecoder.init();
this._posAlignDecoder.init();
this._rangeDecoder.init();
};
LZMA.Decoder.prototype.decode = function ( inStream, outStream, outSize ) {
var state = 0, rep0 = 0, rep1 = 0, rep2 = 0, rep3 = 0, nowPos64 = 0, prevByte = 0,
posState, decoder2, len, distance, posSlot, numDirectBits;
this._rangeDecoder.setStream( inStream );
this._outWindow.setStream( outStream );
this.init();
while ( outSize < 0 || nowPos64 < outSize ) {
posState = nowPos64 & this._posStateMask;
if ( this._rangeDecoder.decodeBit( this._isMatchDecoders, ( state << 4 ) + posState ) === 0 ) {
decoder2 = this._literalDecoder.getDecoder( nowPos64 ++, prevByte );
if ( state >= 7 ) {
prevByte = decoder2.decodeWithMatchByte( this._rangeDecoder, this._outWindow.getByte( rep0 ) );
} else {
prevByte = decoder2.decodeNormal( this._rangeDecoder );
}
this._outWindow.putByte( prevByte );
state = state < 4 ? 0 : state - ( state < 10 ? 3 : 6 );
} else {
if ( this._rangeDecoder.decodeBit( this._isRepDecoders, state ) === 1 ) {
len = 0;
if ( this._rangeDecoder.decodeBit( this._isRepG0Decoders, state ) === 0 ) {
if ( this._rangeDecoder.decodeBit( this._isRep0LongDecoders, ( state << 4 ) + posState ) === 0 ) {
state = state < 7 ? 9 : 11;
len = 1;
}
} else {
if ( this._rangeDecoder.decodeBit( this._isRepG1Decoders, state ) === 0 ) {
distance = rep1;
} else {
if ( this._rangeDecoder.decodeBit( this._isRepG2Decoders, state ) === 0 ) {
distance = rep2;
} else {
distance = rep3;
rep3 = rep2;
}
rep2 = rep1;
}
rep1 = rep0;
rep0 = distance;
}
if ( len === 0 ) {
len = 2 + this._repLenDecoder.decode( this._rangeDecoder, posState );
state = state < 7 ? 8 : 11;
}
} else {
rep3 = rep2;
rep2 = rep1;
rep1 = rep0;
len = 2 + this._lenDecoder.decode( this._rangeDecoder, posState );
state = state < 7 ? 7 : 10;
posSlot = this._posSlotDecoder[ len <= 5 ? len - 2 : 3 ].decode( this._rangeDecoder );
if ( posSlot >= 4 ) {
numDirectBits = ( posSlot >> 1 ) - 1;
rep0 = ( 2 | ( posSlot & 1 ) ) << numDirectBits;
if ( posSlot < 14 ) {
rep0 += LZMA.reverseDecode2( this._posDecoders,
rep0 - posSlot - 1, this._rangeDecoder, numDirectBits );
} else {
rep0 += this._rangeDecoder.decodeDirectBits( numDirectBits - 4 ) << 4;
rep0 += this._posAlignDecoder.reverseDecode( this._rangeDecoder );
if ( rep0 < 0 ) {
if ( rep0 === - 1 ) {
break;
}
return false;
}
}
} else {
rep0 = posSlot;
}
}
if ( rep0 >= nowPos64 || rep0 >= this._dictionarySizeCheck ) {
return false;
}
this._outWindow.copyBlock( rep0, len );
nowPos64 += len;
prevByte = this._outWindow.getByte( 0 );
}
}
this._outWindow.flush();
this._outWindow.releaseStream();
this._rangeDecoder.releaseStream();
return true;
};
LZMA.Decoder.prototype.setDecoderProperties = function ( properties ) {
var value, lc, lp, pb, dictionarySize;
if ( properties.size < 5 ) {
return false;
}
value = properties.readByte();
lc = value % 9;
value = ~~ ( value / 9 );
lp = value % 5;
pb = ~~ ( value / 5 );
if ( ! this.setLcLpPb( lc, lp, pb ) ) {
return false;
}
dictionarySize = properties.readByte();
dictionarySize |= properties.readByte() << 8;
dictionarySize |= properties.readByte() << 16;
dictionarySize += properties.readByte() * 16777216;
return this.setDictionarySize( dictionarySize );
};
LZMA.decompress = function ( properties, inStream, outStream, outSize ) {
var decoder = new LZMA.Decoder();
if ( ! decoder.setDecoderProperties( properties ) ) {
throw "Incorrect stream properties";
}
if ( ! decoder.decode( inStream, outStream, outSize ) ) {
throw "Error in data stream";
}
return true;
};
LZMA.decompressFile = function ( inStream, outStream ) {
var decoder = new LZMA.Decoder(), outSize;
if ( ! decoder.setDecoderProperties( inStream ) ) {
throw "Incorrect stream properties";
}
outSize = inStream.readByte();
outSize |= inStream.readByte() << 8;
outSize |= inStream.readByte() << 16;
outSize += inStream.readByte() * 16777216;
inStream.readByte();
inStream.readByte();
inStream.readByte();
inStream.readByte();
if ( ! decoder.decode( inStream, outStream, outSize ) ) {
throw "Error in data stream";
}
return true;
};
return LZMA;
}();
/**
* SEA3D LZMA
* @author Sunag / http://www.sunag.com.br/
*/
SEA3D.File.LZMAUncompress = function ( data ) {
data = new Uint8Array( data );
var inStream = {
data: data,
position: 0,
readByte: function () {
return this.data[ this.position ++ ];
}
};
var outStream = {
data: [],
position: 0,
writeByte: function ( value ) {
this.data[ this.position ++ ] = value;
}
};
SEA3D.LZMA.decompressFile( inStream, outStream );
return new Uint8Array( outStream.data ).buffer;
};
SEA3D.File.setDecompressionEngine( 2, "lzma", SEA3D.File.LZMAUncompress );

View File

@@ -0,0 +1,720 @@
/**
* SEA3D Legacy for Three.JS
* @author Sunag / http://www.sunag.com.br/
*/
'use strict';
//
// Header
//
Object.assign( THREE.SEA3D.prototype, {
_onHead: THREE.SEA3D.prototype.onHead,
_updateTransform: THREE.SEA3D.prototype.updateTransform,
_readVertexAnimation: THREE.SEA3D.prototype.readVertexAnimation,
_readGeometryBuffer: THREE.SEA3D.prototype.readGeometryBuffer,
_readLine: THREE.SEA3D.prototype.readLine,
_getAnimationType: THREE.SEA3D.prototype.getAnimationType,
_readAnimation: THREE.SEA3D.prototype.readAnimation
} );
//
// Utils
//
THREE.SEA3D.prototype.isLegacy = function ( sea ) {
var sea3d = sea.sea3d;
if ( sea3d.sign == 'S3D' && ! sea._legacy ) {
sea._legacy = sea3d.typeUnique[ sea.type ] == true;
return sea3d.config.legacy;
}
return false;
};
THREE.SEA3D.prototype.flipVec3 = function ( v ) {
if ( ! v ) return;
var i = 2;
while ( i < v.length ) {
v[ i ] = - v[ i ];
i += 3;
}
return v;
};
THREE.SEA3D.prototype.expandJoints = function ( sea ) {
var numJoints = sea.numVertex * 4;
var joint = sea.isBig ? new Uint32Array( numJoints ) : new Uint16Array( numJoints );
var weight = new Float32Array( numJoints );
var w = 0, jpv = sea.jointPerVertex;
for ( var i = 0; i < sea.numVertex; i ++ ) {
var tjsIndex = i * 4;
var seaIndex = i * jpv;
joint[ tjsIndex ] = sea.joint[ seaIndex ];
if ( jpv > 1 ) joint[ tjsIndex + 1 ] = sea.joint[ seaIndex + 1 ];
if ( jpv > 2 ) joint[ tjsIndex + 2 ] = sea.joint[ seaIndex + 2 ];
if ( jpv > 3 ) joint[ tjsIndex + 3 ] = sea.joint[ seaIndex + 3 ];
weight[ tjsIndex ] = sea.weight[ seaIndex ];
if ( jpv > 1 ) weight[ tjsIndex + 1 ] = sea.weight[ seaIndex + 1 ];
if ( jpv > 2 ) weight[ tjsIndex + 2 ] = sea.weight[ seaIndex + 2 ];
if ( jpv > 3 ) weight[ tjsIndex + 3 ] = sea.weight[ seaIndex + 3 ];
w = weight[ tjsIndex ] + weight[ tjsIndex + 1 ] + weight[ tjsIndex + 2 ] + weight[ tjsIndex + 3 ];
weight[ tjsIndex ] += 1 - w;
}
sea.joint = joint;
sea.weight = weight;
sea.jointPerVertex = 4;
};
THREE.SEA3D.prototype.compressJoints = function ( sea ) {
var numJoints = sea.numVertex * 4;
var joint = sea.isBig ? new Uint32Array( numJoints ) : new Uint16Array( numJoints );
var weight = new Float32Array( numJoints );
var w = 0, jpv = sea.jointPerVertex;
for ( var i = 0; i < sea.numVertex; i ++ ) {
var tjsIndex = i * 4;
var seaIndex = i * jpv;
joint[ tjsIndex ] = sea.joint[ seaIndex ];
joint[ tjsIndex + 1 ] = sea.joint[ seaIndex + 1 ];
joint[ tjsIndex + 2 ] = sea.joint[ seaIndex + 2 ];
joint[ tjsIndex + 3 ] = sea.joint[ seaIndex + 3 ];
weight[ tjsIndex ] = sea.weight[ seaIndex ];
weight[ tjsIndex + 1 ] = sea.weight[ seaIndex + 1 ];
weight[ tjsIndex + 2 ] = sea.weight[ seaIndex + 2 ];
weight[ tjsIndex + 3 ] = sea.weight[ seaIndex + 3 ];
w = weight[ tjsIndex ] + weight[ tjsIndex + 1 ] + weight[ tjsIndex + 2 ] + weight[ tjsIndex + 3 ];
weight[ tjsIndex ] += 1 - w;
}
sea.joint = joint;
sea.weight = weight;
sea.jointPerVertex = 4;
};
THREE.SEA3D.prototype.flipIndexes = function ( v ) {
var i = 1; // y >-< z
while ( i < v.length ) {
var idx = v[ i + 1 ];
v[ i + 1 ] = v[ i ];
v[ i ] = idx;
i += 3;
}
return v;
};
THREE.SEA3D.prototype.flipBoneMatrix = function () {
var zero = new THREE.Vector3();
return function ( mtx ) {
var pos = THREE.SEA3D.VECBUF.setFromMatrixPosition( mtx );
pos.z = - pos.z;
mtx.setPosition( zero );
mtx.multiplyMatrices( THREE.SEA3D.MTXBUF.makeRotationZ( THREE.Math.degToRad( 180 ) ), mtx );
mtx.setPosition( pos );
return mtx;
};
}();
THREE.SEA3D.prototype.flipScaleMatrix = function () {
var pos = new THREE.Vector3();
var qua = new THREE.Quaternion();
var slc = new THREE.Vector3();
return function ( local, rotate, parent, parentRotate ) {
if ( parent ) local.multiplyMatrices( parent, local );
local.decompose( pos, qua, slc );
slc.z = - slc.z;
local.compose( pos, qua, slc );
if ( rotate ) {
local.multiplyMatrices( local, THREE.SEA3D.MTXBUF.makeRotationZ( THREE.Math.degToRad( 180 ) ) );
}
if ( parent ) {
parent = parent.clone();
this.flipScaleMatrix( parent, parentRotate );
local.multiplyMatrices( parent.getInverse( parent ), local );
}
return local;
};
}();
//
// Legacy
//
THREE.SEA3D.prototype.flipDefaultAnimation = function () {
var buf1 = new THREE.Matrix4();
var buf2 = new THREE.Matrix4();
var pos = new THREE.Vector3();
var qua = new THREE.Quaternion();
var slc = new THREE.Vector3();
var to_pos = new THREE.Vector3();
var to_qua = new THREE.Quaternion();
var to_slc = new THREE.Vector3();
return function ( animation, obj3d, relative ) {
if ( animation.isFliped ) return;
var dataList = animation.dataList,
t_anm = [];
for ( var i = 0; i < dataList.length; i ++ ) {
var data = dataList[ i ],
raw = data.data,
kind = data.kind,
numFrames = raw.length / data.blockSize;
switch ( kind ) {
case SEA3D.Animation.POSITION:
case SEA3D.Animation.ROTATION:
case SEA3D.Animation.SCALE:
t_anm.push( {
kind: kind,
numFrames: numFrames,
raw: raw
} );
break;
}
}
if ( t_anm.length > 0 ) {
var numFrames = t_anm[ 0 ].numFrames,
parent = undefined;
if ( relative ) {
buf1.identity();
parent = this.flipScaleMatrix( buf2.copy( obj3d.matrixWorld ) );
} else {
if ( obj3d.parent ) {
parent = this.flipScaleMatrix( buf2.copy( obj3d.parent.matrixWorld ) );
}
this.flipScaleMatrix( buf1.copy( obj3d.matrix ), false, parent );
}
buf1.decompose( pos, qua, slc );
for ( var f = 0, t, c; f < numFrames; f ++ ) {
for ( t = 0; t < t_anm.length; t ++ ) {
var raw = t_anm[ t ].raw,
kind = t_anm[ t ].kind;
switch ( kind ) {
case SEA3D.Animation.POSITION:
c = f * 3;
pos.set(
raw[ c ],
raw[ c + 1 ],
raw[ c + 2 ]
);
break;
case SEA3D.Animation.ROTATION:
c = f * 4;
qua.set(
raw[ c ],
raw[ c + 1 ],
raw[ c + 2 ],
raw[ c + 3 ]
);
break;
case SEA3D.Animation.SCALE:
c = f * 4;
slc.set(
raw[ c ],
raw[ c + 1 ],
raw[ c + 2 ]
);
break;
}
}
buf1.compose( pos, qua, slc );
this.flipScaleMatrix( buf1, false, buf2 );
buf1.decompose( to_pos, to_qua, to_slc );
for ( t = 0; t < t_anm.length; t ++ ) {
var raw = t_anm[ t ].raw,
kind = t_anm[ t ].kind;
switch ( kind ) {
case SEA3D.Animation.POSITION:
c = f * 3;
raw[ c ] = to_pos.x;
raw[ c + 1 ] = to_pos.y;
raw[ c + 2 ] = to_pos.z;
break;
case SEA3D.Animation.ROTATION:
c = f * 4;
raw[ c ] = to_qua.x;
raw[ c + 1 ] = to_qua.y;
raw[ c + 2 ] = to_qua.z;
raw[ c + 3 ] = to_qua.w;
break;
case SEA3D.Animation.SCALE:
c = f * 3;
raw[ c ] = to_slc.x;
raw[ c + 1 ] = to_slc.y;
raw[ c + 2 ] = to_slc.z;
break;
}
}
}
}
animation.isFliped = true;
};
}();
THREE.SEA3D.prototype.readAnimation = function ( sea ) {
if ( ! this.isLegacy( sea ) ) {
this._readAnimation( sea );
}
};
THREE.SEA3D.prototype.getAnimationType = function ( req ) {
var sea = req.sea;
if ( this.isLegacy( sea ) ) {
switch ( sea.type ) {
case SEA3D.SkeletonAnimation.prototype.type:
this.readSkeletonAnimationLegacy( sea, req.skeleton );
return sea.tag;
break;
case SEA3D.Animation.prototype.type:
if ( req.scope instanceof THREE.Object3D ) {
this.flipDefaultAnimation( sea, req.scope, req.relative );
}
this._readAnimation( sea );
return sea.tag;
break;
}
}
return this._getAnimationType( req );
};
THREE.SEA3D.prototype.updateTransform = function () {
var buf1 = new THREE.Matrix4();
var identity = new THREE.Matrix4();
return function ( obj3d, sea ) {
if ( this.isLegacy( sea ) ) {
if ( sea.transform ) buf1.elements.set( sea.transform );
else buf1.makeTranslation( sea.position.x, sea.position.y, sea.position.z );
this.flipScaleMatrix(
buf1, false,
obj3d.parent ? obj3d.parent.matrixWorld : identity,
obj3d.parent instanceof THREE.Bone
);
obj3d.position.setFromMatrixPosition( buf1 );
obj3d.scale.setFromMatrixScale( buf1 );
// ignore rotation scale
buf1.scale( THREE.SEA3D.VECBUF.set( 1 / obj3d.scale.x, 1 / obj3d.scale.y, 1 / obj3d.scale.z ) );
obj3d.rotation.setFromRotationMatrix( buf1 );
obj3d.updateMatrixWorld();
} else {
this._updateTransform( obj3d, sea );
}
};
}();
THREE.SEA3D.prototype.readSkeleton = function () {
var mtx_tmp_inv = new THREE.Matrix4(),
mtx_local = new THREE.Matrix4(),
mtx_parent = new THREE.Matrix4(),
pos = new THREE.Vector3(),
qua = new THREE.Quaternion();
return function ( sea ) {
var bones = [],
isLegacy = sea.sea3d.config.legacy;
for ( var i = 0; i < sea.joint.length; i ++ ) {
var bone = sea.joint[ i ];
// get world inverse matrix
mtx_tmp_inv.elements = bone.inverseBindMatrix;
// convert to world matrix
mtx_local.getInverse( mtx_tmp_inv );
// convert to three.js order
if ( isLegacy ) this.flipBoneMatrix( mtx_local );
if ( bone.parentIndex > - 1 ) {
// to world
mtx_tmp_inv.elements = sea.joint[ bone.parentIndex ].inverseBindMatrix;
mtx_parent.getInverse( mtx_tmp_inv );
// convert parent to three.js order
if ( isLegacy ) this.flipBoneMatrix( mtx_parent );
// to local
mtx_parent.getInverse( mtx_parent );
mtx_local.multiplyMatrices( mtx_parent, mtx_local );
}
// apply matrix
pos.setFromMatrixPosition( mtx_local );
qua.setFromRotationMatrix( mtx_local );
bones[ i ] = {
name: bone.name,
pos: [ pos.x, pos.y, pos.z ],
rotq: [ qua.x, qua.y, qua.z, qua.w ],
parent: bone.parentIndex
};
}
return sea.tag = bones;
};
}();
THREE.SEA3D.prototype.readSkeletonAnimationLegacy = function () {
var mtx_tmp_inv = new THREE.Matrix4(),
mtx_local = new THREE.Matrix4(),
mtx_global = new THREE.Matrix4(),
mtx_parent = new THREE.Matrix4();
return function ( sea, skl ) {
if ( sea.tag ) return sea.tag;
var animations = [],
delta = ( 1000 / sea.frameRate ) / 1000,
scale = [ 1, 1, 1 ];
for ( var i = 0; i < sea.sequence.length; i ++ ) {
var seq = sea.sequence[ i ];
var start = seq.start;
var end = start + seq.count;
var animation = {
name: seq.name,
repeat: seq.repeat,
fps: sea.frameRate,
JIT: 0,
length: delta * seq.count,
hierarchy: []
};
var numJoints = sea.numJoints,
raw = sea.raw;
for ( var j = 0; j < numJoints; j ++ ) {
var bone = skl.joint[ j ],
node = { parent: bone.parentIndex, keys: [] },
keys = node.keys,
time = 0;
for ( var frame = start; frame < end; frame ++ ) {
var idx = ( frame * numJoints * 7 ) + ( j * 7 );
mtx_local.makeRotationFromQuaternion( THREE.SEA3D.QUABUF.set( raw[ idx + 3 ], raw[ idx + 4 ], raw[ idx + 5 ], raw[ idx + 6 ] ) );
mtx_local.setPosition( THREE.SEA3D.VECBUF.set( raw[ idx ], raw[ idx + 1 ], raw[ idx + 2 ] ) );
if ( bone.parentIndex > - 1 ) {
// to global
mtx_tmp_inv.elements = skl.joint[ bone.parentIndex ].inverseBindMatrix;
mtx_parent.getInverse( mtx_tmp_inv );
mtx_global.multiplyMatrices( mtx_parent, mtx_local );
// convert to three.js matrix
this.flipBoneMatrix( mtx_global );
// flip parent inverse
this.flipBoneMatrix( mtx_parent );
// to local
mtx_parent.getInverse( mtx_parent );
mtx_local.multiplyMatrices( mtx_parent, mtx_global );
} else {
this.flipBoneMatrix( mtx_local );
}
var posQ = THREE.SEA3D.VECBUF.setFromMatrixPosition( mtx_local );
var newQ = THREE.SEA3D.QUABUF.setFromRotationMatrix( mtx_local );
keys.push( {
time: time,
pos: [ posQ.x, posQ.y, posQ.z ],
rot: [ newQ.x, newQ.y, newQ.z, newQ.w ],
scl: scale
} );
time += delta;
}
animation.hierarchy[ j ] = node;
}
animations.push( THREE.SEA3D.AnimationClip.fromClip( THREE.AnimationClip.parseAnimation( animation, skl.tag ), seq.repeat ) );
}
this.domain.clips = this.clips = this.clips || [];
this.clips.push( this.objects[ sea.name + '.anm' ] = sea.tag = animations );
};
}();
THREE.SEA3D.prototype.readVertexAnimation = function ( sea ) {
if ( this.isLegacy( sea ) ) {
for ( var i = 0, l = sea.frame.length; i < l; i ++ ) {
var frame = sea.frame[ i ];
this.flipVec3( frame.vertex );
this.flipVec3( frame.normal );
}
}
this._readVertexAnimation( sea );
};
THREE.SEA3D.prototype.readGeometryBuffer = function ( sea ) {
if ( this.isLegacy( sea ) ) {
this.flipVec3( sea.vertex, true );
this.flipVec3( sea.normal, true );
this.flipIndexes( sea.indexes );
if ( sea.jointPerVertex > 4 ) this.compressJoints( sea );
else if ( sea.jointPerVertex < 4 ) this.expandJoints( sea );
}
this._readGeometryBuffer( sea );
};
THREE.SEA3D.prototype.readLines = function ( sea ) {
if ( this.isLegacy( sea ) ) {
this.flipVec3( sea.vertex );
}
this._readLines( sea );
};
THREE.SEA3D.prototype.onHead = function ( args ) {
if ( args.sign != "S3D" && args.sign != "TJS" ) {
throw new Error( "Sign '" + args.sign + "' unknown." );
}
};
THREE.SEA3D.EXTENSIONS_LOADER.push( { setTypeRead: function () {
// CONFIG
this.config.legacy = this.config.legacy == undefined ? true : this.config.legacy;
this.file.typeRead[ SEA3D.Skeleton.prototype.type ] = this.readSkeleton;
} } );

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,154 @@
/**
* SEA3D - o3dgc
* @author Sunag / http://www.sunag.com.br/
*/
'use strict';
//
// Lossy Compression
//
SEA3D.GeometryGC = function ( name, data, sea3d ) {
this.name = name;
this.data = data;
this.sea3d = sea3d;
var i;
var attrib = data.readUShort();
var uvIDs = [], jointID, weightID;
this.isBig = ( attrib & 1 ) != 0;
data.readVInt = this.isBig ? data.readUInt : data.readUShort;
// Geometry Flags
// ..
// 1 isBig
// 2 groups
// 4 uv
// 8 tangent
// 16 colors
// 32 joints
// 64 morph
// 128 vertex-animation
// ..
if ( attrib & 2 ) {
this.groups = [];
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;
}
} else {
this.groups = [];
}
if ( attrib & 4 ) {
this.uv = [];
var uvCount = data.readUByte();
for ( i = 0; i < uvCount; i ++ ) {
uvIDs[ i ] = data.readUByte();
}
}
if ( attrib & 32 ) {
jointID = data.readUByte();
weightID = data.readUByte();
}
var size = data.readUInt();
var bytes = data.concat( data.position, size );
var bstream = new o3dgc.BinaryStream( bytes.buffer );
var decoder = new o3dgc.SC3DMCDecoder();
var ifs = new o3dgc.IndexedFaceSet();
decoder.DecodeHeader( ifs, bstream );
var numIndexes = ifs.GetNCoordIndex();
var numVertex = ifs.GetNCoord();
if ( ! this.groups.length ) this.groups.push( { start: 0, count: numIndexes * 3 } );
this.indexes = this.isBig ? new Uint32Array( numIndexes * 3 ) : new Uint16Array( numIndexes * 3 );
this.vertex = new Float32Array( numVertex * 3 );
ifs.SetCoordIndex( this.indexes );
ifs.SetCoord( this.vertex );
if ( ifs.GetNNormal() > 0 ) {
this.normal = new Float32Array( numVertex * 3 );
ifs.SetNormal( this.normal );
}
for ( i = 0; i < uvIDs.length; i ++ ) {
this.uv[ i ] = new Float32Array( numVertex * 2 );
ifs.SetFloatAttribute( uvIDs[ i ], this.uv[ i ] );
}
if ( jointID !== undefined ) {
this.jointPerVertex = ifs.GetIntAttributeDim( jointID );
this.joint = new Uint16Array( numVertex * this.jointPerVertex );
this.weight = new Float32Array( numVertex * this.jointPerVertex );
ifs.SetIntAttribute( jointID, this.joint );
ifs.SetFloatAttribute( weightID, this.weight );
}
// decode mesh
decoder.DecodePlayload( ifs, bstream );
};
SEA3D.GeometryGC.prototype.type = "s3D";
//
// Extension
//
THREE.SEA3D.EXTENSIONS_LOADER.push( {
setTypeRead: function () {
this.file.addClass( SEA3D.GeometryGC, true );
this.file.typeRead[ SEA3D.GeometryGC.prototype.type ] = this.readGeometryBuffer;
}
} );

View File

@@ -0,0 +1,409 @@
/** _ _ _ _____ __ _______ ______
* | |___| |_| |__ /__ | | | | _ | * *
* | / _ \ _| | __\ | | \ | _ | U _
* |_\___/\__|_||_| _ |____/____ |__ \_|_ |_|_____|
*
* @author LoTh / http://3dflashlo.wordpress.com/
* @author SUNAG / http://www.sunag.com.br/
* @author Ammo.lab / https://github.com/lo-th/Ammo.lab/
*/
'use strict';
SEA3D.AMMO = {
world: null,
rigidBodies: [],
rigidBodiesTarget: [],
rigidBodiesEnabled: [],
constraints: [],
vehicles: [],
vehiclesWheels: [],
ACTIVE: 1,
ISLAND_SLEEPING: 2,
WANTS_DEACTIVATION: 3,
DISABLE_DEACTIVATION: 4,
DISABLE_SIMULATION: 5,
VERSION: 0.8,
init: function ( gravity, worldScale, broadphase ) {
gravity = gravity !== undefined ? gravity : - 90.8;
this.worldScale = worldScale == undefined ? 1 : worldScale;
this.broadphase = broadphase == undefined ? 'bvt' : broadphase;
this.solver = new Ammo.btSequentialImpulseConstraintSolver();
this.collisionConfig = new Ammo.btDefaultCollisionConfiguration();
this.dispatcher = new Ammo.btCollisionDispatcher( this.collisionConfig );
switch ( this.broadphase ) {
case 'bvt':
this.broadphase = new Ammo.btDbvtBroadphase();
break;
case 'sap':
this.broadphase = new Ammo.btAxisSweep3(
new Ammo.btVector3( - this.worldScale, - this.worldScale, - this.worldScale ),
new Ammo.btVector3( this.worldScale, this.worldScale, this.worldScale ),
4096
);
break;
case 'simple':
this.broadphase = new Ammo.btSimpleBroadphase();
break;
}
this.world = new Ammo.btDiscreteDynamicsWorld( this.dispatcher, this.broadphase, this.solver, this.collisionConfig );
this.setGravity( gravity );
console.log( "THREE.AMMO " + this.VERSION );
},
setGravity: function ( gravity ) {
this.gravity = gravity;
this.world.setGravity( new Ammo.btVector3( 0, gravity, 0 ) );
return this;
},
getGravity: function () {
return this.gravity;
},
setEnabledRigidBody: function ( rb, enabled ) {
var index = this.rigidBodies.indexOf( rb );
if ( this.rigidBodiesEnabled[ index ] == enabled ) return;
if ( enabled ) this.world.addRigidBody( rb );
else this.world.removeRigidBody( rb );
this.rigidBodiesEnabled[ index ] = true;
return this;
},
getEnabledRigidBody: function ( rb ) {
return this.rigidBodiesEnabled[ this.rigidBodies.indexOf( rb ) ];
},
addRigidBody: function ( rb, target, enabled ) {
enabled = enabled !== undefined ? enabled : true;
this.rigidBodies.push( rb );
this.rigidBodiesTarget.push( target );
this.rigidBodiesEnabled.push( false );
this.setEnabledRigidBody( rb, enabled );
return this;
},
removeRigidBody: function ( rb, destroy ) {
var index = this.rigidBodies.indexOf( rb );
this.setEnabledRigidBody( rb, false );
this.rigidBodies.splice( index, 1 );
this.rigidBodiesTarget.splice( index, 1 );
this.rigidBodiesEnabled.splice( index, 1 );
if ( destroy ) Ammo.destroy( rb );
return this;
},
containsRigidBody: function ( rb ) {
return this.rigidBodies.indexOf( rb ) > - 1;
},
addConstraint: function ( ctrt, disableCollisionsBetweenBodies ) {
disableCollisionsBetweenBodies = disableCollisionsBetweenBodies == undefined ? true : disableCollisionsBetweenBodies;
this.constraints.push( ctrt );
this.world.addConstraint( ctrt, disableCollisionsBetweenBodies );
return this;
},
removeConstraint: function ( ctrt, destroy ) {
this.constraints.splice( this.constraints.indexOf( ctrt ), 1 );
this.world.removeConstraint( ctrt );
if ( destroy ) Ammo.destroy( ctrt );
return this;
},
containsConstraint: function ( ctrt ) {
return this.constraints.indexOf( rb ) > - 1;
},
addVehicle: function ( vehicle, wheels ) {
this.vehicles.push( vehicle );
this.vehiclesWheels.push( wheels != undefined ? wheels : [] );
this.world.addAction( vehicle );
return this;
},
removeVehicle: function ( vehicle, destroy ) {
var index = this.vehicles.indexOf( vehicle );
this.vehicles.splice( index, 1 );
this.vehiclesWheels.splice( index, 1 );
this.world.removeAction( vehicle );
if ( destroy ) Ammo.destroy( vehicle );
return this;
},
containsVehicle: function ( vehicle ) {
return this.vehicles.indexOf( vehicle ) > - 1;
},
createTriangleMesh: function ( geometry, index, removeDuplicateVertices ) {
index = index == undefined ? - 1 : index;
removeDuplicateVertices = removeDuplicateVertices == undefined ? false : removeDuplicateVertices;
var mTriMesh = new Ammo.btTriangleMesh();
var v0 = new Ammo.btVector3( 0, 0, 0 );
var v1 = new Ammo.btVector3( 0, 0, 0 );
var v2 = new Ammo.btVector3( 0, 0, 0 );
var vertex = geometry.getAttribute( 'position' ).array;
var indexes = geometry.getIndex().array;
var group = index >= 0 ? geometry.groups[ index ] : undefined,
start = group ? group.start : 0,
count = group ? group.count : indexes.length;
var scale = 1 / this.worldScale;
for ( var idx = start; idx < count; idx += 3 ) {
var vx1 = indexes[ idx ] * 3,
vx2 = indexes[ idx + 1 ] * 3,
vx3 = indexes[ idx + 2 ] * 3;
v0.setValue( vertex[ vx1 ] * scale, vertex[ vx1 + 1 ] * scale, vertex[ vx1 + 2 ] * scale );
v1.setValue( vertex[ vx2 ] * scale, vertex[ vx2 + 1 ] * scale, vertex[ vx2 + 2 ] * scale );
v2.setValue( vertex[ vx3 ] * scale, vertex[ vx3 + 1 ] * scale, vertex[ vx3 + 2 ] * scale );
mTriMesh.addTriangle( v0, v1, v2, removeDuplicateVertices );
}
return mTriMesh;
},
createConvexHull: function ( geometry, index ) {
index = index == undefined ? - 1 : index;
var mConvexHull = new Ammo.btConvexHullShape();
var v0 = new Ammo.btVector3( 0, 0, 0 );
var vertex = geometry.getAttribute( 'position' ).array;
var indexes = geometry.getIndex().array;
var group = index >= 0 ? geometry.groups[ index ] : undefined,
start = group ? group.start : 0,
count = group ? group.count : indexes.length;
var scale = 1 / this.worldScale;
for ( var idx = start; idx < count; idx += 3 ) {
var vx1 = indexes[ idx ] * 3;
var point = new Ammo.btVector3(
vertex[ vx1 ] * scale, vertex[ vx1 + 1 ] * scale, vertex[ vx1 + 2 ] * scale
);
mConvexHull.addPoint( point );
}
return mConvexHull;
},
getTargetByRigidBody: function ( rb ) {
return this.rigidBodiesTarget[ this.rigidBodies.indexOf( rb ) ];
},
getRigidBodyByTarget: function ( target ) {
return this.rigidBodies[ this.rigidBodiesTarget.indexOf( target ) ];
},
getTransformFromMatrix: function ( mtx ) {
var transform = new Ammo.btTransform();
var pos = THREE.SEA3D.VECBUF.setFromMatrixPosition( mtx );
transform.setOrigin( new Ammo.btVector3( pos.x, pos.y, pos.z ) );
var scl = THREE.SEA3D.VECBUF.setFromMatrixScale( mtx );
mtx.scale( scl.set( 1 / scl.x, 1 / scl.y, 1 / scl.z ) );
var quat = new THREE.Quaternion().setFromRotationMatrix( mtx );
var q = new Ammo.btQuaternion();
q.setValue( quat.x, quat.y, quat.z, quat.w );
transform.setRotation( q );
Ammo.destroy( q );
return transform;
},
getMatrixFromTransform: function ( transform ) {
var position = new THREE.Vector3();
var quaternion = new THREE.Quaternion();
var scale = new THREE.Vector3( 1, 1, 1 );
return function ( transform, matrix ) {
matrix = matrix || new THREE.Matrix4();
var pos = transform.getOrigin(),
quat = transform.getRotation();
position.set( pos.x(), pos.y(), pos.z() );
quaternion.set( quat.x(), quat.y(), quat.z(), quat.w() );
matrix.compose( position, quaternion, scale );
return matrix;
};
}(),
updateTargetTransform: function () {
var matrix = new THREE.Matrix4();
var position = new THREE.Vector3();
var quaternion = new THREE.Quaternion();
var scale = new THREE.Vector3( 1, 1, 1 );
return function ( obj3d, transform, offset ) {
var pos = transform.getOrigin(),
quat = transform.getRotation();
if ( offset ) {
position.set( pos.x(), pos.y(), pos.z() );
quaternion.set( quat.x(), quat.y(), quat.z(), quat.w() );
matrix.compose( position, quaternion, scale );
matrix.multiplyMatrices( matrix, offset );
obj3d.position.setFromMatrixPosition( matrix );
obj3d.quaternion.setFromRotationMatrix( matrix );
} else {
obj3d.position.set( pos.x(), pos.y(), pos.z() );
obj3d.quaternion.set( quat.x(), quat.y(), quat.z(), quat.w() );
}
return this;
};
}(),
update: function ( delta, iteration, fixedDelta ) {
this.world.stepSimulation( delta, iteration || 0, fixedDelta || ( 60 / 1000 ) );
var i, j;
for ( i = 0; i < this.vehicles.length; i ++ ) {
var vehicle = this.vehicles[ i ],
numWheels = vehicle.getNumWheels(),
wheels = this.vehiclesWheels[ i ];
for ( j = 0; j < numWheels; j ++ ) {
vehicle.updateWheelTransform( j, true );
var wheelsTransform = vehicle.getWheelTransformWS( j ),
wheelTarget = wheels[ j ];
if ( wheelTarget ) {
this.updateTargetTransform( wheelTarget, wheelsTransform, wheelTarget.physics ? wheelTarget.physics.offset : null );
}
}
}
for ( i = 0; i < this.rigidBodies.length; i ++ ) {
var rb = this.rigidBodies[ i ],
target = this.rigidBodiesTarget[ i ];
if ( target && rb.isActive() ) {
this.updateTargetTransform( target, rb.getWorldTransform(), target.physics ? target.physics.offset : null );
}
}
return this;
}
};

View File

@@ -0,0 +1,590 @@
/**
* SEA3D+AMMO for Three.JS
* @author Sunag / http://www.sunag.com.br/
*/
'use strict';
THREE.SEA3D.prototype.toAmmoVec3 = function ( v ) {
return new Ammo.btVector3( v.x, v.y, v.z );
};
//
// Sphere
//
THREE.SEA3D.prototype.readSphere = function ( sea ) {
var shape = new Ammo.btSphereShape( sea.radius );
this.domain.shapes = this.shapes = this.shapes || [];
this.shapes.push( this.objects[ "shpe/" + sea.name ] = sea.tag = shape );
};
//
// Box
//
THREE.SEA3D.prototype.readBox = function ( sea ) {
var shape = new Ammo.btBoxShape( new Ammo.btVector3( sea.width * .5, sea.height * .5, sea.depth * .5 ) );
this.domain.shapes = this.shapes = this.shapes || [];
this.shapes.push( this.objects[ "shpe/" + sea.name ] = sea.tag = shape );
};
//
// Cone
//
THREE.SEA3D.prototype.readCone = function ( sea ) {
var shape = new Ammo.btConeShape( sea.radius, sea.height );
this.domain.shapes = this.shapes = this.shapes || [];
this.shapes.push( this.objects[ "shpe/" + sea.name ] = sea.tag = shape );
};
//
// Cylinder
//
THREE.SEA3D.prototype.readCylinder = function ( sea ) {
var shape = new Ammo.btCylinderShape( new Ammo.btVector3( sea.height, sea.radius, sea.radius ) );
this.domain.shapes = this.shapes = this.shapes || [];
this.shapes.push( this.objects[ "shpe/" + sea.name ] = sea.tag = shape );
};
//
// Capsule
//
THREE.SEA3D.prototype.readCapsule = function ( sea ) {
var shape = new Ammo.btCapsuleShape( sea.radius, sea.height );
this.domain.shapes = this.shapes = this.shapes || [];
this.shapes.push( this.objects[ "shpe/" + sea.name ] = sea.tag = shape );
};
//
// Convex Geometry
//
THREE.SEA3D.prototype.readConvexGeometry = function ( sea ) {
if ( this.config.convexHull ) {
var shape = SEA3D.AMMO.createConvexHull( sea.geometry.tag, sea.subGeometryIndex );
} else {
var triMesh = SEA3D.AMMO.createTriangleMesh( sea.geometry.tag, sea.subGeometryIndex );
var shape = new Ammo.btConvexTriangleMeshShape( triMesh, true );
}
this.domain.shapes = this.shapes = this.shapes || [];
this.shapes.push( this.objects[ "shpe/" + sea.name ] = sea.tag = shape );
};
//
// Triangle Geometry
//
THREE.SEA3D.prototype.readTriangleGeometry = function ( sea ) {
var triMesh = SEA3D.AMMO.createTriangleMesh( sea.geometry.tag, sea.subGeometryIndex );
var shape = new Ammo.btBvhTriangleMeshShape( triMesh, true, true );
this.domain.shapes = this.shapes = this.shapes || [];
this.shapes.push( this.objects[ "shpe/" + sea.name ] = sea.tag = shape );
};
//
// Compound
//
THREE.SEA3D.prototype.readCompound = function ( sea ) {
var shape = new Ammo.btCompoundShape();
for ( var i = 0; i < sea.compounds.length; i ++ ) {
var compound = sea.compounds[ i ];
THREE.SEA3D.MTXBUF.elements = compound.transform;
var transform = SEA3D.AMMO.getTransformFromMatrix( THREE.SEA3D.MTXBUF );
shape.addChildShape( transform, compound.shape.tag );
}
this.domain.shapes = this.shapes = this.shapes || [];
this.shapes.push( this.objects[ "shpe/" + sea.name ] = sea.tag = shape );
};
//
// Rigid Body Base
//
THREE.SEA3D.prototype.readRigidBodyBase = function ( sea ) {
var shape = sea.shape.tag,
transform, target;
if ( sea.target ) {
target = sea.target.tag;
target.physics = { enabled: true };
target.updateMatrix();
transform = SEA3D.AMMO.getTransformFromMatrix( sea.target.tag.matrix );
} else {
THREE.SEA3D.MTXBUF.elements.set( sea.transform );
transform = SEA3D.AMMO.getTransformFromMatrix( THREE.SEA3D.MTXBUF );
}
var motionState = new Ammo.btDefaultMotionState( transform );
var localInertia = new Ammo.btVector3( 0, 0, 0 );
shape.calculateLocalInertia( sea.mass, localInertia );
var info = new Ammo.btRigidBodyConstructionInfo( sea.mass, motionState, shape, localInertia );
info.set_m_friction( sea.friction );
info.set_m_restitution( sea.restitution );
info.set_m_linearDamping( sea.linearDamping );
info.set_m_angularDamping( sea.angularDamping );
var rb = new Ammo.btRigidBody( info );
if ( target ) {
target.physics.rigidBody = rb;
if ( sea.offset ) {
var offset = new THREE.Matrix4();
offset.elements.set( sea.offset );
target.physics.offset = offset;
}
}
Ammo.destroy( info );
this.domain.rigidBodies = this.rigidBodies = this.rigidBodies || [];
this.rigidBodies.push( this.objects[ "rb/" + sea.name ] = sea.tag = rb );
return rb;
};
//
// Rigid Body
//
THREE.SEA3D.prototype.readRigidBody = function ( sea ) {
var rb = this.readRigidBodyBase( sea );
SEA3D.AMMO.addRigidBody( rb, sea.target ? sea.target.tag : undefined, this.config.enabledPhysics );
};
//
// Car Controller
//
THREE.SEA3D.prototype.readCarController = function ( sea ) {
var body = this.readRigidBodyBase( sea );
body.setActivationState( SEA3D.AMMO.DISABLE_DEACTIVATION );
// Car
var vehicleRayCaster = new Ammo.btDefaultVehicleRaycaster( SEA3D.AMMO.world );
var tuning = new Ammo.btVehicleTuning();
tuning.set_m_suspensionStiffness( sea.suspensionStiffness );
tuning.set_m_suspensionDamping( sea.suspensionDamping );
tuning.set_m_suspensionCompression( sea.suspensionCompression );
tuning.set_m_maxSuspensionTravelCm( sea.maxSuspensionTravelCm );
tuning.set_m_maxSuspensionForce( sea.maxSuspensionForce );
tuning.set_m_frictionSlip( sea.frictionSlip );
var vehicle = new Ammo.btRaycastVehicle( tuning, body, vehicleRayCaster ),
wheels = [];
vehicle.setCoordinateSystem( 0, 1, 2 );
for ( var i = 0; i < sea.wheel.length; i ++ ) {
var wheel = sea.wheel[ i ];
var wheelInfo = vehicle.addWheel(
this.toAmmoVec3( wheel.pos ),
this.toAmmoVec3( wheel.dir ),
this.toAmmoVec3( wheel.axle ),
wheel.suspensionRestLength,
wheel.radius,
tuning,
wheel.isFront
);
var target = wheels[ i ] = wheel.target ? wheel.target.tag : undefined;
if ( target ) {
target.physics = { enabled: true, rigidBody: wheelInfo };
if ( wheel.offset ) {
var offset = new THREE.Matrix4();
offset.elements.set( wheel.offset );
target.physics.offset = offset;
}
if ( target.parent ) {
target.parent.remove( target );
}
if ( this.container ) {
this.container.add( target );
}
}
wheelInfo.set_m_suspensionStiffness( sea.suspensionStiffness );
wheelInfo.set_m_wheelsDampingRelaxation( sea.dampingRelaxation );
wheelInfo.set_m_wheelsDampingCompression( sea.dampingCompression );
wheelInfo.set_m_frictionSlip( sea.frictionSlip );
}
SEA3D.AMMO.addVehicle( vehicle, wheels );
SEA3D.AMMO.addRigidBody( body, sea.target ? sea.target.tag : undefined, this.config.enabledPhysics );
this.domain.vehicles = this.vehicles = this.vehicles || [];
this.vehicles.push( this.objects[ "vhc/" + sea.name ] = sea.tag = vehicle );
};
//
// P2P Constraint
//
THREE.SEA3D.prototype.readP2PConstraint = function ( sea ) {
var ctrt;
if ( sea.targetB ) {
ctrt = new Ammo.btPoint2PointConstraint(
sea.targetA.tag,
sea.targetB.tag,
this.toAmmoVec3( sea.pointA ),
this.toAmmoVec3( sea.pointB )
);
} else {
ctrt = new Ammo.btPoint2PointConstraint(
sea.targetA.tag,
this.toAmmoVec3( sea.pointA )
);
}
SEA3D.AMMO.addConstraint( ctrt );
this.domain.constraints = this.constraints = this.constraints || [];
this.constraints.push( this.objects[ "ctnt/" + sea.name ] = sea.tag = ctrt );
};
//
// Hinge Constraint
//
THREE.SEA3D.prototype.readHingeConstraint = function ( sea ) {
var ctrt;
if ( sea.targetB ) {
ctrt = new Ammo.btHingeConstraint(
sea.targetA.tag,
sea.targetB.tag,
this.toAmmoVec3( sea.pointA ),
this.toAmmoVec3( sea.pointB ),
this.toAmmoVec3( sea.axisA ),
this.toAmmoVec3( sea.axisB ),
false
);
} else {
ctrt = new Ammo.btHingeConstraint(
sea.targetA.tag,
this.toAmmoVec3( sea.pointA ),
this.toAmmoVec3( sea.axisA ),
false
);
}
if ( sea.limit ) {
ctrt.setLimit( sea.limit.low, sea.limit.high, sea.limit.softness, sea.limit.biasFactor, sea.limit.relaxationFactor );
}
if ( sea.angularMotor ) {
ctrt.enableAngularMotor( true, sea.angularMotor.velocity, sea.angularMotor.impulse );
}
SEA3D.AMMO.addConstraint( ctrt );
this.domain.constraints = this.constraints = this.constraints || [];
this.constraints.push( this.objects[ "ctnt/" + sea.name ] = sea.tag = ctrt );
};
//
// Cone Twist Constraint
//
THREE.SEA3D.prototype.readConeTwistConstraint = function ( sea ) {
var ctrt;
if ( sea.targetB ) {
ctrt = new Ammo.btConeTwistConstraint(
sea.targetA.tag,
sea.targetB.tag,
this.toAmmoVec3( sea.pointA ),
this.toAmmoVec3( sea.pointB ),
false
);
} else {
ctrt = new Ammo.btConeTwistConstraint(
sea.targetA.tag,
this.toAmmoVec3( sea.pointA ),
false
);
}
SEA3D.AMMO.addConstraint( ctrt );
this.domain.constraints = this.constraints = this.constraints || [];
this.constraints.push( this.objects[ "ctnt/" + sea.name ] = sea.tag = ctrt );
};
//
// Domain
//
THREE.SEA3D.Domain.prototype.enabledPhysics = function ( enabled ) {
var i = this.rigidBodies ? this.rigidBodies.length : 0;
while ( i -- ) {
SEA3D.AMMO.setEnabledRigidBody( this.rigidBodies[ i ], enabled );
}
};
THREE.SEA3D.Domain.prototype.applyContainerTransform = function () {
this.container.updateMatrix();
var matrix = this.container.matrix.clone();
this.container.position.set( 0, 0, 0 );
this.container.rotation.set( 0, 0, 0 );
this.container.scale.set( 1, 1, 1 );
this.applyTransform( matrix );
};
THREE.SEA3D.Domain.prototype.applyTransform = function ( matrix ) {
var mtx = THREE.SEA3D.MTXBUF, vec = THREE.SEA3D.VECBUF;
var i = this.rigidBodies ? this.rigidBodies.length : 0,
childs = this.container ? this.container.children : [],
targets = [];
while ( i -- ) {
var rb = this.rigidBodies[ i ],
target = SEA3D.AMMO.getTargetByRigidBody( rb ),
transform = rb.getWorldTransform(),
transformMatrix = SEA3D.AMMO.getMatrixFromTransform( transform );
transformMatrix.multiplyMatrices( transformMatrix, matrix );
transform = SEA3D.AMMO.getTransformFromMatrix( transformMatrix );
rb.setWorldTransform( transform );
if ( target ) targets.push( target );
}
for ( i = 0; i < childs.length; i ++ ) {
var obj3d = childs[ i ];
if ( targets.indexOf( obj3d ) > - 1 ) continue;
obj3d.updateMatrix();
mtx.copy( obj3d.matrix );
mtx.multiplyMatrices( matrix, mtx );
obj3d.position.setFromMatrixPosition( mtx );
obj3d.scale.setFromMatrixScale( mtx );
// ignore rotation scale
mtx.scale( vec.set( 1 / obj3d.scale.x, 1 / obj3d.scale.y, 1 / obj3d.scale.z ) );
obj3d.rotation.setFromRotationMatrix( mtx );
}
};
//
// Extension
//
THREE.SEA3D.Domain.prototype.getShape = THREE.SEA3D.prototype.getShape = function ( name ) {
return this.objects[ "shpe/" + name ];
};
THREE.SEA3D.Domain.prototype.getRigidBody = THREE.SEA3D.prototype.getRigidBody = function ( name ) {
return this.objects[ "rb/" + name ];
};
THREE.SEA3D.Domain.prototype.getConstraint = THREE.SEA3D.prototype.getConstraint = function ( name ) {
return this.objects[ "ctnt/" + name ];
};
THREE.SEA3D.EXTENSIONS_LOADER.push( {
parse: function () {
delete this.shapes;
delete this.rigidBodies;
delete this.vehicles;
delete this.constraints;
},
setTypeRead: function () {
// CONFIG
this.config.physics = this.config.physics !== undefined ? this.config.physics : true;
this.config.convexHull = this.config.convexHull !== undefined ? this.config.convexHull : true;
this.config.enabledPhysics = this.config.enabledPhysics !== undefined ? this.config.enabledPhysics : true;
if ( this.config.physics ) {
// SHAPES
this.file.typeRead[ SEA3D.Sphere.prototype.type ] = this.readSphere;
this.file.typeRead[ SEA3D.Box.prototype.type ] = this.readBox;
this.file.typeRead[ SEA3D.Capsule.prototype.type ] = this.readCapsule;
this.file.typeRead[ SEA3D.Cone.prototype.type ] = this.readCone;
this.file.typeRead[ SEA3D.Cylinder.prototype.type ] = this.readCylinder;
this.file.typeRead[ SEA3D.ConvexGeometry.prototype.type ] = this.readConvexGeometry;
this.file.typeRead[ SEA3D.TriangleGeometry.prototype.type ] = this.readTriangleGeometry;
this.file.typeRead[ SEA3D.Compound.prototype.type ] = this.readCompound;
// CONSTRAINTS
this.file.typeRead[ SEA3D.P2PConstraint.prototype.type ] = this.readP2PConstraint;
this.file.typeRead[ SEA3D.HingeConstraint.prototype.type ] = this.readHingeConstraint;
this.file.typeRead[ SEA3D.ConeTwistConstraint.prototype.type ] = this.readConeTwistConstraint;
// PHYSICS
this.file.typeRead[ SEA3D.RigidBody.prototype.type ] = this.readRigidBody;
this.file.typeRead[ SEA3D.CarController.prototype.type ] = this.readCarController;
}
}
} );
THREE.SEA3D.EXTENSIONS_DOMAIN.push( {
dispose: function () {
var i;
i = this.rigidBodies ? this.rigidBodies.length : 0;
while ( i -- ) SEA3D.AMMO.removeRigidBody( this.rigidBodies[ i ], true );
i = this.vehicles ? this.vehicles.length : 0;
while ( i -- ) SEA3D.AMMO.removeVehicle( this.vehicles[ i ], true );
i = this.constraints ? this.constraints.length : 0;
while ( i -- ) SEA3D.AMMO.removeConstraint( this.constraints[ i ], true );
i = this.shapes ? this.shapes.length : 0;
while ( i -- ) Ammo.destroy( this.shapes[ i ] );
}
} );

View File

@@ -0,0 +1,442 @@
/**
* SEA3D - Rigid Body
* @author Sunag / http://www.sunag.com.br/
*/
'use strict';
//
// Sphere
//
SEA3D.Sphere = function ( name, data, sea3d ) {
this.name = name;
this.data = data;
this.sea3d = sea3d;
this.radius = data.readFloat();
};
SEA3D.Sphere.prototype.type = "sph";
//
// Box
//
SEA3D.Box = function ( name, data, sea3d ) {
this.name = name;
this.data = data;
this.sea3d = sea3d;
this.width = data.readFloat();
this.height = data.readFloat();
this.depth = data.readFloat();
};
SEA3D.Box.prototype.type = "box";
//
// Cone
//
SEA3D.Cone = function ( name, data, sea3d ) {
this.name = name;
this.data = data;
this.sea3d = sea3d;
this.radius = data.readFloat();
this.height = data.readFloat();
};
SEA3D.Cone.prototype.type = "cone";
//
// Capsule
//
SEA3D.Capsule = function ( name, data, sea3d ) {
this.name = name;
this.data = data;
this.sea3d = sea3d;
this.radius = data.readFloat();
this.height = data.readFloat();
};
SEA3D.Capsule.prototype.type = "cap";
//
// Cylinder
//
SEA3D.Cylinder = function ( name, data, sea3d ) {
this.name = name;
this.data = data;
this.sea3d = sea3d;
this.radius = data.readFloat();
this.height = data.readFloat();
};
SEA3D.Cylinder.prototype.type = "cyl";
//
// Convex Geometry
//
SEA3D.ConvexGeometry = function ( name, data, sea3d ) {
this.name = name;
this.data = data;
this.sea3d = sea3d;
this.geometry = sea3d.getObject( data.readUInt() );
this.subGeometryIndex = data.readUByte();
};
SEA3D.ConvexGeometry.prototype.type = "gs";
//
// Triangle Geometry
//
SEA3D.TriangleGeometry = function ( name, data, sea3d ) {
this.name = name;
this.data = data;
this.sea3d = sea3d;
this.geometry = sea3d.getObject( data.readUInt() );
this.subGeometryIndex = data.readUByte();
};
SEA3D.TriangleGeometry.prototype.type = "sgs";
//
// Compound
//
SEA3D.Compound = function ( name, data, sea3d ) {
this.name = name;
this.data = data;
this.sea3d = sea3d;
this.compounds = [];
var count = data.readUByte();
for ( var i = 0; i < count; i ++ ) {
this.compounds.push( {
shape: sea3d.getObject( data.readUInt() ),
transform: data.readMatrix()
} );
}
};
SEA3D.Compound.prototype.type = "cmps";
//
// Physics
//
SEA3D.Physics = function ( name, data, sea3d ) {
this.name = name;
this.data = data;
this.sea3d = sea3d;
this.attrib = data.readUShort();
this.shape = sea3d.getObject( data.readUInt() );
if ( this.attrib & 1 ) this.target = sea3d.getObject( data.readUInt() );
else this.transform = data.readMatrix();
if ( this.attrib & 2 ) this.offset = data.readMatrix();
if ( this.attrib & 4 ) this.scripts = data.readScriptList( sea3d );
if ( this.attrib & 16 ) this.attributes = sea3d.getObject( data.readUInt() );
};
SEA3D.Physics.prototype.readTag = function ( kind, data, size ) {
};
//
// Rigidy Body Base
//
SEA3D.RigidBodyBase = function ( name, data, sea3d ) {
SEA3D.Physics.call( this, name, data, sea3d );
if ( this.attrib & 32 ) {
this.linearDamping = data.readFloat();
this.angularDamping = data.readFloat();
} else {
this.linearDamping = 0;
this.angularDamping = 0;
}
this.mass = data.readFloat();
this.friction = data.readFloat();
this.restitution = data.readFloat();
};
SEA3D.RigidBodyBase.prototype = Object.create( SEA3D.Physics.prototype );
SEA3D.RigidBodyBase.prototype.constructor = SEA3D.RigidBodyBase;
//
// Rigidy Body
//
SEA3D.RigidBody = function ( name, data, sea3d ) {
SEA3D.RigidBodyBase.call( this, name, data, sea3d );
data.readTags( this.readTag.bind( this ) );
};
SEA3D.RigidBody.prototype = Object.create( SEA3D.RigidBodyBase.prototype );
SEA3D.RigidBody.prototype.constructor = SEA3D.RigidBody;
SEA3D.RigidBody.prototype.type = "rb";
//
// Car Controller
//
SEA3D.CarController = function ( name, data, sea3d ) {
SEA3D.RigidBodyBase.call( this, name, data, sea3d );
this.suspensionStiffness = data.readFloat();
this.suspensionCompression = data.readFloat();
this.suspensionDamping = data.readFloat();
this.maxSuspensionTravelCm = data.readFloat();
this.frictionSlip = data.readFloat();
this.maxSuspensionForce = data.readFloat();
this.dampingCompression = data.readFloat();
this.dampingRelaxation = data.readFloat();
var count = data.readUByte();
this.wheel = [];
for ( var i = 0; i < count; i ++ ) {
this.wheel[ i ] = new SEA3D.CarController.Wheel( data, sea3d );
}
data.readTags( this.readTag.bind( this ) );
};
SEA3D.CarController.Wheel = function ( data, sea3d ) {
this.data = data;
this.sea3d = sea3d;
this.attrib = data.readUShort();
this.isFront = ( this.attrib & 1 ) != 0;
if ( this.attrib & 2 ) {
this.target = sea3d.getObject( data.readUInt() );
}
if ( this.attrib & 4 ) {
this.offset = data.readMatrix();
}
this.pos = data.readVector3();
this.dir = data.readVector3();
this.axle = data.readVector3();
this.radius = data.readFloat();
this.suspensionRestLength = data.readFloat();
};
SEA3D.CarController.prototype = Object.create( SEA3D.RigidBodyBase.prototype );
SEA3D.CarController.prototype.constructor = SEA3D.CarController;
SEA3D.CarController.prototype.type = "carc";
//
// Constraints
//
SEA3D.Constraints = function ( name, data, sea3d ) {
this.name = name;
this.data = data;
this.sea3d = sea3d;
this.attrib = data.readUShort();
this.disableCollisionsBetweenBodies = this.attrib & 1 != 0;
this.targetA = sea3d.getObject( data.readUInt() );
this.pointA = data.readVector3();
if ( this.attrib & 2 ) {
this.targetB = sea3d.getObject( data.readUInt() );
this.pointB = data.readVector3();
}
};
//
// P2P Constraint
//
SEA3D.P2PConstraint = function ( name, data, sea3d ) {
this.name = name;
this.data = data;
this.sea3d = sea3d;
SEA3D.Constraints.call( this, name, data, sea3d );
};
SEA3D.P2PConstraint.prototype = Object.create( SEA3D.Constraints.prototype );
SEA3D.P2PConstraint.prototype.constructor = SEA3D.P2PConstraint;
SEA3D.P2PConstraint.prototype.type = "p2pc";
//
// Hinge Constraint
//
SEA3D.HingeConstraint = function ( name, data, sea3d ) {
SEA3D.Constraints.call( this, name, data, sea3d );
this.axisA = data.readVector3();
if ( this.attrib & 1 ) {
this.axisB = data.readVector3();
}
if ( this.attrib & 4 ) {
this.limit = {
low: data.readFloat(),
high: data.readFloat(),
softness: data.readFloat(),
biasFactor: data.readFloat(),
relaxationFactor: data.readFloat()
};
}
if ( this.attrib & 8 ) {
this.angularMotor = {
velocity: data.readFloat(),
impulse: data.readFloat()
};
}
};
SEA3D.HingeConstraint.prototype = Object.create( SEA3D.Constraints.prototype );
SEA3D.HingeConstraint.prototype.constructor = SEA3D.HingeConstraint;
SEA3D.HingeConstraint.prototype.type = "hnec";
//
// Cone Twist Constraint
//
SEA3D.ConeTwistConstraint = function ( name, data, sea3d ) {
SEA3D.Constraints.call( this, name, data, sea3d );
this.axisA = data.readVector3();
if ( this.attrib & 1 ) {
this.axisB = data.readVector3();
}
if ( this.attrib & 4 ) {
this.limit = {
swingSpan1: data.readFloat(),
swingSpan2: data.readFloat(),
twistSpan: data.readFloat(),
softness: data.readFloat(),
biasFactor: data.readFloat(),
relaxationFactor: data.readFloat()
};
}
};
SEA3D.ConeTwistConstraint.prototype = Object.create( SEA3D.Constraints.prototype );
SEA3D.ConeTwistConstraint.prototype.constructor = SEA3D.ConeTwistConstraint;
SEA3D.ConeTwistConstraint.prototype.type = "ctwc";
//
// Extension
//
SEA3D.File.setExtension( function () {
// PHYSICS
this.addClass( SEA3D.Sphere );
this.addClass( SEA3D.Box );
this.addClass( SEA3D.Cone );
this.addClass( SEA3D.Capsule );
this.addClass( SEA3D.Cylinder );
this.addClass( SEA3D.ConvexGeometry );
this.addClass( SEA3D.TriangleGeometry );
this.addClass( SEA3D.Compound );
this.addClass( SEA3D.RigidBody );
this.addClass( SEA3D.P2PConstraint );
this.addClass( SEA3D.HingeConstraint );
this.addClass( SEA3D.ConeTwistConstraint );
this.addClass( SEA3D.CarController );
} );