private void CreateInstancedBatches(int cellIndex) { GGrassPatchNativeData nativeData = cellsNativeData[cellIndex]; NativeArray <Matrix4x4> trs = nativeData.trs; NativeArray <Vector3Int> metadata = nativeData.metadata; GInstancedBatch[] batches = new GInstancedBatch[metadata[0].z]; for (int i = 0; i < batches.Length; ++i) { int prototypeIndex = metadata[i + 1].x; int startIndex = metadata[i + 1].y; int length = metadata[i + 1].z; int indexLimit = startIndex + length; GInstancedBatch batch = new GInstancedBatch(BATCH_MAX_INSTANCE_COUNT); batch.prototypeIndex = prototypeIndex; for (int j = startIndex; j < indexLimit; ++j) { batch.AddTransform(trs[j]); } batches[i] = batch; } cellsData[cellIndex].instancedBatches = batches; }
private void Submit(int cellIndex) { GGrassPatchData data = cellsData[cellIndex]; if (data == null) { return; } GInstancedBatch[] batches = data.instancedBatches; if (batches == null) { return; } for (int i = 0; i < batches.Length; ++i) { GInstancedBatch b = batches[i]; if (b.prototypeIndex >= prototypes.Count) { continue; } GGrassPrototype proto = prototypes[b.prototypeIndex]; MaterialPropertyBlock propertyBlock = propertyBlocks[b.prototypeIndex]; Mesh baseMesh = baseMeshes[b.prototypeIndex]; if (baseMesh == null) { continue; } Material material = materials[b.prototypeIndex]; if (material == null || !material.enableInstancing) { continue; } Graphics.DrawMeshInstanced( baseMesh, 0, material, b.transforms, b.instanceCount, propertyBlock, proto.shadowCastingMode, proto.receiveShadow, proto.layer, camera, LightProbeUsage.BlendProbes); } }