Ejemplo n.º 1
0
        protected override void OnEnableFunc()
        {
            if (current && current != this)
            {
                enabled = false;
                Debug.LogError("Only One Terrain allowed!");
                return;
            }
            current = this;
            int indexMapSize = 1;

            for (int i = 1; i < lodDistances.Length; ++i)
            {
                indexMapSize *= 2;
            }
            indexMapSize *= chunkCount;
            NativeArray <VirtualTextureFormat> allFormats = new NativeArray <VirtualTextureFormat>(4, Allocator.Temp, NativeArrayOptions.UninitializedMemory);

            allFormats[0]  = new VirtualTextureFormat(VirtualTextureSize.x512, RenderTextureFormat.RHalf, "_VirtualHeightMap");
            allFormats[1]  = new VirtualTextureFormat(VirtualTextureSize.x1024, RenderTextureFormat.ARGB32, "_VirtualAlbedoMap");
            allFormats[2]  = new VirtualTextureFormat(VirtualTextureSize.x1024, RenderTextureFormat.ARGB32, "_VirtualNormalMap");
            allFormats[3]  = new VirtualTextureFormat(VirtualTextureSize.x1024, RenderTextureFormat.ARGB32, "_VirtualSMOMap");
            virtualTexture = new VirtualTexture(129, indexMapSize, allFormats);
            allFormats.Dispose();
            InitializeMesh();
            dispatchDrawBuffer = new ComputeBuffer(5, sizeof(int), ComputeBufferType.IndirectArguments);
            const int INIT_LENGTH = 500;

            culledResultsBuffer = new ComputeBuffer(INIT_LENGTH, sizeof(int));
            loadedBuffer        = new ComputeBuffer(INIT_LENGTH, sizeof(TerrainChunkBuffer));
            loadedBufferList    = new NativeList <TerrainChunkBuffer>(INIT_LENGTH, Allocator.Persistent);
            shader = Resources.Load <ComputeShader>("TerrainCompute");
            NativeArray <uint> dispatchDraw = new NativeArray <uint>(5, Allocator.Temp, NativeArrayOptions.ClearMemory);

            dispatchDraw[0] = (uint)meshBuffer.count;
            dispatchDrawBuffer.SetData(dispatchDraw);
            allTrees = new Native2DArray <TerrainQuadTree>(chunkCount, Allocator.Persistent, true);
            setting  = new TerrainQuadTreeSettings
            {
                allLodLevles     = new NativeList_Float(lodDistances.Length + 1, Allocator.Persistent),
                largestChunkSize = chunkSize,
                screenOffset     = chunkOffset
            };
            for (int i = 0; i < lodDistances.Length; ++i)
            {
                setting.allLodLevles.Add(min(lodDistances[max(0, i - 1)], lodDistances[i]));
                setting.allLodLevles[i] *= setting.allLodLevles[i];
            }
            setting.allLodLevles[lodDistances.Length] = 0;
            int2 len = allTrees.Length;

            for (int x = 0; x < len.x; ++x)
            {
                for (int y = 0; y < len.y; ++y)
                {
                    allTrees[int2(x, y)] = new TerrainQuadTree(-1, setting.Ptr(), TerrainQuadTree.LocalPos.LeftDown, 0, int2(x, y));
                }
            }
        }
Ejemplo n.º 2
0
        protected override void OnEnableFunc()
        {
            if (current && current != this)
            {
                enabled = false;
                Debug.LogError("Only One Terrain allowed!");
                return;
            }
            if (!terrainData)
            {
                enabled = false;
                Debug.LogError("No Data!");
                return;
            }
            msb           = new MStringBuilder(32);
            textureShader = Resources.Load <ComputeShader>("ProceduralTexture");
            shader        = Resources.Load <ComputeShader>("TerrainCompute");
            current       = this;
            int indexMapSize = 1;

            for (int i = 1; i < terrainData.lodDistances.Length; ++i)
            {
                indexMapSize *= 2;
            }
            dispatchDrawBuffer = new ComputeBuffer(5, sizeof(int), ComputeBufferType.IndirectArguments);
            const int INIT_LENGTH = 500;

            culledResultsBuffer = new ComputeBuffer(INIT_LENGTH, sizeof(int));
            loadedBuffer        = new ComputeBuffer(INIT_LENGTH, sizeof(TerrainChunkBuffer));
            loadedBufferList    = new NativeList <TerrainChunkBuffer>(INIT_LENGTH, Allocator.Persistent);
            lodOffset           = terrainData.lodDistances.Length - terrainData.renderingLevelCount;
            loader       = new VirtualTextureLoader(lodOffset, terrainData.renderingLevelCount, terrainData.readWritePath, this);
            loadDataList = new NativeQueue <TerrainLoadData>(100, Allocator.Persistent);
            NativeArray <uint> dispatchDraw = new NativeArray <uint>(5, Allocator.Temp, NativeArrayOptions.ClearMemory);

            dispatchDraw[0] = 96;
            dispatchDrawBuffer.SetData(dispatchDraw);
            VirtualTextureFormat *formats = stackalloc VirtualTextureFormat[]
            {
                new VirtualTextureFormat((VirtualTextureSize)HEIGHT_RESOLUTION, RenderTextureFormat.R16, "_VirtualHeightmap"),
                new VirtualTextureFormat((VirtualTextureSize)COLOR_RESOLUTION, RenderTextureFormat.ARGB32, "_VirtualMainTex"),
                new VirtualTextureFormat((VirtualTextureSize)COLOR_RESOLUTION, RenderTextureFormat.RGHalf, "_VirtualBumpMap"),
                new VirtualTextureFormat((VirtualTextureSize)COLOR_RESOLUTION, RenderTextureFormat.RG16, "_VirtualSMMap")
            };

            vt           = new VirtualTexture(terrainData.virtualTexCapacity, min(2048, (int)(pow(2.0, terrainData.lodDistances.Length) + 0.1)), formats, 4, "_TerrainVTIndexTex");
            allLodLevles = new NativeList_Float(terrainData.lodDistances.Length, Allocator.Persistent);
            for (int i = 0; i < terrainData.lodDistances.Length; ++i)
            {
                allLodLevles.Add(min(terrainData.lodDistances[max(0, i - 1)], terrainData.lodDistances[i]));
            }
            allLodLevles[terrainData.lodDistances.Length] = 0;

            albedoTex = new RenderTexture(new RenderTextureDescriptor
            {
                colorFormat       = RenderTextureFormat.ARGB32,
                dimension         = TextureDimension.Tex2DArray,
                width             = COLOR_RESOLUTION,
                height            = COLOR_RESOLUTION,
                volumeDepth       = Mathf.Max(1, terrainData.textures.Length),
                enableRandomWrite = true,
                msaaSamples       = 1
            });
            albedoTex.Create();
            normalTex = new RenderTexture(new RenderTextureDescriptor
            {
                colorFormat       = RenderTextureFormat.RGHalf,
                dimension         = TextureDimension.Tex2DArray,
                width             = COLOR_RESOLUTION,
                height            = COLOR_RESOLUTION,
                volumeDepth       = Mathf.Max(1, terrainData.textures.Length),
                enableRandomWrite = true,
                msaaSamples       = 1
            });
            normalTex.Create();
            smTex = new RenderTexture(new RenderTextureDescriptor
            {
                colorFormat       = RenderTextureFormat.RG16,
                dimension         = TextureDimension.Tex2DArray,
                width             = COLOR_RESOLUTION,
                height            = COLOR_RESOLUTION,
                volumeDepth       = Mathf.Max(1, terrainData.textures.Length),
                enableRandomWrite = true,
                msaaSamples       = 1
            });
            smTex.Create();
            smTex.wrapMode     = TextureWrapMode.Repeat;
            normalTex.wrapMode = TextureWrapMode.Repeat;
            albedoTex.wrapMode = TextureWrapMode.Repeat;
            mask = new RenderTexture(new RenderTextureDescriptor
            {
                colorFormat       = RenderTextureFormat.R8,
                dimension         = TextureDimension.Tex2D,
                width             = MASK_RESOLUTION,
                height            = MASK_RESOLUTION,
                volumeDepth       = 1,
                enableRandomWrite = true,
                msaaSamples       = 1
            });
            mask.Create();
            textureBuffer = new ComputeBuffer(max(MASK_RESOLUTION * MASK_RESOLUTION / 4, HEIGHT_RESOLUTION * HEIGHT_RESOLUTION / 2), 4);
            tree          = new TerrainQuadTree(-1, TerrainQuadTree.LocalPos.LeftDown, 0, terrainData.largestChunkSize);
            StartCoroutine(AsyncLoader());
        }

        void UpdateBuffer()
        {
            if (!loadedBufferList.isCreated)
            {
                return;
            }
            if (loadedBufferList.Length > loadedBuffer.count)
            {
                loadedBuffer.Dispose();
                culledResultsBuffer.Dispose();
                loadedBuffer        = new ComputeBuffer(loadedBufferList.Capacity, sizeof(TerrainChunkBuffer));
                culledResultsBuffer = new ComputeBuffer(loadedBufferList.Capacity, sizeof(int));
            }
            loadedBuffer.SetDataPtr(loadedBufferList.unsafePtr, loadedBufferList.Length);
        }
Ejemplo n.º 3
0
        protected override void OnEnableFunc()
        {
            if (current && current != this)
            {
                enabled = false;
                Debug.LogError("Only One Terrain allowed!");
                return;
            }
            if (!terrainData)
            {
                enabled = false;
                Debug.LogError("No Data!");
                return;
            }
            if (!cam || !decalCamera)
            {
                enabled = false;
                Debug.LogError("No Decal Camera!");
                return;
            }
            initializing      = true;
            lodOffset         = terrainData.GetLodOffset();
            largestChunkCount = (int)(0.1 + pow(2.0, lodOffset));
            msb = new MStringBuilder(32);
            oneVTPixelWorldLength = terrainData.VTTexelLength();
            textureShader         = Resources.Load <ComputeShader>("ProceduralTexture");
            shader  = Resources.Load <ComputeShader>("TerrainCompute");
            current = this;
            int indexMapSize = 1;

            for (int i = 1; i < terrainData.lodDistances.Length; ++i)
            {
                indexMapSize *= 2;
            }
            vtContainer  = new NativeArray <int>(4, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            maskLoadList = new NativeQueue <MaskLoadCommand>(10, Allocator.Persistent);
            ComputeShader editShader = Resources.Load <ComputeShader>("TerrainEdit");

            loadingThread = new MTerrainLoadingThread(10);
            maskLoader    = new VirtualTextureLoader(terrainData.maskmapPath, editShader, largestChunkCount, MASK_RESOLUTION, false, loadingThread);
            heightLoader  = new VirtualTextureLoader(terrainData.heightmapPath, editShader, largestChunkCount, MASK_RESOLUTION, true, loadingThread);

            loadDataList       = new NativeQueue <TerrainLoadData>(100, Allocator.Persistent);
            initializeLoadList = new NativeQueue <TerrainLoadData>(100, Allocator.Persistent);
            NativeArray <uint> dispatchDraw = new NativeArray <uint>(5, Allocator.Temp, NativeArrayOptions.ClearMemory);

            dispatchDraw[0] = 6;
            VirtualTextureFormat *formats = stackalloc VirtualTextureFormat[]
            {
                new VirtualTextureFormat((VirtualTextureSize)COLOR_RESOLUTION, GraphicsFormat.R8G8B8A8_UNorm, "_VirtualMainTex", 2),
                new VirtualTextureFormat((VirtualTextureSize)COLOR_RESOLUTION, GraphicsFormat.R16G16_SNorm, "_VirtualBumpMap", 2),
                new VirtualTextureFormat((VirtualTextureSize)COLOR_RESOLUTION, GraphicsFormat.R8G8_UNorm, "_VirtualSMMap", 2),
                new VirtualTextureFormat((VirtualTextureSize)HEIGHT_RESOLUTION, HEIGHT_FORMAT, "_VirtualDisplacement", 0)
            };

            mipIDs          = new NativeArray <int>(2, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            mipIDs[0]       = Shader.PropertyToID("_Mip0");
            mipIDs[1]       = Shader.PropertyToID("_Mip1");
            chunkCount      = (int)(pow(2.0, terrainData.lodDistances.Length - 1) + 0.1);
            vt              = new VirtualTexture(terrainData.virtualTexCapacity, min(2048, chunkCount), formats, 4, "_TerrainVTIndexTex");
            textureCapacity = terrainData.virtualTexCapacity;
            VirtualTextureFormat *maskFormats = stackalloc VirtualTextureFormat[]
            {
                new VirtualTextureFormat((VirtualTextureSize)MASK_RESOLUTION, GraphicsFormat.R8_UNorm, "_VirtualMaskmap"),
                new VirtualTextureFormat((VirtualTextureSize)MASK_RESOLUTION, GraphicsFormat.R16_UNorm, "_VirtualHeightmap"),
            };

            maskVT = new VirtualTexture(terrainData.heightmapTexCapacity, largestChunkCount, maskFormats, 2, "_MaskIndexMap");
            maskVT.GetTexture(0).filterMode = FilterMode.Point;
            maskVT.GetTexture(1).filterMode = FilterMode.Point;
            vt.GetTexture(0).filterMode     = FilterMode.Trilinear;
            vt.GetTexture(1).filterMode     = FilterMode.Trilinear;
            vt.GetTexture(2).filterMode     = FilterMode.Trilinear;
            vt.GetTexture(3).filterMode     = FilterMode.Bilinear;
            allLodLevles = new NativeList_Float(terrainData.lodDistances.Length, Allocator.Persistent);
            for (int i = 0; i < terrainData.lodDistances.Length; ++i)
            {
                allLodLevles.Add((float)min(terrainData.lodDistances[max(0, i - 1)], terrainData.lodDistances[i]));
            }
            allLodLevles[terrainData.lodDistances.Length] = 0;
            meshResolution = terrainData.GetMeshResolution();
            cullingFlags   = new RenderTexture(new RenderTextureDescriptor
            {
                graphicsFormat    = GraphicsFormat.R8_UNorm,
                dimension         = TextureDimension.Tex2D,
                width             = meshResolution,
                height            = meshResolution,
                volumeDepth       = 1,
                enableRandomWrite = true,
                msaaSamples       = 1,
                useMipMap         = false
            });
            cullingFlags.filterMode = FilterMode.Point;
            cullingFlags.Create();
            albedoTex = new RenderTexture(new RenderTextureDescriptor
            {
                graphicsFormat    = GraphicsFormat.R8G8B8A8_UNorm,
                dimension         = TextureDimension.Tex2DArray,
                width             = COLOR_RESOLUTION,
                height            = COLOR_RESOLUTION,
                volumeDepth       = Mathf.Max(1, terrainData.textures.Length),
                enableRandomWrite = true,
                msaaSamples       = 1,
                autoGenerateMips  = false,
                useMipMap         = true,
                mipCount          = 6,
            });
            albedoTex.Create();
            normalTex = new RenderTexture(new RenderTextureDescriptor
            {
                graphicsFormat    = GraphicsFormat.R16G16_SNorm,
                dimension         = TextureDimension.Tex2DArray,
                width             = COLOR_RESOLUTION,
                height            = COLOR_RESOLUTION,
                autoGenerateMips  = false,
                useMipMap         = true,
                volumeDepth       = Mathf.Max(1, terrainData.textures.Length),
                enableRandomWrite = true,
                msaaSamples       = 1,
                mipCount          = 6,
            });
            normalTex.Create();
            smTex = new RenderTexture(new RenderTextureDescriptor
            {
                graphicsFormat    = GraphicsFormat.R16G16_UNorm,
                dimension         = TextureDimension.Tex2DArray,
                width             = COLOR_RESOLUTION,
                height            = COLOR_RESOLUTION,
                volumeDepth       = Mathf.Max(1, terrainData.textures.Length),
                enableRandomWrite = true,
                msaaSamples       = 1,
                useMipMap         = true,
                autoGenerateMips  = false,
                mipCount          = 6,
                depthBufferBits   = 0,
                useDynamicScale   = false
            });
            smTex.Create();

            /*   heightTex = new RenderTexture(new RenderTextureDescriptor
             * {
             *     graphicsFormat = GraphicsFormat.R8_UNorm,
             *     dimension = TextureDimension.Tex2DArray,
             *     width = COLOR_RESOLUTION,
             *     height = COLOR_RESOLUTION,
             *     volumeDepth = Mathf.Max(1, terrainData.textures.Length),
             *     enableRandomWrite = true,
             *     msaaSamples = 1,
             *     useMipMap = true,
             *     autoGenerateMips = false,
             *     mipCount = 6,
             *     depthBufferBits = 0,
             *     useDynamicScale = false
             * });*/
            heightloadingCacheRT = new RenderTexture(new RenderTextureDescriptor
            {
                width             = COLOR_RESOLUTION + 2,
                height            = COLOR_RESOLUTION + 2,
                volumeDepth       = 1,
                enableRandomWrite = true,
                dimension         = TextureDimension.Tex2D,
                graphicsFormat    = GraphicsFormat.R16_UNorm,
                msaaSamples       = 1,
                useMipMap         = false,
                autoGenerateMips  = false,
                mipCount          = 0,
                depthBufferBits   = 0,
                useDynamicScale   = false,
            });
            randomTileRT = new RenderTexture(256, 256, 0, GraphicsFormat.R16G16B16A16_SNorm, 0);
            randomTileRT.enableRandomWrite = true;
            randomTileRT.wrapMode          = TextureWrapMode.Repeat;
            randomTileRT.filterMode        = FilterMode.Point;
            randomTileRT.Create();

            heightloadingCacheRT.Create();
            //    heightTex.Create();
            smTex.wrapMode     = TextureWrapMode.Repeat;
            normalTex.wrapMode = TextureWrapMode.Repeat;
            albedoTex.wrapMode = TextureWrapMode.Repeat;
            //   heightTex.wrapMode = TextureWrapMode.Repeat;
            boundBoxLoadList   = new NativeQueue <MaskLoadCommand>(10, Allocator.Persistent);
            boundingLoadStream = new FileStream(terrainData.boundPath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            boundingDict       = new NativeDictionary <int2, MTerrainBoundingTree, Int2Equal>(20, Allocator.Persistent, new Int2Equal());
            InitializeMeshData();
            allDrawCommand = new NativeList <TerrainDrawCommand>(20, Allocator.Persistent);
            materialBuffer = new ComputeBuffer(max(1, terrainData.allMaterials.Length), sizeof(MTerrainData.HeightBlendMaterial));
            materialBuffer.SetData(terrainData.allMaterials);
            decalLayerOffset = max(0, terrainData.lodDistances.Length - terrainData.allDecalLayers.Length);
            tree             = new TerrainQuadTree(-1, TerrainQuadTree.LocalPos.LeftDown, 0, 0, terrainData.largestChunkSize, double3(1, 0, 0), 0);

            StartCoroutine(AsyncLoader());
        }