Beispiel #1
0
        public void GenerateAsync(bool listCommand = true)
        {
            clusterBuffer = new NativeArray <CullBox>(property.clusterCount, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            pointsBuffer  = new NativeArray <Point>(property.clusterCount * PipelineBaseBuffer.CLUSTERCLIPCOUNT, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            indicesBuffer = new NativeArray <int>(property.clusterCount, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            NativeList <ulong> pointerContainer = SceneController.pointerContainer;

            pointerContainer.AddCapacityTo(pointerContainer.Length + indicesBuffer.Length);
            CullBox *      clusterData  = clusterBuffer.Ptr();
            Point *        verticesData = pointsBuffer.Ptr();
            const string   infosPath    = "Assets/BinaryData/MapInfos/";
            const string   pointsPath   = "Assets/BinaryData/MapPoints/";
            MStringBuilder sb           = new MStringBuilder(pointsPath.Length + property.name.Length + ".txt".Length);

            allStrings[0] = infosPath;
            allStrings[1] = property.name;
            allStrings[2] = ".txt";
            sb.Combine(allStrings);
            using (BinaryReader reader = new BinaryReader(File.Open(sb.str, FileMode.Open)))
            {
                byte[] bytes = reader.ReadBytes((int)reader.BaseStream.Length);
                fixed(byte *b = bytes)
                {
                    UnsafeUtility.MemCpy(clusterData, b, bytes.Length);
                }
            }
            allStrings[0] = pointsPath;
            sb.Combine(allStrings);
            using (BinaryReader reader = new BinaryReader(File.Open(sb.str, FileMode.Open)))
            {
                byte[] bytes = reader.ReadBytes((int)reader.BaseStream.Length);
                fixed(byte *b = bytes)
                {
                    UnsafeUtility.MemCpy(verticesData, b, bytes.Length);
                }
            }
            int *indicesPtr = indicesBuffer.Ptr();
            LoadingCommandQueue commandQueue = SceneController.commandQueue;

            for (int i = 0; i < indicesBuffer.Length; ++i)
            {
                indicesPtr[i] = pointerContainer.Length;
                pointerContainer.Add((ulong)(indicesPtr + i));
            }
            LoadTextures();
            propertiesPool = SceneController.commonData.GetPropertyIndex(property.properties.Length);
            uint *poolPtr = propertiesPool.Ptr();

            for (int i = 0; i < pointsBuffer.Length; ++i)
            {
                verticesData[i].objIndex = poolPtr[verticesData[i].objIndex];
            }
            if (listCommand)
            {
                lock (commandQueue)
                {
                    commandQueue.Queue(GenerateRun());
                }
            }
        }
Beispiel #2
0
 public static void Dispose()
 {
     singletonReady = false;
     PipelineFunctions.Dispose(baseBuffer);
     pointerContainer.Dispose();
     commandQueue = null;
     commonData.avaiableProperties.Dispose();
     commonData.avaiableTexs.Dispose();
     commonData.avaiableLightmap.Dispose();
     commonData.texCopyBuffer.Dispose();
     commonData.lightmapCopyBuffer.Dispose();
     commonData.propertyBuffer.Dispose();
     commonData.texDict.Clear();
     commonData.lightmapDict.Clear();
     commonData.terrainDrawStreaming.Dispose();
     UnityEngine.Object.DestroyImmediate(commonData.terrainMaterial);
     UnityEngine.Object.DestroyImmediate(commonData.terrainMaterial);
     UnityEngine.Object.DestroyImmediate(commonData.lightmapArray);
     UnityEngine.Object.DestroyImmediate(commonData.texArray);
     addList.Dispose();
     foreach (var i in commonData.allTempBuffers.Values)
     {
         i.Dispose();
     }
 }
 public void Update(LoadingCommandQueue commandQueue)
 {
     lock (commandQueue)
     {
         commandQueue.Run();
     }
 }
        public static void Awake(PipelineResources resources, int resolution, int texArrayCapacity, int propertyCapacity, string mapResources)
        {
            SceneController.resolution = resolution;
            singletonReady = true;
            SceneController.resources = resources;
            addList = new NativeList<ulong>(10, Allocator.Persistent);
            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(resources.shaders.copyShader),
                texArray = new RenderTexture(desc),
                clusterMaterial = new Material(resources.shaders.clusterRenderShader),
                terrainMaterial = new Material(resources.shaders.terrainShader),
                terrainDrawStreaming = new TerrainDrawStreaming(100, 16, resources.shaders.terrainCompute)
            };

            for (int i = 0; i < propertyCapacity; ++i)
            {
                commonData.avaiableProperties.Add(i);
            }
            for (int i = 0; i < texArrayCapacity; ++i)
            {
                commonData.avaiableTexs.Add(i);
            }
        }
Beispiel #5
0
        public void GenerateAsync(bool listCommand = true)
        {
            clusterBuffer = new NativeArray <CullBox>(property.clusterCount, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            pointsBuffer  = new NativeArray <float3>(property.clusterCount * PipelineBaseBuffer.CLUSTERCLIPCOUNT, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            indicesBuffer = new NativeArray <int>(property.clusterCount, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            NativeList <ulong> pointerContainer = SceneController.pointerContainer;

            pointerContainer.AddCapacityTo(pointerContainer.Length + indicesBuffer.Length);
            CullBox *      clusterData  = clusterBuffer.Ptr();
            float3 *       verticesData = pointsBuffer.Ptr();
            const string   infosPath    = "Assets/BinaryData/MapInfos/";
            const string   pointsPath   = "Assets/BinaryData/MapPoints/";
            MStringBuilder sb           = new MStringBuilder(pointsPath.Length + property.name.Length + ".mpipe".Length);

            allStrings[0] = infosPath;
            allStrings[1] = property.name;
            allStrings[2] = ".mpipe";
            sb.Combine(allStrings);
            // FileStream fileStream = new FileStream(sb.str, FileMode.Open, FileAccess.Read);
            using (FileStream reader = new FileStream(sb.str, FileMode.Open, FileAccess.Read))
            {
                int    length = (int)reader.Length;
                byte[] bytes  = GetByteArray(length);
                reader.Read(bytes, 0, length);
                fixed(byte *b = bytes)
                {
                    UnsafeUtility.MemCpy(clusterData, b, length);
                }
            }
            allStrings[0] = pointsPath;
            sb.Combine(allStrings);
            using (FileStream reader = new FileStream(sb.str, FileMode.Open, FileAccess.Read))
            {
                int    length = (int)reader.Length;
                byte[] bytes  = GetByteArray(length);
                reader.Read(bytes, 0, length);
                fixed(byte *b = bytes)
                {
                    UnsafeUtility.MemCpy(verticesData, b, length);
                }
            }
            int *indicesPtr = indicesBuffer.Ptr();
            LoadingCommandQueue commandQueue = LoadingThread.commandQueue;

            for (int i = 0; i < indicesBuffer.Length; ++i)
            {
                indicesPtr[i] = pointerContainer.Length;
                pointerContainer.Add((ulong)(indicesPtr + i));
            }

            if (listCommand)
            {
                lock (commandQueue)
                {
                    commandQueue.Queue(GenerateRun());
                }
            }
        }
 public void Init()
 {
     if (current != null)
     {
         Debug.LogError("Loading Thread should be singleton!");
         return;
     }
     current    = this;
     isRunning  = true;
     resetEvent = new AutoResetEvent(false);
     thread     = new Thread(Run);
     thread.Start();
     commandQueue = new LoadingCommandQueue();
 }
Beispiel #7
0
        public void GenerateAsync(bool listCommand = true)
        {
            clusterBuffer = new NativeArray <Cluster>(property.clusterCount, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            pointsBuffer  = new NativeArray <Point>(property.clusterCount * PipelineBaseBuffer.CLUSTERCLIPCOUNT, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            Cluster *      clusterData  = clusterBuffer.Ptr();
            Point *        verticesData = pointsBuffer.Ptr();
            const string   infosPath    = "Assets/BinaryData/MapInfos/";
            const string   pointsPath   = "Assets/BinaryData/MapPoints/";
            MStringBuilder sb           = new MStringBuilder(pointsPath.Length + property.name.Length + ".mpipe".Length);

            allStrings[0] = infosPath;
            allStrings[1] = property.name;
            allStrings[2] = ".mpipe";
            sb.Combine(allStrings);
            // FileStream fileStream = new FileStream(sb.str, FileMode.Open, FileAccess.Read);
            using (FileStream reader = new FileStream(sb.str, FileMode.Open, FileAccess.Read))
            {
                int    length = (int)reader.Length;
                byte[] bytes  = GetByteArray(length);
                reader.Read(bytes, 0, length);
                fixed(byte *b = bytes)
                {
                    UnsafeUtility.MemCpy(clusterData, b, length);
                }
            }
            allStrings[0] = pointsPath;
            sb.Combine(allStrings);
            using (FileStream reader = new FileStream(sb.str, FileMode.Open, FileAccess.Read))
            {
                int    length = (int)reader.Length;
                byte[] bytes  = GetByteArray(length);
                reader.Read(bytes, 0, length);
                fixed(byte *b = bytes)
                {
                    UnsafeUtility.MemCpy(verticesData, b, length);
                }
            }
            LoadingCommandQueue commandQueue = LoadingThread.commandQueue;

            if (listCommand)
            {
                lock (commandQueue)
                {
                    commandQueue.Queue(GenerateRun());
                }
            }
        }
Beispiel #8
0
 public static void Dispose()
 {
     singletonReady = false;
     PipelineFunctions.Dispose(baseBuffer);
     pointerContainer.Dispose();
     commandQueue = null;
     commonData.avaiableProperties.Dispose();
     commonData.avaiableTexs.Dispose();
     commonData.texCopyBuffer.Dispose();
     commonData.propertyBuffer.Dispose();
     commonData.texDict.Clear();
     commonData.texArray.Release();
     commonData.terrainDrawStreaming.Dispose();
     UnityEngine.Object.DestroyImmediate(commonData.terrainMaterial);
     UnityEngine.Object.DestroyImmediate(commonData.copyTextureMat);
     addList.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);
            }