public static Texture3D BuildTexture(byte[] data, Int3 volumeSize, Int3 volumeSizePow2)
        {
            var colorData = VolumeTextureUtils.ByteArrayToColor32Array(data, volumeSize, volumeSizePow2);

            var tex = new Texture3D(volumeSizePow2.x, volumeSizePow2.y, volumeSizePow2.z, TextureFormat.RGBA4444, false);

            tex.filterMode = FilterMode.Bilinear;
            tex.wrapMode   = TextureWrapMode.Clamp;

            tex.SetPixels32(colorData);
            tex.Apply();

            return(tex);
        }
        public override void OnInspectorGUI()
        {
            var volInfo = this.target as VolumeInformation;

            base.OnInspectorGUI();

            if (GUILayout.Button("Bake"))
            {
                if (volInfo == null)
                {
                    throw new ArgumentNullException();
                }

                var texPath = EditorUtility.SaveFilePanel("Save volume", Application.dataPath, "volume", "asset");
                texPath = texPath.Replace(Application.dataPath, "Assets");

                AssetDatabase.DeleteAsset(texPath);
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();

                var size           = Int3.zero;
                var bakedSliceData = VolumeImportImages.ConvertFolderToVolume(Application.dataPath + volInfo.ImageSourceFolder,
                                                                              volInfo.InferAlpha, out size);

                if (volInfo.AutoSizeOnBake)
                {
                    volInfo.Size = size;
                }

                var volumeSizePow2 = MathExtensions.PowerOfTwoGreaterThanOrEqualTo(volInfo.Size);
                var tex3D          = VolumeTextureUtils.BuildTexture(bakedSliceData, volInfo.Size, volumeSizePow2);

                AssetDatabase.CreateAsset(tex3D, texPath);
                AssetDatabase.SaveAssets();

                volInfo.BakedTexture = AssetDatabase.LoadAssetAtPath <Texture3D>(texPath);

                Debug.Log("Baked volume to: " + texPath);

                EditorUtility.SetDirty(volInfo);
            }
        }
Exemple #3
0
        //private static readonly string[] ValidImageFileExtensions = { ".psd", ".tiff", ".jpg", ".tga", ".png", ".gif", ".bmp", ".iff", ".pict" };

        public static byte[] ConvertFolderToVolume(string folder, bool inferAlpha, out Int3 size)
        {
            var imageNames = GetImagesInFolder(folder);

            size = GetSizeOfVolumeFolder(folder);
            var voxels = new VolumeBuffer <Color32>(size);

            var tex = new Texture2D(2, 2);

            int z = 0;

            foreach (var imageFile in imageNames)
            {
                bool loaded = tex.LoadImage(FileSystemHelper.ReadBytesFromLocalFile(imageFile));
                if (!loaded)
                {
                    Debug.LogError("Couldn't load '" + imageFile + "'...");
                    return(null);
                }
                var fromPixels = tex.GetPixels32();
                for (var y = 0; y < size.y; ++y)
                {
                    for (var x = 0; x < size.x; ++x)
                    {
                        var from = fromPixels[x + (y * size.x)];
                        if (inferAlpha)
                        {
                            from.a = (byte)Mathf.Max(from.r, from.g, from.b);
                        }
                        voxels.SetVoxel(new Int3(x, y, z), from);
                    }
                }
                ++z;
            }

            voxels.ClearEdges(new Color32(0, 0, 0, 0));
            return(VolumeTextureUtils.Color32ArrayToByteArray(voxels.DataArray));
        }