public void RemoveQuadTrees(NativeList <ulong> removeList) { void ErasePoint(TerrainQuadTree.QuadTreeNode *node) { node->listPosition = -1; notUsedHeightmapIndices.Add(node->panel.heightMapIndex); } int length = removeList.Length; TerrainQuadTree.QuadTreeNode **tree = (TerrainQuadTree.QuadTreeNode * *)removeList.unsafePtr; int targetLength = referenceBuffer.Length - length; int len = 0; if (targetLength <= 0) { for (int i = 0; i < length; ++i) { ErasePoint(tree[i]); } referenceBuffer.Clear(); return; } for (int i = 0; i < length; ++i) { TerrainQuadTree.QuadTreeNode *currentNode = tree[i]; if (currentNode->listPosition >= targetLength) { referenceBuffer[currentNode->listPosition] = 0; ErasePoint(currentNode); } } NativeArray <int2> transformList = new NativeArray <int2>(length, Allocator.Temp, NativeArrayOptions.UninitializedMemory); int2 *transformPtr = transformList.Ptr(); len = 0; int currentIndex = referenceBuffer.Length - 1; for (int i = 0; i < length; ++i) { TerrainQuadTree.QuadTreeNode *treeNode = tree[i]; if (treeNode->listPosition < 0) { continue; } while (referenceBuffer[currentIndex] == 0) { currentIndex--; if (currentIndex < 0) { goto FINALIZE; } } TerrainQuadTree.QuadTreeNode *lastNode = (TerrainQuadTree.QuadTreeNode *)referenceBuffer[currentIndex]; currentIndex--; transformPtr[len] = new int2(treeNode->listPosition, lastNode->listPosition); len++; lastNode->listPosition = treeNode->listPosition; referenceBuffer[lastNode->listPosition] = (ulong)lastNode; ErasePoint(treeNode); } FINALIZE: referenceBuffer.RemoveLast(length); if (len <= 0) { return; } if (len > removebuffer.count) { removebuffer.Dispose(); removebuffer = new ComputeBuffer(len, sizeof(int2)); } removebuffer.SetData(transformList, 0, 0, len); transformShader.SetBuffer(0, ShaderIDs._IndexBuffer, removebuffer); transformShader.SetBuffer(0, ShaderIDs.clusterBuffer, clusterBuffer); ComputeShaderUtility.Dispatch(transformShader, 0, len, 64); transformList.Dispose(); }
public void Awake(MonoBehaviour behavior) { current = this; addList = new NativeList <ulong>(10, Allocator.Persistent); this.behavior = behavior; baseBuffer = new PipelineBaseBuffer(); clusterResources = Resources.Load <ClusterMatResources>("MapMat/" + mapResources); int clusterCount = 0; allScenes = new List <SceneStreaming>(clusterResources.clusterProperties.Count); foreach (var i in clusterResources.clusterProperties) { clusterCount += i.clusterCount; allScenes.Add(new SceneStreaming(i)); } PipelineFunctions.InitBaseBuffer(baseBuffer, clusterResources, mapResources, clusterCount); pointerContainer = new NativeList <ulong>(clusterCount, Allocator.Persistent); commandQueue = new LoadingCommandQueue(); RenderTextureDescriptor desc = new RenderTextureDescriptor { autoGenerateMips = false, bindMS = false, colorFormat = RenderTextureFormat.ARGB32, depthBufferBits = 0, dimension = TextureDimension.Tex2DArray, enableRandomWrite = false, height = resolution, width = resolution, memoryless = RenderTextureMemoryless.None, msaaSamples = 1, vrUsage = VRTextureUsage.None, volumeDepth = texArrayCapacity, shadowSamplingMode = ShadowSamplingMode.None, sRGB = false, useMipMap = false }; commonData = new SceneCommonData { texDict = new Dictionary <string, SceneCommonData.TextureIdentifier>(), avaiableProperties = new NativeList <int>(propertyCapacity, Allocator.Persistent), avaiableTexs = new NativeList <int>(texArrayCapacity, Allocator.Persistent), texCopyBuffer = new ComputeBuffer(resolution * resolution, sizeof(int)), propertyBuffer = new ComputeBuffer(propertyCapacity, sizeof(PropertyValue)), copyTextureMat = new Material(RenderPipeline.current.resources.copyShader), texArray = new RenderTexture(desc), clusterMaterial = new Material(RenderPipeline.current.resources.clusterRenderShader), terrainMaterial = new Material(RenderPipeline.current.resources.terrainShader), terrainDrawStreaming = new TerrainDrawStreaming(100, 16, RenderPipeline.current.resources.terrainCompute) }; commonData.clusterMaterial.SetBuffer(ShaderIDs._PropertiesBuffer, commonData.propertyBuffer); commonData.clusterMaterial.SetTexture(ShaderIDs._MainTex, commonData.texArray); commonData.copyTextureMat.SetVector("_TextureSize", new Vector4(resolution, resolution)); commonData.copyTextureMat.SetBuffer("_TextureBuffer", commonData.texCopyBuffer); for (int i = 0; i < propertyCapacity; ++i) { commonData.avaiableProperties.Add(i); } for (int i = 0; i < texArrayCapacity; ++i) { commonData.avaiableTexs.Add(i); } testNodeArray = new NativeList <ulong>(terrainTransforms.Length, Allocator.Persistent); foreach (var i in terrainTransforms) { TerrainQuadTree.QuadTreeNode *testNode = (TerrainQuadTree.QuadTreeNode *)UnsafeUtility.Malloc(sizeof(TerrainQuadTree.QuadTreeNode), 16, Allocator.Persistent); testNode->listPosition = -1; ref TerrainPanel panel = ref testNode->panel; if (i.localScale.x > 1.1f) { panel.edgeFlag = 0; } else { panel.edgeFlag = 15; } panel.extent = i.localScale * 0.5f; panel.position = i.position; panel.textureIndex = 0; panel.heightMapIndex = 0; testNodeArray.Add((ulong)testNode); }