layaair-js
Version:
An open source HTML5 engine. Edition JavaScript.
1,376 lines (1,263 loc) • 597 kB
JavaScript
(function(window,document,Laya){
var __un=Laya.un,__uns=Laya.uns,__static=Laya.static,__class=Laya.class,__getset=Laya.getset,__newvec=Laya.__newvec;
var AnimationPlayer=laya.ani.AnimationPlayer,AnimationState=laya.ani.AnimationState,Arith=laya.maths.Arith;
var Bitmap=laya.resource.Bitmap,Browser=laya.utils.Browser,Buffer=laya.webgl.utils.Buffer,Buffer2D=laya.webgl.utils.Buffer2D;
var Byte=laya.utils.Byte,ClassUtils=laya.utils.ClassUtils,Config=Laya.Config,EmitterBase=laya.particle.emitter.EmitterBase;
var Event=laya.events.Event,EventDispatcher=laya.events.EventDispatcher,Handler=laya.utils.Handler,IndexBuffer2D=laya.webgl.utils.IndexBuffer2D;
var KeyframesAniTemplet=laya.ani.KeyframesAniTemplet,Loader=laya.net.Loader,Node=laya.display.Node,ParticleSetting=laya.particle.ParticleSetting;
var ParticleShader=laya.particle.shader.ParticleShader,ParticleTemplateWebGL=laya.particle.ParticleTemplateWebGL;
var Rectangle=laya.maths.Rectangle,Render=laya.renders.Render,RenderContext=laya.renders.RenderContext,RenderSprite=laya.renders.RenderSprite;
var RenderState2D=laya.webgl.utils.RenderState2D,Resource=laya.resource.Resource,RunDriver=laya.utils.RunDriver;
var Shader=laya.webgl.shader.Shader,ShaderDefines=laya.webgl.shader.ShaderDefines,Sprite=laya.display.Sprite;
var Stat=laya.utils.Stat,URL=laya.net.URL,ValusArray=laya.webgl.utils.ValusArray,VertexBuffer2D=laya.webgl.utils.VertexBuffer2D;
var WebGL=laya.webgl.WebGL,WebGLContext=laya.webgl.WebGLContext,WebGLContext2D=laya.webgl.canvas.WebGLContext2D;
var WebGLImage=laya.webgl.resource.WebGLImage;
Laya.interface('laya.d3.graphics.IVertex');
Laya.interface('laya.d3.core.render.IUpdate');
Laya.interface('laya.d3.core.render.IRenderable');
/**
*<code>Glitter</code> 类用于创建闪光配置信息。
*/
//class laya.d3.core.glitter.GlitterSetting
var GlitterSetting=(function(){
function GlitterSetting(){
this.texturePath=null;
this.lifeTime=0.5;
this.minSegmentDistance=0.1;
this.minInterpDistance=0.6;
this.maxSlerpCount=128;
this.maxSegments=200;
this.color=new Vector4(1.0,1.0,1.0,1.0);
}
__class(GlitterSetting,'laya.d3.core.glitter.GlitterSetting');
return GlitterSetting;
})()
/**
*<code>SplineCurvePositionVelocity</code> 类用于通过顶点和速度创建闪光插值。
*/
//class laya.d3.core.glitter.SplineCurvePositionVelocity
var SplineCurvePositionVelocity=(function(){
function SplineCurvePositionVelocity(){
this._tempVector30=new Vector3();
this._tempVector31=new Vector3();
this._tempVector32=new Vector3();
this._a=new Vector3();
this._b=new Vector3();
this._c=new Vector3();
this._d=new Vector3();
}
__class(SplineCurvePositionVelocity,'laya.d3.core.glitter.SplineCurvePositionVelocity');
var __proto=SplineCurvePositionVelocity.prototype;
/**
*初始化插值所需信息。
*@param position0 顶点0的位置。
*@param velocity0 顶点0的速度。
*@param position1 顶点1的位置。
*@param velocity1 顶点1的速度。
*/
__proto.Init=function(position0,velocity0,position1,velocity1){
position0.cloneTo(this._d);
velocity0.cloneTo(this._c);
Vector3.scale(position0,2.0,this._a);
Vector3.scale(position1,2.0,this._tempVector30);
Vector3.subtract(this._a,this._tempVector30,this._a);
Vector3.add(this._a,velocity0,this._a);
Vector3.add(this._a,velocity1,this._a);
Vector3.scale(position1,3.0,this._b);
Vector3.scale(position0,3.0,this._tempVector30);
Vector3.subtract(this._b,this._tempVector30,this._b);
Vector3.subtract(this._b,velocity1,this._b);
Vector3.scale(velocity0,2.0,this._tempVector30);
Vector3.subtract(this._b,this._tempVector30,this._b);
}
/**
*初始化插值所需信息。
*@param t 插值比例
*@param out 输出结果
*/
__proto.Slerp=function(t,out){
Vector3.scale(this._a,t *t *t,this._tempVector30);
Vector3.scale(this._b,t *t,this._tempVector31);
Vector3.scale(this._c,t,this._tempVector32);
Vector3.add(this._tempVector30,this._tempVector31,out);
Vector3.add(out,this._tempVector32,out);
Vector3.add(out,this._d,out);
}
return SplineCurvePositionVelocity;
})()
/**
*<code>HeightMap</code> 类用于实现高度图数据。
*/
//class laya.d3.core.HeightMap
var HeightMap=(function(){
function HeightMap(width,height,minHeight,maxHeight){
this._datas=null;
this._w=0;
this._h=0;
this._minHeight=NaN;
this._maxHeight=NaN;
this._datas=[];
this._w=width;
this._h=height;
this._minHeight=minHeight;
this._maxHeight=maxHeight;
}
__class(HeightMap,'laya.d3.core.HeightMap');
var __proto=HeightMap.prototype;
/**@private */
__proto._inBounds=function(row,col){
return row >=0 && row < this._h && col >=0 && col < this._w;
}
/**
*获取高度。
*@param row 列数。
*@param col 行数。
*@return 高度。
*/
__proto.getHeight=function(row,col){
if (this._inBounds(row,col))
return this._datas[row][col];
else
return NaN;
}
/**
*获取宽度。
*@return value 宽度。
*/
__getset(0,__proto,'width',function(){
return this._w;
});
/**
*获取高度。
*@return value 高度。
*/
__getset(0,__proto,'height',function(){
return this._h;
});
/**
*最大高度。
*@return value 最大高度。
*/
__getset(0,__proto,'maxHeight',function(){
return this._maxHeight;
});
/**
*最大高度。
*@return value 最大高度。
*/
__getset(0,__proto,'minHeight',function(){
return this._minHeight;
});
HeightMap.creatFromMesh=function(mesh,width,height,outCellSize){
var vertices=[];
var indexs=[];
var submesheCount=mesh.getSubMeshCount();
for (var i=0;i < submesheCount;i++){
var subMesh=mesh.getSubMesh(i);
var vertexBuffer=subMesh._getVertexBuffer();
var verts=vertexBuffer.getData();
var subMeshVertices=[];
for (var j=0;j < verts.length;j+=vertexBuffer.vertexDeclaration.vertexStride / 4){
var position=new Vector3(verts[j+0],verts[j+1],verts[j+2]);
subMeshVertices.push(position);
}
vertices.push(subMeshVertices);
var ib=subMesh._getIndexBuffer();
indexs.push(ib.getData());
};
var boundingBox=mesh.boundingBox;
var minX=boundingBox.min.x;
var minZ=boundingBox.min.z;
var maxX=boundingBox.max.x;
var maxZ=boundingBox.max.z;
var minY=boundingBox.min.y;
var maxY=boundingBox.max.y;
var widthSize=maxX-minX;
var heightSize=maxZ-minZ;
var cellWidth=outCellSize.elements[0]=widthSize / (width-1);
var cellHeight=outCellSize.elements[1]=heightSize / (height-1);
var heightMap=new HeightMap(width,height,minY,maxY);
var ray=HeightMap._tempRay;
var rayDirE=ray.direction.elements;
rayDirE[0]=0;
rayDirE[1]=-1;
rayDirE[2]=0;
var heightOffset=0.1;
var rayY=maxY+heightOffset;
ray.origin.elements[1]=rayY;
for (var w=0;w < width;w++){
var posZ=minZ+w *cellHeight;
heightMap._datas[w]=[];
for (var h=0;h < height;h++){
var posX=minX+h *cellWidth;
var rayOriE=ray.origin.elements;
rayOriE[0]=posX;
rayOriE[2]=posZ;
var closestIntersection=HeightMap._getPosition(ray,vertices,indexs);
heightMap._datas[w][h]=(closestIntersection===Number.MAX_VALUE)? NaN :rayY-closestIntersection;
}
}
return heightMap;
}
HeightMap.createFromImage=function(texture,minHeight,maxHeight){
var textureWidth=texture.width;
var textureHeight=texture.height;
var heightMap=new HeightMap(textureWidth,textureHeight,minHeight,maxHeight);
var compressionRatio=(maxHeight-minHeight)/ 254;
Browser.canvas.size(textureWidth,textureHeight);
Browser.context.drawImage(texture._image,0,0,textureWidth,textureHeight);
var pixelsInfo=Browser.context.getImageData(0,0,textureWidth,textureHeight).data;
var index=0;
for (var w=0;w < textureWidth;w++){
var colDatas=heightMap._datas[w]=[];
for (var h=0;h < textureHeight;h++){
var r=pixelsInfo[index++];
var g=pixelsInfo[index++];
var b=pixelsInfo[index++];
var a=pixelsInfo[index++];
if (r==255 && g==255 && b==255 && a==255)
colDatas[h]=NaN;
else {
colDatas[h]=(r+g+b)/ 3 *compressionRatio+minHeight;
}
}
}
return heightMap;
}
HeightMap._getPosition=function(ray,vertices,indexs){
var closestIntersection=Number.MAX_VALUE;
for (var i=0;i < vertices.length;i++){
var subMeshVertices=vertices[i];
var subMeshIndexes=indexs[i];
for (var j=0;j < subMeshIndexes.length;j+=3){
var vertex1=subMeshVertices[subMeshIndexes[j+0]];
var vertex2=subMeshVertices[subMeshIndexes[j+1]];
var vertex3=subMeshVertices[subMeshIndexes[j+2]];
var intersection=Picker.rayIntersectsTriangle(ray,vertex1,vertex2,vertex3);
if (!isNaN(intersection)&& intersection < closestIntersection){
closestIntersection=intersection;
}
}
}
return closestIntersection;
}
__static(HeightMap,
['_tempRay',function(){return this._tempRay=new Ray(new Vector3(),new Vector3());}
]);
return HeightMap;
})()
/**
*<code>Layer</code> 类用于实现遮罩层。
*/
//class laya.d3.core.Layer
var Layer=(function(){
function Layer(){
this._id=0;
this._number=0;
this._mask=0;
this._active=true;
this._visible=true;
this.name=null;
this._id=Layer._uniqueIDCounter;
Layer._uniqueIDCounter++;
if (this._id > 1+31)
throw new Error("不允许创建Layer,请参考函数getLayerByNumber、getLayerByMask、getLayerByName!");
}
__class(Layer,'laya.d3.core.Layer');
var __proto=Layer.prototype;
/**
*获取编号。
*@return 编号。
*/
__getset(0,__proto,'number',function(){
return this._number;
});
/**
*设置是否显示。
*@param value 是否显示。
*/
/**
*获取是否显示。
*@return 是否显示。
*/
__getset(0,__proto,'visible',function(){
return this._visible;
},function(value){
if (this._number===29 || this._number==30)
return;
this._visible=value;
if (value)
Layer._visibleLayers=Layer._visibleLayers | this.mask;
else
Layer._visibleLayers=Layer._visibleLayers & ~this.mask;
});
/**
*获取蒙版值。
*@return 蒙版值。
*/
__getset(0,__proto,'mask',function(){
return this._mask;
});
/**
*设置是否激活。
*@param value 是否激活。
*/
/**
*获取是否激活。
*@return 是否激活。
*/
__getset(0,__proto,'active',function(){
return this._active;
},function(value){
if (this._number===29 || this._number==30)
return;
this._active=value;
if (value)
Layer._activeLayers=Layer._activeLayers | this.mask;
else
Layer._activeLayers=Layer._activeLayers & ~this.mask;
});
/**
*设置Layer激活层。
*@param value 激活层。
*/
/**
*获取Layer激活层。
*@return 激活层。
*/
__getset(1,Layer,'activeLayers',function(){
return Layer._activeLayers;
},function(value){
Layer._activeLayers=value | Layer.getLayerByNumber(29).mask | Layer.getLayerByNumber(30).mask;
for (var i=0;i < Layer._layerList.length;i++){
var layer=Layer._layerList[i];
layer._active=(layer._mask & Layer._activeLayers)!==0;
}
});
/**
*设置Layer显示层。
*@param value 显示层。
*/
/**
*获取Layer显示层。
*@return 显示层。
*/
__getset(1,Layer,'visibleLayers',function(){
return Layer._visibleLayers;
},function(value){
Layer._visibleLayers=value | Layer.getLayerByNumber(29).mask | Layer.getLayerByNumber(30).mask;
for (var i=0;i < Layer._layerList.length;i++){
var layer=Layer._layerList[i];
layer._visible=(layer._mask & Layer._visibleLayers)!==0;
}
});
Layer.__init__=function(){
Layer._layerList.length=31;
for (var i=0;i < 31;i++){
var layer=new Layer();
Layer._layerList[i]=layer;
if (i===0)
layer.name="Default Layer";
else if (i===29)
layer.name="Reserved Layer0";
else if (i===30)
layer.name="Reserved Layer1";
else
layer.name="Layer-"+i;
layer._number=i;
layer._mask=Math.pow(2,i);
}
Layer._activeLayers=2147483647;
Layer._visibleLayers=2147483647;
Layer._currentCameraCullingMask=2147483647;
Layer.currentCreationLayer=Layer._layerList[0];
}
Layer.getLayerByNumber=function(number){
if (number < 0 || number > 30)
throw new Error("无法返回指定Layer,该number超出范围!");
return Layer._layerList[number];
}
Layer.getLayerByMask=function(mask){
for (var i=0;i < 31;i++){
if (Layer._layerList[i].mask===mask)
return Layer._layerList[i];
}
throw new Error("无法返回指定Layer,该mask不存在");
}
Layer.getLayerByName=function(name){
for (var i=0;i < 31;i++){
if (Layer._layerList[i].name===name)
return Layer._layerList[i];
}
throw new Error("无法返回指定Layer,该name不存在");
}
Layer.isActive=function(mask){
return (mask & Layer._activeLayers)!=0;
}
Layer.isVisible=function(mask){
return (mask & Layer._currentCameraCullingMask & Layer._visibleLayers)!=0;
}
Layer._uniqueIDCounter=1;
Layer._layerCount=31;
Layer._layerList=[];
Layer._activeLayers=0;
Layer._visibleLayers=0;
Layer._currentCameraCullingMask=0;
Layer.currentCreationLayer=null
return Layer;
})()
/**
*@private
*<code>PhasorSpriter3D</code> 类用于创建矢量笔刷。
*/
//class laya.d3.core.PhasorSpriter3D
var PhasorSpriter3D=(function(){
function PhasorSpriter3D(){
this._tempInt0=0;
this._tempInt1=0;
this._tempUint0=0;
this._tempUint1=0;
this._tempUint2=0;
this._tempUint3=0;
this._tempUint4=0;
this._tempUint5=0;
this._tempUint6=0;
this._tempUint7=0;
this._tempNumver0=NaN;
this._tempNumver1=NaN;
this._tempNumver2=NaN;
this._tempNumver3=NaN;
this._floatSizePerVer=7;
this._defaultBufferSize=600 *this._floatSizePerVer;
this._vb=null;
this._posInVBData=0;
this._ib=null;
this._posInIBData=0;
this._primitiveType=NaN;
this._hasBegun=false;
this._numVertsPerPrimitive=0;
this._renderState=null;
this._sharderNameID=0;
this._shader=null;
this._posShaderValue=[3,/*laya.webgl.WebGLContext.FLOAT*/0x1406,false,28,0];
this._colorShaderValue=[4,/*laya.webgl.WebGLContext.FLOAT*/0x1406,false,28,12];
this._albedo=new Vector4(1.0,1.0,1.0,1.0);
this._vbData=new Float32Array(this._defaultBufferSize);
this._ibData=new Uint16Array(this._defaultBufferSize);
this._wvpMatrix=new Matrix4x4();
;
this._vb=new VertexBuffer2D(-1,/*laya.webgl.WebGLContext.DYNAMIC_DRAW*/0x88E8);
this._ib=new IndexBuffer2D();
this._sharderNameID=Shader.nameKey.get("SIMPLE");
}
__class(PhasorSpriter3D,'laya.d3.core.PhasorSpriter3D');
var __proto=PhasorSpriter3D.prototype;
__proto.line=function(startX,startY,startZ,startR,startG,startB,startA,endX,endY,endZ,endR,endG,endB,endA){
if (!this._hasBegun || this._primitiveType!==/*laya.webgl.WebGLContext.LINES*/0x0001)
this.drawLinesException();
if (this._posInVBData+2 *this._floatSizePerVer > this._vbData.length || this._posInIBData+2 > this._ibData.length)
this.flush();
this._tempUint0=this._posInVBData / this._floatSizePerVer;
this.addVertex(startX,startY,startZ,startR,startG,startB,startA);
this.addVertex(endX,endY,endZ,endR,endG,endB,endA);
this.addIndexes(this._tempUint0,this._tempUint0+1);
return this;
}
__proto.circle=function(radius,numberOfPoints,r,g,b,a){
if (!this._hasBegun || (this._primitiveType!==/*laya.webgl.WebGLContext.LINES*/0x0001))
this.drawLinesException();
this._tempUint0=numberOfPoints *2;
if (this._posInVBData+this._tempUint0 *this._floatSizePerVer > this._vbData.length || this._posInIBData+2 *this._tempUint0 > this._ibData.length)
this.flush();
this._tempUint1=this._posInVBData / this._floatSizePerVer;
for (this._tempNumver0=0,this._tempInt0=0;this._tempNumver0 < 3.1416 *2;this._tempNumver0=this._tempNumver0+(3.1416 / numberOfPoints),this._tempInt0++){
this.addVertex(Math.sin(this._tempNumver0)*radius,Math.cos(this._tempNumver0)*radius,0,r,g,b,a);
if (this._tempInt0===0){
this.addIndexes(this._tempUint1);
}else if (this._tempInt0===this._tempUint0-1){
this._tempUint2=this._tempUint1+this._tempInt0;
this.addIndexes(this._tempUint2,this._tempUint2,this._tempUint1);
}else {
this._tempUint2=this._tempUint1+this._tempInt0;
this.addIndexes(this._tempUint2,this._tempUint2);
}
}
return this;
}
__proto.plane=function(positionX,positionY,positionZ,width,height,r,g,b,a){
if (!this._hasBegun || this._primitiveType!==/*laya.webgl.WebGLContext.TRIANGLES*/0x0004)
this.drawTrianglesException();
if (this._posInVBData+4 *this._floatSizePerVer > this._vbData.length || this._posInIBData+6 > this._ibData.length)
this.flush();
this._tempNumver0=width / 2;
this._tempNumver1=height / 2;
this._tempUint0=this._posInVBData / this._floatSizePerVer;
this.addVertex(positionX-this._tempNumver0,positionY+this._tempNumver1,positionZ,r,g,b,a);
this.addVertex(positionX+this._tempNumver0,positionY+this._tempNumver1,positionZ,r,g,b,a);
this.addVertex(positionX-this._tempNumver0,positionY-this._tempNumver1,positionZ,r,g,b,a);
this.addVertex(positionX+this._tempNumver0,positionY-this._tempNumver1,positionZ,r,g,b,a);
this._tempUint1=this._tempUint0+1;
this._tempUint2=this._tempUint0+2;
this.addIndexes(this._tempUint0,this._tempUint1,this._tempUint2,this._tempUint2,this._tempUint1,this._tempUint0+3);
return this;
}
__proto.box=function(positionX,positionY,positionZ,width,height,depth,r,g,b,a){
if (!this._hasBegun || this._primitiveType!==/*laya.webgl.WebGLContext.TRIANGLES*/0x0004)
this.drawTrianglesException();
if (this._posInVBData+8 *this._floatSizePerVer > this._vbData.length || this._posInIBData+36 > this._ibData.length)
this.flush();
this._tempNumver0=width / 2;
this._tempNumver1=height / 2;
this._tempNumver2=depth / 2;
this._tempUint0=this._posInVBData / this._floatSizePerVer;
this.addVertex(positionX-this._tempNumver0,positionY+this._tempNumver1,positionZ+this._tempNumver2,r,g,b,a);
this.addVertex(positionX+this._tempNumver0,positionY+this._tempNumver1,positionZ+this._tempNumver2,r,g,b,a);
this.addVertex(positionX-this._tempNumver0,positionY-this._tempNumver1,positionZ+this._tempNumver2,r,g,b,a);
this.addVertex(positionX+this._tempNumver0,positionY-this._tempNumver1,positionZ+this._tempNumver2,r,g,b,a);
this.addVertex(positionX+this._tempNumver0,positionY+this._tempNumver1,positionZ-this._tempNumver2,r,g,b,a);
this.addVertex(positionX-this._tempNumver0,positionY+this._tempNumver1,positionZ-this._tempNumver2,r,g,b,a);
this.addVertex(positionX+this._tempNumver0,positionY-this._tempNumver1,positionZ-this._tempNumver2,r,g,b,a);
this.addVertex(positionX-this._tempNumver0,positionY-this._tempNumver1,positionZ-this._tempNumver2,r,g,b,a);
this._tempUint1=this._tempUint0+1;
this._tempUint2=this._tempUint0+2;
this._tempUint3=this._tempUint0+3;
this._tempUint4=this._tempUint0+4;
this._tempUint5=this._tempUint0+5;
this._tempUint6=this._tempUint0+6;
this._tempUint7=this._tempUint0+7;
this.addIndexes(this._tempUint0,this._tempUint1,this._tempUint2,this._tempUint2,this._tempUint1,this._tempUint3,
this._tempUint4,this._tempUint5,this._tempUint6,this._tempUint6,this._tempUint5,this._tempUint7,
this._tempUint5,this._tempUint0,this._tempUint7,this._tempUint7,this._tempUint0,this._tempUint2,
this._tempUint1,this._tempUint4,this._tempUint3,this._tempUint3,this._tempUint4,this._tempUint6,
this._tempUint5,this._tempUint4,this._tempUint0,this._tempUint0,this._tempUint4,this._tempUint1,
this._tempUint2,this._tempUint3,this._tempUint7,this._tempUint7,this._tempUint3,this._tempUint6);
return this;
}
__proto.cone=function(radius,length,Slices,r,g,b,a){
if (!this._hasBegun || this._primitiveType!==/*laya.webgl.WebGLContext.TRIANGLES*/0x0004)
this.drawTrianglesException();
if (this._posInVBData+(2 *Slices+2)*this._floatSizePerVer > this._vbData.length || this._posInIBData+6 *Slices > this._ibData.length)
this.flush();
this._tempUint0=this._posInVBData;
this._tempUint1=this._posInVBData / this._floatSizePerVer;
this._tempNumver0=Math.PI *2 / Slices;
this.addVertexIndex(0,length,0,r,g,b,a,this._tempUint0);
this.addVertexIndex(0,0,0,r,g,b,a,this._tempUint0+this._floatSizePerVer);
this._tempInt0=2;
this._tempNumver1=0;
for (this._tempInt1=0;this._tempInt1 < Slices;this._tempInt1++){
this._tempNumver2=Math.cos(this._tempNumver1);
this._tempNumver3=Math.sin(this._tempNumver1);
this.addVertexIndex(radius *this._tempNumver2,0,radius *this._tempNumver3,r,g,b,a,this._tempUint0+this._tempInt0 *this._floatSizePerVer);
this.addIndexes(this._tempUint1,this._tempUint1+this._tempInt0);
if (this._tempInt1==Slices-1)
this.addIndexes(this._tempUint1+2);
else
this.addIndexes(this._tempUint1+this._tempInt0+1);
this.addVertexIndex(radius *this._tempNumver2,0,radius *this._tempNumver3,r,g,b,a,this._tempUint0+(this._tempInt0+Slices)*this._floatSizePerVer);
this.addIndexes(this._tempUint1+1);
if (this._tempInt1==Slices-1)
this.addIndexes(this._tempUint1+Slices+2);
else
this.addIndexes(this._tempUint1+this._tempInt0+Slices+1);
this.addIndexes(this._tempUint1+this._tempInt0+Slices);
this._tempInt0++;
this._tempNumver1+=this._tempNumver0;
}
return this;
}
__proto.boundingBoxLine=function(minX,minY,minZ,maxX,maxY,maxZ,r,g,b,a){
if (!this._hasBegun || this._primitiveType!==/*laya.webgl.WebGLContext.LINES*/0x0001)
this.drawLinesException();
if (this._posInVBData+8 *this._floatSizePerVer > this._vbData.length || this._posInIBData+48 > this._ibData.length)
this.flush();
this._tempUint0=this._posInVBData / this._floatSizePerVer;
this.addVertex(minX,maxY,maxZ,r,g,b,a);
this.addVertex(maxX,maxY,maxZ,r,g,b,a);
this.addVertex(minX,minY,maxZ,r,g,b,a);
this.addVertex(maxX,minY,maxZ,r,g,b,a);
this.addVertex(maxX,maxY,minZ,r,g,b,a);
this.addVertex(minX,maxY,minZ,r,g,b,a);
this.addVertex(maxX,minY,minZ,r,g,b,a);
this.addVertex(minX,minY,minZ,r,g,b,a);
this._tempUint1=this._tempUint0+1;
this._tempUint2=this._tempUint0+2;
this._tempUint3=this._tempUint0+3;
this._tempUint4=this._tempUint0+4;
this._tempUint5=this._tempUint0+5;
this._tempUint6=this._tempUint0+6;
this._tempUint7=this._tempUint0+7;
this.addIndexes(this._tempUint0,this._tempUint1,this._tempUint1,this._tempUint3,this._tempUint3,this._tempUint2,this._tempUint2,this._tempUint0,
this._tempUint4,this._tempUint5,this._tempUint5,this._tempUint7,this._tempUint7,this._tempUint6,this._tempUint6,this._tempUint4,
this._tempUint5,this._tempUint0,this._tempUint0,this._tempUint2,this._tempUint2,this._tempUint7,this._tempUint7,this._tempUint5,
this._tempUint1,this._tempUint4,this._tempUint4,this._tempUint6,this._tempUint6,this._tempUint3,this._tempUint3,this._tempUint1,
this._tempUint5,this._tempUint4,this._tempUint4,this._tempUint1,this._tempUint1,this._tempUint0,this._tempUint0,this._tempUint5,
this._tempUint2,this._tempUint3,this._tempUint3,this._tempUint6,this._tempUint6,this._tempUint7,this._tempUint7,this._tempUint2);
return this;
}
__proto.addVertex=function(x,y,z,r,g,b,a){
if (!this._hasBegun)
this.addVertexIndexException();
this._vbData[this._posInVBData]=x,this._vbData[this._posInVBData+1]=y,this._vbData[this._posInVBData+2]=z;
this._vbData[this._posInVBData+3]=r,this._vbData[this._posInVBData+4]=g,this._vbData[this._posInVBData+5]=b,this._vbData[this._posInVBData+6]=a;
this._posInVBData+=this._floatSizePerVer;
return this;
}
__proto.addVertexIndex=function(x,y,z,r,g,b,a,index){
if (!this._hasBegun)
this.addVertexIndexException();
this._vbData[index]=x,this._vbData[index+1]=y,this._vbData[index+2]=z;
this._vbData[index+3]=r,this._vbData[index+4]=g,this._vbData[index+5]=b,this._vbData[index+6]=a;
index+=this._floatSizePerVer;
if (index > this._posInVBData)
this._posInVBData=index;
return this;
}
__proto.addIndexes=function(__indexes){
var indexes=arguments;
if (!this._hasBegun)
this.addVertexIndexException();
for (var i=0;i < indexes.length;i++){
this._ibData[this._posInIBData]=indexes[i];
this._posInIBData++;
}
return this;
}
__proto.begin=function(primitive,wvpMatrix,renState){
if (this._hasBegun)
this.beginException0();
if (primitive!==/*laya.webgl.WebGLContext.LINES*/0x0001 && primitive!==/*laya.webgl.WebGLContext.TRIANGLES*/0x0004)
this.beginException1();
this._primitiveType=primitive;
this._wvpMatrix=wvpMatrix;
this._renderState=renState;
this._hasBegun=true;
return this;
}
__proto.end=function(){
if (!this._hasBegun)
this.endException();
this.flush();
this._hasBegun=false;
return this;
}
__proto.flush=function(){
if (this._posInVBData===0)
return;
this._ib.clear();
this._ib.append(this._ibData);
this._vb.clear();
this._vb.append(this._vbData);
this._vb.bind_upload(this._ib);
var presz=this._renderState.shaderValue.length;
var predef=this._renderState.shaderDefs.getValue();
this._shader=this.getShader(this._renderState);
this._renderState.shaderValue.pushValue(/*laya.d3.graphics.VertexElementUsage.POSITION0*/"POSITION",this._posShaderValue);
this._renderState.shaderValue.pushValue(/*laya.d3.graphics.VertexElementUsage.COLOR0*/"COLOR",this._colorShaderValue);
this._renderState.shaderValue.pushValue(/*laya.webgl.utils.Buffer2D.MVPMATRIX*/"MVPMATRIX",this._wvpMatrix.elements);
this._renderState.shaderValue.pushValue(/*laya.webgl.utils.Buffer2D.ALBEDO*/"ALBEDO",this._albedo.elements);
this._shader.uploadArray(this._renderState.shaderValue.data,this._renderState.shaderValue.length,null);
this._renderState.shaderDefs.setValue(predef);
this._renderState.shaderValue.length=presz;
Stat.drawCall++;
WebGL.mainContext.drawElements(this._primitiveType,this._posInIBData,/*laya.webgl.WebGLContext.UNSIGNED_SHORT*/0x1403,0);
this._posInIBData=0;
this._posInVBData=0;
}
__proto.getShader=function(state){
var preDef=state.shaderDefs._value;
state.shaderDefs._value=preDef & (~(/*laya.d3.shader.ShaderDefines3D.POINTLIGHT*/0x80 | /*laya.d3.shader.ShaderDefines3D.SPOTLIGHT*/0x100 | /*laya.d3.shader.ShaderDefines3D.DIRECTIONLIGHT*/0x40));
state.shaderDefs.add(/*laya.d3.shader.ShaderDefines3D.COLOR*/0x20);
var nameID=state.shaderDefs.getValue()+this._sharderNameID */*laya.webgl.shader.Shader.SHADERNAME2ID*/0.0002;
var shader=this._shader ? this._shader :Shader.getShader(nameID);
return shader || (shader=Shader.withCompile(this._sharderNameID,state.shaderDefs.toNameDic(),nameID,null));
}
__proto.addVertexIndexException=function(){
throw new Error("请先调用begin()函数");
}
__proto.beginException0=function(){
throw new Error("调用begin()前请确保已成功调用end()!");
}
__proto.beginException1=function(){
throw new Error("只支持“LINES”和“TRIANGLES”两种基元!");
}
__proto.endException=function(){
throw new Error("调用end()前请确保已成功调用begin()!");
}
__proto.drawLinesException=function(){
throw new Error("您必须确保在此之前已调用begin()且使用“LINES”基元!");
}
__proto.drawTrianglesException=function(){
throw new Error("您必须确保在此之前已调用begin()且使用“TRIANGLES”基元!");
}
return PhasorSpriter3D;
})()
/**
*<code>RenderConfig</code> 类用于实现渲染配置。
*/
//class laya.d3.core.render.RenderConfig
var RenderConfig=(function(){
function RenderConfig(){
this.depthTest=true;
this.depthMask=1;
this.blend=false;
this.cullFace=true;
this.sFactor=/*laya.webgl.WebGLContext.ONE*/1;
this.dFactor=/*laya.webgl.WebGLContext.ZERO*/0;
this.frontFace=/*laya.webgl.WebGLContext.CW*/0x0900;
}
__class(RenderConfig,'laya.d3.core.render.RenderConfig');
return RenderConfig;
})()
/**
*@private
*<code>RenderElement</code> 类用于实现渲染物体。
*/
//class laya.d3.core.render.RenderElement
var RenderElement=(function(){
function RenderElement(){
this._type=0;
this._mainSortID=0;
this._renderObject=null;
this._sprite3D=null;
this._material=null;
this._renderObj=null;
this._staticBatch=null;
this._batchIndexStart=0;
this._batchIndexEnd=0;
}
__class(RenderElement,'laya.d3.core.render.RenderElement');
var __proto=RenderElement.prototype;
/**
*@private
*/
__proto.getStaticBatchBakedVertexs=function(index){
var byteSizeInFloat=4;
var vb=this._renderObj._getVertexBuffer(index);
var bakedVertexes=vb.getData().slice();
var vertexDeclaration=vb.vertexDeclaration;
var positionOffset=vertexDeclaration.getVertexElementByUsage(/*laya.d3.graphics.VertexElementUsage.POSITION0*/"POSITION").offset / byteSizeInFloat;
var normalOffset=vertexDeclaration.getVertexElementByUsage(/*laya.d3.graphics.VertexElementUsage.NORMAL0*/"NORMAL").offset / byteSizeInFloat;
var rootTransform=this._staticBatch._rootSprite.transform.worldMatrix;
var transform=this._sprite3D.transform.worldMatrix;
var rootInvertMat=RenderElement._tempMatrix4x40;
var result=RenderElement._tempMatrix4x41;
rootTransform.invert(rootInvertMat);
Matrix4x4.multiply(rootInvertMat,transform,result);
var rotation=RenderElement._tempQuaternion0;
result.decompose(RenderElement._tempVector30,rotation,RenderElement._tempVector31);
var vertexFloatCount=vertexDeclaration.vertexStride / byteSizeInFloat;
for (var i=0,n=bakedVertexes.length;i < n;i+=vertexFloatCount){
var posOffset=i+positionOffset;
var norOffset=i+normalOffset;
Utils3D.transformVector3ArrayToVector3ArrayCoordinate(bakedVertexes,posOffset,result,bakedVertexes,posOffset);
Utils3D.transformVector3ArrayByQuat(bakedVertexes,normalOffset,rotation,bakedVertexes,normalOffset);
}
return bakedVertexes;
}
/**
*@private
*/
__proto.getDynamicBatchBakedVertexs=function(index){
var byteSizeInFloat=4;
var vb=this._renderObj._getVertexBuffer(index);
var bakedVertexes=vb.getData().slice();
var vertexDeclaration=vb.vertexDeclaration;
var positionOffset=vertexDeclaration.getVertexElementByUsage(/*laya.d3.graphics.VertexElementUsage.POSITION0*/"POSITION").offset / byteSizeInFloat;
var normalOffset=vertexDeclaration.getVertexElementByUsage(/*laya.d3.graphics.VertexElementUsage.NORMAL0*/"NORMAL").offset / byteSizeInFloat;
var transform=this._sprite3D.transform;
var worldMatrix=transform.worldMatrix;
var rotation=transform.rotation;
var vertexFloatCount=vertexDeclaration.vertexStride / byteSizeInFloat;
for (var i=0,n=bakedVertexes.length;i < n;i+=vertexFloatCount){
var posOffset=i+positionOffset;
var norOffset=i+normalOffset;
Utils3D.transformVector3ArrayToVector3ArrayCoordinate(bakedVertexes,posOffset,worldMatrix,bakedVertexes,posOffset);
Utils3D.transformVector3ArrayByQuat(bakedVertexes,normalOffset,rotation,bakedVertexes,normalOffset);
}
return bakedVertexes;
}
/**
*@private
*/
__proto.getBakedIndices=function(){
return this._renderObj._getIndexBuffer().getData();
}
__getset(0,__proto,'renderObj',function(){
return this._renderObj;
},function(value){
if (this._renderObj!==value){
this._renderObj=value;
}
});
__static(RenderElement,
['_tempVector30',function(){return this._tempVector30=new Vector3();},'_tempVector31',function(){return this._tempVector31=new Vector3();},'_tempQuaternion0',function(){return this._tempQuaternion0=new Quaternion();},'_tempMatrix4x40',function(){return this._tempMatrix4x40=new Matrix4x4();},'_tempMatrix4x41',function(){return this._tempMatrix4x41=new Matrix4x4();}
]);
return RenderElement;
})()
/**
*@private
*<code>RenderQuene</code> 类用于实现渲染队列。
*/
//class laya.d3.core.render.RenderQueue
var RenderQueue=(function(){
function RenderQueue(renderConfig,scene){
this._id=0;
this._needSort=false;
this._renderElements=null;
this._staticBatches=null;
this._renderableRenderObjects=null;
this._renderConfig=null;
this._staticBatchCombineRenderElements=null;
this._dynamicBatchCombineRenderElements=null;
this._finalElements=null;
this._scene=null;
this._id=++RenderQueue._uniqueIDCounter;
this._needSort=false;
this._renderConfig=renderConfig;
this._scene=scene;
this._renderElements=[];
this._renderableRenderObjects=[];
this._staticBatchCombineRenderElements=[];
this._dynamicBatchCombineRenderElements=[];
this._staticBatches=[];
}
__class(RenderQueue,'laya.d3.core.render.RenderQueue');
var __proto=RenderQueue.prototype;
__proto._sortAlphaFunc=function(a,b){
if (a._renderObject && b._renderObject)
return Vector3.distance(b._renderObject._boundingSphere.center,RenderQueue._cameraPosition)-Vector3.distance(a._renderObject._boundingSphere.center,RenderQueue._cameraPosition);
else
return 0;
}
/**
*@private
*/
__proto._begainRenderElement=function(state,renderObj,material){
if (renderObj._beforeRender(state)){
state.shaderValue.pushArray(renderObj._getVertexBuffer(0).vertexDeclaration.shaderValues);
return true;
}
return false;
}
/**
*@private
*/
__proto._endRenderElement=function(state,renderObj,material){
material._upload(state,renderObj._getVertexBuffer(0).vertexDeclaration,null);
renderObj._render(state);
}
/**
*@private
*更新组件preRenderUpdate函数
*@param state 渲染相关状态
*/
__proto._preRenderUpdateComponents=function(sprite3D,state){
for (var i=0;i < sprite3D.componentsCount;i++){
var component=sprite3D.getComponentByIndex(i);
(!component.started)&& (component._start(state),component.started=true);
(component.isActive)&& (component._preRenderUpdate(state));
}
}
/**
*@private
*更新组件postRenderUpdate函数
*@param state 渲染相关状态
*/
__proto._postRenderUpdateComponents=function(sprite3D,state){
for (var i=0;i < sprite3D.componentsCount;i++){
var component=sprite3D.getComponentByIndex(i);
(!component.started)&& (component._start(state),component.started=true);
(component.isActive)&& (component._postRenderUpdate(state));
}
}
/**
*@private
*/
__proto._sortAlpha=function(cameraPos){
RenderQueue._cameraPosition=cameraPos;
this._finalElements.sort(this._sortAlphaFunc);
}
/**
*@private
*应用渲染状态到显卡。
*@param gl WebGL上下文。
*/
__proto._setState=function(gl,state){
WebGLContext.setDepthTest(gl,this._renderConfig.depthTest);
WebGLContext.setDepthMask(gl,this._renderConfig.depthMask);
WebGLContext.setBlend(gl,this._renderConfig.blend);
WebGLContext.setBlendFunc(gl,this._renderConfig.sFactor,this._renderConfig.dFactor);
WebGLContext.setCullFace(gl,this._renderConfig.cullFace);
if (state.camera.renderTarget)
WebGLContext.setFrontFaceCCW(gl,this._renderConfig.frontFace===/*laya.webgl.WebGLContext.CW*/0x0900 ? /*laya.webgl.WebGLContext.CCW*/0x0901 :/*laya.webgl.WebGLContext.CW*/0x0900);
else
WebGLContext.setFrontFaceCCW(gl,this._renderConfig.frontFace);
}
/**
*@private
*准备渲染队列。
*@param state 渲染状态。
*/
__proto._preRender=function(state){
this._staticBatchCombineRenderElements.length=0;
for (var i=0,n=this._staticBatches.length;i < n;i++)
this._staticBatches[i]._getRenderElement(this._staticBatchCombineRenderElements);
this._finalElements=this._renderElements.concat(this._staticBatchCombineRenderElements,this._dynamicBatchCombineRenderElements);
}
/**
*@private
*渲染队列。
*@param state 渲染状态。
*/
__proto._render=function(state){
var preShaderValue=state.shaderValue.length;
var preShadeDef=state.shaderDefs.getValue();
for (var i=0,n=this._finalElements.length;i < n;i++){
var renderElement=this._finalElements[i];
var renderObj,material;
if (renderElement._type===0){
var owner=renderElement._sprite3D;
state.owner=owner;
state.renderElement=renderElement;
this._preRenderUpdateComponents(owner,state);
renderObj=renderElement.renderObj,material=renderElement._material;
if (this._begainRenderElement(state,renderObj,material)){
material._setLoopShaderParams(state,state.projectionViewMatrix,owner.transform.worldMatrix,renderElement.renderObj,material);
this._endRenderElement(state,renderObj,material);
}
this._postRenderUpdateComponents(owner,state);
}else if (renderElement._type===1){
var staticBatch=renderElement.renderObj;
state.owner=null;
state.renderElement=renderElement;
state._batchIndexStart=renderElement._batchIndexStart;
state._batchIndexEnd=renderElement._batchIndexEnd;
renderObj=renderElement.renderObj,material=renderElement._material;
if (this._begainRenderElement(state,renderObj,material)){
renderElement._material._setLoopShaderParams(state,state.projectionViewMatrix,staticBatch._rootSprite.transform.worldMatrix,renderElement.renderObj,renderElement._material);
this._endRenderElement(state,renderObj,material);
}
}else if (renderElement._type===2){
var dynamicBatch=renderElement.renderObj;
state.owner=null;
state.renderElement=renderElement;
state._batchIndexStart=renderElement._batchIndexStart;
state._batchIndexEnd=renderElement._batchIndexEnd;
renderObj=renderElement.renderObj,material=renderElement._material;
if (this._begainRenderElement(state,renderObj,material)){
renderElement._material._setLoopShaderParams(state,state.projectionViewMatrix,Matrix4x4.DEFAULT,renderElement.renderObj,renderElement._material);
this._endRenderElement(state,renderObj,material);
}
}
state.shaderDefs.setValue(preShadeDef);
state.shaderValue.length=preShaderValue;
}
}
/**
*清空队列中的渲染物体。
*/
__proto._clearRenderElements=function(){
this._staticBatches.length=0;
this._dynamicBatchCombineRenderElements.length=0;
this._renderElements.length=0;
this._needSort=true;
}
/**
*添加渲染物体。
*@param renderObj 渲染物体。
*/
__proto._addRenderElement=function(renderElement){
this._renderElements.push(renderElement);
this._needSort=true;
}
/**
*添加静态批处理。
*@param renderObj 静态批处理。
*/
__proto._addStaticBatch=function(staticBatch){
this._staticBatches.push(staticBatch)
}
/**
*添加动态批处理。
*@param renderObj 动态批处理。
*/
__proto._addDynamicBatchElement=function(dynamicBatchElement){
this._dynamicBatchCombineRenderElements.push(dynamicBatchElement);
}
/**
*获取唯一标识ID(通常用于优化或识别)。
*/
__getset(0,__proto,'id',function(){
return this._id;
});
RenderQueue._uniqueIDCounter=0;
RenderQueue.OPAQUE=1;
RenderQueue.OPAQUE_DOUBLEFACE=2;
RenderQueue.ALPHA_BLEND=3;
RenderQueue.ALPHA_BLEND_DOUBLEFACE=4;
RenderQueue.ALPHA_ADDTIVE_BLEND=5;
RenderQueue.ALPHA_ADDTIVE_BLEND_DOUBLEFACE=6;
RenderQueue.DEPTHREAD_ALPHA_BLEND=7;
RenderQueue.DEPTHREAD_ALPHA_BLEND_DOUBLEFACE=8;
RenderQueue.DEPTHREAD_ALPHA_ADDTIVE_BLEND=9;
RenderQueue.DEPTHREAD_ALPHA_ADDTIVE_BLEND_DOUBLEFACE=10;
RenderQueue.NONDEPTH_ALPHA_BLEND=11;
RenderQueue.NONDEPTH_ALPHA_BLEND_DOUBLEFACE=12;
RenderQueue.NONDEPTH_ALPHA_ADDTIVE_BLEND=13;
RenderQueue.NONDEPTH_ALPHA_ADDTIVE_BLEND_DOUBLEFACE=14;
RenderQueue._cameraPosition=null
return RenderQueue;
})()
/**
*<code>RenderState</code> 类用于实现渲染状态。
*/
//class laya.d3.core.render.RenderState
var RenderState=(function(){
function RenderState(){
this.elapsedTime=NaN;
this.loopCount=0;
this.context=null;
this.scene=null;
this.owner=null;
this.renderElement=null;
this._staticBatch=null;
this._batchIndexStart=0;
this._batchIndexEnd=0;
this.camera=null;
this.viewMatrix=null;
this.projectionMatrix=null;
this.projectionViewMatrix=null;
this.cameraBoundingFrustum=null;
this.viewport=null;
this.worldShaderValue=new ValusArray;
this.shaderValue=new ValusArray;
this.shaderDefs=new ShaderDefines3D();
this.reset();
}
__class(RenderState,'laya.d3.core.render.RenderState');
var __proto=RenderState.prototype;
/**
*重置。
*/
__proto.reset=function(){
this.worldShaderValue.length=0;
this.shaderValue.length=0;
this.shaderDefs.setValue(0);
(WebGL.frameShaderHighPrecision)&& (this.shaderDefs.setValue(/*laya.d3.shader.ShaderDefines3D.FSHIGHPRECISION*/0x100000));
}
RenderState.VERTEXSHADERING=0x04;
RenderState.PIXELSHADERING=0x08;
RenderState.clientWidth=0;
RenderState.clientHeight=0;
return RenderState;
})()
/**
*@private
*<code>DynamicBatch</code> 类用于动态批处理。
*/
//class laya.d3.graphics.DynamicBatch
var DynamicBatch=(function(){
function DynamicBatch(vertexDeclaration){
this._vertexDeclaration=null;
this._vertexDatas=null;
this._indexDatas=null;
this._vertexBuffer=null;
this._indexBuffer=null;
this._currentCombineVertexCount=0;
this._currentCombineIndexCount=0;
this._combineRenderElements=null;
this._materials=null;
this._materialToRenderElementsOffsets=null;
this._merageElements=null;
this._combineRenderElementPool=null;
this._combineRenderElementPoolIndex=0;
this._currentCombineVertexCount=0;
this._currentCombineIndexCount=0;
this._combineRenderElements=[];
this._materialToRenderElementsOffsets=[];
this._materials=[];
this._merageElements=[];
this._combineRenderElementPool=[];
this._combineRenderElementPoolIndex=0;
this._vertexDeclaration=vertexDeclaration;
}
__class(DynamicBatch,'laya.d3.graphics.DynamicBatch');
var __proto=DynamicBatch.prototype;
Laya.imps(__proto,{"laya.d3.core.render.IRenderable":true})
__proto._getVertexBuffer=function(index){
(index===void 0)&& (index=0);
if (index===0)
return this._vertexBuffer;
else
return null;
}
__proto._getIndexBuffer=function(){
return this._indexBuffer;
}
__proto._testTangent=function(state){}
//}
__proto._getCombineRenderElementFromPool=function(){
var renderElement=this._combineRenderElementPool[this._combineRenderElementPoolIndex++];
return renderElement || (this._combineRenderElementPool[this._combineRenderElementPoolIndex-1]=new RenderElement());
}
__proto._getRenderElement=function(){
if (!this._vertexDatas){
this._vertexDatas=new Float32Array(this._vertexDeclaration.vertexStride / 4 *DynamicBatch.maxVertexCount);
this._indexDatas=new Uint16Array(DynamicBatch.maxIndexCount);
this._vertexBuffer=VertexBuffer3D.create(this._vertexDeclaration,DynamicBatch.maxVertexCount,/*laya.webgl.WebGLContext.DYNAMIC_DRAW*/0x88E8);
this._indexBuffer=IndexBuffer3D.create(/*laya.d3.graphics.IndexBuffer3D.INDEXTYPE_USHORT*/"ushort",DynamicBatch.maxIndexCount,/*laya.webgl.WebGLContext.DYNAMIC_DRAW*/0x88E8);
}
this._merageElements.length=0;
var curMerVerCount=0;
var curIndexCount=0;
for (var i=0,n=this._combineRenderElements.length;i < n;i++){
var renderElement=this._combineRenderElements[i];
var subVertexDatas=renderElement.getDynamicBatchBakedVertexs(0);
var subIndexDatas=renderElement.getBakedIndices();
var indexOffset=curMerVerCount / (this._vertexDeclaration.vertexStride / 4);
var indexStart=curIndexCount;
var indexEnd=indexStart+subIndexDatas.length;
renderElement._batchIndexStart=indexStart;
renderElement._batchIndexEnd=indexEnd;
this._indexDatas.set(subIndexDatas,curIndexCount);
for (var k=indexStart;k < indexEnd;k++)
this._indexDatas[k]=indexOffset+this._indexDatas[k];
curIndexCount+=subIndexDatas.length;
this._vertexDatas.set(subVertexDatas,curMerVerCount);
curMerVerCount+=subVertexDatas.length;
}
this._vertexBuffer.setData(this._vertexDatas);
this._indexBuffer.setData(this._indexDatas);
this._combineRenderElementPoolIndex=0;
for (i=0,n=this._materials.length;i < n;i++){
var merageElement=this._getCombineRenderElementFromPool();
merageElement._type=2;
merageElement._staticBatch=null;
merageElement.renderObj=this;
var renderElementStartIndex=this._combineRenderElements[this._materialToRenderElementsOffsets[i]]._batchIndexStart;
var renderElementEndIndex=(i+1===this._materialToRenderElementsOffsets.length)? curIndexCount :this._combineRenderElements[this._materialToRenderElementsOffsets[i+1]]._batchIndexStart;
merageElement._batchIndexStart=renderElementStartIndex;
merageElement._batchIndexEnd=renderElementEndIndex;
merageElement._material=this._materials[i];
this._merageElements.push(merageElement);
}
}
__proto._addCombineRenderObjTest=function(renderElement){
var renderObj=renderElement.renderObj;
var indexCount=this._currentCombineIndexCount+renderObj._getIndexBuffer().indexCount;
var vertexCount=this._currentCombineVertexCount+renderObj._getVertexBuffer().vertexCount;
if (vertexCount > DynamicBatch.maxVertexCount || indexCount > DynamicBatch.maxIndexCount){
return false;
}
return true;
}
__proto._addCombineRenderObj=function(renderElement){
var renderObj=renderElement.renderObj;
this._combineRenderElements.push(renderElement);
this._currentCombineIndexCount=this._currentCombineIndexCount+renderObj._getIndexBuffer().indexCount;
this._currentCombineVertexCount=this._currentCombineVertexCount+renderObj._getVertexBuffer().vertexCount;
}
__proto._addCombineMaterial=function(material){
this._materials.push(material);
}
__proto._addMaterialToRenderElementOffset=function(offset){
this._materialToRenderElementsOffsets.push(offset);
}
__proto._clearRenderElements=function(){
this._combineRenderElements.length=0;
this._materials.length=0;
this._materialToRenderElementsOffsets.length=0;
this._currentCombineVertexCount=0;
this._currentCombineIndexCount=0;
}
__proto._addToRenderQueue=function(scene){
this._getRenderElement();
for (var i=0,n=this._materials.length;i < n;i++)
scene.getRenderQueue(this._materials[i].renderQueue)._addDynamicBatchElement(this._merageElements[i]);
}
__proto._beforeRender=function(state){
this._vertexBuffer._bind();
this._indexBuffer._bind();
return true;
}
__proto._render=function(state){
var indexCount=state._batchIndexEnd-state._batchIndexStart;
state.context.drawElements(/*laya.webgl.WebGLContext.TRIANGLES*/0x0004,indexCount,/*laya.webgl.WebGLContext.UNSIGNED_SHORT*/0x1403,state._batchIndexStart *2);
Stat.drawCall++;
Stat.trianglesFaces+=indexCount / 3;
}
__getset(0,__proto,'indexOfHost',function(){
return 0;
});
__getset(0,__proto,'_vertexBufferCount',function(){
return 1;
});
__getset(0,__proto,'triangleCount',function(){
return this._indexBuffer.indexCount / 3;
});
__getset(0,__proto,'combineRenderElementsCount',function(){
return this._combineRenderElements.length;
});
DynamicBatch.maxVertexCount=20000;
DynamicBatch.maxIndexCount=40000;
DynamicBatch.maxCombineTriangleCount=50;
return DynamicBatch;
})()
/**
*@private
*<code>DynamicBatchManager</code> 类用于管理动态批处理。
*/
//class laya.d3.graphics.DynamicBatchManager
var DynamicBatchManager=(function(){
function DynamicBatchManager(){
this._dynamicBatches=null;
this._prepareDynamicBatchCombineElements=null;
this._dynamicBatches={};
this._prepareDynamicBatchCombineElements=[];
}
__class(DynamicBatchManager,'laya.d3.graphics.DynamicBatchManager');
var __proto=DynamicBatchManager.prototype;
__proto.getDynamicBatch=function(_vertexDeclaration,number){
var dynamicBatch;
var key=_vertexDeclaration.id.toString()+number;
if (!this._dynamicBatches[key]){
this._dynamicBatches[key]=dynamicBatch=new DynamicBatch(_vertexDeclaration);
}else {
dynamicBatch=this._dynamicBatches[key];
}
return dynamicBatch;
}
/**需手动调用*/
__proto._garbageCollection=function(){
for (var key in this._dynamicBatches)
if (this._dynamicBatches[key].combineRenderElementsCount===0)
delete this._dynamicBatches[key];
}
__proto._addPrepareRenderElement=function(renderElement){
this._prepareDynamicBatchCombineElements.push(renderElement);
}
/**@private */
__proto._finishCombineDynamicBatch=function(scene){
this._prepareDynamicBatchCombineElements.sort(DynamicBatchManager._sortPrepareDynamicBatch);
var lastMaterial;
var lastVertexDeclaration;
var lastRenderElement;
var lastBatchNumber=-1;
var lastCanMerage=true;
var curMaterial;
var curRenderElement;
var curDynamicBatch;
var curbatchNumber=0;
var laterAddMaterial;
var laterAddRenderElement;
var laterAddMatToElementOffset=-1;
for (var i=0,n=this._prepareDynamicBatchCombineElements.length;i < n;i++){
curRenderElement=this._prepareDynamicBatchCombineElements[i];
var curDeclaration=curRenderElement.renderObj._getVertexBuffer(0).vertexDeclaration;
var declarationChanged=(lastVertexDeclaration!==curDeclaration);
declarationChanged && (curbatchNumber=0,lastVertexDeclaration=curDeclaration);
var batchNumbrChanged=(curbatchNumber!==lastBatchNumber);
batchNumbrChanged && (lastBatchNumber=curbatchNumber);
if ((declarationChanged)|| batchNumbrChanged){
curDynamicBatch=this.getDynamicBatch(curDeclaration,curbat