- Home /
Need help with Glow shader!
Hi All.I need a little help.I ported a glow shader from CG.This is not a post process effect ,but applied directly to the object.The whole process of glowing is done in a single pass using only input colors.I need to add another pass with Texture input so that I can have an object textured with glow effect surrounding it.I added such a pass but it seems that the glow pass still overlays the texure.I suppose it is due to the fact that I transform the glow into view matrix so that it always faces the camera.And because I don't do this for the texture pass ,I believe ,that is the reason the texture pass is not seen to the camera .So here is the shader code:
Shader "Custom/GlowShader" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
/// _Color ("Main Color", Color) = (1,1,1,0)
_Inflate("Infalate", Float) = 0.5
_GlowColor ("Glow Color", Color) = (1,1,1,0.5)
_GlowExpon ("GlowExpon", Range(1.0, 7.0)) = 0.1
}
SubShader {
Tags { "RenderType"="Opaque" }
//LOD 200
Tags { "Queue" = "Transparent" }
Pass {
Blend One One
SetTexture [_MainTex] { combine texture, texture}
}
Pass {
// Blend One One
Blend SrcAlpha OneMinusSrcAlpha
// Blend DstColor Zero
CGPROGRAM
#include "UnityCG.cginc"
#pragma vertex vert
#pragma fragment frag
sampler2D _MainTex;
uniform float _Inflate;
uniform float4x4 ViewIXf;
uniform float4 _GlowColor;
uniform float _GlowExpon;
struct appdata {
float4 vertex : POSITION;
float4 UV:TEXCOORD0;
float4 Normal :NORMAL;
float4 Tangent :TANGENT;
// float4 Binormal :BINORMAL0;
};
struct gloVertOut {
float4 HPosition : SV_POSITION;
float3 WorldNormal : TEXCOORD0;
float3 WorldView : TEXCOORD1;
};
gloVertOut vert (appdata IN) {
gloVertOut OUT = (gloVertOut)0;
OUT.WorldNormal = mul(UNITY_MATRIX_IT_MV,IN.Normal).xyz;/////UNITY_MATRIX_IT_MV =WorldITXf
float4 Po = float4(IN.vertex.xyz,1);
Po += (_Inflate*normalize(float4(IN.Normal.xyz,0))); // the balloon effect
float4 Pw = mul(_Object2World,Po);///_Object2World =WorldXf
OUT.WorldView = normalize(float3(ViewIXf[0].w,ViewIXf[1].w,ViewIXf[2].w) - Pw.xyz);///ViewIXf=ViewInverse
OUT.HPosition = mul(UNITY_MATRIX_MVP ,Po);//WvpXf=UNITY_MATRIX_MVP
return OUT;
}
float4 frag( gloVertOut IN ) : COLOR {
float3 Nn = normalize(IN.WorldNormal);
float3 Vn = normalize(IN.WorldView);
float edge = 1.0 - dot(Nn,Vn);
edge = pow(edge,_GlowExpon);
float3 result = edge * _GlowColor.rgb;
float4 texcol= tex2D (_MainTex, IN.WorldView);
return float4(result,edge);
//float4 res=float4(result,edge);
// return texcol*res;
}
ENDCG
}
}
}
Additionally one has to add a MonoBehaviour script to the glowing object with the following script.It is needed to get View Inverse Matrix into the shader ,without which the glow effect will not face the camera.
using UnityEngine;
using System.Collections;
public class GlowScript : MonoBehaviour {
// Use this for initialization
public Camera cam;
//GameObject camera;
void Start () {
}
// Update is called once per frame
void Update () {
if(cam){
Matrix4x4 viewm=cam.cameraToWorldMatrix;
Debug.Log(viewm);
renderer.sharedMaterial.SetMatrix("ViewIXf",viewm.inverse);
}
}
}
Shader questions are hard. Hardly anyone knows anything about this stuff!
Because most people are consent to use the basic ones, and Unity doesn't have a nice shader editor like, say, UE3 or Source. While the asset store provides a few similar solutions - and a free one called Strumpy Shader - that's very different from coding shaders. Have you tried the relevant forum section? $$anonymous$$ight have better luck there.
Curious if you made progress with this. Seems like a very good shader to lear from, once it works correctly.
This shader looks really awesome in the preview using a normal sphere, but when using it in game it don't work properly. Is there any possibility for you to be able to fix it?
Answer by zagor · Feb 18, 2012 at 07:24 PM
Matrix4x4 scaleOffset = Matrix4x4.TRS( new Vector3(0f,0f,0f), Quaternion.identity,new Vector3(0.5f,0.5f,0.5f));
//renderer.sharedMaterial.SetMatrix("ViewIXf",viewm.inverse);
renderer.material.SetMatrix( "ViewIXf",scaleOffset *cam.projectionMatrix *cam.worldToCameraMatrix *transform.localToWorldMatrix );
Your answer
Follow this Question
Related Questions
How to force the compilation of a shader in Unity? 5 Answers
Shader Not Blending Textures on Mobile 1 Answer
Shader error in ... : Syntax error at line 30 1 Answer
CG Programming, Points Lights attenuation and light range 3 Answers
Can I read `CGPROGRAM` `#define` constants in `.cs` at runtime? 0 Answers