public void Execute(int index) { var bv = batches[index]; var visibleInstanceIndex = 0; var isOrtho = lodParams.isOrtho; var distanceScale = lodParams.distanceScale; for (int i = 0; i < bv.instancesCount; i++) { var cullData = cullDatas[index * 50 + i]; var rootLodDistance = math.select(distanceScale * math.length(lodParams.cameraPos - cullData.position), distanceScale, isOrtho); var rootLodIntersect = (rootLodDistance < cullData.maxDistance) && (rootLodDistance >= cullData.minDistance); if (rootLodIntersect) { var chunkIn = OtherUtils.Intersect2NoPartial(planes, cullData.bound); if (chunkIn != OtherUtils.IntersectResult.Out) { //设置batch的index indexList[bv.offset + visibleInstanceIndex] = i; visibleInstanceIndex++; } } } //设置batch的visibleCount bv.visibleCount = visibleInstanceIndex; batches[index] = bv; }
private JobHandle MyOnPerformCulling(BatchRendererGroup renderergroup, BatchCullingContext cullingContext) { var planes = OtherUtils.BuildSOAPlanePackets(cullingContext.cullingPlanes, Allocator.TempJob); var lodParams = OtherUtils.CalculateLODParams(cullingContext.lodParameters); //传入visibleIndices和batchVisibility 在job里面修改 var cull = new MyCullJob() { planes = planes, lodParams = lodParams, indexList = cullingContext.visibleIndices, batches = cullingContext.batchVisibility, cullDatas = cullData, }; //50组lod0 + 50组lod1 = 100 // cullingDependency 形成队列关系 var handle = cull.Schedule(100, 32, cullingDependency); cullingDependency = JobHandle.CombineDependencies(handle, cullingDependency); return(handle); }
private void AddBatch(int offset, int count, float3[] pos, quaternion[] rot, float3[] scale) { var localBound = mesh.bounds; var block = new MaterialPropertyBlock(); var colors = new List <Vector4>(count); for (int i = 0; i < count; i++) { colors.Add(new Vector4(Random.value, Random.value, Random.value, Random.value)); } // 因为srp batcher buffer的关系 instance buffer 会混乱 // block.SetVectorArray(BaseColor_ID, colors); //下面的Bounds是需要组合过的 var batchIndex = batchRendererGroup.AddBatch( mesh, 0, material, 0, ShadowCastingMode.On, true, false, new Bounds(Vector3.zero, 1000 * Vector3.one), count, block, null ); var matrices = batchRendererGroup.GetBatchMatrices(batchIndex); for (int i = 0; i < count; i++) { float4x4 tempMatrix = float4x4.TRS(pos[i], rot[i], scale[i]); Bounds aabb = OtherUtils.Transform(tempMatrix, localBound); tempMatrix.c0.w = colors[i].x; tempMatrix.c1.w = colors[i].y; tempMatrix.c2.w = colors[i].z; tempMatrix.c3.w = colors[i].w; matrices[i] = tempMatrix; cullData[offset + i] = new CullData() { bound = aabb, position = pos[i], minDistance = 0, maxDistance = lodDis }; } //----------------- for (int i = 0; i < count; i++) { colors[i] = (new Vector4(Random.value, Random.value, Random.value, Random.value)); } // 因为srp batcher buffer的关系 instance buffer 会混乱 // block.SetVectorArray(BaseColor_ID, colors); //https://docs.unity3d.com/ScriptReference/Rendering.BatchRendererGroup.AddBatch.html //下面的Bounds是需要组合过的 batchIndex = batchRendererGroup.AddBatch( lowMesh, 0, material, 0, ShadowCastingMode.On, true, false, new Bounds(Vector3.zero, 1000 * Vector3.one), count, block, null ); matrices = batchRendererGroup.GetBatchMatrices(batchIndex); for (int i = 0; i < count; i++) { var tempMatrix = float4x4.TRS(pos[i], rot[i], scale[i]); Bounds aabb = OtherUtils.Transform(tempMatrix, localBound); tempMatrix.c0.w = colors[i].x; tempMatrix.c1.w = colors[i].y; tempMatrix.c2.w = colors[i].z; tempMatrix.c3.w = colors[i].w; matrices[i] = tempMatrix; cullData[offset + count + i] = new CullData() { bound = aabb, position = pos[i], minDistance = lodDis, maxDistance = 10000, }; } }