Difference between revisions of "3D Space Example.as"
(Been a damn typo there for ages :-() |
(Try z-sort, then depth-test) |
||
Line 23: | Line 23: | ||
− | function byDepth( a, b ) { return a.getDepth() | + | function byDepth( a, b ) { return a.getDepth() > b.getDepth(); } |
+ | function byZ( a, b ) { return a.az > b.az; } | ||
// Per frame | // Per frame | ||
Line 32: | Line 33: | ||
// for ( var i = 0; i < allSpaces.length; i++ ) | // for ( var i = 0; i < allSpaces.length; i++ ) | ||
// if ( allSpaces[i].active ) allSpaces[i].frame(); | // if ( allSpaces[i].active ) allSpaces[i].frame(); | ||
− | |||
− | |||
// testing z-ordering algorithm | // testing z-ordering algorithm | ||
Line 43: | Line 42: | ||
obj._x = width/2+Math.sin(b)*width/3; | obj._x = width/2+Math.sin(b)*width/3; | ||
obj.az = obj._xscale = obj._yscale = 50+Math.cos(b)*25; | obj.az = obj._xscale = obj._yscale = 50+Math.cos(b)*25; | ||
+ | obj.az = -obj.az; | ||
obj._y = height/2; | obj._y = height/2; | ||
} | } | ||
+ | |||
+ | balls.sort( byZ ); | ||
msg = "<font face='courier New' size='12pt' color='#00ff00'>"; | msg = "<font face='courier New' size='12pt' color='#00ff00'>"; | ||
Line 54: | Line 56: | ||
for ( i = 0; i < balls.length-1; i++ ) { | for ( i = 0; i < balls.length-1; i++ ) { | ||
obj = balls[i]; | obj = balls[i]; | ||
− | + | max = obj.getDepth(); | |
maxp = i; | maxp = i; | ||
for ( j = i + 1; j < balls.length; j++ ) { | for ( j = i + 1; j < balls.length; j++ ) { | ||
− | if ( balls[j]. | + | if ( balls[j].getDepth() > max ) { |
− | + | max = balls[j].getDepth(); | |
maxp = j; | maxp = j; | ||
} | } |
Revision as of 10:25, 20 March 2006
- include "Space.as"
// Instantiate a new 3D-space //createEmptyMovieClip( 'test', 1 ); //initialiseSpace( test ); //test._x = width/2; //test._y = height/2;
// testing z-ordering algorithm var balls = []; for ( var i = 0; i < 5; i++ ) { attachMovie( 'ball', 'ball'+i, i ); o = balls[i] = _root['ball'+i]; j = i + 1; r = (j & 1) ? 0xff : 0; g = (j & 2) ? 0xff : 0; b = (j & 4) ? 0xff : 0; c = new Color(o); c.setTransform( { ra:r, ga:g, ba:b } ); }
function byDepth( a, b ) { return a.getDepth() > b.getDepth(); }
function byZ( a, b ) { return a.az > b.az; }
// Per frame function reduce() { now = new Date(); time = now.getTime(); //if (_root.ctr++ < 2) // for ( var i = 0; i < allSpaces.length; i++ ) // if ( allSpaces[i].active ) allSpaces[i].frame();
// testing z-ordering algorithm a += 0.01; c = 2*Math.PI/5; for ( i = 0; i < balls.length; i++ ) { obj = balls[i]; b = a + c * i; obj._x = width/2+Math.sin(b)*width/3; obj.az = obj._xscale = obj._yscale = 50+Math.cos(b)*25; obj.az = -obj.az; obj._y = height/2; }
balls.sort( byZ );
msg = ""; for ( i = 0; i < balls.length; i++ )
msg = msg + i + ': '+balls[i].getDepth()+' : '+balls[i]._name+'
';
debug.htmlText = msg + '';
// Sort buffer by Z and synchronise rendering levels for ( i = 0; i < balls.length-1; i++ ) { obj = balls[i]; max = obj.getDepth(); maxp = i; for ( j = i + 1; j < balls.length; j++ ) { if ( balls[j].getDepth() > max ) { max = balls[j].getDepth(); maxp = j; } } if ( maxp > i ) { obj.swapDepths( balls[maxp] ); balls[i] = balls[maxp]; balls[maxp] = obj; } }
}
createTextField( 'debug', 100, 0, 0, width, height ); with ( debug ) { selectable = true; border = false; multiline = true; wordWrap = true; html = true; }
// Test object //test.create( [ 0, 0, , 'torus', 5, 200, 5000 ] );