LayaAir2.x 动态创建网格(一) 单一材质贴图

export default class Test extends Laya.Script{
	
	protected onAwake():void{
		let scene = Laya.stage.addChild(new Laya.Scene3D());
		console.log(scene);
			
		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 sprite3D = new Laya.Sprite3D();
		scene.addChild(sprite3D);
		
		//创建网格
		let mesh=this.createTriangle();
		let meshSprite3d = new Laya.MeshSprite3D(mesh);
		
		sprite3D.addChild(meshSprite3d);
		meshSprite3d.transform.position = new Laya.Vector3(0, 0, 0);
		meshSprite3d.transform.rotate(new Laya.Vector3(0, 0, 0), false, false);
		
		//设置材质、贴图
		Laya.Texture2D.load("res/atlas/comp.png",Laya.Handler.create(null,(texture:Laya.Texture2D)=>{
			//在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;
			meshSprite3d.meshRenderer.material=material;
		}));
	}
	
	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 createMesh(vertexDeclaration:Laya.VertexDeclaration, vertices:Float32Array, indices:Uint16Array):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 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;
		let subMeshes = [];
		subMeshes.push(subMesh);
		mesh["_setSubMeshes"](subMeshes);
		
		mesh.calculateBounds();
		let memorySize = vertexBuffer._byteLength + indexBuffer._byteLength;
		mesh["_setCPUMemory"](memorySize);
		mesh["_setGPUMemory"](memorySize);
		return mesh;
	}
	
}
原文地址:https://www.cnblogs.com/kingBook/p/14241412.html