Hi guys,
I written a UV animation plane extends from Plane, but when I new more than 1000 plane, it become very slowly.
Every plane just have 2 triangles.
I think the reason is more plane object here.
How to solve this problem?
Here is my code:
UVPlane.aS
package
{
import away3d.materials.MaterialBase;
import away3d.primitives.Plane;
/**
* ...
* @author liu yi
*/
public class UVPlane extends Plane
{
private var _maxU:uint;
private var _maxV:uint;
private var _uvIndex:uint;
private var _uvLength:uint;
private var _uvList:Vector.<Number>
private var _nextUVMap:Vector.<Number>
public var duration:Number;//0-1;
public var speed:Number;
protected var _delta:Number = 0;
//now use 1X1 segments first.
public function UVPlane(material : MaterialBase = null, width : Number = 100, height : Number = 100,uvIndex:uint=0,materialU:uint=1,materialV:uint=1,yUp : Boolean = true)
{
super(material, width, height, 1 , 1, yUp);
_maxU = materialU;
_maxV = materialV;
_uvIndex = uvIndex;
_uvLength = (_maxU + 1) * (_maxV + 1);
_nextUVMap = new Vector.<Number>(8,true);
duration = 1;
speed = 1;
createUVMap();//store uv data to an vector list.
moveUVTo(_uvIndex);
}
public function get maxU():uint {
return _maxU;
}
public function get maxV():uint {
return _maxV
}
public function get uvIndex():uint {
return _uvIndex
}
public function moveUVTo(id:uint):void {
if (id >= _uvLength) {
id = _uvLength - 1;
}
_uvIndex = id;
this.geometry.subGeometries[0].updateUVData( getUVMap(_uvIndex));
}
public function nextUV():void {
if (_delta < duration) {
_delta += speed;
return;
}
_delta = 0;
if(_uvIndex>=_uvLength){
_uvIndex = 0;
}else {
_uvIndex++;
}
this.geometry.subGeometries[0].updateUVData( getUVMap(_uvIndex));
}
protected function createUVMap():void {
var u:Number
var v:Number
_uvList = new Vector.<Number>();
for (var i:int = 0; i <_uvLength; i++) {
u= (i % (_maxU+1))/_maxU;
v = Math.floor(i / (_maxU+1))/_maxV;
_uvList.push(u,v);
}
}
protected function getUVMap(id:uint):Vector.<Number> {
//0,0, 0.25,0, 0.5,0, 0.75,0, 1,0,
//0,0.25, 0.25,0.25, 0.5,0.25, 0.75,0.25, 1,0.25,
//0,0.5, 0.25,0.5, 0.5,0.5, 0.75,0.5, 1,0.5,
//0,0.75, 0.25,0.75, 0.5,0.75, 0.75,0.75, 1,0.75,
//0,1, 0.25,1, 0.5,1, 0.75,1, 1,1
//start from left bottom:
//p1
_nextUVMap[0] = _uvList[id + (_maxU+1)*2+0];
_nextUVMap[1] = _uvList[id + (_maxU + 1) * 2 + 1];
//p2
_nextUVMap[2] = _uvList[id + (_maxU+1)*2+2];
_nextUVMap[3] = _uvList[id + (_maxU+1)*2+3];
//p3
_nextUVMap[4] = _uvList[id ];
_nextUVMap[5] = _uvList[id + 1];
//p4
_nextUVMap[6] = _uvList[id + 2];
_nextUVMap[7] = _uvList[id + 3];
return _nextUVMap
}
}
}
Test.as
private function createUVPlaneList():void {
var mat:BitmapMaterial = new BitmapMaterial((new fireTexture() as Bitmap).bitmapData);
_particalList = new Vector.<UVPlane>();
for (var i:int = 0; i < 10; i++) {
var p:UVPlane = new UVPlane(mat, 10, 10, 0, 4, 4);
p.speed = 0.5;
p.position = new Vector3D(Math.random() * 500, Math.random() * 500, Math.random() * 500);
p.lookAt(_camera.position);
_view.scene.addChild(p);
_particalList[i] = p;
}
trace(_particalList.length);
}
function update(e:Event):void
{
for (var i:int = 0; i < 10; i++) {
_particalList[i].nextUV();
}
....
}