根sprite下面如果包含多种不同的状态,则会造成严重性能问题,
所谓的状态切换包括:
"是否染色","贴图切换","平滑度","纹理是否平铺显示",或者"混合模式"设置, 可能是因为批次满了(一个批次最多包含8192个四边形)
可参考QuadBatch.isStateChange这个函数。
测试代码如下:
package
{
import starling.display.BlendMode;
import starling.display.Quad;
import starling.display.Sprite;
import starling.events.Event;
public class Test extends Sprite
{
public static var one:Test;
public function Test()
{
this.addEventListener(Event.ENTER_FRAME,tick);
}
private var k:int;
public function tick():void{
//if(k%30==0){
removeChildren();
var q:Quad;
for (var i:int = 0; i < 100; i++) {
q = new Quad(200,200,0xff0000);
addChild(q);
q.x = 400*Math.random();
q.y = 400*Math.random();
var Mode:Boolean = Math.random()>.5? true:false;
if(Mode)q.blendMode = BlendMode.ADD;//此句会造成状态切换,使100个Quad被分割成多个QuadBatch分开渲染
}
this.flatten();
//}
k++;
}
}
}
生成的agal也杂乱不堪,如下:
Context3D.clear(1, 1, 1, 1, 1, 0, ALL)
Context3D.setDepthTest(false, ALWAYS)
Context3D.setCulling(NONE)
Context3D.setScissorRectangle(0, 0, 0, 0)
Context3D.setRenderToBackBuffer()
Context3D.setScissorRectangle(0, 0, 0, 0)
VertexBuffer3D(181).dispose()
IndexBuffer3D(161).dispose()
VertexBuffer3D(37).dispose()
IndexBuffer3D(153).dispose()
VertexBuffer3D(35).dispose()
IndexBuffer3D(157).dispose()
Context3D.setScissorRectangle(0, 0, 0, 0)
VertexBuffer3D(1).uploadDouble(0, 64)
Context3D.setBlendFactors(1, 4)
Context3D.setProgram(1)
Context3D.setProgramConstantsDouble(VERTEX, 0, 1)
Context3D.setProgramConstants(VERTEX, 1, 4)
Context3D.setVertexBufferAt(0, 1, 0, FLOAT_2)
Context3D.setVertexBufferAt(1, 1, 2, FLOAT_4)
Context3D.drawTriangles(1, 0, 2)
Context3D.setVertexBufferAt(1, 0, 0, DISABLED)
Context3D.setVertexBufferAt(0, 0, 0, DISABLED)
VertexBuffer3D(2).uploadDouble(0, 64)
Context3D.setBlendFactors(1, 1)
Context3D.setProgram(1)
Context3D.setProgramConstantsDouble(VERTEX, 0, 1)
Context3D.setProgramConstants(VERTEX, 1, 4)
Context3D.setVertexBufferAt(0, 2, 0, FLOAT_2)
Context3D.setVertexBufferAt(1, 2, 2, FLOAT_4)
Context3D.drawTriangles(2, 0, 2)
Context3D.setVertexBufferAt(1, 0, 0, DISABLED)
Context3D.setVertexBufferAt(0, 0, 0, DISABLED)
//此处省略N行,N大于50
VertexBuffer3D(29).uploadDouble(0, 64)
Context3D.setBlendFactors(1, 4)
Context3D.setProgram(1)
Context3D.setProgramConstantsDouble(VERTEX, 0, 1)
Context3D.setProgramConstants(VERTEX, 1, 4)
Context3D.setVertexBufferAt(0, 29, 0, FLOAT_2)
Context3D.setVertexBufferAt(1, 29, 2, FLOAT_4)
Context3D.drawTriangles(29, 0, 4)
Context3D.setVertexBufferAt(1, 0, 0, DISABLED)
Context3D.setVertexBufferAt(0, 0, 0, DISABLED)
VertexBuffer3D(30).uploadDouble(0, 64)
Context3D.setBlendFactors(1, 1)
Context3D.setProgram(1)
Context3D.setProgramConstantsDouble(VERTEX, 0, 1)
Context3D.setProgramConstants(VERTEX, 1, 4)
Context3D.setVertexBufferAt(0, 30, 0, FLOAT_2)
Context3D.setVertexBufferAt(1, 30, 2, FLOAT_4)
Context3D.drawTriangles(30, 0, 2)
Context3D.setVertexBufferAt(1, 0, 0, DISABLED)
Context3D.setVertexBufferAt(0, 0, 0, DISABLED)
VertexBuffer3D(31).uploadDouble(0, 64)
Context3D.setBlendFactors(1, 4)
Context3D.setProgram(1)
Context3D.setProgramConstantsDouble(VERTEX, 0, 1)
Context3D.setProgramConstants(VERTEX, 1, 4)
Context3D.setVertexBufferAt(0, 31, 0, FLOAT_2)
Context3D.setVertexBufferAt(1, 31, 2, FLOAT_4)
Context3D.drawTriangles(31, 0, 4)
Context3D.setVertexBufferAt(1, 0, 0, DISABLED)
Context3D.setVertexBufferAt(0, 0, 0, DISABLED)
VertexBuffer3D(32).uploadDouble(0, 64)
Context3D.setBlendFactors(1, 1)
Context3D.setProgram(1)
Context3D.setProgramConstantsDouble(VERTEX, 0, 1)
Context3D.setProgramConstants(VERTEX, 1, 4)
Context3D.setVertexBufferAt(0, 32, 0, FLOAT_2)
Context3D.setVertexBufferAt(1, 32, 2, FLOAT_4)
Context3D.drawTriangles(32, 0, 6)
Context3D.setVertexBufferAt(1, 0, 0, DISABLED)
Context3D.setVertexBufferAt(0, 0, 0, DISABLED)
VertexBuffer3D(33).uploadDouble(0, 64)
Context3D.setBlendFactors(1, 4)
Context3D.setProgram(1)
Context3D.setProgramConstantsDouble(VERTEX, 0, 1)
Context3D.setProgramConstants(VERTEX, 1, 4)
Context3D.setVertexBufferAt(0, 33, 0, FLOAT_2)
Context3D.setVertexBufferAt(1, 33, 2, FLOAT_4)
Context3D.drawTriangles(33, 0, 2)
Context3D.setVertexBufferAt(1, 0, 0, DISABLED)
Context3D.setVertexBufferAt(0, 0, 0, DISABLED)
VertexBuffer3D(156).uploadDouble(0, 64)
Context3D.setBlendFactors(1, 1)
Context3D.setProgram(1)
Context3D.setProgramConstantsDouble(VERTEX, 0, 1)
Context3D.setProgramConstants(VERTEX, 1, 4)
Context3D.setVertexBufferAt(0, 156, 0, FLOAT_2)
Context3D.setVertexBufferAt(1, 156, 2, FLOAT_4)
Context3D.drawTriangles(180, 0, 6)
Context3D.setVertexBufferAt(1, 0, 0, DISABLED)
Context3D.setVertexBufferAt(0, 0, 0, DISABLED)
VertexBuffer3D(143).uploadDouble(0, 64)
Context3D.setBlendFactors(1, 4)
Context3D.setProgram(1)
Context3D.setProgramConstantsDouble(VERTEX, 0, 1)
Context3D.setProgramConstants(VERTEX, 1, 4)
Context3D.setVertexBufferAt(0, 143, 0, FLOAT_2)
Context3D.setVertexBufferAt(1, 143, 2, FLOAT_4)
Context3D.drawTriangles(171, 0, 4)
Context3D.setVertexBufferAt(1, 0, 0, DISABLED)
Context3D.setVertexBufferAt(0, 0, 0, DISABLED)
VertexBuffer3D(165).uploadDouble(0, 64)
Context3D.setBlendFactors(1, 1)
Context3D.setProgram(1)
Context3D.setProgramConstantsDouble(VERTEX, 0, 1)
Context3D.setProgramConstants(VERTEX, 1, 4)
Context3D.setVertexBufferAt(0, 165, 0, FLOAT_2)
Context3D.setVertexBufferAt(1, 165, 2, FLOAT_4)
Context3D.drawTriangles(79, 0, 2)
Context3D.setVertexBufferAt(1, 0, 0, DISABLED)
Context3D.setVertexBufferAt(0, 0, 0, DISABLED)
VertexBuffer3D(149).uploadDouble(0, 64)
Context3D.setBlendFactors(1, 4)
Context3D.setProgram(1)
Context3D.setProgramConstantsDouble(VERTEX, 0, 1)
Context3D.setProgramConstants(VERTEX, 1, 4)
Context3D.setVertexBufferAt(0, 149, 0, FLOAT_2)
Context3D.setVertexBufferAt(1, 149, 2, FLOAT_4)
Context3D.drawTriangles(149, 0, 2)
Context3D.setVertexBufferAt(1, 0, 0, DISABLED)
Context3D.setVertexBufferAt(0, 0, 0, DISABLED)
VertexBuffer3D(160).uploadDouble(0, 64)
Context3D.setBlendFactors(1, 1)
Context3D.setProgram(1)
Context3D.setProgramConstantsDouble(VERTEX, 0, 1)
Context3D.setProgramConstants(VERTEX, 1, 4)
Context3D.setVertexBufferAt(0, 160, 0, FLOAT_2)
Context3D.setVertexBufferAt(1, 160, 2, FLOAT_4)
Context3D.drawTriangles(122, 0, 10)
Context3D.setVertexBufferAt(1, 0, 0, DISABLED)
Context3D.setVertexBufferAt(0, 0, 0, DISABLED)
VertexBuffer3D(150).uploadDouble(0, 64)
Context3D.setBlendFactors(1, 4)
Context3D.setProgram(1)
Context3D.setProgramConstantsDouble(VERTEX, 0, 1)
Context3D.setProgramConstants(VERTEX, 1, 4)
Context3D.setVertexBufferAt(0, 150, 0, FLOAT_2)
Context3D.setVertexBufferAt(1, 150, 2, FLOAT_4)
Context3D.drawTriangles(147, 0, 4)
Context3D.setVertexBufferAt(1, 0, 0, DISABLED)
Context3D.setVertexBufferAt(0, 0, 0, DISABLED)
VertexBuffer3D(146).uploadDouble(0, 64)
Context3D.setBlendFactors(1, 1)
Context3D.setProgram(1)
Context3D.setProgramConstantsDouble(VERTEX, 0, 1)
Context3D.setProgramConstants(VERTEX, 1, 4)
Context3D.setVertexBufferAt(0, 146, 0, FLOAT_2)
Context3D.setVertexBufferAt(1, 146, 2, FLOAT_4)
Context3D.drawTriangles(172, 0, 6)
Context3D.setVertexBufferAt(1, 0, 0, DISABLED)
Context3D.setVertexBufferAt(0, 0, 0, DISABLED)
VertexBuffer3D(151).uploadDouble(0, 64)
Context3D.setBlendFactors(1, 4)
Context3D.setProgram(1)
Context3D.setProgramConstantsDouble(VERTEX, 0, 1)
Context3D.setProgramConstants(VERTEX, 1, 4)
Context3D.setVertexBufferAt(0, 151, 0, FLOAT_2)
Context3D.setVertexBufferAt(1, 151, 2, FLOAT_4)
Context3D.drawTriangles(170, 0, 10)
Context3D.setVertexBufferAt(1, 0, 0, DISABLED)
Context3D.setVertexBufferAt(0, 0, 0, DISABLED)
VertexBuffer3D(38).uploadDouble(0, 64)
Context3D.setBlendFactors(1, 1)
Context3D.setProgram(1)
Context3D.setProgramConstantsDouble(VERTEX, 0, 1)
Context3D.setProgramConstants(VERTEX, 1, 4)
Context3D.setVertexBufferAt(0, 38, 0, FLOAT_2)
Context3D.setVertexBufferAt(1, 38, 2, FLOAT_4)
Context3D.drawTriangles(178, 0, 2)
Context3D.setVertexBufferAt(1, 0, 0, DISABLED)
Context3D.setVertexBufferAt(0, 0, 0, DISABLED)
VertexBuffer3D(107).uploadDouble(0, 64)
Context3D.setBlendFactors(1, 4)
Context3D.setProgram(1)
Context3D.setProgramConstantsDouble(VERTEX, 0, 1)
Context3D.setProgramConstants(VERTEX, 1, 4)
Context3D.setVertexBufferAt(0, 107, 0, FLOAT_2)
Context3D.setVertexBufferAt(1, 107, 2, FLOAT_4)
Context3D.drawTriangles(107, 0, 4)
Context3D.setVertexBufferAt(1, 0, 0, DISABLED)
Context3D.setVertexBufferAt(0, 0, 0, DISABLED)
VertexBuffer3D(162).uploadDouble(0, 64)
Context3D.setBlendFactors(1, 1)
Context3D.setProgram(1)
Context3D.setProgramConstantsDouble(VERTEX, 0, 1)
Context3D.setProgramConstants(VERTEX, 1, 4)
Context3D.setVertexBufferAt(0, 162, 0, FLOAT_2)
Context3D.setVertexBufferAt(1, 162, 2, FLOAT_4)
Context3D.drawTriangles(186, 0, 2)
Context3D.setVertexBufferAt(1, 0, 0, DISABLED)
Context3D.setVertexBufferAt(0, 0, 0, DISABLED)
Context3D.present()
如果去掉那句if(Mode)q.blendMode = BlendMode.ADD; 则Starling最终生成的agal 代码会特别简洁,所有三角形一次性绘出:
Context3D.clear(1, 1, 1, 1, 1, 0, ALL)
Context3D.setDepthTest(false, ALWAYS)
Context3D.setCulling(NONE)
Context3D.setScissorRectangle(0, 0, 0, 0)
Context3D.setRenderToBackBuffer()
Context3D.setScissorRectangle(0, 0, 0, 0)
Context3D.setScissorRectangle(0, 0, 0, 0)
VertexBuffer3D(4).uploadDouble(0, 512)
Context3D.setBlendFactors(1, 4)
Context3D.setProgram(1)
Context3D.setProgramConstantsDouble(VERTEX, 0, 1)
Context3D.setProgramConstants(VERTEX, 1, 4)
Context3D.setVertexBufferAt(0, 4, 0, FLOAT_2)
Context3D.setVertexBufferAt(1, 4, 2, FLOAT_4)
Context3D.drawTriangles(4, 0, 200)
Context3D.setVertexBufferAt(1, 0, 0, DISABLED)
Context3D.setVertexBufferAt(0, 0, 0, DISABLED)
Context3D.present()