package laya.d3.core {
import laya.d3.graphics.IndexBuffer3D;
import laya.d3.graphics.VertexBuffer3D;
import laya.d3.graphics.VertexDeclaration;
import laya.d3.graphics.VertexElement;
import laya.d3.graphics.VertexElementFormat;
import laya.d3.graphics.VertexElementUsage;
import laya.d3.math.Vector3;
import laya.d3.math.Vector4;
import laya.d3.shader.Shader3D;
import laya.d3.shader.ShaderCompile3D;
import laya.d3.shader.ValusArray;
import laya.utils.Stat;
import laya.webgl.WebGL;
import laya.webgl.WebGLContext;
/**
* @private
* <code>PhasorSpriter3D</code> 类用于创建矢量笔刷。
*/
public class PhasorSpriter3D {
/**混合状态_不透明*/
public static const BLENDSTATE_OPAQUE:int = 0;
/**混合状态_加色法*/
public static const BLENDSTATE_ADDITIVE:int = 1;
/**混合状态_透明混合*/
public static const BLENDSTATE_ALPHABLEND:int = 2;
/**深度状态_默认*/
public static const DEPTHSTATE_DEFAULT:int = 0;
/**深度状态_只读*/
public static const DEPTHSTATE_ONLYREAD:int = 1;
/**深度状态_无*/
public static const DEPTHSTATE_NONE:int = 2;
private static const _vertexDeclaration:VertexDeclaration = new VertexDeclaration(28, [new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.POSITION0), new VertexElement(12, VertexElementFormat.Vector4, VertexElementUsage.COLOR0)]);
private var _tempInt0:int;
private var _tempInt1:int;
private var _tempUint0:uint;
private var _tempUint1:uint;
private var _tempUint2:uint;
private var _tempUint3:uint;
private var _tempUint4:uint;
private var _tempUint5:uint;
private var _tempUint6:uint;
private var _tempUint7:uint;
private var _tempNumver0:Number;
private var _tempNumver1:Number;
private var _tempNumver2:Number;
private var _tempNumver3:Number;
private const _floatSizePerVer:int = 7;//顶点结构为Position(3个float)+Color(4个float)
private const _defaultBufferSize:int = 600 * _floatSizePerVer;
private var _vbData:Float32Array = new Float32Array(_defaultBufferSize);
private var _vb:VertexBuffer3D;
private var _posInVBData:uint;
private var _ibData:Uint16Array = new Uint16Array(_defaultBufferSize);
private var _ib:IndexBuffer3D;
private var _posInIBData:uint;
private var _primitiveType:Number;
private var _blendState:Number;
private var _depthState:Number;
private var _hasBegun:Boolean;
private var _numVertsPerPrimitive:uint;
private var _camera:BaseCamera;
private var _sharderNameID:int;
private var _shader:Shader3D;
/** @private */
protected var _shaderCompile:ShaderCompile3D;
/** @private */
private var _spriteShaderValue:ValusArray = new ValusArray();
public function PhasorSpriter3D() {
super();
_vb = VertexBuffer3D.create(_vertexDeclaration, _defaultBufferSize / _floatSizePerVer, WebGLContext.DYNAMIC_DRAW);
_ib = IndexBuffer3D.create(IndexBuffer3D.INDEXTYPE_USHORT, _defaultBufferSize, WebGLContext.DYNAMIC_DRAW);
_sharderNameID = Shader3D.nameKey.getID("LINE");
_shaderCompile = ShaderCompile3D._preCompileShader[_sharderNameID];
}
public function line(startPosition:Vector3, startColor:Vector4, endPosition:Vector3, endColor:Vector4):PhasorSpriter3D {
if (!_hasBegun || _primitiveType !== WebGLContext.LINES)
drawLinesException();
if (_posInVBData + 2 * _floatSizePerVer > _vbData.length || _posInIBData + 2 > _ibData.length)
_flush();
_tempUint0 = _posInVBData / _floatSizePerVer;//_tempUint0为curVBPosVertex
addVertex(startPosition.x, startPosition.y, startPosition.z, startColor.x, startColor.y, startColor.z, startColor.w); // start
addVertex(endPosition.x, endPosition.y, endPosition.z, endColor.x, endColor.y, endColor.z, endColor.w); // end
addIndexes(_tempUint0, _tempUint0 + 1);
return this;
}
public function circle(radius:Number, numberOfPoints:uint, r:Number, g:Number, b:Number, a:Number):PhasorSpriter3D {
if (!_hasBegun || (_primitiveType !== WebGLContext.LINES))
drawLinesException();
_tempUint0 = numberOfPoints * 2;//_tempUint0为总顶点数
if (_posInVBData + _tempUint0 * _floatSizePerVer > _vbData.length || _posInIBData + 2 * _tempUint0 > _ibData.length)
_flush();
_tempUint1 = _posInVBData / _floatSizePerVer;//_tempUint1为curVBPosVertex
for (_tempNumver0 = 0, _tempInt0 = 0; _tempNumver0 < 3.1416 * 2; _tempNumver0 = _tempNumver0 + (3.1416 / numberOfPoints), _tempInt0++) {
addVertex(Math.sin(_tempNumver0) * radius, Math.cos(_tempNumver0) * radius, 0, r, g, b, a);
if (_tempInt0 === 0) {
addIndexes(_tempUint1);
} else if (_tempInt0 === _tempUint0 - 1) {
_tempUint2 = _tempUint1 + _tempInt0;//_tempUit2为当前索引数
addIndexes(_tempUint2, _tempUint2, _tempUint1);
} else {
_tempUint2 = _tempUint1 + _tempInt0; //_tempUint2为当前索引数
addIndexes(_tempUint2, _tempUint2);
}
}
return this;
}
public function plane(positionX:Number, positionY:Number, positionZ:Number, width:Number, height:Number, r:Number, g:Number, b:Number, a:Number):PhasorSpriter3D {
if (!_hasBegun || _primitiveType !== WebGLContext.TRIANGLES)
drawTrianglesException();
if (_posInVBData + 4 * _floatSizePerVer > _vbData.length || _posInIBData + 6 > _ibData.length)
_flush();
_tempNumver0 = width / 2;//tempNumver0为halfWidth
_tempNumver1 = height / 2;//tempNumver1为halfHeight
_tempUint0 = _posInVBData / _floatSizePerVer;//_tempUint0为curVBPosVertex
addVertex(positionX - _tempNumver0, positionY + _tempNumver1, positionZ, r, g, b, a); // LeftUp
addVertex(positionX + _tempNumver0, positionY + _tempNumver1, positionZ, r, g, b, a); // RightUp
addVertex(positionX - _tempNumver0, positionY - _tempNumver1, positionZ, r, g, b, a); // LeftBottom
addVertex(positionX + _tempNumver0, positionY - _tempNumver1, positionZ, r, g, b, a); // RightBottom
_tempUint1 = _tempUint0 + 1;//_tempUint0为curVBPosVertex1
_tempUint2 = _tempUint0 + 2;//_tempUint1为curVBPosVertex2
addIndexes(_tempUint0, _tempUint1, _tempUint2, _tempUint2, _tempUint1, _tempUint0 + 3);
return this;
}
public function box(positionX:Number, positionY:Number, positionZ:Number, width:Number, height:Number, depth:Number, r:Number, g:Number, b:Number, a:Number):PhasorSpriter3D {
if (!_hasBegun || _primitiveType !== WebGLContext.TRIANGLES)
drawTrianglesException();
if (_posInVBData + 8 * _floatSizePerVer > _vbData.length || _posInIBData + 36 > _ibData.length)
_flush();
_tempNumver0 = width / 2;//tempNumver0为halfWidth
_tempNumver1 = height / 2;//tempNumver1为halfHeight
_tempNumver2 = depth / 2;//tempNumver2为halfDepth
_tempUint0 = _posInVBData / _floatSizePerVer;//_tempUint0为curVBPosVertex
//frontVertex
addVertex(positionX - _tempNumver0, positionY + _tempNumver1, positionZ + _tempNumver2, r, g, b, a); // V0
addVertex(positionX + _tempNumver0, positionY + _tempNumver1, positionZ + _tempNumver2, r, g, b, a); // V1
addVertex(positionX - _tempNumver0, positionY - _tempNumver1, positionZ + _tempNumver2, r, g, b, a); // V2
addVertex(positionX + _tempNumver0, positionY - _tempNumver1, positionZ + _tempNumver2, r, g, b, a); // V3
//backVertex
addVertex(positionX + _tempNumver0, positionY + _tempNumver1, positionZ - _tempNumver2, r, g, b, a); // V4
addVertex(positionX - _tempNumver0, positionY + _tempNumver1, positionZ - _tempNumver2, r, g, b, a); // V5
addVertex(positionX + _tempNumver0, positionY - _tempNumver1, positionZ - _tempNumver2, r, g, b, a); // V6
addVertex(positionX - _tempNumver0, positionY - _tempNumver1, positionZ - _tempNumver2, r, g, b, a); // V7
// v5----- v4
// /| /|
// v0----- v1
评论3
最新资源