Beispiel #1
0
        public VoxModel Import(AssetImportContext ctx)
        {
            if (Settings == null)
            {
                Debug.LogError("Not imported: Settings is null");
                return(null);
            }
            var objects = new List <Object>();

            ctx.GetObjects(objects);
            var model = objects.Select(o => o as VoxModel).FirstOrDefault();

            if (model == null)
            {
                model          = ScriptableObject.CreateInstance <VoxModel>();
                model.name     = "vox";
                model.Settings = Settings;
            }
            else
            {
                model.Settings = Settings;
            }
            ctx.AddObjectToAsset("model", model);
            try {
                EditorUtility.DisplayProgressBar("Vox", "", 0);
                GenerateModel(model, ctx);
            } catch (System.Exception ex) {
                Debug.LogException(ex);
            } finally {
                EditorUtility.ClearProgressBar();
            }
            return(model);
        }
Beispiel #2
0
        public int GetNumberOfObjects()
        {
            var objects = new List <UnityEngine.Object>();

            m_Context.GetObjects(objects);
            return(objects.Count);
        }
        public int GetNumberOfObjects()
        {
            var objects = new List <UnityEngine.Object>();

#if UNITY_2018_3_OR_NEWER
            m_Context.GetObjects(objects);
#endif
            return(objects.Count);
        }
    public override void OnImportAsset(AssetImportContext ctx)
    {
        List <Object> objectList = new List <Object>();

        Debug.Log("MAIN OBJECT  " + ctx.mainObject);
        ctx.GetObjects(objectList);
        Debug.Log(" OBJECTS  " + objectList.Count);


        var textGraph = File.ReadAllText(ctx.assetPath, Encoding.UTF8);
        LogicGraphObject loadedGraphObject = AssetDatabase.LoadAssetAtPath <LogicGraphObject>(ctx.assetPath);

        if (loadedGraphObject == null)
        {
            Debug.Log("Generating new");
            var graph = JsonUtility.FromJson <LogicGraphData>(textGraph);
            LogicGraphObject logicGraphObject = ScriptableObject.CreateInstance <LogicGraphObject>();
            logicGraphObject.Initialize(graph);
            ctx.AddObjectToAsset("MainAsset", logicGraphObject);
            ctx.SetMainObject(logicGraphObject);
        }
        else
        {
            Debug.Log("Updating Old");
            JsonUtility.FromJsonOverwrite(textGraph, loadedGraphObject.GraphData);
            ctx.AddObjectToAsset("MainAsset", loadedGraphObject);
            ctx.SetMainObject(loadedGraphObject);
        }

        Debug.Log(loadedGraphObject);

//        AssetDatabase.SaveAssets();
//        EditorSceneManager.SaveOpenScenes();



        Debug.Log("Set Asset");

//        AssetDatabase.Refresh();
    }
Beispiel #5
0
        GameObject GenerateModel(VoxModel model, AssetImportContext ctx)
        {
            var parser = new MagicaVoxelParser();

            parser.LoadModel(ctx.assetPath, model,
                             s => EditorUtility.DisplayProgressBar("vox", s, 0));

            var subAssets = new List <Object>();

            ctx.GetObjects(subAssets);
            var assetMeshes = subAssets
                              .Select(s => s as Mesh)
                              .Where(s => s != null)
                              .ToArray();
            var updateOpaque      = false;
            var updateTransparent = false;

            var uv3 = new List <Vector4>();

            var assetIndex = 0;

            for (int i = 0; i < model.meshes.Count; i++)
            {
                for (int l = 0; l < model.meshes[i].LODs.Count; l++)
                {
                    MeshSet m;
                    //get mesh
                    m.opaque      = assetIndex < assetMeshes.Length ? assetMeshes[assetIndex++] : null;
                    m.transparent = assetIndex < assetMeshes.Length ? assetMeshes[assetIndex++] : null;
                    updateOpaque  = m.opaque == null;
                    if (updateOpaque)
                    {
                        m.opaque = new Mesh();
                    }
                    updateTransparent = m.transparent == null;
                    if (updateTransparent)
                    {
                        m.transparent = new Mesh();
                    }

                    //turn temp meshes into assets

                    EditorUtility.DisplayProgressBar("vox",
                                                     $"asset: frame={i}/{model.meshes.Count}, lod={l}/{model.meshes[i].LODs.Count}",
                                                     .5f + (.5f * i) / model.voxelFrames.Count);
                    var frame = model.meshes[i].LODs[l];
                    BuildMesh(m.opaque, frame.opaque,
                              $"{name}.{i}.{l}.opaque", uv3, model.Settings.OmitsUVUnwrapping);
                    BuildMesh(m.transparent, frame.transparent,
                              $"{name}.{i}.{l}.transparent", uv3, model.Settings.OmitsUVUnwrapping);
                    //new mesh
                    if (updateOpaque)
                    {
                        ctx.AddObjectToAsset(m.opaque.name, m.opaque);
                    }
                    if (updateTransparent)
                    {
                        ctx.AddObjectToAsset(m.transparent.name, m.transparent);
                    }
                    model.meshes[i].LODs[l] = m;
                }
            }

            var baseGO = ctx.mainObject as GameObject;

            if (baseGO == null)
            {
                baseGO = new GameObject();
                ctx.AddObjectToAsset("main", baseGO);
                ctx.SetMainObject(baseGO);
            }
            var gos = Enumerable.Range(0, baseGO.transform.childCount)
                      .Select(i => baseGO.transform.GetChild(i))
                      .Select(t => t.gameObject)
                      .ToList();
            var gosCount = gos.Count;

            for (int i = gosCount; i < model.meshes.Count; i++)
            {
                var target = new GameObject();
                target.transform.SetParent(baseGO.transform);
                gos.Add(target.gameObject);
            }
            BuildLODGroups(model, gos);
            if (model.Settings.asSceneGraph)
            {
                var root = BuildSceneGraph(model, gos);
                DestroyImmediate(baseGO);
                baseGO = root;
                ctx.AddObjectToAsset("main", root);
                ctx.SetMainObject(root);
            }
            //destroy unneeded meshes
            var meshSetContained = model.meshes.SelectMany(x => x.LODs).ToArray();

            foreach (var go in subAssets
                     .Where(s => (s as Mesh) != null)
                     .Where(s => !meshSetContained.Any(x => x.Contains(s as Mesh))))
            {
                DestroyImmediate(go, true);
            }

            //DestroyImmediate(baseGO);
            return(baseGO);
        }