예제 #1
0
        public void BuildMesh(float xOffset, float yOffset, TextureCamera camera)
        {
            // Hole sort it if needed:
            Path.HoleSort();

            // How many samples?
            int   samples     = 0;
            int   triCount    = 0;
            int   moveToCount = 0;
            float accuracy    = TextureCameras.Accuracy;

            VectorPoint current = Path.FirstPathNode;

            while (current != null)
            {
                if (current.IsClose || current.Next == null)
                {
                    moveToCount--;
                }

                if (!current.HasLine)
                {
                    // Ignore moveto's - Hop to the next one:
                    moveToCount++;
                    current = current.Next;
                    continue;
                }

                if (current.IsCurve || !TextureCameras.SD)
                {
                    // It's a curve (or a HD line) - get the line length:
                    VectorLine line = current as VectorLine;

                    float length = line.Length;

                    // And just add on extra points:
                    int count = (int)(length / accuracy);

                    if (count <= 0)
                    {
                        count = 1;
                    }

                    samples += count;

                    // Anything that isn't a moveto also has 6 additional triangle indices (2 tris):
                    triCount += count * 6;
                }
                else
                {
                    // Add the end node:
                    samples++;

                    // Anything that isn't a moveto also has 6 additional triangle indices (2 tris):
                    triCount += 6;
                }

                // Hop to the next one:
                current = current.Next;
            }


            // Each sample point has an associated line.
            // Each line generates 6 vertices and 4 triangles.
            int vertCount = samples * 6;

            triCount += samples * 4 * 3;

            // Triangulation set next.
            int triangulatedVerts = 0;

            current = Path.FirstPathNode;

            while (current != null)
            {
                if (current.HasLine && current.IsCurve)
                {
                    VectorLine line = current as VectorLine;

                    int extraPoints = (int)(line.Length / TextureCameras.TriangulationAccuracy);

                    if (extraPoints <= 0)
                    {
                        extraPoints = 1;
                    }

                    triangulatedVerts += extraPoints;
                }
                else
                {
                    triangulatedVerts++;
                }

                if (current.IsClose || current.Next == null)
                {
                    // End of a main contour.

                    vertCount += triangulatedVerts;

                    if (triangulatedVerts > 2)
                    {
                        triCount += (triangulatedVerts - 2) * 3;
                    }

                    triangulatedVerts = 0;
                }

                // Hop to the next one:
                current = current.Next;
            }

            camera.Triangulator.Clockwise = true;

            Mesh = TextureCameras.GetBuffer();

            // For each MoveToPair (moveToCount/2), add 4 tri's [2 tris for each "gap"]:
            if (moveToCount != 0)
            {
                moveToCount = moveToCount >> 1;
                triCount   += 12 * moveToCount;
            }

            Mesh.RequireSize(vertCount, triCount);

            // Map offsets:
            xOffset += OffsetX * Mesh.XScaleFactor;
            yOffset += OffsetY * Mesh.YScaleFactor;

            // Let's get generating!
            Mesh.AddMesh(Path, xOffset - ((float)X * 0.1f), yOffset + ((float)Y * 0.1f), camera.Triangulator);
        }
예제 #2
0
        public void DrawNow()
        {
            RequiresRender = false;
            IsDrawing      = true;

            if (Gameobject == null)
            {
                // Scene change - recreate:
                Gameobject = CreateGameObject();
            }

            // Clear timer because a draw was requested.
            // This makes the texture camera system survive a little longer.
            Timer = 0f;

            SourceCamera.enabled = true;

            // Ensure it's the right size - Force it to be square:
            SourceCamera.pixelRect = new Rect(0, 0, pixelWidth, pixelHeight);

            // Get current active RT:
            RenderTexture prevActive = RenderTexture.active;

            // Apply and set:
            RenderTexture.active = Texture;

            // For each actual drawing..
            DrawingTexture draw = FirstDrawing;

            while (draw != null)
            {
                if (draw.Active)
                {
                    // Flush mesh:
                    draw.Mesh.Flush();

                    // Display the GO:
                    draw.Mesh.SetActive(true);
                }

                // Hop to next one:
                draw = draw.NextDrawing;
            }

            // Render it right now:
            SourceCamera.Render();

            if (CPUAccess)
            {
                // Read pixels:
                CPUBuffer.ReadPixels(new Rect(0, 0, pixelWidth, pixelHeight), 0, 0);
                CPUBuffer.Apply();
                // Get them:
                CPUPixels = CPUBuffer.GetPixels32();
            }

            // Disable:
            SourceCamera.enabled = false;

            // For each actual drawing..
            DrawingTexture drawing = FirstDrawing;

            while (drawing != null)
            {
                // Add mesh to pool:
                TextureCameras.PoolBuffer(drawing.Mesh);

                if (drawing.Active)
                {
                    // Copy results over into target:
                    ReadInto(drawing.Location, drawing.X, drawing.Y);
                }

                // Hop to next one:
                drawing = drawing.NextDrawing;
            }

            // All done!

            // Clear:
            RenderTexture.active = prevActive;

            // Clear this cameras meta data:
            Clear();

            // No longer drawing:
            IsDrawing = false;

            // Check if there's any waiting textures:
            if (TextureCameras.Pending == null)
            {
                return;
            }

            // Try drawing them now:
            DrawingTexture pending = TextureCameras.Pending;

            while (pending != null)
            {
                DrawingTexture next = pending.NextDrawing;

                if (TryFit(pending))
                {
                    // Next one:
                    pending = next;

                    // Remove current from pending:
                    TextureCameras.Pending = pending;
                }
                else
                {
                    break;
                }
            }

            // Any want drawing?
            if (RequiresRender)
            {
                // Yep - Draw again.
                DrawNow();
            }
        }