- Home /
Generating a triangle from 3 points
I try to generate a one colored triangle from 3 points, but I'm stuck...
So I want to make an empty game object with a script (preferably C#), that does this. The script uses 3 3d coordinates and a color (lets say blue) to generate it.
Can anyone help?
Answer by s_guy · May 31, 2013 at 11:50 PM
From the official docs on meshes
// Builds a mesh containing a single triangle with uv's.
function Start () {
gameObject.AddComponent("MeshFilter");
gameObject.AddComponent("MeshRenderer");
var mesh : Mesh = GetComponent(MeshFilter).mesh;
mesh.Clear();
mesh.vertices = [Vector3(0,0,0), Vector3(0,1,0), Vector3(1, 1, 0)];
mesh.uv = [Vector2 (0, 0), Vector2 (0, 1), Vector2 (1, 1)];
mesh.triangles = [0, 1, 2];
}
...and then the official docs on materials has an example for setting material color
Answer by aldonaletto · May 31, 2013 at 10:56 PM
If you want to play in the OpenGL style, use the GL class to draw the triangle - there's a very useful example in this topic.
But if you want to create a game object with a simple triangular mesh, take a look at the mesh topic in the Unity manual, or at the Mesh class in the docs - both have useful examples, and the later allows selection of C# language.
Answer by huse · Nov 17, 2019 at 04:24 PM
Here you can learn step by step how to draw a triangle just by code (Turn on English subtitles) https://www.youtube.com/watch?v=ThWyvoqWjhE&list=PLp2zpxnnEFuemzgoUFYGkKQWwygDFXiIt![alt text][1]
[1]: /storage/temp/148854-triangle-unity.png
Answer by ExpiredIndexCard · May 31, 2013 at 10:32 PM
Make a triangle I'm blender then export to unity and turn off the mesh renderer. Simply solution
Answer by Xathereal · May 31, 2013 at 10:44 PM
Well, I not so recommended way, but a way you can generate a triangle is using calls to OpenGL directly:
GL.Begin( GL.TRIANGLES);
GL.Color( Color(1,1,1,1) );
GL.Vertex3( 0, 0, 1 );
GL.Vertex3( 1, 0, 1 );
GL.Vertex3( 0, 1, 1 );
GL.End();
Something to that effect, do some googling and you will find plenty of resources on OpenGL.
But, to make a GameObject that will rotate, scale and do everything you want, and have a triangle shape, you would be best off making the object in something like Blender.
Your answer
Follow this Question
Related Questions
Draw 3d animations using Graphics.DrawMesh 0 Answers
Why CYAN is default object color (with default materials & shaders) 0 Answers
GL not drawing in 3D? 0 Answers
Can Unity open .3ws file 1 Answer