GridModule - A module for creating triangle grids

Introduction

Hello, I’ve seen a lot of cool creations that involved using triangle grids, so I’ve decided to create a module so that you can create them with ease!
(Credit to EgoMoose for the triangle function)

Here’s the link: GridModule - Roblox

cool demo


(note that you can make a lot more stuff than this with the module, such as triangle terrain generation or audio visualizers)

How do I use it?

Disclaimer: You need to have a solid understanding of multidimensional arrays to use this.

First off, let’s require the module.

local gridModule = require(PATH_TO_GRID_MODULE) -- Replace PATH_TO_GRID_MODULE with where it is in your game!

Great! Now we are able to use all of the module’s functions.

Let’s look at the most important function first, InitializeGrid. This function is pretty self explanatory(it initializes the grid). It takes in a width, a height, and a cell size(distance between each point on the grid). Let’s just add it in:

local gridModule = require(PATH_TO_GRID_MODULE)
gridModule.InitializeGrid(10, 10, 4)

Now let’s go into play mode, and observe what happens:

Cool! We now have a grid. To distinguish between all the wedges, I have added a property to the module called DebugColors. It just randomizes the wedge colors.

local gridModule = require(PATH_TO_GRID_MODULE)
gridModule.DebugColors = true -- Always remember to set DebugColors before InitializeGrid!
gridModule.InitializeGrid(10, 10, 4)

image
It’s still a bit boring, the grid is pretty static. We want to make these move!

So let’s create a multidimensional array and set each coordinate to a Vector3:

local gridModule = require(script.Parent)
gridModule.DebugColors = true
gridModule.InitializeGrid(10, 10, 4)

local points = {}
local RNG = Random.new()

for y = 1, 10, 1 do -- This creates a multidimensional array.
	points[y] = {}
	for x = 1, 10, 1 do
		points[y][x] = Vector3.new(x*4,RNG:NextNumber(0, 5),y*4) -- Set the Y to a random number, multiply the X and Z by the distance between each point.
	end
end

gridModule.ModifyPoints(points)

image
Woah! All the points are now random and it looks pretty cool. Now you can modify the script to make your own cool triangle grids!

By the way, calling ModifyPoints every frame won’t lag since this module reuses the wedges(Creating new wedges every frame would cause a ton of lag).

Fin

Thanks for reading! :slightly_smiling_face:

I hope you like this module, and I can’t wait to see what you’ll make with it. Feedback is also appreciated.

43 Likes

What are the benefits and uses to using this module?

2 Likes

This module is useful for stuff such as:

  • Audio visualizers
  • Water simulations
  • Terrain generation with triangles
  • A load of other cool visual stuff

Hope this answers your question!

5 Likes

This is pretty cool! I have a fair bit of experience with these from working on my fluid simulation, so I’ll share my findings.

Even reusing the WedgeParts can get very laggy very quickly as you add more, because updating a part triggers a lot of events and updates internally for the Roblox engine.

There are a few alternative methods that make the issue a little less severe.


A) Using SpecialMesh with a triangle mesh file and updating mesh Scale and Offset instead of a part’s Size and Position.

Pros:

  • Cheaper
  • Faster
  • Looks identical to WedgeParts

Cons:

  • No collisions (which is why it’s faster, but players can no longer walk on it)

B) Using Beams to draw triangles

Pros:

  • Super cheap
  • Relatively fast
  • Beams don’t have proper shading so it ends up looking really smooth. Can be a pro or a con depending on what you want!

Cons:

  • Roblox limits the number of beams drawn at any one time, so you can’t use this for a massive grid
  • Roblox throttles how many Beams can update per frame so some might be one frame behind and leave gaps
  • Beams don’t have proper shading so it ends up looking really smooth. Can be a pro or a con depending on what you want!

C) Using Beams to draw quads instead of triangles

Pros:

  • Cheapest option
  • Fast as hecc
  • Uses half as many Beams as the tri method, so you get throttled less
  • Beams don’t have proper shading so it ends up looking really smooth. Can be a pro or a con depending on what you want!

Cons:

  • Roblox limits the number of beams drawn at any one time, so you can’t use this for a super massive grid
  • Roblox throttles how many Beams can update per frame so some might be one frame behind and leave gaps if you use this on a massive grid
  • Beams don’t have proper shading so it ends up looking really smooth. Can be a pro or a con depending on what you want!

Edit: I’m now very tempted to write a module that allows you to pick any of these methods and handle everything for you. If I find time, I’ll probably do it.

Edit2: Yeah I couldn’t resist, I made one:

19 Likes

this was what i was looking for thanks bro