import { Camera , CameraEventFlags } from "laya/d3/core/Camera" ; import { CommandBuffer } from "laya/d3/core/render/command/CommandBuffer" ; import { DrawMeshInstancedCMD } from "laya/d3/core/render/command/DrawMeshInstancedCMD" ; import { InstanceLocation , MaterialInstancePropertyBlock } from "laya/d3/core/render/command/MaterialInstancePropertyBlock" ; import { VertexMesh } from "laya/d3/graphics/Vertex/VertexMesh" ; import { VertexDeclaration } from "laya/d3/graphics/VertexDeclaration" ; import { Matrix4x4 } from "laya/d3/math/Matrix4x4" ; import { Mesh } from "laya/d3/resource/models/Mesh" ; import { PrimitiveMesh } from "laya/d3/resource/models/PrimitiveMesh" ; import { GrassMaterial } from "./GrassMaterial" ; import { GrassRenderManager } from "./GrassRenderManager" ; export class GlassRender { private grassManager : GrassRenderManager ; private instanceCMD : DrawMeshInstancedCMD ; private materialBlock : MaterialInstancePropertyBlock ; private grassMaterial : GrassMaterial ; private buf : CommandBuffer ; private camera : Camera ; constructor ( manager , camera ) { this . grassManager = manager ; this . createCommandBuffer ( ) ; this . camera = camera ; } internal private creatGrassMesh ( ) { var vertexArray = new Float32Array ( 3 * 3 ) ; vertexArray [ 0 ] = - 0.25 ; vertexArray [ 3 ] = 0.25 ; vertexArray [ 7 ] = 1 ; var indexArray = new Uint16Array ( [ 2 , 1 , 0 ] ) ; var vertexDeclaration = VertexMesh . getVertexDeclaration ( "POSITION" ) ; var mesh = PrimitiveMesh . _createMesh ( vertexDeclaration , vertexArray , indexArray ) ; return mesh ; } internal private createMaterial ( ) { var mat = new GrassMaterial ( ) ; this . grassMaterial = mat ; return mat ; } param camera createCommandBuffer ( ) { DrawMeshInstancedCMD . maxInstanceCount = 1000000 ; this . buf = new CommandBuffer ( ) ; this . materialBlock = new MaterialInstancePropertyBlock ( ) ; this . materialBlock . setVector3Array ( "a_privotPosition" , this . grassManager . dataArrayBuffer , InstanceLocation . CUSTOME0 ) ; this . instanceCMD = this . buf . drawMeshInstance ( this . creatGrassMesh ( ) , 0 , null , this . createMaterial ( ) , 0 , this . materialBlock , this . grassManager . drawArrayLength ) ; return ; } removeCommandBuffer ( ) { this . camera . removeCommandBuffer ( CameraEventFlags . BeforeTransparent , this . buf ) ; } addCommandBuffer ( ) { this . camera . addCommandBuffer ( CameraEventFlags . BeforeTransparent , this . buf ) ; } changeDrawNums ( ) { this . materialBlock . setVector3Array ( "a_privotPosition" , this . grassManager . dataArrayBuffer , InstanceLocation . CUSTOME0 ) ; this . instanceCMD . setDrawNums ( this . grassManager . drawArrayLength ) ; } }