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 drak8888 · Nov 25, 2013 at 04:17 PM · shadertransparentspecularindie

Spec only Shader, Again.

I already asked that question and I got a good awnser using GrabPass and it lead to a great looking water shader but, the big problem is that I am using Unity Free and it seems that GrabPass is not working in executables created by Unity Free.

What I am looking for is a shader that uses 2 bumps and a specular, without any diffuse. The problem is when I put the alpha of the diffuse to 0, it removes the specular as well, thats what I don't want. So I was asking myself, is there any way to use the specular output as an alpha or some other trick like this that will not require Unity Pro? I don't really care about the refraction in the water since I know that I will need Unity Pro for that.

And also, if there is no way of getting a spec only map, is there a way to blur the edges between the ground and the water, in Unity Free once again?

Comment
Add comment · Show 2
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 Professor Snake · Nov 25, 2013 at 04:49 PM 0
Share

As far as blurring the edges goes, you can use the camera's depth/normals texture. $$anonymous$$ake sure your camera is set to generate a depth/normals texture and add a new variable to your shader called sampler2D _CameraDepthNormalsTexture;. Also make sure that your shader is has the "Queue"="Transparent" tag so it gets drawn after opaque objects. Inside the surface function, get the depth at the pixel you are rendering with this code (which, since the object is not drawn yet, is the depth of the object right behind the water mesh):

 half4 d = tex2D (_CameraDepthNormalsTexture, IN.screenPos.xy/IN.screenPos.w);
 float depth=DecodeFloatRG(d.zw);

Then get the depth of the water mesh

 float od=(IN.screenPos.w*_ProjectionParams.w);

Both depth values are in the 0-1 range, so you can just set the alpha to

 pow(depth-od,12/*or some other value you like*/);

As far as i know, Unity Free does allow you to use the depth/normals texture. PS: Effects based on the depth/normals texture won't be visible in the scene view; only in play mode.

avatar image drak8888 · Nov 25, 2013 at 07:32 PM 0
Share

Nvm that, right now it seems that the value that I change in the alpha just affects the whole shader. Putting it at 1 to 12 makes it invisible, while puting it at like 0.1, 0.5, makes it more visible but everywhere..

1 Reply

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

Answer by tanoshimi · Nov 25, 2013 at 06:00 PM

Unfortunately, simply setting c.a = spec (or similar) in the lighting model of a surface shader won't work (in fact, making any change to alpha in the lighting function has no effect, as noted here and here) because the surface shader output always seems to override whatever alpha was set in the lighting function with an additional c.a = o.Alpha; placed at the end of the fragment function.

Instead, you can still write a surface shader that handles only the specular component together with the alpha, something like this:

 half4 LightingSpecularOnly (SurfaceOutput 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, s.Specular*128.0);
   half4 c;
   c.rgb = (_LightColor0.rgb * spec) * (atten * 2);
   c.a = spec;
   return c;

}

But then also add #pragma debug to generate the underlying vertex/fragment shaders from your code. Then simply copy and paste the generated code and remove the offending final c.a = o.Alpha; and you should be good to go. Here's a quick test:

alt text

EDIT as suggested by @Professor Snake below, you can override the c.a = o.Alpha; by using a finalcolor function. Like this:

 Shader "Custom/SpecOnly" {
     Properties {
     }
     SubShader {
         Tags { "RenderType"="Transparent" "Queue" = "Transparent"}
         LOD 200
         
         CGPROGRAM
         #pragma surface surf SpecularOnly finalcolor:alphaFix alpha 
 
         struct Input {
           half Specular;            
         };
 
         half4 LightingSpecularOnly (SurfaceOutput 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, s.Specular*128.0);
           half4 c;
           c.rgb = (_LightColor0.rgb * spec) * (atten * 2);
           c.a = spec;
           return c;
           }
 
         void alphaFix (Input IN, SurfaceOutput o, inout fixed4 color) {
           color.a = color.rgb;
           }
 
         void surf (Input IN, inout SurfaceOutput o) {
             o.Specular = 1;
         }
         ENDCG
     } 
 }




trans_spec.jpg (50.1 kB)
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 Professor Snake · Nov 25, 2013 at 06:09 PM 1
Share

Quite a neat solution. Would a finalcolor function override the additional c.a = o.Alpha ? Also, is it possible to write to o.Alpha in a lighting function ins$$anonymous$$d?

avatar image tanoshimi · Nov 25, 2013 at 07:18 PM 0
Share

Good thinking with the finalcolor function! I've amended code.

avatar image drak8888 · Nov 25, 2013 at 07:33 PM 0
Share

Ok. The shader works I only see the Spec which is really nice. Is there a way to apply a normal map on it?

Edit : Never$$anonymous$$d, added it on the surf at the end, works like a charm, thanks a lot!

avatar image vlames · Jul 09, 2014 at 05:06 PM 0
Share

this is really helpful, is it possible to change the color and shininess too somehow?

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

18 People are following this question.

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

Related Questions

About shader! 0 Answers

Shader for overlaying textures 1 Answer

Why are Cubemaps and Specular highlights not equally blurry at the same Roughness / Glossyness value with standard pbr shader? 0 Answers

Transparent & reflective shader 1 Answer

Transparency shader that also writes to Camera Depth Texture 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