private void CreateScreenShot()
        {
            string path = Util.FixPath(EditorUtility.SaveFilePanel("Select Export Path", "Assets", Util.GetNameWithoutExtension(VoxelFilePath) + "_screenShot", "png"));

            if (!string.IsNullOrEmpty(path))
            {
                path = Util.FixedRelativePath(path);
                if (!string.IsNullOrEmpty(path))
                {
                    bool oldShow = ShowBackgroundBox;
                    CubeTF.gameObject.SetActive(false);
                    SetBoxBackgroundActive(false);
                    var texture = Util.RenderTextureToTexture2D(Camera);
                    if (texture)
                    {
                        texture = Util.TrimTexture(texture, 0.01f, 12);
                        Util.ByteToFile(texture.EncodeToPNG(), path);
                        VoxelPostprocessor.AddScreenshot(path);
                        AssetDatabase.SaveAssets();
                        AssetDatabase.Refresh();
                        EditorApplication.delayCall += VoxelPostprocessor.ClearAsset;
                    }
                    CubeTF.gameObject.SetActive(true);
                    SetBoxBackgroundActive(oldShow);
                }
                else
                {
                    Util.Dialog("Warning", "Export path must in Assets folder.", "OK");
                }
            }
        }
Пример #2
0
        private void CreateSprite(Core_Sprite.SpriteType type)
        {
            if (!Data)
            {
                return;
            }
            string path = Util.FixPath(EditorUtility.SaveFilePanel("Select Export Path", "Assets", Util.GetNameWithoutExtension(VoxelFilePath) + type.ToString(), "png"));

            if (!string.IsNullOrEmpty(path))
            {
                path = Util.FixedRelativePath(path);
                if (!string.IsNullOrEmpty(path))
                {
                    bool oldShow = ShowBackgroundBox;
                    CubeTF.gameObject.SetActive(false);
                    SetBoxBackgroundActive(false);

                    var result = Core_Sprite.CreateSprite(
                        Data,
                        CurrentModelIndex,
                        type,
                        GetSpriteNum(type),
                        GetSpriteLight(type),
                        GetSpritePivot(type),
                        Camera,
                        Sprite25DCameraScale
                        );

                    CubeTF.gameObject.SetActive(true);
                    SetBoxBackgroundActive(oldShow);

                    if (result.Texture)
                    {
                        Util.ByteToFile(result.Texture.EncodeToPNG(), path);
                        VoxelPostprocessor.AddSprite(path, new VoxelPostprocessor.SpriteConfig()
                        {
                            width       = result.Width,
                            height      = result.Height,
                            Pivots      = result.Pivots,
                            spriteRects = result.Rects,
                            Names       = result.NameFixes,
                        });
                        AssetDatabase.SaveAssets();
                        AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
                        EditorApplication.delayCall += VoxelPostprocessor.ClearAsset;
                    }
                }
                else
                {
                    Util.Dialog("Warning", "Export path must in Assets folder.", "OK");
                }
            }
        }
        // Data



        #endregion



        #region --- TSK ---



        private void CreateSprite(bool _8bit)
        {
            if (!Data)
            {
                return;
            }
            string path = Util.FixPath(EditorUtility.SaveFilePanel("Select Export Path", "Assets", Util.GetNameWithoutExtension(VoxelFilePath) + (_8bit ? "_8bit" : "_2D"), "png"));

            if (!string.IsNullOrEmpty(path))
            {
                path = Util.FixedRelativePath(path);
                if (!string.IsNullOrEmpty(path))
                {
                    var result = _8bit ?
                                 EditorSpriteCore.Create8bitSprite(Data, CurrentModelIndex) :
                                 EditorSpriteCore.Create2DSprite(Data, CurrentModelIndex);
                    if (result.Texture)
                    {
                        Util.ByteToFile(result.Texture.EncodeToPNG(), path);
                        VoxelPostprocessor.AddSprite(path, new VoxelPostprocessor.SpriteConfig()
                        {
                            width       = result.Width,
                            height      = result.Height,
                            Pivots      = result.Pivots,
                            spriteRects = result.Rects,
                            Names       = result.NameFixes,
                        });
                        AssetDatabase.SaveAssets();
                        AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
                        EditorApplication.delayCall += VoxelPostprocessor.ClearAsset;
                    }
                }
                else
                {
                    Util.Dialog("Warning", "Export path must in Assets folder.", "OK");
                }
            }
        }
Пример #4
0
        public static void CreateFileForResult(List <Core_Voxel.Result> resultList, Shader[] shaders, string[] shaderKeywords, Vector2[] shaderRemaps, float scale, Vector3 pivot)
        {
            var diffuseShader = shaders[0];

            for (int index = 0; index < resultList.Count; index++)
            {
                var  result     = resultList[index];
                bool lod        = result.VoxelModels.Length > 1;
                bool isRig      = !lod && result.IsRigged;
                int  realLodNum = result.IsRigged ? 1 : result.VoxelModels.Length;

                var         root         = new GameObject(result.FileName).transform;
                var         meshs        = new List <Mesh>();
                var         materialsMap = new Dictionary <Texture2D, Material[]>();
                Transform[] lodRoots     = new Transform[realLodNum];
                for (int lodIndex = 0; lodIndex < realLodNum; lodIndex++)
                {
                    var voxelModel = result.VoxelModels[lodIndex];
                    var model      = CreateModelFrom(voxelModel.RootNode, voxelModel.Materials, root, pivot, ref meshs, ref materialsMap, isRig, result.WithAvatar, shaders, shaderKeywords, shaderRemaps, scale);
                    model.name         = string.Format("Root{0}", lod ? "_lod " + lodIndex.ToString() : "");
                    lodRoots[lodIndex] = model;

                    // Rig
                    if (isRig)
                    {
                        Vector3 halfModelSize = voxelModel.ModelSize[0] * 0.5f;
                        halfModelSize.x = Mathf.Floor(halfModelSize.x);
                        halfModelSize.y = Mathf.Floor(halfModelSize.y);
                        halfModelSize.z = Mathf.Floor(halfModelSize.z);

                        var skinMR = model.GetComponent <SkinnedMeshRenderer>();
                        if (skinMR)
                        {
                            Vector3 rootBoneOffset = halfModelSize * scale;
                            var     boneTFList     = new List <Transform>();
                            if (voxelModel.RootBones != null)
                            {
                                for (int i = 0; i < voxelModel.RootBones.Length; i++)
                                {
                                    var boneTF = CreateBoneTransform(voxelModel.RootBones[i], model, scale, ref boneTFList);
                                    if (boneTF)
                                    {
                                        boneTF.localPosition -= rootBoneOffset;
                                    }
                                }
                            }

                            skinMR.bones    = boneTFList.ToArray();
                            skinMR.rootBone = model;

                            // Bind Poses
                            var poses = new Matrix4x4[boneTFList.Count];
                            for (int i = 0; i < boneTFList.Count; i++)
                            {
                                poses[i] = boneTFList[i].worldToLocalMatrix * model.localToWorldMatrix;
                            }
                            skinMR.sharedMesh.bindposes = poses;
                        }

                        // Foot Fix
                        model.localPosition = (halfModelSize - voxelModel.FootPoints[lodIndex]) * scale;
                    }
                }



                // Lod
                if (lod)
                {
                    LODGroup group = root.gameObject.AddComponent <LODGroup>();
                    LOD[]    lods  = new LOD[realLodNum];
                    for (int i = 0; i < realLodNum; i++)
                    {
                        lods[i] = new LOD(
                            i == realLodNum - 1 ? 0.001f : GetLodRant(result.VoxelModels[i].MaxModelBounds, i),
                            lodRoots[i].GetComponentsInChildren <MeshRenderer>(true)
                            );
                    }
#if UNITY_5_0 || UNITY_5_1 || UNITY_4
                    group.SetLODS(lods);
                    group.RecalculateBounds();
#else
                    group.SetLODs(lods);
                    group.RecalculateBounds();
#endif
                }
                else if (!isRig && root.childCount > 0)
                {
                    var newRoot = root.GetChild(0);
                    newRoot.name = root.name;
                    root         = newRoot;
                }



                // File
                string path = Util.CombinePaths(
                    result.ExportRoot,
                    result.ExportSubRoot,
                    result.FileName + result.Extension
                    );
                path = Util.FixPath(path);
                string parentPath = Util.GetParentPath(path);
                Util.CreateFolder(parentPath);

                if (result.Extension == ".prefab")
                {
                    Object prefab;

                    if (Util.FileExists(path))
                    {
                        prefab = AssetDatabase.LoadAssetAtPath <Object>(path);
                        if (prefab as GameObject)
                        {
                            var group = (prefab as GameObject).GetComponent <LODGroup>();
                            if (group)
                            {
                                Object.DestroyImmediate(group, true);
                            }
                        }
                        Object[] things = AssetDatabase.LoadAllAssetRepresentationsAtPath(path);
                        foreach (Object o in things)
                        {
                            Object.DestroyImmediate(o, true);
                        }
                    }
                    else
                    {
#if UNITY_4 || UNITY_5 || UNITY_2017 || UNITY_2018_1 || UNITY_2018_2
                        prefab = PrefabUtility.CreateEmptyPrefab(path);
#else   // 2018.3+
                        var tempObject = new GameObject();
                        prefab = PrefabUtility.SaveAsPrefabAsset(tempObject, path);
                        Object.DestroyImmediate(tempObject, false);
#endif
                    }

                    if (prefab)
                    {
                        // Assets
                        for (int i = 0; i < meshs.Count; i++)
                        {
                            meshs[i].name = GetIndexedName("Mesh", i, meshs.Count);
                            AssetDatabase.AddObjectToAsset(meshs[i], path);
                        }
                        int currentIndex = 0;
                        foreach (var textureMat in materialsMap)
                        {
                            textureMat.Key.name = GetIndexedName("Texture", currentIndex, materialsMap.Count);
                            AssetDatabase.AddObjectToAsset(textureMat.Key, path);
                            var mats = textureMat.Value;
                            for (int i = 0; i < mats.Length; i++)
                            {
                                mats[i].name = GetIndexedName("Material", currentIndex, materialsMap.Count) + "_" + i.ToString();
                                AssetDatabase.AddObjectToAsset(mats[i], path);
                            }
                            currentIndex++;
                        }

                        // Avatar
                        if (isRig && result.WithAvatar)
                        {
                            var avatar = GetVoxelAvatarInRoot(root);
                            if (avatar)
                            {
                                avatar.name = result.FileName;
                                AssetDatabase.AddObjectToAsset(avatar, path);

                                // Animator
                                var ani = root.GetComponent <Animator>();
                                if (!ani)
                                {
                                    ani = root.gameObject.AddComponent <Animator>();
                                }
                                ani.avatar = avatar;
                            }
                            else
                            {
                                Debug.LogWarning("[Voxel to Unity] Failed to get avatar from the prefab. Use \"+ Human Bones\" button in rig editor to create bones and don\'t change their names and layout.");
                            }
                        }
                        // Prefab
#if UNITY_4 || UNITY_5 || UNITY_2017 || UNITY_2018_1 || UNITY_2018_2
                        PrefabUtility.ReplacePrefab(root.gameObject, prefab, ReplacePrefabOptions.ReplaceNameBased);
#else  // 2018.3+
                        prefab = PrefabUtility.SaveAsPrefabAsset(root.gameObject, path);
#endif
                    }
                }
                else                     // Obj

                {
                    string objFolderPath     = Util.CombinePaths(parentPath, result.FileName);
                    string textureFolderPath = Util.CombinePaths(objFolderPath, "Textures");
                    Util.CreateFolder(objFolderPath);

                    VoxelPostprocessor.TheShader = diffuseShader;

                    // Assets
                    var model = result.VoxelModels[0];
                    for (int modelIndex = 0; modelIndex < model.Meshs.Length; modelIndex++)
                    {
                        string modelIndexedName = GetIndexedName(result.FileName, modelIndex, model.Meshs.Length);
                        string modelPathRoot    = Util.CombinePaths(objFolderPath, modelIndexedName);

                        // Texture
                        string texturePath = Util.CombinePaths(textureFolderPath, modelIndexedName + ".png");
                        texturePath = Util.FixPath(texturePath);
                        var texture = model.Textures[modelIndex];
                        Util.ByteToFile(texture.EncodeToPNG(), texturePath);
                        VoxelPostprocessor.AddTexture(texturePath);

                        // Meshs
                        var uMesh = model.Meshs[modelIndex];
                        for (int i = 0; i < uMesh.Count; i++)
                        {
                            uMesh[i].name = GetIndexedName("Mesh", i, uMesh.Count);
                            string obj     = Util.GetObj(uMesh[i]);
                            string objPath = GetIndexedName(modelPathRoot, i, uMesh.Count) + ".obj";

                            bool hasObjBefore = Util.FileExists(objPath);

                            Util.Write(obj, objPath);
                            VoxelPostprocessor.AddObj(objPath, texturePath);

                            if (hasObjBefore)
                            {
                                AssetDatabase.ImportAsset(Util.FixedRelativePath(objPath), ImportAssetOptions.ForceUpdate);
                            }
                        }
                    }
                }


                // Delete Objects
                if (root.parent)
                {
                    Object.DestroyImmediate(root.parent.gameObject, false);
                }
                else
                {
                    Object.DestroyImmediate(root.gameObject, false);
                }
            }
            AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
            AssetDatabase.SaveAssets();
            Resources.UnloadUnusedAssets();

            EditorApplication.delayCall += VoxelPostprocessor.ClearAsset;
        }