Beispiel #1
0
        public static IEnumerator CreateMaterial(GameObject selection)
        {
            var sampler = new CombinedMeshSampler(selection);
            var vtex    = new VertexTex(sampler);

            var mesh = sampler.CombinedMesh;

            //mesh.uv2 = vtex.uv2;
            mesh.bounds = vtex.Bounds();

            var folderPath = DIR_ASSETS + "/" + DIR_ROOT;

            if (!Directory.Exists(folderPath))
            {
                AssetDatabase.CreateFolder(DIR_ASSETS, DIR_ROOT);
            }
            var guid = AssetDatabase.CreateFolder(folderPath, selection.name);

            folderPath = AssetDatabase.GUIDToAssetPath(guid);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            yield return(0);

            var posPngPath  = folderPath + "/" + selection.name + ".png";
            var normPngPath = folderPath + "/" + selection.name + "_normal.png";
            var posTex      = Save(vtex.positionTex, posPngPath);
            var normTex     = Save(vtex.normalTex, normPngPath);

            var      renderer = selection.GetComponentInChildren <Renderer> ();
            Material mat      = new Material(Shader.Find(ShaderConst.SHADER_NAME));

            if (renderer != null && renderer.sharedMaterial != null)
            {
                mat.mainTexture = renderer.sharedMaterial.mainTexture;
            }
            mat.SetTexture(ShaderConst.SHADER_ANIM_TEX, posTex);
            mat.SetVector(ShaderConst.SHADER_SCALE, vtex.scale);
            mat.SetVector(ShaderConst.SHADER_OFFSET, vtex.offset);
            mat.SetVector(ShaderConst.SHADER_ANIM_END, new Vector4(sampler.Length, vtex.verticesList.Count - 1, 0f, 0f));
            mat.SetFloat(ShaderConst.SHADER_FPS, FPS);
            mat.SetTexture(ShaderConst.SHADER_NORM_TEX, normTex);

            AssetDatabase.CreateAsset(mat, folderPath + "/" + selection.name + "Mat.mat");
            AssetDatabase.CreateAsset(mesh, folderPath + "/" + selection.name + "Mesh.asset");
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();

            GameObject go = new GameObject(selection.name);

            go.AddComponent <MeshRenderer>().sharedMaterial = mat;
            go.AddComponent <MeshFilter>().sharedMesh       = mesh;
            PrefabUtility.CreatePrefab(folderPath + "/" + selection.name + ".prefab", go);
        }
        public static IEnumerator CreateMaterial(GameObject selection, CreationModeFlags flags)
        {
            var sampler = ContainsAllFlags(flags, CreationModeFlags.COMBINED_MESH)
                ? (IMeshSampler) new CombinedMeshSampler(selection) : (IMeshSampler) new SingleMeshSampler(selection);
            var vtex = new VertexTex(sampler);

            var folderPath = AssureExistAndGetRootFolder();

            folderPath = CreateTargetFolder(selection, folderPath);
            yield return(0);

            Material mat = CreateMaterial(sampler, vtex);

            SaveAsset(mat, folderPath + "/" + selection.name + ".mat");
        }
        private static Material CreateMaterial(IMeshSampler sampler, VertexTex vtex,
                                               Texture2D posTex = null, Texture2D normTex = null, Renderer renderer = null)
        {
            Material mat = new Material(Shader.Find(ShaderConst.SHADER_NAME));

            if (renderer != null && renderer.sharedMaterial != null)
            {
                mat.mainTexture = renderer.sharedMaterial.mainTexture;
            }
            if (posTex != null)
            {
                mat.SetTexture(ShaderConst.SHADER_ANIM_TEX, posTex);
            }
            mat.SetVector(ShaderConst.SHADER_SCALE, vtex.scale);
            mat.SetVector(ShaderConst.SHADER_OFFSET, vtex.offset);
            mat.SetVector(ShaderConst.SHADER_ANIM_END, new Vector4(sampler.Length, vtex.verticesList.Count - 1, 0f, 0f));
            mat.SetFloat(ShaderConst.SHADER_FPS, FPS);
            if (normTex != null)
            {
                mat.SetTexture(ShaderConst.SHADER_NORM_TEX, normTex);
            }
            return(mat);
        }
        public static IEnumerator CreateVertexTexture(GameObject selection, CreationModeFlags flags)
        {
            var sampler = ContainsAllFlags(flags, CreationModeFlags.COMBINED_MESH)
                ? (IMeshSampler) new CombinedMeshSampler(selection) : (IMeshSampler) new SingleMeshSampler(selection);
            var vtex = new VertexTex(sampler);

            var folderPath = AssureExistAndGetRootFolder();

            folderPath = CreateTargetFolder(selection, folderPath);
            yield return(0);

            var posPngPath  = folderPath + "/" + selection.name + ".png";
            var normPngPath = folderPath + "/" + selection.name + "_normal.png";
            var posTex      = Save(vtex.positionTex, posPngPath);
            var normTex     = Save(vtex.normalTex, normPngPath);

            var      renderer = selection.GetComponentInChildren <Renderer>();
            Material mat      = CreateMaterial(sampler, vtex, posTex, normTex, renderer);

            SaveAsset(mat, folderPath + "/" + selection.name + ".mat");

            var smr  = selection.GetComponentInChildren <SkinnedMeshRenderer>();
            var mesh = (smr != null ? smr.sharedMesh : null);

            if (ContainsAllFlags(flags, CreationModeFlags.NEW_MESH))
            {
                mesh        = sampler.Output;
                mesh.bounds = vtex.Bounds();
                SaveAsset(mesh, folderPath + "/" + selection.name + ".asset");
            }

            GameObject go = new GameObject(selection.name);

            go.AddComponent <MeshRenderer>().sharedMaterial = mat;
            go.AddComponent <MeshFilter>().sharedMesh       = mesh;
            PrefabUtility.CreatePrefab(folderPath + "/" + selection.name + ".prefab", go);
        }