I would like to know what’s the best way to optimize the update of a material.
What I’m doing so far is I have visual assets from an embedded SWC (created in the Flash IDE). I instantiate the MC, then draw it to a BitmapData. When I move something with TweenMax in the MovieClip children, I redraw the BitmapData, however that is considerably slow sometimes (it can go from 60 fps to 15 fps)
So I wonder what are the alternatives? One thing is the MC is not a simple playback timelines, elements are placed and are dynamic (just so I don’t get told to simply playback the MC )
Thanks!
PS: Below is an example of the code:
package
{
import away3d.containers.View3D;
import away3d.materials.BitmapMaterial;
import away3d.primitives.Plane;
import net.hires.debug.Stats;
import com.greensock.TweenMax;
import com.greensock.easing.Cubic;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.geom.Matrix;
[SWF(frameRate="60")]
public class Main extends Sprite
{
private const WIDTH :uint = 1280;
private const HEIGHT :uint = 720;
private const MATERIAL_WIDTH :uint = 1024;
private const MATERIAL_HEIGHT :uint = 512;
private const PLANE_WIDTH :uint = 640;
private const PLANE_HEIGHT :uint = 360;
private var _assetsMC :AssetsMC;
private var _bitmapData :BitmapData;
private var _matrix :Matrix;
private var _material :BitmapMaterial;
private var _plane :Plane;
private var _view3D :View3D;
public function Main()
{
initStage();
init();
}
private function initStage():void
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
}
private function init():void
{
_matrix = new Matrix();
_matrix.scale(MATERIAL_WIDTH / PLANE_WIDTH, MATERIAL_HEIGHT / PLANE_HEIGHT);
_assetsMC = new AssetsMC();
_bitmapData = new BitmapData(MATERIAL_WIDTH, MATERIAL_HEIGHT);
_bitmapData.draw(_assetsMC, _matrix, null, null, null, true);
_material = new BitmapMaterial(_bitmapData);
_material.bothSides = true;
_plane = new Plane(_material, PLANE_WIDTH, PLANE_HEIGHT);
_view3D = new View3D();
_view3D.scene.addChild(_plane);
_view3D.width = WIDTH;
_view3D.height = HEIGHT;
_assetsMC.width *= 0.25;
_assetsMC.x = WIDTH - _assetsMC.width;
_assetsMC.height *= 0.25;
_assetsMC.y = HEIGHT - _assetsMC.height;
addChild(_view3D);
addChild(_assetsMC);
addChild(new Stats());
addEventListener(Event.ENTER_FRAME, updateHandler);
moveLeft();
}
private function moveLeft():void
{
TweenMax.to(_assetsMC.circleMC, 0.45, {x:0, ease:Cubic.easeInOut, onComplete:moveLeftComplete});
}
private function moveLeftComplete():void
{
moveRight();
}
private function moveRight():void
{
var xTo:int = 640 - _assetsMC.circleMC.width;
TweenMax.to(_assetsMC.circleMC, 0.45, {x:xTo, ease:Cubic.easeInOut, onComplete:moveRightComplete});
}
private function moveRightComplete():void
{
moveLeft();
}
private function updateHandler(event:Event):void
{
_bitmapData.draw(_assetsMC, _matrix, null, null, null, true);
_material.updateTexture();
_plane.rotationY += 3;
_view3D.render();
}
}
}