Example #1
0
        public static void ReLoad(List <LODAsset> lods, LodNode node)
        {
            var go = GameObject.Find(node.prefab);

            if (go)
            {
                GameObject.DestroyImmediate(go);
            }
            go = CreateRole(node, lods.Count, false);
            for (int i = 0; i < lods.Count; i++)
            {
                string name = node.prefab + "_LOD" + i;
                var    tf   = go.transform.Find(name);
                if (tf)
                {
                    lods[i].Drop(tf.gameObject);
                }
                string pat = prefix + node.prefab;
                lods[i].LoadMesh(pat, i);
                if (tf)
                {
                    lods[i].Drop(tf.gameObject);
                }
            }
            camera.transform.position = new Vector3(1.8f, 1, 3.0f);
        }
Example #2
0
        public static void Export(LODAsset[] lods, LodNode lodNode)
        {
            SortLod(lods, lodNode);

            lod0_parts = lods[0].go.GetComponentsInChildren <SkinnedMeshRenderer>();
            string name = lods[0].go.name;

            name = name.Substring(0, name.LastIndexOf("_LOD"));
            // lod0 export directly
            for (int i = 0; i < lod0_parts.Length; i++)
            {
                var mesh = CopyMesh(lod0_parts[i]);
                Save(name, 0, mesh, lodNode);
            }
            // recalculte mesh weights & bindpose
            for (int i = 1; i < lods.Length; i++)
            {
                var ps = lods[i].go.GetComponentsInChildren <SkinnedMeshRenderer>();
                foreach (var render in ps)
                {
                    var mesh = CopyMesh(render);
                    if (Recalculate(render, ref mesh))
                    {
                        Save(name, i, mesh, lodNode);
                    }
                }
            }
            AssetDatabase.Refresh();
        }
Example #3
0
        public void LoadRole(LodNode data)
        {
            m_NumberOfLODs = data.levels.Length;
            m_LODs.Clear();

            var go = LodUtil.CreateRole(data, m_NumberOfLODs, true);

            for (int i = 0; i < m_NumberOfLODs; i++)
            {
                LODAsset oDAsset = new LODAsset();
                oDAsset.screenPercentage = data.levels[i];
                if (i == 0)
                {
                    m_selectLOD     = 0;
                    m_cameraPercent = data.levels[i] + (1 - data.levels[i]) * 0.5f;
                }
                string name = data.prefab + "_LOD" + i;
                var    tf   = go.transform.Find(name);
                if (tf)
                {
                    oDAsset.Drop(tf.gameObject);
                }
                m_LODs.Add(oDAsset);
            }
            LodUtil.CheckBoneValid(m_LODs, out bone_error);
        }
Example #4
0
 private void OpenLodWin(LodNode lod)
 {
     if (LodUtil.MakeLodEnv())
     {
         var win = LodWindow.LodShow();
         win.LoadRole(lod);
         win.UpdateInfo();
     }
 }
Example #5
0
        private static void Save(string name, int level, Mesh mesh, LodNode lodNode)
        {
            string part = mesh.name;
            string dir  = LodUtil.prefix + name + "/lod" + level;
            string file = part + ".mesh";

            ExMesh format = lodNode.Format(level);

            if ((format & ExMesh.COLOR) <= 0)
            {
                mesh.colors = null;
            }
            if ((format & ExMesh.UV1) <= 0)
            {
                mesh.uv = null;
            }
            if ((format & ExMesh.UV2) <= 0)
            {
                mesh.uv2 = null;
            }
            if ((format & ExMesh.UV3) <= 0)
            {
                mesh.uv3 = null;
            }
            if ((format & ExMesh.UV4) <= 0)
            {
                mesh.uv4 = null;
            }
            if ((format & ExMesh.NORMAL) <= 0)
            {
                mesh.normals = null;
            }
            if ((format & ExMesh.TANGENT) <= 0)
            {
                mesh.tangents = null;
            }

            mesh.Optimize();
            var path = Path.Combine(dir, file);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            AssetDatabase.CreateAsset(mesh, path);
            AssetDatabase.ImportAsset(path);
        }
Example #6
0
        public override void OnInspectorGUI()
        {
            GUILayout.BeginVertical();

            GUILayout.BeginHorizontal();
            GUILayout.Label(icon, LODGUI.Styles.LODRendererButton, GUILayout.MaxWidth(32), GUILayout.MaxHeight(32));
            GUILayout.Label("Lod Data", LODGUI.titleLableStyle);
            GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();

            LodNode action = null;

            search = GuiSearch(search);
            if (Data.nodes != null)
            {
                for (int i = 0; i < Data.nodes.Length; i++)
                {
                    var node = Data.nodes[i];
                    if (node.Match(search))
                    {
                        var op = node.GUI();
                        if (op == LodOP.DELETE)
                        {
                            Data.nodes = LodUtil.Remv <LodNode>(Data.nodes, i);
                            break;
                        }
                        else if (op == LodOP.DETAIL)
                        {
                            action = node;
                        }
                    }
                }
            }
            GuiButtons();
            GUILayout.EndVertical();
            if (action != null)
            {
                OpenLodWin(action);
            }
        }
Example #7
0
        public static GameObject CreateRole(LodNode node, int numLod, bool useFbx)
        {
            var go = GameObject.Find(node.prefab);

            if (go == null)
            {
                go = new GameObject(node.prefab);
                go.transform.position   = Vector3.zero;
                go.transform.rotation   = Quaternion.identity;
                go.transform.localScale = Vector3.one;

                for (int i = 0; i < numLod; i++)
                {
                    string name  = node.prefab + "_LOD" + i;
                    string path  = useFbx ? node.dir + "/" + name + ".fbx" : pref + node.prefab + ".prefab";
                    var    obj   = AssetDatabase.LoadAssetAtPath <GameObject>(path);
                    var    child = GameObject.Instantiate(obj);
                    child.transform.parent   = go.transform;
                    child.name               = name;
                    child.transform.position = new Vector3(useFbx ? 8 * rand++ : i * 2, 0, 0);
                }
            }
            return(go);
        }
Example #8
0
        private void GenerateBytes()
        {
            string       path   = "Assets/Resources/conf.bytes";
            FileStream   fs     = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
            BinaryWriter writer = new BinaryWriter(fs);
            int          cnt    = nodes.Length;

            writer.Write(cnt);
            for (int i = 0; i < cnt; i++)
            {
                LodNode node = nodes[i];
                writer.Write(node.prefab);
                writer.Write(node.levels.Length);
                for (int j = 0; j < node.levels.Length; j++)
                {
                    writer.Write(node.levels[j]);
                }
            }
            writer.Flush();
            writer.Close();
            fs.Close();

            AssetDatabase.ImportAsset(path);
        }
Example #9
0
        public static void SortLod(LODAsset[] lods, LodNode lodNode)
        {
            for (int k = 0; k < lods.Length - 1; k++)
            {
                for (int i = 0; i < lods[0].renders.Length; i++)
                {
                    List <Bone> list = new List <Bone>();
                    // sort based lod0
                    var render = lods[k].renders[i];
                    for (int j = 0; j < render.bones.Length; j++)
                    {
                        list.Add(new Bone()
                        {
                            name = render.bones[j].name, flag = -1, tf = render.bones[j]
                        });
                    }
                    int g_idx = 0;
                    for (int j = lods.Length - 1; j >= k; j--)
                    {
                        render = lods[j].renders[i];
                        foreach (var b in render.bones)
                        {
                            var bone = list.Find(x => x.name == b.name);
                            if (bone == null)
                            {
                                Debug.LogError("lod" + j + " " + b.name);
                                continue;
                            }
                            if (bone.flag < 0)
                            {
                                bone.flag = g_idx++;
                            }
                        }
                    }

                    Mesh        mesh  = CopyMesh(render);
                    Matrix4x4[] obind = render.sharedMesh.bindposes;
                    Matrix4x4[] nbind = new Matrix4x4[obind.Length];
                    for (int j = 0; j < obind.Length; j++)
                    {
                        nbind[list[j].flag] = obind[j];
                    }
                    var weights  = render.sharedMesh.boneWeights;
                    var nweights = new BoneWeight[weights.Length];
                    for (int j = 0; j < weights.Length; j++)
                    {
                        nweights[j]            = weights[j];
                        nweights[j].boneIndex0 = list[weights[j].boneIndex0].flag;
                        nweights[j].boneIndex1 = list[weights[j].boneIndex1].flag;
                        nweights[j].boneIndex2 = list[weights[j].boneIndex2].flag;
                        nweights[j].boneIndex3 = list[weights[j].boneIndex3].flag;
                    }
                    mesh.boneWeights = nweights;
                    mesh.bindposes   = nbind;
                    list.Sort((x, y) => x.flag.CompareTo(y.flag));
                    render.bones      = list.Select(x => x.tf).ToArray();
                    render.sharedMesh = mesh;
                    lods[0].boneInfo  = null;
                }
            }

            GeneratePrefab(lodNode.prefab, lods[0]);
        }