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
0
Question by Katatafisch · Aug 01, 2012 at 12:42 PM · shaderalphaspecular

Unity Shader: Specular reflection as Alpha Value.

Hello, I am currently trying to learn shader programming. I wanted to test out several shaders by their functionality only. I want to give my object several materials with one functionality only. For example: 1) Diffuse(Base) 2) Specularity only (+BumMap) 3) Rim-Shader

I am not sure if this is more expensive than normal materials, but it allows a greater flexibility. Plus in case of learning shaders itd be a great opportunity if i knew how to access the reflected specular information.

I need to somehow get access to the lightDir and viewDir, multiply it with the specular map, specular color and light color and then store it in a texture to apply it to the o.Alpha. Thats what I think. Now I need your help.

How do I store the spec variable into a 2D texture to use as alpha map. All I found was this part where the light and viewDir were used to calc the specularity...

 inline half4 LightingColoredSpecular (MySurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
     {
       half3 h = normalize (lightDir + viewDir);
      
       half diff = max (0, dot (s.Normal, lightDir));
      
       float nh = max (0, dot (s.Normal, h));
       float spec = pow (nh, 32.0);
       half3 specCol = spec * s.GlossColor;
      
       half4 c;
       c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * specCol) * (atten * 2);
       c.a = s.Alpha;
       return c;
     }




Here is my current working code without the working alpha specularity: How can I use the specular reflection as alpha values???

////////////////////////

 Shader "Mine/Specular/BumpedSpecular" {
 Properties {
  _MainTex ("Base (RGB) TransGloss (A)", 2D) = "white" {}
  _BumpMap ("Normalmap", 2D) = "bump" {}
  _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 0)
  _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
  _Intensity ("Intensity", Range (0.01, 3)) = 1
 }
 
 SubShader {
  Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
  LOD 600
  
 CGPROGRAM
 #pragma surface surf BlinnPhong alpha
 #pragma exclude_renderers flash
 
 
 sampler2D _MainTex;
 sampler2D _BumpMap;
 sampler2D _AlphaMap;
 half _Shininess;
 
 struct Input {
  float2 uv_MainTex;
  float2 uv_BumpMap;
 };
 
 struct MySurfaceOutput {
         half3 Albedo;
         half3 Normal;
         half3 Emission;
         half Specular;
         half3 GlossColor;
         half Alpha;
 };
      
 
 float _Intensity;
 
 void surf (Input IN, inout SurfaceOutput o) {
  fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
  o.Gloss = pow( (tex.r + tex.g + tex.b)/3 , _Intensity )*_Intensity ;
  o.Specular = _Shininess;
  o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
  o.Alpha = o.Gloss;
 }
 ENDCG
 }
 
 FallBack "Diffuse"
 }
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
1

Answer by Katatafisch · Aug 03, 2012 at 04:56 PM

Hey Scroodge, thanks so much for your reply.

Maybe I was a but too vague or confusing with what I want.

I want a shader which shows ONLY the specularity. Where there is no light reflected, the alpha should be 0. Where there is light reflected, the alpha should be between 0 and 1 according to the strength of the specular reflection.

Therefore I think I do not need to apply the alpha to the specular. I think I need to apply the specular information to the alpha. But none of my tests worked well. I think I somehow need to get the information of the lightDir and viewDir to know where there is specular reflection on the surface and where not.

Like in this image on the third image in the top-row. Everything else is transparent and where there is reflection/specularity its not transparent.

alt text

Comment
Add comment · Show 4 · 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 ScroodgeM · Aug 03, 2012 at 07:32 PM 0
Share

check my answer again

avatar image ScroodgeM · Aug 03, 2012 at 07:42 PM 0
Share

PS if you need ONLY specular power like at top-right image, shader can be cheaper a lot. i just improved your one.

avatar image Katatafisch · Aug 04, 2012 at 08:07 AM 0
Share

Wow, thanks alot for your time! Did this work for you? I applied your shader, but nothing shows. I am trying to figure a way to get a simple and cheap shader that draws only the specularity. But it seems i do not understand shaders well enough to come up with a proper script. Do you have an idea how to write such a shader?

EDIT: I changed your shader a bit and replaced this line: specPower = _LightColor0.a _SpecColor.a spec (atten); by this specPower = _LightColor0.rgb _SpecColor.rgb spec (atten*2);

Now it does what I wanted! Thanks.

If we now find a way to make it cheaper, it'd be great!

avatar image ScroodgeM · Aug 04, 2012 at 11:31 AM 0
Share

describe exactly what you need on this shader output? RGB=0 and A=calculated specular intensity at point?

avatar image
0

Answer by ScroodgeM · Aug 02, 2012 at 09:45 PM

Shader "Mine/Specular/BumpedSpecular" { Properties { _MainTex ("Base (RGB) TransGloss (A)", 2D) = "white" {} _BumpMap ("Normalmap", 2D) = "bump" {} _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 0) _Shininess ("Shininess", Range (0.01, 1)) = 0.078125 _Intensity ("Intensity", Range (0.01, 3)) = 1 } SubShader { Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"} LOD 600

     CGPROGRAM
     #pragma surface surf BlinnPhong2 alpha finalcolor:mycolor
     #pragma exclude_renderers flash

     sampler2D _MainTex;
     sampler2D _BumpMap;
     sampler2D _AlphaMap;
     half _Shininess;

     struct Input
     {
         float2 uv_MainTex;
         float2 uv_BumpMap;
     };

     struct MySurfaceOutput
     {
         half3 Albedo;
         half3 Normal;
         half3 Emission;
         half Specular;
         half3 GlossColor;
         half Alpha;
     };

     float _Intensity;

     void surf (Input IN, inout SurfaceOutput o)
     {
         fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
         o.Gloss = pow( (tex.r + tex.g + tex.b)/3 , _Intensity )*_Intensity ;
         o.Specular = _Shininess;
         o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
         o.Alpha = o.Gloss;
     }

     float specPower;

     inline fixed4 LightingBlinnPhong2 (SurfaceOutput s, fixed3 lightDir, half3 viewDir, fixed atten)
     {
         half3 h = normalize (lightDir + viewDir);
         fixed diff = max (0, dot (s.Normal, lightDir));
         float nh = max (0, dot (s.Normal, h));
         float spec = pow (nh, s.Specular*128.0) * s.Gloss;
         fixed4 c;
         c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * _SpecColor.rgb * spec) * (atten * 2);
         specPower = _LightColor0.a * _SpecColor.a * spec * atten;
         c.a = s.Alpha + specPower;
         return c;
     }

     void mycolor (Input IN, SurfaceOutput o, inout fixed4 color)
     {
         color.a = specPower;
         return;
     }

     ENDCG
 }
 FallBack "Diffuse"

}

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

7 People are following this question.

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

Related Questions

How to make a specular shader fully transparent 0 Answers

Glass Shader with Strumpy Shader Editor 1 Answer

About shader! 0 Answers

Shader alpha setting being ignored? 1 Answer

specular shader alpha not working? 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