oskarsigvardsson / unity-delaunay Goto Github PK
View Code? Open in Web Editor NEWA Delaunay/Voronoi library for Unity, and a simple destruction effect
License: MIT License
A Delaunay/Voronoi library for Unity, and a simple destruction effect
License: MIT License
Hello
I want to use your unity-delaunay to triangulate 2D mesh for bitmap images.
Could you help me what should I do?
Thanks
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.
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
.
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 ?
When opening project in 2020.1, loading breaks at screen with layout and scripting issues.
I assume it is the same in 2020.2b.
When using this script, the shards just disappear? instead of falling like in the gif. any help would be appreciated!
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.
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));
}
`
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)?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.