LayaAir2.x 动态创建网格(二) 多个材质贴图
作者:互联网
export default class Test extends Laya.Script{
protected onAwake():void{
//创建3d场景
let scene = Laya.stage.addChild(new Laya.Scene3D());
//创建相机
let camera = new Laya.Camera(0, 0.1, 100);
scene.addChild(camera);
camera.transform.translate(new Laya.Vector3(0, 0, 5));
camera.transform.rotate(new Laya.Vector3(0, 0, 0), true, false);
//创建灯光
let directionLight = new Laya.DirectionLight();
scene.addChild(directionLight);
let mat = directionLight.transform.worldMatrix;
mat.setForward(new Laya.Vector3(1.0, -1.0, -1.0));
directionLight.transform.worldMatrix = mat;
//创建网格
//let mesh=this.createTriangle();
let mesh=this.createQuad2();
let meshSprite3d = new Laya.MeshSprite3D(mesh);
scene.addChild(meshSprite3d);
meshSprite3d.transform.position = new Laya.Vector3(0, 0, 0);
meshSprite3d.transform.rotate(new Laya.Vector3(0, 0, 0), false, false);
//设置材质、贴图
let textureUrls=["res/atlas/comp.png","res/flower.jpg"];
Laya.loader.create(textureUrls.slice(),Laya.Handler.create(null,()=>{//注意此处的 textureUrls.slice() 对数组进行浅拷贝,因为 Laya.loader.create() 内部会对数组进行修改
let materials=[];
for(let i=0;i<textureUrls.length;i++){
let texture:Laya.Texture2D=Laya.loader.getRes(textureUrls[i]);
//在U方向上使用WARPMODE_CLAMP
texture.wrapModeU = Laya.WarpMode.Clamp;
//在V方向使用WARPMODE_REPEAT
texture.wrapModeV = Laya.WarpMode.Repeat;
//设置过滤方式
texture.filterMode = Laya.FilterMode.Bilinear;
//设置各向异性等级
texture.anisoLevel = 2;
//设置材质
let material=new Laya.BlinnPhongMaterial();
material.albedoTexture=texture;
materials[i]=material;
}
meshSprite3d.meshRenderer.materials=materials;
}));
}
/*private createQuad():Laya.Mesh{
const long=2;
const width=1;
let vertexDeclaration = Laya.VertexMesh.getVertexDeclaration("POSITION,NORMAL,UV");
let halfLong = long / 2;
let halfWidth = width / 2;
let vertices = new Float32Array([
-halfLong, halfWidth, 0, 0, 0, 1, 0, 0,
halfLong, halfWidth, 0, 0, 0, 1, 1, 0,
-halfLong, -halfWidth, 0, 0, 0, 1, 0, 1,
halfLong, -halfWidth, 0, 0, 0, 1, 1, 1
]);
let indices = new Uint16Array([0, 1, 2, 3, 2, 1]);
return this.createMesh(vertexDeclaration, vertices, indices);
}*/
/*private createTriangle():Laya.Mesh{
const w=2;
const h=1;
let vertexDeclaration = Laya.VertexMesh.getVertexDeclaration("POSITION,NORMAL,UV");
let vertices = new Float32Array([
-w, h,0, 0,0,1, 0,0,
w, h,0, 0,0,1, 1,0,
-w,-h,0, 0,0,1, 0,1,
]);
let indices = new Uint16Array([
0,1,2
]);
return this.createMesh(vertexDeclaration, vertices, indices);
}*/
private createQuad2():Laya.Mesh{
const w=2;
const h=1;
let vertexDeclaration = Laya.VertexMesh.getVertexDeclaration("POSITION,NORMAL,UV");
let vertices = new Float32Array([
-w, h,0, 0,0,1, 0,0,
w, h,0, 0,0,1, 1,0,
-w,-h,0, 0,0,1, 0,1,
w,-h,0, 0,0,1, 1,1
]);
let indices = new Uint16Array([
0,1,2,
3,2,1
]);
let subMeshIndexRangeList=new Uint16Array([
0,3,
3,3
])
return this.createMesh(vertexDeclaration, vertices, indices,subMeshIndexRangeList);
}
/**
* 创建网格
* @param vertexDeclaration 顶点声明,如:let vertexDeclaration = Laya.VertexMesh.getVertexDeclaration("POSITION,NORMAL,UV");
* @param vertices 顶点列表
* @param indices 三角形索引列表
* @param subMeshIndexRangeList 各子网格的三角形索引起始点和计数,长度必须是2的倍数且大于等于2,如:[0,3, 3,3]
*/
private createMesh(vertexDeclaration:Laya.VertexDeclaration, vertices:Float32Array, indices:Uint16Array,subMeshIndexRangeList:Uint16Array=null):Laya.Mesh{
let gl = Laya["LayaGL"].instance;
let mesh = new Laya.Mesh();
let vertexBuffer = new Laya.VertexBuffer3D(vertices.length * 4, gl.STATIC_DRAW, true);
vertexBuffer.vertexDeclaration = vertexDeclaration;
vertexBuffer.setData(vertices.buffer);
mesh["_vertexBuffer"] = vertexBuffer;
mesh["_vertexCount"] = vertexBuffer._byteLength / vertexDeclaration.vertexStride;
let indexBuffer = new Laya.IndexBuffer3D(Laya.IndexFormat.UInt16, indices.length, gl.STATIC_DRAW, true);
indexBuffer.setData(indices);
mesh["_indexBuffer"] = indexBuffer;
mesh["_setBuffer"](vertexBuffer, indexBuffer);
mesh["_setInstanceBuffer"](mesh["_instanceBufferStateType"]);
let subMeshes = [];
if(subMeshIndexRangeList && subMeshIndexRangeList.length>0){
for(let i=0,len=subMeshIndexRangeList.length/2;i<len;i++){
let subMeshIndexStart=subMeshIndexRangeList[i*2+0];
let subMeshIndexCount=subMeshIndexRangeList[i*2+1];
let subMesh = new Laya.SubMesh(mesh);
subMesh["_vertexBuffer"] = vertexBuffer;
subMesh["_indexBuffer"] = indexBuffer;
subMesh["_setIndexRange"](subMeshIndexStart, subMeshIndexCount);
let subIndexBufferStart = subMesh["_subIndexBufferStart"];
let subIndexBufferCount = subMesh["_subIndexBufferCount"];
let boneIndicesList = subMesh["_boneIndicesList"];
subIndexBufferStart.length = 1;
subIndexBufferCount.length = 1;
boneIndicesList.length = 1;
subIndexBufferStart[0] = subMeshIndexStart;
subIndexBufferCount[0] = subMeshIndexCount;
subMeshes.push(subMesh);
}
}else{
let subMesh = new Laya.SubMesh(mesh);
subMesh["_vertexBuffer"] = vertexBuffer;
subMesh["_indexBuffer"] = indexBuffer;
subMesh["_setIndexRange"](0, indexBuffer.indexCount);
let subIndexBufferStart = subMesh["_subIndexBufferStart"];
let subIndexBufferCount = subMesh["_subIndexBufferCount"];
let boneIndicesList = subMesh["_boneIndicesList"];
subIndexBufferStart.length = 1;
subIndexBufferCount.length = 1;
boneIndicesList.length = 1;
subIndexBufferStart[0] = 0;
subIndexBufferCount[0] = indexBuffer.indexCount;
subMeshes.push(subMesh);
}
mesh["_setSubMeshes"](subMeshes);
mesh.calculateBounds();
let memorySize = vertexBuffer._byteLength + indexBuffer._byteLength;
mesh["_setCPUMemory"](memorySize);
mesh["_setGPUMemory"](memorySize);
return mesh;
}
}
标签:贴图,addChild,Laya,Vector3,transform,动态创建,let,new,LayaAir2 来源: https://www.cnblogs.com/kingBook/p/14246092.html