## Description Simple Gon Generator allows you to create n-gon meshes with just a few mouse clicks inside the editor or dynamically in your code. All gon parameters, like segment colors can be changed in runtime which basically allows you to animate your gons. The gons will always look as sharp as possible with any device and resolution since there is no textures involved and the color interpolation is handled by the gpu. ![simplegongenerator](img/simplegongenerator_ingame_example.png "pmjo's Simple Gon Generator") ![simplegongenerator](img/simplegongenerator_example2.png "pmjo's Simple Gon Generator") ![simplegongenerator](img/simplegongenerator_example3.png "pmjo's Simple Gon Generator") ## Adding a Simple Gon Generator using the editor Click GameObject / Simple Gon Generator / Generator menu item to add a new Simple Gon Generator to your scene. This will automatically create a new game object with MeshFilter, MeshRenderer and SimpleGonGenerator component. The SimpleGonGenerator component will contain the default gon which is a flat red circle. Click the newly created game object to customize it using the editor inside the inspector view. ![simplegongenerator](img/simplegongenerator_generator.png "pmjo's Simple Gon Generator") ![simplegongenerator](img/simplegongenerator_default.png "pmjo's Simple Gon Generator") A valid gon contains at least 3 sides and 2 segments. By default the gon size is 1x1 units. To make your gon bigger or smaller just use the transform scale like with any other game object. The segment position is the normalized distance from the center of the gon to the outer ridge of the gon. There can be any amount of segments in your gon. The order of the defined segments matter because the triangles are built in the same order the segments are in the segments array. When N+1 segment's normalized position is greater than N's the generated triangles will have clockwise winding order and counter clockwise when the normalized position is smaller. ![simplegongenerator](img/simplegongenerator_meshfilter.png "pmjo's Simple Gon Generator") ![simplegongenerator](img/simplegongenerator_shader.png "pmjo's Simple Gon Generator") By default the MeshRenderer is using a material with a simple alpha blended transparent shader but you may use a custom shader too. If you are using a shader that does not use lighting you can save memory and cpu time by disabling the normal generation. To see the colors that you have defined on your segments your shader must support vertex colors. If your shader has culling enabled you must take the segment order into account. <b>Bonus feature: </b>By default the gons are flat however you may add depth to the segments by checking the Allow Depth Change option. The depth basically allows you to create 3d gon meshes. <b>Note: </b>This version does not yet calculate normals for 3d meshes. The generated normals always face up however you can easily calculate correct normals for your Unity mesh by calling its GenerateNormals() function. If you are using SimpleGonGenerator component instead of your own renderer the GenerateNormals() is called automatically for 3d meshes. ![simplegongenerator](img/simplegongenerator_depth.png "pmjo's Simple Gon Generator") ![simplegongenerator](img/simplegongenerator_3d.png "pmjo's Simple Gon Generator") The gons are generated when needed and never serialized to the disk to minimize the build size. ## Creating a SimpleGonMesh in code You can easily create your gons dynamically in a script. Dynamic generation comes especially handy if you want your gons to have different amount of vertices on different quality levels. Instead of writing the code you can also convert your existing Simple Gon Generator shapes to code by clicking the To Clipboard button on your Simple Gon Generator inspector view. It will add the C# code required to create your shape dynamically to the clipboard. ### Creating a simple octagon void Awake () { SimpleGonMesh simpleGonMesh = new SimpleGonMesh (8); SimpleGonMesh.Segment[] segments = new SimpleGonMesh.Segment[2]; segments [0] = new SimpleGonMesh.Segment (Color.red, 1.0f); segments [1] = new SimpleGonMesh.Segment (Color.blue, 0.5f); simpleGonMesh.segments = segments; // Create mesh Mesh mesh = new Mesh (); mesh.name = "MyOctagon"; simpleGonMesh.Generate (); mesh.vertices = simpleGonMesh.vertices; mesh.colors = simpleGonMesh.colors; mesh.normals = simpleGonMesh.normals; mesh.triangles = simpleGonMesh.triangles; mesh.RecalculateBounds (); // Add mesh to MeshFilter if one exists MeshFilter meshFilter = GetComponent<MeshFilter> (); if(meshFilter != null) { meshFilter.mesh = mesh; } } You can modify all the gon properties in code. For example you might want to animate the segment colors or change the position of a segment every frame. To change the segments you must first obtain access to your SimpleGonMesh and then use the segments property which is an array of segments. See the API reference and the examples for more info. ### Animating segment position when using the SimpleGonGenerator component public float animationSpeed = 2.0f; private SimpleGonGenerator mGonGenerator; void Awake() { mGonGenerator = GetComponent<SimpleGonGenerator>(); } void FixedUpdate() { float newPosition = 0.6f + Mathf.Abs(Mathf.Sin(Time.time * animationSpeed)) * 0.3f; // Update the position of the last segment mGonGenerator.simpleGonMesh.segments[mGonGenerator.simpleGonMesh.segments.Length - 1].normalizedPosition = newPosition; // Update the mesh mGonGenerator.UpdateMesh(); } Changing the segments does not automatically update the vertices or other mesh properties. When using the SimpleGonGenerator component you must call its UpdateMesh() method to update the mesh. If you are dynamically creating a SimpleGonMesh you must call its Generate() method to update the vertices, colors, normals and triangles and apply those to your mesh instance. ### Animating segment color when using a SimpleGonMesh public float animationSpeed = 2.0f; private SimpleGonMesh simpleGonMesh; private Mesh mesh; void Awake () { simpleGonMesh = new SimpleGonMesh (32); SimpleGonMesh.Segment[] segments = new SimpleGonMesh.Segment[4]; segments [0] = new SimpleGonMesh.Segment (new Color(1, 0, 0, 0), 1.0f); segments [1] = new SimpleGonMesh.Segment (Color.red, 0.99f); segments [2] = new SimpleGonMesh.Segment (Color.blue, 0.3f); segments [3] = new SimpleGonMesh.Segment (new Color(0, 0, 1, 0), 0.29f); simpleGonMesh.segments = segments; // Add event callback to update the mesh when SimpleGonMesh is changed simpleGonMesh.OnMeshUpdated += MeshUpdated; // Create mesh mesh = new Mesh (); mesh.name = "MyAnimatedCircle"; simpleGonMesh.Generate (); mesh.RecalculateBounds (); // Add mesh to MeshFilter if one exists MeshFilter meshFilter = GetComponent<MeshFilter> (); if(meshFilter != null) { meshFilter.mesh = mesh; } } private int oldVertexCount = 0; void MeshUpdated (SimpleGonMesh simpleGonMesh, bool verticesUpdated, bool colorsUpdated, bool normalsUpdated, bool trianglesUpdated) { if(verticesUpdated) { if(simpleGonMesh.vertices.Length < oldVertexCount) { mesh.triangles = null; } mesh.vertices = simpleGonMesh.vertices; oldVertexCount = simpleGonMesh.vertices.Length; } if(colorsUpdated) { mesh.colors = simpleGonMesh.colors; } if(normalsUpdated) { mesh.normals = simpleGonMesh.normals; } if(trianglesUpdated) { mesh.triangles = simpleGonMesh.triangles; } } void FixedUpdate() { // Animate blue color component for all segments for(int i=0; i<simpleGonMesh.segments.Length; i++) { Color newColor = simpleGonMesh.segments[i].color; newColor.b = Mathf.Abs(Mathf.Sin(Time.time * animationSpeed)); simpleGonMesh.segments[i].color = newColor; } // Commit changes and update mesh vertices, colors, normals and triangles if needed simpleGonMesh.Generate(); } ## API reference ### SimpleGonGenerator #### Instance methods public void UpdateMesh () public void UpdateMesh (bool forceUpdate) public void ClearMesh () #### Instance properties public SimpleGonMesh simpleGonMesh { get; set; } ### SimpleGonMesh #### Constructors public SimpleGonMesh(int sideCount) #### Instance methods public void Generate() public void Generate(bool forceUpdate) public void Clear() public System.Version GetVersion() public string GetProductName() #### Instance properties public int sides { get; set; } public bool generateNormals { get; set; } public bool useDepth { get; set; } public Segment[] segments { get; set; } public event MeshUpdatedDelegate OnMeshUpdated #### Delegate methods public delegate void MeshUpdatedDelegate(SimpleGonMesh mesh, bool verticesUpdated, bool colorsUpdated, bool normalsUpdated, bool trianglesUpdated) ### SimpleGonMesh.Segment #### Constructors public Segment(Color c, float pos) public Segment(Color c, float pos, float depth) #### Instance properties public Color color { get; set; } public float normalizedPosition { get; set; } public float normalizedDepth { get; set; } ## Support Incase you are having problems with Simple Gon Generator, don't hesitate to contact ![Support](img/support.png "Support") ## About the author See more about the author by clicking [here](http://www.pmjo.org/about/ "About").