Git Product home page Git Product logo

unity-delaunay's People

Contributors

andyjphu avatar oskarsigvardsson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

unity-delaunay's Issues

Sites close together are not calculated in the Voronoi diagram.

Hey there, I recently started using your library but I've encountered an issue where the Voronoi diagram is not computed correctly when the points are distributed close to each other. This is a pretty big deal for me as I need to created small fragments instead of larger ones like those shown in the gif on the readme.

I want to use this library for simple destruction of walls where I create a simple small hole in a wall where the player shoots or a large hole that the player can go through if they use an explosive weapon.

Attached I have examples of what I am talking about. The red lines are for the Voronoi diagram, the tiny green dots spheres are the site coordinates, and the blue lines are for the Delaunay triangulation.

This is an example of a Voronoi diagram on its own:

Voronoi Diagram

This is another example of a Voronoi diagram but also with it's Delaunay triangulation:

Voronoi Diagram with Delaunay Triangulation

These were displayed using Gizmos in Unity.

As you can see, I don't think the problem is with how I'm rendering the diagrams which was my first assumption, but instead, it has to be a problem with something inside VoronoiCalculator.cs as the Delaunay triangulation looks as expected.

The code I'm using is down below:

namespace GK
{
    public class VoronoiDestruction : MonoBehaviour
    {
        [Range(0, 1000)]
        public int points = 100;
        [Range(0.0f, 10.0f)]
        public float mean = 0;
        [Range(0.0f, 10.0f)]
        public float stddev = 0.2;

        Vector2[] sites;
        VoronoiDiagram diagram = new VoronoiDiagram();
        DelaunayTriangulation trig = new DelaunayTriangulation();

        void Start()
        {
            sites = new Vector2[points];
        }

        void OnDrawGizmos()
        {
            if (Application.isPlaying)
            {
                Gizmos.color = Color.green;
                if (sites.Length > 0)
                {
                    // Draw Voronoi sites.
                    for (int i = 0; i < sites.Length; i++)
                    {
                        Vector2 worldVert = transform.TransformPoint(sites[i]);
                        Gizmos.DrawSphere(new Vector3(worldVert.x, worldVert.y, 4), 0.01f);
                    }

                    // Draw Voronoi edges.
                    Gizmos.color = Color.red;
                    for (int i = 0; i < diagram.Edges.Count; i++)
                    {
                        VoronoiDiagram.Edge edge = diagram.Edges[i];
                        Vector2 worldVert = transform.TransformPoint(diagram.Vertices[edge.Vert0]);
                        if (edge.Type == VoronoiDiagram.EdgeType.RayCW || edge.Type == VoronoiDiagram.EdgeType.RayCCW)
                        {
                            //Vector2 worldDir = transform.TransformPoint(edge.Direction);
                            Vector2 worldDir = edge.Direction;
                            Gizmos.DrawRay(new Vector3(worldVert.x, worldVert.y, 4), new Vector3(worldDir.x, worldDir.y, 0));
                        }
                        else if (edge.Type == VoronoiDiagram.EdgeType.Segment)
                        {
                            Vector2 worldVert2 = transform.TransformPoint(diagram.Vertices[edge.Vert1]);
                            Gizmos.DrawLine(new Vector3(worldVert.x, worldVert.y, 4), new Vector3(worldVert2.x, worldVert2.y, 4));
                        }
                    }

                    // Draw Delaunay triangulation.
                    Gizmos.color = Color.blue;
                    for (int i = 0; i < trig.Triangles.Count; i += 3)
                    {
                        Vector2 worldVert1 = transform.TransformPoint(trig.Vertices[trig.Triangles[i]]);
                        Vector2 worldVert2 = transform.TransformPoint(trig.Vertices[trig.Triangles[i + 1]]);
                        Vector2 worldVert3 = transform.TransformPoint(trig.Vertices[trig.Triangles[i + 2]]);
                        Gizmos.DrawLine(new Vector3(worldVert1.x, worldVert1.y, 4), new Vector3(worldVert2.x, worldVert2.y, 4));
                        Gizmos.DrawLine(new Vector3(worldVert1.x, worldVert1.y, 4), new Vector3(worldVert3.x, worldVert3.y, 4));
                        Gizmos.DrawLine(new Vector3(worldVert2.x, worldVert2.y, 4), new Vector3(worldVert3.x, worldVert3.y, 4));
                    }
                }
            }
        }

        public void redrawDiagram(Vector3 pos)
        {
            Vector3 temp = transform.InverseTransformPoint(pos);
            Vector2 position = new Vector2(temp.x, temp.y);

            var calc = new VoronoiCalculator();

            sites = new Vector2[points];

            for (int i = 0; i < sites.Length; i++)
            {
                //var dist = Mathf.Abs(NormalizedRandom(10.0f, 1.0f / 2.0f));
                var dist = Mathf.Abs(NormalizedRandom(mean, stddev));
                var angle = 2.0f * Mathf.PI * Random.value;

                sites[i] = position + new Vector2(
                        dist * Mathf.Cos(angle),
                        dist * Mathf.Sin(angle));
            }

            diagram = calc.CalculateDiagram(sites);
            trig = diagram.Triangulation;
        }

        private float NormalizedRandom(float mean, float stddev)
        {
            var u1 = UnityEngine.Random.value;
            var u2 = UnityEngine.Random.value;

            var randStdNormal = Mathf.Sqrt(-2.0f * Mathf.Log(u1)) *
                Mathf.Sin(2.0f * Mathf.PI * u2);

            return mean + stddev * randStdNormal;
        }
    }
}

Do you know why this might be and if there is a way to solve this?

EDIT: Forgot to mention, redrawDiagram(Vector3 pos) is called when the player shoots the wall. A 'shooting' script is attached to the camera, when left click is pressed, a raycast is shot out and if it hits the wall, it calls redrawDiagram and feeds in the position where the raycast hit as pos.

Other Examples

Hi, great destruction effect on cubic meshes but couldnt figure it out how to use it on cylinder for example. Is is possible? What classes in plugin folder are for? Should we call some functions inside of them to BreakableSurface.cs ?

Import in 2020

When opening project in 2020.1, loading breaks at screen with layout and scripting issues.
I assume it is the same in 2020.2b.

Shards just disappear?

When using this script, the shards just disappear? instead of falling like in the gif. any help would be appreciated!

Index out of Range in FindTriangleNode()

Hi,
really cool work! I am sometimes getting an Index out of Range exception in the FindTriangleNode function. I debugged a bit: t.C2 is chosen in these cases, but C2 has the value -1. With curr == C2 == -1, triangles[-1] is out of range. This happens with polygons with a size of about 200 vertices. Do you maybe have an idea how to fix this issue?

int FindTriangleNode(int pi) {
	var curr = 0;

	while (!triangles[curr].IsLeaf)
	{
		var t = triangles[curr];

		if (t.C0 >= 0 && PointInTriangle(pi, t.C0)) {
			curr = t.C0;
		} else if (t.C1 >= 0 && PointInTriangle(pi, t.C1)) {
			curr = t.C1;
		} else {
			curr = t.C2;
		}
	}
	return curr;
}

Do you think it would be possible for me to extend this library to build constrained conforming or conforming triangulations? Or are these based on completely different algorithms. Maybe you looked into these topics.

I am working on generating meshes out of outlines in a video stream. With OpenCV I'm getting the outlines in the image as polygons and then I want to build meshes out of these polygons via Delaunay Triangulation. Your library is well structured and good to read, thanks a lot.

Uniform split problem

Hi. First, great work, I really liked the result and the organization of the code. I am making a game in which an arrow must pierce glass panels, and wanted to use an algorithm for runtime generation. However, in the original version, it was not very visually suitable, and I slightly changed the logic of generating points for splitting. And here I started having problems - some of the polygons stopped drawing, because they have an invalid area. I tried to do a split just spawn points around the center at the same distance and with the same angle. As a result, only 4 of 10 meshes are drawn. I began to explore the algorithm myself, but it will take a lot of time. I would be grateful for the help.
Below piece of code that I changed.
`
public void Break(Vector2 position)
{
...
float angleStep = 2.0f * Mathf.PI / 10f;

for(int i = 0; i < 10; ++i)
 {
     float dist = 0.4f;
     var angle = angleStep * i;

     sites[i] = position + new Vector2(
            dist * Mathf.Cos(angle),
            dist * Mathf.Sin(angle));
  }

`

Neighbours

Hi

Amazing work :).

I plan to us this to build map based on voronoi diagram. Would it be possible to implement here also properties to obtain site neighbour and edge sibling (from neighobur site)?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.