Exemplo n.º 1
0
    public void SetMaterialData(CloudAPI.FileParameterDataBin data, int layer)
    {
        int            num_dates = data.info.num_dates;
        int            w         = data.info.width;
        int            h         = data.info.height;
        int            l         = data.info.num_layers;
        int            offset    = w * h * layer;
        Texture2DArray array     = new Texture2DArray(w, h, num_dates, TextureFormat.RFloat, false, false);

        for (int t = 0; t < num_dates; t++)
        {
            array.SetPixelData <float>(data.data, 0, t, l * w * h * t + offset);
        }
        array.Apply(false);
        material.SetTexture("Texture2DArray_28e7e253cb18486a8c14899af2143136", array);                                     // Textures
        material.SetVector("Vector2_540929bd5a9e4b91803993f7ccde8e73", new Vector4(data.info.lon_min, data.info.lon_max)); // LonMinMax
        material.SetVector("Vector2_a03a182a418a49fd8146e64479d05bb6", new Vector4(data.info.lat_min, data.info.lat_max)); // LatMinMax
        material.SetFloat("Vector1_8211db379e8f4f01b70f108ef594b658", data.info.width);                                    // Width
        material.SetFloat("Vector1_51fb8cd46af548f6af8aef823b6bffa7", 0);                                                  // Overlay Opacity
        // material.SetFloat("Vector1_eb79a7293bb64596949928ce17eb143b", 0); // Texture Index

        currentLayer = layer;
        currentData  = data;

        ResetRange();
    }
Exemplo n.º 2
0
    private void LoadTextures(string[] texturePaths, Material chunkMaterial)
    {
        int            mipMapCount  = 1 + (int)Math.Floor(Math.Log(Math.Max(16, 16)));
        Texture2DArray textureArray = new Texture2DArray(16, 16, texturePaths.Length, TextureFormat.RGBA32, mipMapCount, false)
        {
            filterMode = FilterMode.Point
        };

        for (int i = 0; i < texturePaths.Length; i++)
        {
            string texturePath = texturePaths[i];
            if (!File.Exists(texturePath))
            {
                throw new FileNotFoundException("The provided path does not contain an image file!");
            }

            Texture2D textureData = new Texture2D(16, 16, TextureFormat.RGBA32, true, false);
            if (!textureData.LoadImage(File.ReadAllBytes(texturePath)))
            {
                throw new Exception("The provided image did not load correctly! Ensure that the file is a 32bit RGBA PNG, and that it isn't corrupted!");
            }

            textureData.Apply(true);

            for (int j = 0; j < mipMapCount; j++)
            {
                textureArray.SetPixelData(textureData.GetPixels32(j), j, i);
            }
        }

        textureArray.Apply(true);

        chunkMaterial.SetTexture("_TextureArray", textureArray);
    }
            public void Apply()
            {
                if (textureArray)
                {
                    Object.Destroy(textureArray);
                }

                // Build Expanded Texture Array
                textureArray = new Texture2DArray(
                    width: width,
                    height: height,
                    depth: indicesByShapeDescriptor.Count,
                    textureFormat: format,
                    mipChain: hasMipLevels,
                    linear: false);

                textureArray.filterMode = FilterMode.Point;
                textureArray.name       = "Dynamic Texture Array";

                foreach (var descriptorIndexPair in indicesByShapeDescriptor)
                {
                    var texture = GetTexture(descriptorIndexPair.Key, returnPlaceholderIfNotFound: true);

                    for (var mipIndex = 0; mipIndex < texture.mipmapCount; mipIndex++)
                    {
                        if (texture.format == TextureFormat.ARGB32)
                        {
                            var textureMip = texture.GetPixelData <Color32>(mipIndex);
                            textureArray.SetPixelData(textureMip, mipIndex, descriptorIndexPair.Value);
                        }
                        else
                        {
                            var textureMip = texture.GetPixelData <Color24>(mipIndex);
                            textureArray.SetPixelData(textureMip, mipIndex, descriptorIndexPair.Value);
                        }
                    }

#if NO_EDITING
                    // TODO: should unload "texture" when done adding it to the array, if NO_EDITING
#endif
                }

                textureArray.Apply();

                SharedMaterial.mainTexture = textureArray;
            }
Exemplo n.º 4
0
        /// <summary>
        /// Converts the current Texture2DArray in DXT1 and DXT5 format to ARGB32 and makes it readable. Important: this creates a new Texture2DArray
        /// </summary>
        /// <param name="destroyOldTexture2DArray">Destroy the old Texture2DArray?</param>
        public void MakeReadable(bool destroyOldTexture2DArray = false)
        {
            if (isReadable)
            {
                return;
            }

            RenderTexture renderTex = RenderTexture.GetTemporary(
                pageSize,
                pageSize,
                0,
                RenderTextureFormat.Default,
                RenderTextureReadWrite.Linear);

            Texture2D     texture  = new Texture2D(pageSize, pageSize, TextureFormat.ARGB32, false);
            RenderTexture previous = RenderTexture.active;

            RenderTexture.active = renderTex;

            Texture2DArray newArray = new Texture2DArray(pageSize, pageSize, textureArray.depth, TextureFormat.ARGB32, false);

            newArray.wrapMode = TextureWrapMode.Clamp;

            for (int i = 0; i < cols * rows; i++)
            {
                Graphics.Blit(textureArray, renderTex, i, 0);
                texture.ReadPixels(new Rect(0, 0, renderTex.width, renderTex.height), 0, 0);
                texture.Apply();
                newArray.SetPixelData(texture.GetRawTextureData(), 0, i);
            }

            newArray.Apply();
            if (destroyOldTexture2DArray)
            {
                Object.Destroy(textureArray);
            }
            textureArray = newArray;

            Object.Destroy(texture);

            RenderTexture.active = previous;
            RenderTexture.ReleaseTemporary(renderTex);

            isReadable = true;
        }
Exemplo n.º 5
0
        private Texture2DArray InitTexture2DArray(Texture2D sourceTexture, AssetImportContext ctx)
        {
            TextureFormat format = GetTextureFormat(ctx.selectedBuildTarget, compressed, transparent);

            EditorUtility.DisplayProgressBar("Import Huge Texture", ctx.assetPath, 0);

            Texture2DArray array = null;

            try
            {
                if (format == sourceTexture.format)
                {
                    array          = new Texture2DArray(pageSize, pageSize, cols * rows, sourceTexture.format, false);
                    array.wrapMode = TextureWrapMode.Clamp;
                    for (int x = 0; x < cols; x++)
                    {
                        for (int y = 0; y < rows; y++)
                        {
                            EditorUtility.DisplayProgressBar("Import Huge Texture", ctx.assetPath, (x * rows + y) / (float)(cols * rows));
                            Graphics.CopyTexture(sourceTexture, 0, 0, x * pageSize, y * pageSize, pageSize, pageSize, array, y * cols + x, 0, 0, 0);
                        }
                    }
                }
                else if (!compressed)
                {
                    array          = new Texture2DArray(pageSize, pageSize, cols * rows, format, false);
                    array.wrapMode = TextureWrapMode.Clamp;
                    for (int x = 0; x < cols; x++)
                    {
                        for (int y = 0; y < rows; y++)
                        {
                            EditorUtility.DisplayProgressBar("Import Huge Texture", ctx.assetPath, (x * rows + y) / (float)(cols * rows));
                            Color[] colors = sourceTexture.GetPixels(x * pageSize, y * pageSize, pageSize, pageSize);
                            array.SetPixels(colors, y * cols + x);
                        }
                    }
                }
                else if (transparent)
                {
                    array          = new Texture2DArray(pageSize, pageSize, cols * rows, format, false);
                    array.wrapMode = TextureWrapMode.Clamp;
                    for (int x = 0; x < cols; x++)
                    {
                        for (int y = 0; y < rows; y++)
                        {
                            EditorUtility.DisplayProgressBar("Import Huge Texture", ctx.assetPath, (x * rows + y) / (float)(cols * rows));
                            Texture2D tempTexture = new Texture2D(pageSize, pageSize);
                            Color[]   colors      = sourceTexture.GetPixels(x * pageSize, y * pageSize, pageSize, pageSize);
                            tempTexture.SetPixels(colors);
                            tempTexture.Apply();
                            EditorUtility.CompressTexture(tempTexture, format, quality);

                            array.SetPixelData(tempTexture.GetRawTextureData(), 0, y * cols + x);
                            DestroyImmediate(tempTexture);
                        }
                    }
                }
                else
                {
                    array          = new Texture2DArray(pageSize, pageSize, cols * rows, format, false);
                    array.wrapMode = TextureWrapMode.Clamp;
                    for (int x = 0; x < cols; x++)
                    {
                        for (int y = 0; y < rows; y++)
                        {
                            EditorUtility.DisplayProgressBar("Import Huge Texture", ctx.assetPath, (x * rows + y) / (float)(cols * rows));
                            Texture2D tempTexture = new Texture2D(pageSize, pageSize, TextureFormat.RGB24, false);
                            Color[]   colors      = sourceTexture.GetPixels(x * pageSize, y * pageSize, pageSize, pageSize);
                            tempTexture.SetPixels(colors);
                            tempTexture.Apply();
                            EditorUtility.CompressTexture(tempTexture, format, quality);

                            array.SetPixelData(tempTexture.GetRawTextureData(), 0, y * cols + x);
                            DestroyImmediate(tempTexture);
                        }
                    }
                }

                array.Apply(false);
            }
            catch
            {
            }

            EditorUtility.ClearProgressBar();

            return(array);
        }
Exemplo n.º 6
0
        private Texture2DArray InitTexture2DArray(Stream stream, AssetImportContext ctx)
        {
            TextureFormat format = GetTextureFormat(ctx.selectedBuildTarget, compressed, transparent);

            Texture2DArray array = null;

            EditorUtility.DisplayProgressBar("Import Huge Texture", ctx.assetPath, 0);

            try
            {
                array          = new Texture2DArray(pageSize, pageSize, cols * rows, format, false);
                array.wrapMode = TextureWrapMode.Clamp;
                TextureFormat textureFormat = transparent ? TextureFormat.RGBA32 : TextureFormat.RGB24;
                Texture2D     texture       = new Texture2D(pageSize, pageSize, textureFormat, false);

                BinaryReader reader = new BinaryReader(stream);

                int       colorSize  = GetPixelSize(colorFormat);
                int       bufferSize = pageSize * colorSize * depth / 8;
                Color32[] colors     = new Color32[pageSize * pageSize];

                int lastY = rows * pageSize - 1;

                for (int row = 0; row < rows; row++)
                {
                    for (int col = 0; col < cols; col++)
                    {
                        EditorUtility.DisplayProgressBar("Import Huge Texture", ctx.assetPath, (row * cols + col) / (float)(cols * rows));

                        for (int y = 0; y < pageSize; y++)
                        {
                            int srow = lastY - row * pageSize - y;
                            int scol = col * pageSize;

                            long offset = ((long)srow * originalWidth + scol) * colorSize * depth / 8;
                            stream.Seek(offset, SeekOrigin.Begin);

                            byte[] buffer = reader.ReadBytes(bufferSize);
                            if (depth == 8)
                            {
                                Parse8BitRow(buffer, colors, y);
                            }
                            else
                            {
                                Parse16BitRow(buffer, colors, y);
                            }
                        }

                        texture.SetPixels32(colors);
                        texture.Apply(false);

                        if (compressed)
                        {
                            EditorUtility.CompressTexture(texture, format, quality);
                        }

                        array.SetPixelData(texture.GetRawTextureData(), 0, row * cols + col);

                        if (compressed)
                        {
                            DestroyImmediate(texture);
                            texture = new Texture2D(pageSize, pageSize, textureFormat, false);
                        }
                    }
                }

                DestroyImmediate(texture);

                reader.Close();

                array.Apply();
            }
            catch
            {
            }

            EditorUtility.ClearProgressBar();

            return(array);
        }