• 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
0
Question by Unity-Paradox · Jul 07, 2016 at 02:14 PM · renderinggraphicsmaterialsshadinggl

Shading with GL.TRIANGLE_STRIP

I've created a script to draw a sort of tree using GL. It's not yet complete, but when it is, I want it to be shaded, but so far I've only gotten it to work with the "Hidden/Internal-Colored" shader, and nothing else shows the tree on-screen. I'd like to know how to shade this tree with a different, built-in shader. Thanks in advance! Here's my code:

 using UnityEngine;
 using System.Collections;

 public class DrawTree : MonoBehaviour {
     public float baseWidth;
     public float branchHeight;
     public int treeHeight;

     public Color treeColor;
     public Color leafColor;

     Material drawMat;

     void Start () {
         drawMat = new Material(Shader.Find("Hidden/Internal-Colored"));
         drawMat.hideFlags = HideFlags.HideAndDontSave;
         drawMat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
         drawMat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
         drawMat.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
     }

     void OnRenderObject () {
         drawMat.SetPass(0);

         GL.PushMatrix();
         GL.MultMatrix(transform.localToWorldMatrix);

         GL.Begin(GL.TRIANGLE_STRIP);

         GL.Color(treeColor);
         for (int i=0; i<treeHeight; i++) {
             GL.Vertex3((-baseWidth/2)*(treeHeight-1-i), branchHeight*i, 0);
             GL.Vertex3((baseWidth/2)*(treeHeight-1-i), branchHeight*i, 0);
         }

         GL.End();

         GL.PopMatrix();
     }
 }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Jul 07, 2016 at 03:00 PM

The GL class is only for drawing simple things manually. Shading and lighting is more complex. The GL class doesn't allow you to set vertex normals which are required for shading / lighting.

You should use the Mesh class and create an actual mesh. There you can set vertex normals and use Unity's shading pipeline. Most lighting approaches requires multiple shader passes. With the GL class you always run only one pass at a time. In your example code pass "0". So it's way simpler to create a Mesh and use a MeshFilter / MeshRenderer which can use any material you like.

Comment
Add comment · Show 1 · 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 Unity-Paradox · Jul 07, 2016 at 03:11 PM 0
Share

Hmm... I was expecting this might be the case, but I really would like to make it so that I can adjust the vertices of my creation, and the most simple way of doing that was with GL. I'll look into drawing meshes if I can.

avatar image
0

Answer by Glurth · Jul 07, 2016 at 06:23 PM

I realize you said with a "built-in shader" but figured I'd throw this out there anyway. If you wish to use a custom shader you CAN apply lights to GL drawn stuff. You will need to use the GL function MultiTextCoord to pass the normal vector. http://docs.unity3d.com/ScriptReference/GL.MultiTexCoord.html

e.g.

 ...GLsetup stuff...
             GL.Color(lineColor);
             GL.TexCoord(uvVector2);
             GL.MultiTexCoord(1, normalVector3); //since we pass a 1 in here, the shader will find this value in TEXCOORD1
             GL.Vertex(point3d);
 ....
 
  ..GL cleanup...

Your custom shader can then extract this information from the vertex shader input. Note that since we are using TEXCOORD1 in the shader below, we need to specify a 1, in the parameter to MultiTextCoord eg:

 // vertex shader inputs
 struct appdata
 {
   float4 vertex : POSITION; // vertex position
   float2 uv : TEXCOORD0;
   fixed4 color : COLOR;
   float3 normal: TEXCOORD1; //this texture coordinate will be USED as a "normal" for lighting, in the shader code(not shown)
 };

Comment
Add comment · Show 1 · 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 Unity-Paradox · Jul 08, 2016 at 12:10 AM 0
Share

Oh, my. I'm not good at making shaders but I guess I'll have to learn at some point, so I'll see if I can try this out sometime XD

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How can you represent overlapping energy fields? 0 Answers

Starfield and rendering 1 Answer

Why is the CPU usage so high? 1 Answer

Complex Depth Shader 0 Answers

Graphics rendering glitch in Asus Zenfone 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges