Gaming Your Way

May contain nuts.

Unity Flash export test

Grabbed the preview build of Unity 3.5 yesterday just to have an ultra quick play. I wrote a simple plasma cube way back ( You'll see how long ago when you see the source below ) so that was the obvious choice to try and export, as I'm stilling suffering the Christmas lazy hangover and don't really have much work in me.

Here's the flash version ( 2.2 meg ) Plasma effect ( Usual Stage3D stuff applies, you'll need a compatible card etc. )

And just for comparison, the native Unity one ( 64k ! ) Unity Plasma effect

You'll see some corruption on the Flash version, which I guess is to be almost expected as it's a preview build ( And also Stage3D in itself is still very new ). I still find it mental impressive that it works at all.

If you fancy playing with it yourself, drag a cube onto the stage and attach this script.

/*-----------------------------------------------------------------------------
@method: Plasma
@author: Squize / www.gamingyourway.com
@version: 4/4/09
-----------------------------------------------------------------------------*/
#pragma strict
public var width:int=128;
public var height:int=128;
	
private var texture:Texture2D;
private var colourTable:Array;
private var activePixelsStorage:Array;
private var sinOffset:Number;
private var paletteShiftX:Number;
private var paletteShiftY:Number;
private var colourTableResolution:int=2048;

//---------------------------------------------------------------------------------------
// Start method
//---------------------------------------------------------------------------------------
function Start(){
	texture=new Texture2D(width,height);
	renderer.material.mainTexture = texture;
	renderer.material.mainTexture.filterMode = FilterMode.Bilinear;
	renderer.material.mainTexture.anisoLevel = 3;
	renderer.material.shader=Shader.Find("Particles/Additive (Soft)");

	createColourTable();

//Create our pixel instances
	activePixelsStorage=new Array();
	var pixelObj:PlasmaPixel;
	
	var j:int=-1;
	var k:int;
	while(++j!=width){
		k=-1;
		while(++k!=height){
			pixelObj=new PlasmaPixel();
			pixelObj.cX=pixelObj.xPos=j;
			pixelObj.cY=pixelObj.yPos=k;

			var xDist:int=width-pixelObj.cX;
			var yDist:int=height-pixelObj.cY;

			var distance:Number=Mathf.Round((Mathf.Sqrt((xDist*xDist)+(yDist*yDist))/2));
			var distX:Number=256 * Mathf.Sin(distance/8);
			var distY:Number=256 * Mathf.Cos(distance/8);

			pixelObj.jointDist=distX+distY;
			activePixelsStorage.push(pixelObj);
		}
	}	

	sinOffset=0;
}

//---------------------------------------------------------------------------------------
// Update method
//---------------------------------------------------------------------------------------
function Update(){
	sinOffset++;
	paletteShiftX = sinOffset;
	paletteShiftY = sinOffset*(transform.position.z)*2;
	
	var pixelObj:PlasmaPixel;
	var cnt:int=-1;
	var length:int=activePixelsStorage.length;

	while(++cnt!=length){
		pixelObj=activePixelsStorage[cnt];
		pixelObj.cX=pixelObj.xPos+paletteShiftX;
		pixelObj.cY=pixelObj.yPos+paletteShiftY;
		
		pixelObj.offset=(Mathf.Cos(pixelObj.cX/16) + Mathf.Sin(pixelObj.cY/32))*256 + pixelObj.jointDist;

		if(pixelObj.offset<0){			
			pixelObj.offset+=colourTableResolution;
		}

		texture.SetPixel (pixelObj.xPos, pixelObj.yPos,colourTable[pixelObj.offset]);
	}
	
// Apply all SetPixel calls
	texture.Apply(false);
	
	transform.Rotate(Vector3.right * (Time.deltaTime*30));
	transform.Rotate(Vector3.up * (-Time.deltaTime*23));
	transform.Rotate(Vector3.forward * (-Time.deltaTime*3));
	
}

//---------------------------------------------------------------------------------------
// Private
//---------------------------------------------------------------------------------------
private function createColourTable(){
	colourTable=new Array();
	var cnt:int=-1;
	var col:Color;
	var offset:float=3.1415;

	while(++cnt!=256*4){
		col=new Color();
		col.a=1;
		col.r = (128 + 128 * Mathf.Sin(offset * cnt / 32))/255;
		col.g = (128 + 128 * Mathf.Sin(offset * cnt / 64))/255;
		col.b = (128 + 128 * Mathf.Sin(offset * cnt / 128))/255;
		colourTable.push(col);
		colourTable.push(col);
		colourTable.push(col);
		colourTable.push(col);
	}
}

//---------------------------------------------------------------------------------------
// Pixel class
//---------------------------------------------------------------------------------------
class PlasmaPixel{
	var xPos:int;
	var yPos:int;

	var cX:Number;
	var cY:Number;
	
	var jointDist:Number;	
	var offset:Number;
};

And that's all there is to it. It could be optimised a lot, but look how old that code is, I think I'm allowed a pass for that.

If you do anything nice with it please drop a link in the comments.

Squize.

Comments are closed