/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/li3m
*/
package {
import flash.text.TextField;
import flash.geom.Vector3D;
import flash.geom.Matrix3D;
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
mw = 32; mh = 32;
cw = 16; ch = 16;
var num:int; var i:int;
num = mw * mh;
cx = mw *0.5 * cw *-1;
cy = mh * 0.5*ch *-1;
vecHeight = new Vector.<Number>(num, false);
for (i = 0; i < num; i++) { vecHeight[i] = Math.random()*4;}
setRect(128,256-120, 300,500, 10);
setRect(128,256, 200,200, 40);
setRect(128,256, 120,120, 70);
var k:int;
for (i = 0; i < 16; i++) for (k = 0; k < 16; k++)
setHeight(k+0, i+0, -80);
deb = new TextField();
deb.width = 320;
deb.height = 240;
deb.mouseEnabled = false;
addChild(deb);
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public var gt:int = 0;
public var mw:int = 0;
public var mh:int = 0;
public var cw:Number = 16;
public var ch:Number = 16;
public var cx:Number = 0;
public var cy:Number = 0;
public var vecHeight:Vector.<Number>;
public var mat:Matrix3D = new Matrix3D();
public var deb:TextField;
public var px:Number = 0;
public var py:Number = 0;
public function projectPoint(vec:Vector.<Number>, ax:Number, ay:Number, az:Number):void
{
ay *=-1;//this is just because i wanted -Y to be down
px = vec[0] * ax + vec[4] * ay + vec[8] * az + vec[12];
py = vec[1] * ax + vec[5] * ay + vec[9] * az + vec[13];
}//project
public function setRect(ax:Number, ay:Number, aw:Number, ah:Number, h:Number):void
{
//warning -- unoptimised
var tx:int; var ty:int;
var tw:int; var th:int;
tx = ax / cw; ty = ay/ch;
tw = aw / cw; th = ah/ch;
var i:int; var k:int;
for (i = 0; i < th; i++) for (k = 0; k < tw; k++)
{ setHeight(k+tx, i+ty, h); }
}//setrect
public function setHeight(tx:int, ty:int, h:Number):void
{
if (tx < 0) { return; } if (ty < 0) { return; }
if (tx >= mw) { return; } if (ty >= mh) { return; }
vecHeight[(ty*mw)+tx] = h;
}//setheight
public function getHeight(ax:Number, ay:Number):Number
{
var tx:int; var ty:int; var yt:int;
ax -= cx; ay -=cy; //transform by heightmap coords first
tx = Math.floor(ax/cw); ty = Math.floor(ay/ch);
var h0:Number; var h1:Number;
var z0:Number; var z1:Number;
var u:Number; var v:Number;
if (tx < 0) { return 0; } if (ty < 0) { return 0; }
if (tx + 1 >= mw) { return 0; } if (ty + 1 >= mh) { return 0; }
yt = ty * mw;
u = (ax/cw)-tx; v = (ay/ch)-ty;
z0 = vecHeight[yt+tx]; z1 = vecHeight[yt+tx+1];
h0 = z0 + (z1-z0)*u;
z0 = vecHeight[yt+tx+mw]; z1 = vecHeight[yt+tx+mw+1];
h1 = z0 + (z1 -z0)*u;
return (h0 +(h1-h0) * v);
}//getheight
public function onEnter(e:Event):void
{
graphics.clear();
graphics.lineStyle(2,0);
var i:int; var k:int; var yt:int;
var rx:Number; var ry:Number; var rz:Number;
var vec:Vector.<Number>;
var sx:Number; var sy:Number;
var ax:Number; var ay:Number; var az:Number;
mat.identity();
mat.appendRotation(30, Vector3D.Y_AXIS);
mat.appendRotation(30, Vector3D.X_AXIS);
mat.appendTranslation(225, 225, 0);
vec = mat.rawData;
graphics.lineStyle(3, 0, 0.5);
//graphics.lineStyle();
for (i = 0; i < mh; i++)
{
yt = i * mw;
for (k = 0; k < mw; k++)
{
//graphics.drawCircle(k*cw,i*ch, 8);
ax = k * cw + cx;
ay = vecHeight[yt+k] *-1;
az = i * ch + cy;
sx = vec[0] * ax + vec[4] * ay + vec[8] * az + vec[12];
sy = vec[1] * ax + vec[5] * ay + vec[9] * az + vec[13];
//graphics.drawCircle(sx, sy, 4);
if (k == 0) { graphics.moveTo(sx,sy);}
else { graphics.lineTo(sx,sy);}
//graphics.moveTo(sx,sy+8);
// graphics.lineTo(sx,sy);
// graphics.beginFill(0, 0.3);
// graphics.drawRect(sx-8,sy-8,16,16);
// graphics.endFill();
}//nextk
}//nexti
graphics.lineStyle(2,0);
// ax = (gt*4) % 400 - 200;
// az = -75;
ax = Math.cos(gt*0.03) * 128;
az = Math.sin(gt*0.03) * 128;
ay = getHeight(ax,az);
deb.text = "h " + ay;
setHeight(((ax-cx)/cw)|0,((az-cy)/ch)|0, ay-8);
projectPoint(vec, ax, ay, az);
// graphics.drawCircle(px, py, 8);
graphics.moveTo(px,py);
ay = 128;
projectPoint(vec, ax, ay, az);
graphics.lineTo(px,py);
graphics.beginFill(0,1);
graphics.drawCircle(px, py, 8);
graphics.endFill();
gt += 1;
}//onenter
}//classend
}