Skip to content

Repository files navigation

Mesh2d

Mesh2d is a lightweight library for generating 2D stroke meshes in Unity. It build over Native Arrays and is compatible with Unity's Burst Compiler for improved performance.

Installation

To use iShape.Mesh2d in your Unity project, follow these steps:

  • Open your Unity project.
  • Ensure that your Unity version is 2022.2 or above.
  • Check that you have the following packages installed (these are dependencies for iShape.Mesh2d):
    • `com.unity.mathematics` version 1.2.6 or later
    • `com.unity.collections` version 2.1.4 or later
  • In the top menu, select "Window" > "Package Manager".
  • Click on the "+" button in the top-left corner of the Package Manager window.
  • Select "Add package from git URL...".
  • Enter the following URL: https://github.com/iShapeUnity/Mesh2d.git
  • Click the "Add" button.
  • Wait for the package to be imported.

Usage

To use iShape.Mesh2d in your Unity project, you'll need to import the iShape.Mesh2d namespace in your scripts. Here's an example of how to use it:

usingiShape.Mesh2d;usingUnity.Collections;usingUnity.Mathematics;usingUnityEngine;publicclassExample:MonoBehaviour{privatevoidStart(){// Create a new NativeColorMesh with a capacity of 256 verticesvarmesh=newNativeColorMesh(256,Allocator.Temp);// Generate a blue stroke mesh for an edgevaredgeStyle=newStrokeStyle(0.4f);varedgeMesh=MeshGenerator.StrokeForEdge(newfloat2(3,3),newfloat2(-3,3),edgeStyle,0,Allocator.Temp);mesh.AddAndDispose(edgeMesh,Color.blue);// Generate a magenta stroke mesh for a pathvarpathStyle=newStrokeStyle(0.5f);varpath=newNativeList<Vector2>(4,Allocator.Temp);path.Add(newVector2(-4,3));path.Add(newVector2(-4,-3));path.Add(newVector2(4,-3));path.Add(newVector2(4,3));varfloatArray=path.AsArray().Reinterpret<float2>();varpathMesh=MeshGenerator.StrokeByPath(floatArray,false,pathStyle,0,Allocator.Temp);mesh.AddAndDispose(pathMesh,Color.magenta);path.Dispose();// Generate a green stroke mesh for a rectanglevarrectStyle=newStrokeStyle(0.2f);varrectMesh=MeshGenerator.StrokeForRect(float2.zero,newfloat2(4,4),rectStyle,0,Allocator.Temp);mesh.AddAndDispose(rectMesh,Color.green);// Generate a red stroke mesh for a circlevarcircleStyle=newStrokeStyle(0.1f);varcircleMesh=MeshGenerator.StrokeForCircle(float2.zero,1,32,circleStyle,0,Allocator.Temp);mesh.AddAndDispose(circleMesh,Color.red);// Generate a yellow stroke mesh for a soft starvarstarStyle=newStrokeStyle(0.05f);varstarMesh=MeshGenerator.StrokeForSoftStar(float2.zero,0.4f,0.7f,64,starStyle,0,Allocator.Temp);mesh.AddAndDispose(starMesh,Color.yellow);// Generate a white circle meshvarcircleShape=MeshGenerator.FillCircle(newfloat2(6,0),1.0f,32,0,Allocator.Temp);mesh.AddAndDispose(circleShape,Color.white);// Generate a white rectangle meshvarrectShape=MeshGenerator.FillRect(newfloat2(-6,0),newfloat2(2,2),0,Allocator.Temp);mesh.AddAndDispose(rectShape,Color.white);// Set the generated mesh as the MeshFilter's meshvarmeshFilter=this.GetComponent<MeshFilter>();meshFilter.mesh=mesh.Convert();}}

In this example, we're generating stroke and shape meshes and after we adding them to a NativeColorMesh. Then, we're setting the resulting mesh as the MeshFilter's mesh.

For more information on how to use iShape.Mesh2d, please see MeshGenerator

Minimal working project could be found at Mesh2Test

About

Mesh2d is a lightweight library for generating 2D stroke meshes in Unity. It build over Native Arrays and is compatible with Unity's Burst Compiler for improved performance.

Resources

Stars

8 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages