/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/fu8Z
*/
package {
import flash.display.BitmapData;
import flash.display.StageQuality;
import flash.geom.Vector3D;
import flash.geom.Matrix3D;
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
stage.quality = StageQuality.LOW;
var dat:BitmapData;
dat = new BitmapData(17,17, false, 0);
dat.perlinNoise(31,32, 14,15, true,true);
myHeight = new xHeightMap();
myHeight.initHeight(dat, 2, -128);
myHeight.cx = -8*myHeight.cw;
myHeight.cz = -8*myHeight.ch;
myHeight.initMap(16,16);
vecBall = new Vector.<xBall>(0,false);
var i:int; var a:xBall;
for (i = 0; i < 18;i++)
{
a = new xBall();
a.cx = Math.random()*400-200;
a.cz = Math.random()*400-200;
a.cy = myHeight.getHeight(a.cx,a.cz)-8;
vecBall.push(a);
}//nexti
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public var temp:Matrix3D = new Matrix3D();
public var vecBall:Vector.<xBall>;
public var gt:int = 0;
public function onEnter(e:Event):void
{
graphics.clear();
graphics.beginFill(0x312370,1);
graphics.drawRect(0,0,465,465);
graphics.endFill();
graphics.lineStyle(1,0);
gt += 1;
temp.identity();
temp.appendScale(1, 1, 1);
temp.appendRotation(45+gt*0.5, Vector3D.Y_AXIS);
temp.appendRotation(-30, Vector3D.X_AXIS);
myMat = temp.rawData;
drawMesh(myHeight.vecFace, myHeight.vecVert, myMat);
var i:int; var num:int; var a:xBall;
num = vecBall.length;
for (i = 0; i < num;i++)
{
a = vecBall[i];
drawSprite(a.cx,a.cy,a.cz, 8, myMat);
drawSprite(a.cx,a.cy-8,a.cz, 8, myMat);
drawSprite(a.cx,a.cy-16,a.cz, 8, myMat);
drawSprite(a.cx,a.cy-32,a.cz, 16, myMat,0x219412);
}//nexti
}//onenter
public var myHeight:xHeightMap;
public var myMat:Vector.<Number> = Vector.<Number>([
1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
public var scrw:Number = 235;
public var scrh:Number = 235;
public function drawSprite(ax:Number, ay:Number, az:Number, rad:Number, mat:Vector.<Number>, c:uint=0x802343):void
{
var sx0:Number; var sy0:Number;
sx0 = ax * mat[0] + ay * mat[4] + az * mat[8] + mat[12];
sy0 = ax * mat[1] + ay * mat[5] + az * mat[9] + mat[13];
graphics.beginFill(c,0.5);
graphics.drawCircle(sx0+scrw,sy0+scrh, rad);
graphics.endFill();
}//drawsprite
public function drawMesh(vf:Vector.<int>, vec:Vector.<Number>, mat:Vector.<Number>):void
{
var rx:Number; var ry:Number; var rz:Number;
var sx0:Number; var sy0:Number;
var sx1:Number; var sy1:Number;
var sx2:Number; var sy2:Number;
var wind:Number;
var i:int; var num:int;
var k:int;
num = vf.length;
for (k = 0; k < num; k += 3)
{
i = vf[k] * 3;
rx = vec[i]; ry = vec[i+1]; rz = vec[i+2];
sx0 = rx * mat[0] + ry * mat[4] + rz * mat[8] + mat[12];
sy0 = rx * mat[1] + ry * mat[5] + rz * mat[9] + mat[13];
//sz = a.cx * mat[2] + a.cy * mat[6] + a.cz * mat[10] + mat[14];
// w = a.cx * mat[3] + a.cy * mat[7] + a.cz * mat[11] + mat[15];
i = vf[k+1] * 3;
rx = vec[i]; ry = vec[i+1]; rz = vec[i+2];
sx1 = rx * mat[0] + ry * mat[4] + rz * mat[8] + mat[12];
sy1 = rx * mat[1] + ry * mat[5] + rz * mat[9] + mat[13];
i = vf[k+2] * 3;
rx = vec[i]; ry = vec[i+1]; rz = vec[i+2];
sx2 = rx * mat[0] + ry * mat[4] + rz * mat[8] + mat[12];
sy2 = rx * mat[1] + ry * mat[5] + rz * mat[9] + mat[13];
//backface cull -> check winding of verts
wind = (sx1 - sx0) * (sy2 - sy0) - (sx2 - sx0) * (sy1 - sy0);
if (wind > 0) { continue; }
graphics.beginFill(i+0x004513, 0.75);
graphics.moveTo(sx0+scrw, sy0+scrh);
graphics.lineTo(sx1+scrw, sy1+scrh);
graphics.lineTo(sx2+scrw, sy2+scrh);
graphics.endFill();
}//nexti
}//drawmesh2
}//classend
}
import flash.display.BitmapData;
import flash.display.Graphics;
internal class xBall
{
public var cx:Number = 0;
public var cy:Number = 0;
public var cz:Number = 0;
public var vx:Number = 0;
public var vy:Number = 0;
public var vz:Number = 0;
}//xball
internal class xHeightMap
{
public var vecHeight:Vector.<Number>;
public var vecVert:Vector.<Number>;
public var vecFace:Vector.<int>;
//face grid size
public var mw:int = 0;
public var mh:int = 0;
//height grid size
public var vmw:int = 0;
public var vmh:int = 0;
public var cw:Number = 32;
public var ch:Number = 32;
public var cx:Number = 0;
//public var cy:Number = 0;
public var cz:Number = 0;
public function initHeight(bdat:BitmapData, sy:Number=1, ay:Number=0):void
{
var i:int; var k:int;
var w:int; var h:int;
var yt:int;
var num:int;
w = bdat.width; h = bdat.height;
num = w * h;
vecHeight = new Vector.<Number>(num,false);
for (i = 0; i < h; i++)
{
yt = i*w;
for (k = 0; k < w; k++)
{
vecHeight[k+yt] = (bdat.getPixel(k,i) & 0xFF) * sy + ay;
}//nextk
}//nexti
}//initheight
//note -- w and h is for the number of quads
public function initMap(w:int, h:int):void
{
mw = w; mh = h;
vmw = w+1; vmh = h+1;
var i:int; var k:int; var num:int;
var w:int; var yt:int;
num = mw*mh;
vecFace = new Vector.<int>(num,false);
w = 0;
for (i = 0; i < mh; i++)
{
yt = i*(mw+1);
for (k = 0; k < mw; k++)
{
vecFace[w] = 0 + k + yt; //0
vecFace[w+1] = 0 + k + (mw+1) + yt; //1
vecFace[w+2] = 1 + k + yt; //2
vecFace[w+3] = 1 + k + yt; //2
vecFace[w+4] = 0 + k + (mw+1) + yt; //1
vecFace[w+5] = 1 + k + (mw+1) + yt; //3
w+=6;
}//nextk
}//nexti
num = (mw+1)*(mh+1);
if (vecHeight == null)
{
vecHeight = new Vector.<Number>(num,false);
for(i= 0;i <num;i++){vecHeight[i]=Math.random()*3;}
}
vecVert = new Vector.<Number>(num*3,false);
w = 0;
for (i = 0; i < (mh+1); i++)
{
yt = i*(mw+1);
for (k = 0; k < (mw+1); k++)
{
vecVert[w] = k*cw + cx;
vecVert[w+1] = vecHeight[yt+k];
vecVert[w+2] = i * ch + cz;
w+=3;
}//nextk
}//nexti
}//initmap
public function getHeight(ax:Number, az:Number):Number
{
var tx:int; var ty:int; var yt:int;
ax -= cx; az -=cz; //transform by heightmap coords first
tx = Math.floor(ax/cw); ty = Math.floor(az/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 * vmw;
u = (ax/cw)-tx; v = (az/ch)-ty;
z0 = vecHeight[yt+tx]; z1 = vecHeight[yt+tx+1];
h0 = z0 + (z1-z0)*u;
z0 = vecHeight[yt+tx+vmw]; z1 = vecHeight[yt+tx+vmw+1];
h1 = z0 + (z1 -z0)*u;
return (h0 +(h1-h0) * v);
}//getheight
}//xheightmap