[]让 CylinderMesh 支持椎体

function CylinderMesh(radius,radius2,height,slices){
创建出下面半径大小
 
修改第二个循环结构, 也就是柱状顶点绘制
 
__proto.recreateResource=function(){
this._numberVertices=(this._slices+1+1)+(this._slices+1)*2+(this._slices+1+1);
this._numberIndices=3 *this._slices+6 *this._slices+3 *this._slices;
var vertexDeclaration=VertexPositionNormalTexture.vertexDeclaration;
var vertexFloatStride=vertexDeclaration.vertexStride / 4;
var vertices=new Float32Array(this._numberVertices *vertexFloatStride);
var indices=new Uint16Array(this._numberIndices);
var sliceAngle=(Math.PI *2.0)/ this._slices;
var halfHeight=this._height / 2;
var curAngle=0;
var verticeCount=0;
var posX=0;
var posY=0;
var posZ=0;
var vc=0;
var ic=0;
for (var tv=0;tv <=this._slices;tv++){
if (tv===0){
vertices[vc++]=0;
vertices[vc++]=halfHeight;
vertices[vc++]=0;
vertices[vc++]=0;
vertices[vc++]=1;
vertices[vc++]=0;
vertices[vc++]=0.5;
vertices[vc++]=0.5;
}
curAngle=tv *sliceAngle;
posX=Math.cos(curAngle)*this._radius;
posY=halfHeight;
posZ=Math.sin(curAngle)*this._radius;
vertices[vc++]=posX;
vertices[vc++]=posY;
vertices[vc++]=posZ;
vertices[vc++]=0;
vertices[vc++]=1;
vertices[vc++]=0;
vertices[vc++]=0.5+Math.cos(curAngle)*0.5;
vertices[vc++]=0.5+Math.sin(curAngle)*0.5;
}
for (var ti=0;ti < this._slices;ti++){
indices[ic++]=0;
indices[ic++]=ti+1;
indices[ic++]=ti+2;
}
verticeCount+=this._slices+1+1;
for (var rv=0;rv <=this._slices;rv++){
var Cos = Math.cos(curAngle+Math.PI);
var Sin = Math.sin(curAngle+Math.PI);
curAngle=rv *sliceAngle;
posX=Cos*this._radius;
posY=halfHeight;
posZ=Sin*this._radius;
posX2 = Cos*this._radius2;
posZ2 = Sin*this._radius2;
vertices[vc++]=posX;
vertices[vc+(this._slices+1)*8-1]=posX2;
vertices[vc++]=posY;
vertices[vc+(this._slices+1)*8-1]=-posY;
vertices[vc++]=posZ;
vertices[vc+(this._slices+1)*8-1]=posZ2;
vertices[vc++]=posX2;
vertices[vc+(this._slices+1)*8-1]=posX;
vertices[vc++]=0;
vertices[vc+(this._slices+1)*8-1]=0;
vertices[vc++]=posZ2;
vertices[vc+(this._slices+1)*8-1]=posZ;
vertices[vc++]=1-rv *1 / this._slices;
vertices[vc+(this._slices+1)*8-1]=1-rv *1 / this._slices;
vertices[vc++]=0;
vertices[vc+(this._slices+1)*8-1]=1;
// console.log(posY);//(curAngle/Math.PI*180);
}
vc+=(this._slices+1)*8;
for (var ri=0;ri < this._slices;ri++){
indices[ic++]=ri+verticeCount+(this._slices+1);
indices[ic++]=ri+verticeCount+1;
indices[ic++]=ri+verticeCount;
indices[ic++]=ri+verticeCount+(this._slices+1);
indices[ic++]=ri+verticeCount+(this._slices+1)+1;
indices[ic++]=ri+verticeCount+1;
}
verticeCount+=2 *(this._slices+1);
for (var bv=0;bv <=this._slices;bv++){
if (bv===0){
vertices[vc++]=0;
vertices[vc++]=-halfHeight;
vertices[vc++]=0;
vertices[vc++]=0;
vertices[vc++]=-1;
vertices[vc++]=0;
vertices[vc++]=0.5;
vertices[vc++]=0.5;
}
curAngle=bv *sliceAngle;
posX=Math.cos(curAngle+Math.PI)*this._radius;
posY=-halfHeight;
posZ=Math.sin(curAngle+Math.PI)*this._radius;
vertices[vc++]=posX;
vertices[vc++]=posY;
vertices[vc++]=posZ;
vertices[vc++]=0;
vertices[vc++]=-1;
vertices[vc++]=0;
vertices[vc++]=0.5+Math.cos(curAngle)*0.5;
vertices[vc++]=0.5+Math.sin(curAngle)*0.5;
}
for (var bi=0;bi < this._slices;bi++){
indices[ic++]=0+verticeCount;
indices[ic++]=bi+2+verticeCount;
indices[ic++]=bi+1+verticeCount;
}
verticeCount+=this._slices+1+1;
this._vertexBuffer=new VertexBuffer3D(vertexDeclaration,this._numberVertices,/*laya.webgl.WebGLContext.STATIC_DRAW*/0x88E4,true);
this._indexBuffer=new IndexBuffer3D(/*laya.d3.graphics.IndexBuffer3D.INDEXTYPE_USHORT*/"ushort",this._numberIndices,/*laya.webgl.WebGLContext.STATIC_DRAW*/0x88E4,true);
this._vertexBuffer.setData(vertices);
this._indexBuffer.setData(indices);
this.memorySize=(this._vertexBuffer._byteLength+this._indexBuffer._byteLength)*2;
this.completeCreate();
}
已邀请:

zszen

赞同来自:

喝喝 贴错了, 这个是正确的
 
__proto.recreateResource=function(){
this._numberVertices=(this._slices+1+1)+(this._slices+1)*2+(this._slices+1+1);
this._numberIndices=3 *this._slices+6 *this._slices+3 *this._slices;
var vertexDeclaration=VertexPositionNormalTexture.vertexDeclaration;
var vertexFloatStride=vertexDeclaration.vertexStride / 4;
var vertices=new Float32Array(this._numberVertices *vertexFloatStride);
var indices=new Uint16Array(this._numberIndices);
var sliceAngle=(Math.PI *2.0)/ this._slices;
var halfHeight=this._height / 2;
var curAngle=0;
var verticeCount=0;
var posX=0;
var posY=0;
var posZ=0;
var vc=0;
var ic=0;
for (var tv=0;tv <=this._slices;tv++){
if (tv===0){
vertices[vc++]=0;
vertices[vc++]=halfHeight;
vertices[vc++]=0;
vertices[vc++]=0;
vertices[vc++]=1;
vertices[vc++]=0;
vertices[vc++]=0.5;
vertices[vc++]=0.5;
}
curAngle=tv *sliceAngle;
posX=Math.cos(curAngle)*this._radius;
posY=halfHeight;
posZ=Math.sin(curAngle)*this._radius;
vertices[vc++]=posX;
vertices[vc++]=posY;
vertices[vc++]=posZ;
vertices[vc++]=0;
vertices[vc++]=1;
vertices[vc++]=0;
vertices[vc++]=0.5+Math.cos(curAngle)*0.5;
vertices[vc++]=0.5+Math.sin(curAngle)*0.5;
}
for (var ti=0;ti < this._slices;ti++){
indices[ic++]=0;
indices[ic++]=ti+1;
indices[ic++]=ti+2;
}
verticeCount+=this._slices+1+1;
for (var rv=0;rv <=this._slices;rv++){
curAngle=rv*sliceAngle;
var Cos = Math.cos(curAngle+Math.PI);
var Sin = Math.sin(curAngle+Math.PI);
posX=Cos*this._radius;
posY=halfHeight;
posZ=Sin*this._radius;
posX2=Cos*this._radius2;
posZ2=Sin*this._radius2;
vertices[vc++]=posX;
vertices[vc+(this._slices+1)*8-1]=posX2;
vertices[vc++]=posY;
vertices[vc+(this._slices+1)*8-1]=-posY;
vertices[vc++]=posZ;
vertices[vc+(this._slices+1)*8-1]=posZ2;
vertices[vc++]=posX;
vertices[vc+(this._slices+1)*8-1]=posX2;
vertices[vc++]=0;
vertices[vc+(this._slices+1)*8-1]=0;
vertices[vc++]=posZ;
vertices[vc+(this._slices+1)*8-1]=posZ2;
vertices[vc++]=1-rv *1 / this._slices;
vertices[vc+(this._slices+1)*8-1]=1-rv *1 / this._slices;
vertices[vc++]=0;
vertices[vc+(this._slices+1)*8-1]=1;
// console.log(posY);//(curAngle/Math.PI*180);
}
vc+=(this._slices+1)*8;
for (var ri=0;ri < this._slices;ri++){
indices[ic++]=ri+verticeCount+(this._slices+1);
indices[ic++]=ri+verticeCount+1;
indices[ic++]=ri+verticeCount;
indices[ic++]=ri+verticeCount+(this._slices+1);
indices[ic++]=ri+verticeCount+(this._slices+1)+1;
indices[ic++]=ri+verticeCount+1;
}
verticeCount+=2 *(this._slices+1);
for (var bv=0;bv <=this._slices;bv++){
if (bv===0){
vertices[vc++]=0;
vertices[vc++]=-halfHeight;
vertices[vc++]=0;
vertices[vc++]=0;
vertices[vc++]=-1;
vertices[vc++]=0;
vertices[vc++]=0.5;
vertices[vc++]=0.5;
}
curAngle=bv *sliceAngle;
posX=Math.cos(curAngle+Math.PI)*this._radius2;
posY=-halfHeight;
posZ=Math.sin(curAngle+Math.PI)*this._radius2;
vertices[vc++]=posX;
vertices[vc++]=posY;
vertices[vc++]=posZ;
vertices[vc++]=0;
vertices[vc++]=-1;
vertices[vc++]=0;
vertices[vc++]=0.5+Math.cos(curAngle)*0.5;
vertices[vc++]=0.5+Math.sin(curAngle)*0.5;
}
for (var bi=0;bi < this._slices;bi++){
indices[ic++]=0+verticeCount;
indices[ic++]=bi+2+verticeCount;
indices[ic++]=bi+1+verticeCount;
}
verticeCount+=this._slices+1+1;
this._vertexBuffer=new VertexBuffer3D(vertexDeclaration,this._numberVertices,/*laya.webgl.WebGLContext.STATIC_DRAW*/0x88E4,true);
this._indexBuffer=new IndexBuffer3D(/*laya.d3.graphics.IndexBuffer3D.INDEXTYPE_USHORT*/"ushort",this._numberIndices,/*laya.webgl.WebGLContext.STATIC_DRAW*/0x88E4,true);
this._vertexBuffer.setData(vertices);
this._indexBuffer.setData(indices);
this.memorySize=(this._vertexBuffer._byteLength+this._indexBuffer._byteLength)*2;
this.completeCreate();
}

Aar0n

赞同来自:

感谢分享~!

要回复问题请先

商务合作
商务合作