Example #1
0
        public void SaveImport()
        {
            objectManager.currentImport.GO.transform.rotation   = Quaternion.identity;
            objectManager.currentImport.GO.transform.position   = Vector3.zero;
            objectManager.currentImport.GO.transform.localScale = Vector3.one;
            objectManager.currentImport.GO.name = nameInput.text;

            string savePath = Application.streamingAssetsPath + "/models/" + categoryList.options[categoryList.value].text + "/" + nameInput.text; //Create a folder with the name of the mesh under the selected category directory.

            //If a directory with the same name already exists add a unique number on the end.
            if (Directory.Exists(savePath))
            {
                UnityEngine.Random.InitState(UnityEngine.Random.Range(0, 1000));
                savePath = savePath + (UnityEngine.Random.Range(0, 10000));
            }

            Directory.CreateDirectory(savePath);
            //importer.ExportOBJ(objectManager.currentImport.scene, savePath);
            KB_ImportExport.ExportOBJ(objectManager.currentImport.GO, savePath);

            SaveThumbnail(savePath);

            categoryManager.modelsCount++;

            ImportNext();
        }
        /// <summary>
        /// Unwraps the mesh to a texture filling all uv islands white and dilating the edges to remove seam lines.
        /// </summary>
        /// <returns></returns>
        public Texture2D Unwrap()
        {
            //Unwrap mesh:
            Texture2D unwrapTex = new Texture2D(workingResolution, workingResolution, TextureFormat.ARGB32, false);

            unwrapTex.filterMode = FilterMode.Point;

            RenderTexture src = RenderTexture.GetTemporary(workingResolution, workingResolution, 0, RenderTextureFormat.ARGB32);

            src.filterMode = FilterMode.Point;

            Graphics.SetRenderTarget(src);
            GL.Clear(true, true, Color.black);
            GL.PushMatrix();
            GL.LoadOrtho();

            unwrap.SetPass(0);
            Graphics.DrawMeshNow(selectedParts[0].filter.sharedMesh, Matrix4x4.identity);
            Graphics.SetRenderTarget(null);

            //dest is the destination texture we blit a material to to fill in the little black gap between uv seams of the unwrapped texture (rt).
            //To "blit" shadergraph materials we have to use a custom render texture.

            /* CustomRenderTexture dest = new CustomRenderTexture(workingResolution, workingResolution, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear);
             * dest.updateMode = CustomRenderTextureUpdateMode.OnDemand;
             * dest.material = dilate;
             * dest.initializationSource = CustomRenderTextureInitializationSource.Material;
             * dest.initializationMaterial = dilate;
             * dest.doubleBuffered = true;
             * dilate.SetTexture("_MainTex", rt);
             * dest.Initialize();*/

            //If you are not using a shader graph shader to dilate the edges of uv islands these 3 lines will work:
            RenderTexture dest = RenderTexture.GetTemporary(workingResolution, workingResolution, 0, RenderTextureFormat.ARGB32);

            dilate.SetTexture("_MainTex", src);
            Graphics.Blit(src, dest, dilate);

            //TextureExporter.SaveRenderTexture(dest, "test2", TextureExporter.SaveTextureFormat.jpg, Application.streamingAssetsPath);//Test output.

            unwrapTex = KB_ImportExport.ToTexture2D(dest);

            RenderTexture.ReleaseTemporary(src);
            RenderTexture.ReleaseTemporary(dest);
            GL.PopMatrix();


            return(unwrapTex);
        }