Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Sasmaster · Oct 01, 2011 at 10:25 AM · shadercgglow

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);
         }
     
         
     }
 }
Comment
Add comment · Show 5
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 Sasmaster · Oct 17, 2011 at 07:33 AM 0
Share

Anybody ???

avatar image syclamoth · Oct 17, 2011 at 07:51 AM 0
Share

Shader questions are hard. Hardly anyone knows anything about this stuff!

avatar image asafsitner · Oct 17, 2011 at 10:09 AM 0
Share

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.

avatar image roamcel · Apr 03, 2012 at 12:14 PM 0
Share

Curious if you made progress with this. Seems like a very good shader to lear from, once it works correctly.

avatar image stripe103 · Jul 18, 2012 at 05:18 PM 0
Share

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?

1 Reply

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

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 );
Comment
Add comment · 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

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

8 People are following this question.

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

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


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