- Home /
How to draw flat triangles in 3D space?
I'm trying to draw simple, colored, flat triangles using three 3d coordinates, how would I do this? After some searching I came across this library "GL" which seems to do what I want, but I cannot get it to work. After reading the script reference I tried to make this script which runs in a simple cube gameobject:
using UnityEngine;
using System.Collections;
public class drawProjection : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
GL.PushMatrix();
GL.Begin(GL.TRIANGLES);
GL.Color(Color.red);
GL.Vertex3(1,0,0);
GL.Vertex3(0,1,0);
GL.Vertex3(0,2,0);
GL.End();
GL.PopMatrix();
}
}
But it does nothing. I'm very new to Unity, and I don't even understand much of the lingo in the script reference so I don't know where to begin at learning about this.
Problems might include the triangle facing away from the camera (so you are looking at the back of it) or the camera is looking in a different direction to where you are drawing the triangle.
Answer by robertbu · Apr 17, 2014 at 08:03 PM
From the reference:
GL drawing commands execute immediately. That means if you call them in Update(), they will be executed before the camera is rendered (and the camera will most likely clear the screen, making the GL drawing not visible).
From the reference:
The usual place to call GL drawing is most often in OnPostRender() from a script attached to a camera
Attach this script to the Camera:
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
void OnPostRender() {
GL.PushMatrix();
GL.Begin(GL.TRIANGLES);
GL.Color(Color.red);
GL.Vertex3(1,0,0);
GL.Vertex3(0,1,0);
GL.Vertex3(0,2,0);
GL.End();
GL.PopMatrix();
}
}
Thanks, I got a triangle to show up! However, it seems they only one of the faces are visible, is it possible to make it visible from both angles without having to draw a duplicate triangle flipped?
Also, how would I make the triangle plainly colored independently from lightning, and partly transparent? It doesn't seem like the "GL.Color(Color.red);" does anything. I guess this may be too much to explain easily, so if you could just direct me to some relevant references or something I'd appreciate it!
I don't have answer to your additional questions. I've only played with with GL a couple of times. I just read the reference, looked at your code, and spotted the issues that I highlight in my answer. The reference did say that GL uses the currently set material, so getting your color right and setting transparency will depend on figure out how to set materials and material properties when drawing with GL. Note that starting with a new scene (i.e. using whatever default shader exists with a new scene), the triangle drawn by the above code was red.
Your answer

Follow this Question
Related Questions
OnInspectorGUI Custom Drawing? 1 Answer
Draw 2D polygon 1 Answer
How to draw a triangle in the center of a line using GL? 0 Answers
Draw polygon from array of points 1 Answer
Using screencapture to reduce expensive draw calls. 2 Answers