- Home /
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();
}
}
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.
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.
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)
};
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