Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Grix · Apr 17, 2014 at 06:48 PM · drawinggl

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Graham-Dunnett ♦♦ · Apr 17, 2014 at 07:29 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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();
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Grix · Apr 17, 2014 at 08:29 PM 0
Share

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!

avatar image robertbu · Apr 17, 2014 at 09:41 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges