MovieMaterial with alternativa3d 8

Posted by davidejones

You can find a few examples of a MovieMaterial for version 5 and 7 of alternativa3d on their forums and wiki but I have yet to see one for version 8 so I thought I’d give it a go…

You may recognise the swf from the alternativa3d bunker demo that was created in version 5

The usage is the same as most materials just pass in the movieclip and apply the material and the rest is done. Here is a snippet of the usage…

[Embed(source="screen.swf")] static private const TestSWF:Class;

mybox = new Box();			
var mat:MovieMaterial = new MovieMaterial(new TestSWF());
mybox.setMaterialToAllSurfaces(mat);
scene.addChild(mybox);
uploadResources(mybox.getResources(true));

and here is the actual class.. it’s pretty similar to others except for the need to upload the new image resource as well.

package
{
	import alternativa.engine3d.materials.TextureMaterial;
	import alternativa.engine3d.resources.TextureResource;
	import alternativa.engine3d.resources.BitmapTextureResource;
	import alternativa.engine3d.core.Camera3D;
	import alternativa.engine3d.objects.Surface;
	import alternativa.engine3d.resources.Geometry;
	import alternativa.engine3d.core.Light3D;
	import flash.display.MovieClip;
	import flash.display.BitmapData;
	import flash.events.Event;
	
	import alternativa.engine3d.alternativa3d; 
	use namespace alternativa3d;
	
	public class MovieMaterial extends TextureMaterial
	{
		protected var mc:MovieClip;
		protected var bd:BitmapData;
	
		public function MovieMaterial(clip:MovieClip):void
		{
			mc = clip;
			this.diffuseMap = getDiffuse();
			this.alpha = 1;
			super(diffuseMap,null,alpha);
			mc.addEventListener(Event.ENTER_FRAME, updateTexture);
		}
		
		private function getDiffuse():TextureResource {
			bd = new BitmapData (mc.width, mc.height, true);
			return new BitmapTextureResource(bd);
		}
		
		protected function updateTexture(e:Event = null):void {
			if(mc) { bd.draw(mc); }
		}
		
		override alternativa3d function collectDraws(camera:Camera3D, surface:Surface, geometry:Geometry, lights:Vector., lightsLength:int, objectRenderPriority:int = -1):void 
		{
			if(diffuseMap) { diffuseMap.upload(camera.context3D); }
			super.collectDraws(camera, surface, geometry, lights, lightsLength, objectRenderPriority);
		}
	}
}

Its not perfect I can see that it lights up white just before the swf starts playing. I’m guessing this is just the stage background colour of the embedded swf but i’m not 100% sure as I haven’t tested any other swfs. Its also worth noting that the bitmap data or swf still needs to be a power of 2 to be uploaded successfully. The example swf I used was 256×256.

You can download the source files here or see the demo here

Trackback URL for this post: http://davidejones.com/blog/1392-moviematerial-alternativa3d-8/trackback/

Being Sociable...

  • If you like this article then please share it on your favourite social network and follow me on twitter for the latest updates

8 Responses to MovieMaterial with alternativa3d 8

Chris

I forgot to ask something though…
Do you think it would be faster if you had a bigger texture?

You could use a 2048×2048 (or less…) sized texture that could contain 64 256×256 images.
If you needed more than 64 frames you would use a different texture containing the rest of the images.
Then just move the UV data around.
Correct?

I just wonder if setting a texture the size of 2048×2048 would be faster than just constantly uploading bitmapData.

Think you could test that out for me? :P

davidejones

It certainly sounds faster, i’m not sure if i’ll have much time to try it out but if i do i’ll probably make a new post with it.

Jevgenij Dmitrijev

Hi, i was trying to use this code, but for some reasons… it fails to compile with following error:

Error: Incompatible override.

override alternativa3d function collectDraws(camera:Camera3D, surface:Surface, geometry:Geometry, lights:Vector., lightsLength:int, objectRenderPriority:int = -1):void

I’m using the 8.31.0 version of the ALTERNATIVA 3D.

davidejones

collectDraws function has changed in 8.31.0, it has an extra shadow argument.


override alternativa3d function collectDraws(camera:Camera3D, surface:Surface, geometry:Geometry, lights:Vector.<Light3D>, lightsLength:int, useShadow:Boolean, objectRenderPriority:int = -1):void {
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>