Exemplo n.º 1
0
    // Takes a group of contours and draws a silhouette for each contour in the group
    public SilhouetteGroup DrawSilhouettesForGroup(List <Contour> contours)
    {
        SilhouetteGroup sGroup = new SilhouetteGroup();

        foreach (Contour c in contours)
        {
            Silhouette silhouette = DrawSilhouette(c);

            RemovePixelNoise(silhouette);

            for (int i = 0; i < SilhouetteMergingIterations; i++)
            {
                MergeSilhouettePieces(silhouette);
            }

            sGroup.Silhouettes.Add(silhouette);
        }
        return(sGroup);
    }
Exemplo n.º 2
0
    private void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        #region Disable textures
        // Finne mesher
        allMeshes = new List <Transform>();
        foreach (GameObject go in UnityEngine.SceneManagement.SceneManager.GetActiveScene().GetRootGameObjects())
        {
            if (go.GetComponent <Transform>().childCount > 0)
            {
                if (go.GetComponent <Transform>().GetChild(0).GetComponent <Renderer>() != null)
                {
                    for (int i = 0; i < go.GetComponent <Transform>().childCount; i++)
                    {
                        allMeshes.Add(go.GetComponent <Transform>().GetChild(i));
                    }
                }
            }
        }

        // Fjerne teksturene
        for (int i = 0; i < allMeshes.Count; i++)
        {
            Material[] materials = allMeshes[i].GetComponent <Renderer>().materials;
            for (int j = 0; j < materials.Length; j++)
            {
                allMeshes[i].GetComponent <Renderer>().materials[j].mainTexture = null;
            }
        }
        #endregion

        #region Assigning group colors to meshes
        System.Random random = new System.Random();
        materialMap = new Dictionary <string, List <Color> >();

        // Give every triangle in a mesh a single, random color
        for (int i = 0; i < allMeshes.Count; i++)
        {
            List <Color> colorList = new List <Color>();
            Color        newColor  = VectorUtils.RandomRGB(random);

            Material[] materials = allMeshes[i].GetComponent <Renderer>().materials;
            String     meshName  = allMeshes[i].name;
            for (int j = 0; j < materials.Length; j++)
            {
                Color oldColor = materials[j].color; // Original color
                colorList.Add(oldColor);

                allMeshes[i].GetComponent <Renderer>().materials[j].color = newColor; // Temporary ID color
            }
            materialMap.Add(meshName, colorList);
        }

        #endregion

        #region Per-Pixel Linked list

        GeneratePerPixelLinkedList();

        #endregion

        #region ID rendering

        camA.Render();

        if (!runOnceId)
        {
            idContoursAllLayers = new List <List <Contour> >();

            int i = 0;
            while (i < NumberOfLayers) // *4
            {
                Shader.SetGlobalInt("_loopMax", i);

                if (SortLayersByDepth)
                {
                    Shader.SetGlobalFloat("_sortLayers", 1);
                }
                else
                {
                    Shader.SetGlobalFloat("_sortLayers", 0);
                }

                Graphics.ClearRandomWriteTargets();
                Graphics.SetRandomWriteTarget(1, nodeBuffer);
                //Graphics.SetRandomWriteTarget(2, head);

                RenderTexture renderTex = RenderTexture.GetTemporary(Width, Height, 0, RenderTextureFormat.Default);
                Material      mat       = new Material(Shader.Find("Hidden/VectorShader"));

                Graphics.Blit(source, renderTex, mat);
                Graphics.Blit(renderTex, destination);

                // Copy from RenderTexture to a texture
                RenderTexture.active = renderTex;
                Texture2D tex2d = new Texture2D(Width, Height, TextureFormat.RGB24, false);
                tex2d.ReadPixels(new Rect(0, 0, renderTex.width, renderTex.height), 0, 0);
                tex2d.Apply();

                Rect rect = new Rect(0, 0, Width, Height);

                List <Contour> idContoursCurrentLayer = ExtractLayerContours(tex2d); // EXTRACTING CONTOURS
                idContoursAllLayers.Add(idContoursCurrentLayer);

                RenderTexture.ReleaseTemporary(renderTex);

                //VectorUtils.SaveTextureToFile(tex2d, "idldiTex" + i + ".png");

                i += 1; // += 4
            }
            //Graphics.ClearRandomWriteTargets(); // meg

            // Storing textures for viewing

            /*for (int j = 0; j < idContoursAllLayers.Count; j++)
             * {
             *  if (idContoursAllLayers[j].Count != 0)
             *  {
             *      Texture2D contourTexture = VectorUtils.ContourTexture(idContoursAllLayers[j], Width, Height);
             *      VectorUtils.SaveTextureToFile(contourTexture, "idTex" + j + ".png");
             *  }
             * }*/

            runOnceId = true;
        }
        #endregion

        #region Reset mesh colors

        // Reset all mesh colors
        for (int i = 0; i < allMeshes.Count; i++)
        {
            Material[]   materials = allMeshes[i].GetComponent <Renderer>().materials;
            String       meshName  = allMeshes[i].name;
            List <Color> colorList = materialMap[meshName];
            for (int j = 0; j < materials.Length; j++)
            {
                allMeshes[i].GetComponent <Renderer>().materials[j].color = colorList[j]; // NB: sharedMaterials ruins everything!!
            }
        }

        #endregion

        #region Per-Pixel Linked list 2

        ReleaseBuffers();

        GeneratePerPixelLinkedList();

        #endregion

        #region Rendering final scene

        camA.Render();

        if (!runOnceFinal)
        {
            colorContoursAllLayers = new List <List <Contour> >();

            int i = 0;
            while (i < NumberOfLayers) //*4
            {
                Shader.SetGlobalInt("_loopMax", i * 4);

                if (SortLayersByDepth)
                {
                    Shader.SetGlobalFloat("_sortLayers", 1);
                }
                else
                {
                    Shader.SetGlobalFloat("_sortLayers", 0);
                }

                Graphics.ClearRandomWriteTargets();
                Graphics.SetRandomWriteTarget(1, nodeBuffer);
                //Graphics.SetRandomWriteTarget(2, head);

                RenderTexture renderTex = RenderTexture.GetTemporary(Width, Height, 0, RenderTextureFormat.Default);
                Material      mat       = new Material(Shader.Find("Hidden/VectorShader"));

                Graphics.Blit(source, renderTex, mat);
                Graphics.Blit(renderTex, destination);

                // Copy from RenderTexture to a texture
                RenderTexture.active = renderTex;
                Texture2D tex2d = new Texture2D(Width, Height, TextureFormat.RGB24, false);
                tex2d.ReadPixels(new Rect(0, 0, renderTex.width, renderTex.height), 0, 0);
                tex2d.Apply();

                Rect rect = new Rect(0, 0, Width, Height);

                List <Contour> colorContoursCurrentLayer = ExtractLayerContours(tex2d);

                colorContoursAllLayers.Add(colorContoursCurrentLayer);

                RenderTexture.ReleaseTemporary(renderTex);

                //VectorUtils.SaveTextureToFile(tex2d, "ldiTex" + i + ".png");

                i += 1;
            }
            //Graphics.ClearRandomWriteTargets(); // meg

            // Storing textures for viewing

            /*for (int j = 0; j < colorContoursAllLayers.Count; j++)
             * {
             *  if (colorContoursAllLayers[j].Count != 0)
             *  {
             *      Texture2D contourTexture = VectorUtils.ContourTexture(colorContoursAllLayers[j], Width, Height);
             *      VectorUtils.SaveTextureToFile(contourTexture, "colorTex" + j + ".png");
             *  }
             * }*/

            runOnceFinal = true;

            #endregion

            #region Draw SVG

            List <List <ContourGroup> > contourGroupsAllLayers = IdentifyGroups(idContoursAllLayers, colorContoursAllLayers); // IDENTIFY GROUPS

            List <Layer> layers = new List <Layer>();
            foreach (List <ContourGroup> cGroups in contourGroupsAllLayers) // DRAW SILHOUETTES
            {
                Layer layer = new Layer();
                foreach (ContourGroup cg in cGroups)
                {
                    SilhouetteGroup sg = new SilhouetteGroup();
                    layer.SilhouetteGroups.Add(DrawSilhouettesForGroup(cg.Contours));
                }
                layers.Add(layer);
            }
            layers.Reverse();

            SVGProject.SVGWriter writer = new SVGProject.SVGWriter(Width, Height);
            writer.WriteSilhouettesToSVG(layers, Height); // WRITING SVG FILE

            try
            {
                // writer.SaveDocument(@"C:\Users\Vidar Hartveit Ure\Desktop\forsok.svg");
                // writer.SaveDocument(@"E:\Vidar\Desktop\forsok.svg");
                writer.SaveDocument(SVGFilePath);
            }
            catch (DirectoryNotFoundException e)
            {
                Debug.Log("Error on file saving: Directory not found!");
                QuitApplication();
            }

            int layerCount      = layers.Count;
            int groupCount      = 0;
            int pieceCount      = 0;
            int silhouetteCount = 0;
            int pointCount      = 0;

            foreach (Layer l in layers)
            {
                foreach (SilhouetteGroup sg in l.SilhouetteGroups)
                {
                    groupCount++;
                    foreach (Silhouette s in sg.Silhouettes)
                    {
                        silhouetteCount++;
                        foreach (SilhouettePiece spi in s.Pieces)
                        {
                            pieceCount++;
                            foreach (ContourPixel p in spi.Points)
                            {
                                pointCount++;
                            }
                        }
                    }
                }
            }

            Debug.Log("Number of layers: " + layerCount);
            Debug.Log("Number of silhouette groups: " + groupCount);
            Debug.Log("Number of silhouettes: " + silhouetteCount);
            Debug.Log("Number of silhouette pieces: " + pieceCount);
            Debug.Log("Number of points: " + pointCount);

            //Texture2D silhouetteTexture = LayerTexture(layers[0]);
            //SaveTextureToFile(silhouetteTexture, "siltex.png");

            QuitApplication();

            #endregion
        }
    }