How To Generate a Voronoi Diagram in Unity - Tutorial

  Рет қаралды 4,124

Alex K. Dev

Alex K. Dev

Жыл бұрын

How to generate a voronoi diagram using the Unity Engine.
Music:
--------------------------------------------------------------
♪ Home (Prod. by Lukrembo)
Link : • lukrembo - home (royal...
--------------------------------------------------------------

Пікірлер: 22
@vvixell4580
@vvixell4580 Жыл бұрын
I was trying to make a voronoi diagram from scratch by my self but i got stuck and after a while of scrolling I found this video and it helped me a-lot. The algorithm is doing you dirty, This video is really high quality. Thanks bro ❤
@AlexKDev
@AlexKDev Жыл бұрын
I'm glad the video was helpful. Thank you 😄
@neilmarkcorre5524
@neilmarkcorre5524 8 ай бұрын
You deserve more subscribers. Well done!
@AlexKDev
@AlexKDev 8 ай бұрын
Thanks! 😃
@mohammedthaier1718
@mohammedthaier1718 Жыл бұрын
Interesting technique
@kitchaos1118
@kitchaos1118 7 ай бұрын
This is really helpful! My only suggestion would be to slow down a bit at some points, but besides that, this is great!
@phutureproof
@phutureproof 4 ай бұрын
you can slow the video yourself, and rewind it!
@iggyboyo
@iggyboyo Жыл бұрын
Good video, just had a bit of trouble understanding the part where you actually painted the voronoi shapes
@NickKoll
@NickKoll 4 ай бұрын
Great video! Where can we find the code?
@gutzimmumdo4910
@gutzimmumdo4910 2 ай бұрын
this only works if u have points in each cell tho, what if u have 3 points only in random locations.
@off_pudding443
@off_pudding443 Ай бұрын
You might want to look at Fortune's algorithm.
@starsnatcher4659
@starsnatcher4659 2 ай бұрын
can we get the code :>
@MrThijsdc
@MrThijsdc 11 ай бұрын
Hey there, thank you for the video. I was trying to follow along by copying your code (at least the parts visible on the screen) but I cannot get it to work. Could you by any chance provide your full code so I could check whether I made an error somewhere (perhaps you could post it in the description)? Anyways, thanks again for the overview.
@MrThijsdc
@MrThijsdc 11 ай бұрын
I got the script to work by declaring an array of possibleColors and populating it within the script but I can't get it to work when exposing the possiblecolors field to the inspector and populating it from within the inspector.
@undefiant8801
@undefiant8801 10 ай бұрын
it doesn't work for me either. Found the solution yet?@@MrThijsdc
@omarelsayed7577
@omarelsayed7577 6 ай бұрын
@@MrThijsdc I don't understand. Could you post the code?
@MrThijsdc
@MrThijsdc 6 ай бұрын
Sure, no problem. Here is my code: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class voronoiDiagram : MonoBehaviour { private Color[] possibleColors; [SerializeField] private int gridSize = 10; private int imgSize; private int pixelsPerCell; private RawImage image; private Vector2Int[,] pointPositions; private Color[,] colors; private void Awake() { image = GetComponent(); imgSize = Mathf.RoundToInt(image.GetComponent().sizeDelta.x); } private void Start() { GenerateDiagram(); } private void Update() { if(Input.GetKeyDown(KeyCode.Space)) { GenerateDiagram(); } } private void GenerateDiagram() { Texture2D texture = new Texture2D(imgSize, imgSize); texture.filterMode = FilterMode.Point; pixelsPerCell = imgSize / gridSize; possibleColors = new Color[] { new Color(Random.Range(0, 1f),Random.Range(0, 1f), Random.Range(0, 1f)), new Color(Random.Range(0, 1f), Random.Range(0, 1f), Random.Range(0, 1f)), new Color(Random.Range(0, 1f), Random.Range(0, 1f), Random.Range(0, 1f)), new Color(Random.Range(0, 1f), Random.Range(0, 1f), Random.Range(0, 1f)) }; GeneratePoints(); for (int i = 0; i < imgSize; i++) { for (int j = 0; j < imgSize; j++) { int gridX = i / pixelsPerCell; int gridY = j / pixelsPerCell; float nearestDistance = Mathf.Infinity; Vector2Int nearestPoint = new(); for (int a = -1; a < 2; a++) { for (int b = -1; b < 2; b++) { int X = gridX + a; int Y = gridY + b; if (X < 0 || Y < 0 || X >= gridSize || Y >= gridSize) continue; float distance = Vector2Int.Distance(new Vector2Int(i, j), pointPositions[X, Y]); if (distance < nearestDistance) { nearestDistance = distance; nearestPoint = new Vector2Int(X, Y); } } } texture.SetPixel(i, j, colors[nearestPoint.x, nearestPoint.y]); } } texture.Apply(); image.texture = texture; } private void GeneratePoints() { pointPositions = new Vector2Int[gridSize, gridSize]; colors = new Color[gridSize, gridSize]; for (int i = 0; i < gridSize; i++) { for (int j = 0; j < gridSize; j++) { pointPositions[i, j] = new Vector2Int(i * pixelsPerCell + Random.Range(0, pixelsPerCell), j * pixelsPerCell + Random.Range(0, pixelsPerCell)); colors[i, j] = possibleColors[Random.Range(0, possibleColors.Length)]; } } } }
@omarelsayed7577
@omarelsayed7577 6 ай бұрын
@@MrThijsdc Thank you so much! It worked. One question though, is there a way I can choose the colors in the inspector rather than make it randomly generated?
@Auberginax
@Auberginax Ай бұрын
Code complexity of O(n^4) :0
@seanerer
@seanerer 6 ай бұрын
Does KZfaq give you money for getting through video done as fast as possible?
Voronoi Diagrams and Procedural Map Generation
15:36
MAN OF EERIE LETTERS
Рет қаралды 38 М.
What is Voronoi Noise?
6:38
Suboptimal Engineer
Рет қаралды 7 М.
Heartwarming: Stranger Saves Puppy from Hot Car #shorts
00:22
Fabiosa Best Lifehacks
Рет қаралды 9 МЛН
Osman Kalyoncu Sonu Üzücü Saddest Videos Dream Engine 170 #shorts
00:27
Василиса наняла личного массажиста 😂 #shorts
00:22
Денис Кукояка
Рет қаралды 9 МЛН
Please be kind🙏
00:34
ISSEI / いっせい
Рет қаралды 189 МЛН
Делаю процедурную генерацию в Unity
13:15
Easy Art for Indie Dev Programmers
4:09
Nick Brooking
Рет қаралды 2,1 М.
A new way to generate worlds (stitched WFC)
10:51
Watt Designs
Рет қаралды 512 М.
A simple algorithm for 2D Voronoi diagrams
3:27
Edgar Programmator
Рет қаралды 3,4 М.
The Strange Graphics Of LETHAL COMPANY
15:59
Acerola
Рет қаралды 737 М.
I Cracked The Code Behind Nature! ( Voronoi Diagram Explanation )
11:10
Why this pattern shows up everywhere in nature || Voronoi Cell Pattern
14:36
Unity 2D Biomes: Voronoi Noise + Perlin Noise
12:23
Berkay Taşçı
Рет қаралды 3 М.
WoT Blitz. Late Night Birthday Lotto + Gifts and Presents
1:7:55
World of Tanks Blitz
Рет қаралды 462 М.
СБЕЖАЛ ОТ МАМЫ и ЭТО ЗАКОНЧИЛОСЬ ПЛОХО!! (SchoolBoy Runaway)
14:16
ShadowPriestok - Евгений Чернявский
Рет қаралды 526 М.
skibidi toilet zombie universe 33 ( New Virus)
2:59
MonsterUP
Рет қаралды 2,9 МЛН