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 Azial · Apr 02, 2013 at 12:04 AM · shaderinputalphacube

Shader works only on Cubes properly (Alpha problem)

I made a water shader for Indie Unity which works quite good, but it seems that it works only on the cubes from the Gameobjects-menu properly. The problem is that the "fog"-effect which fades out alpha in the distance is not working on non-cubes, the remaining effects work properly.

I would made the fadeout-effect with an extra (alpha)-texture, but then I get a warning that the input limit is reached (I need all the other inputs because I control both bumpmaps with a scrolling-texture script).

Here's the shader:

 Shader "Custom/Water" {
 Properties {
     _Color ("Main Color", Color) = (1,1,1,1)
     _ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
     _Cube ("Reflection Cubemap", Cube) = "_Skybox" { TexGen CubeReflect }
     _BumpMap1 ("Bumpmap (RGB Trans)", 2D) = "bump" {}
     _BumpMap2 ("Bumpmap (RGB Trans)", 2D) = "bump" {}
     _FogFar ("Fog Near", range (0,1000)) = 5
     _FogNear ("Fog Far", range (0,1000)) = 10
 
     _FresnelPower ("_FresnelPower", Range(0.05,5.0)) = 0.75
 }
 SubShader {
     Tags { "RenderType"="Transparent" }
     
 CGPROGRAM
 #pragma surface surf BlinnPhong alpha
 #pragma target 3.0
 
 samplerCUBE _RtReflection;
 sampler2D _BumpMap1, _BumpMap2;
 samplerCUBE _Cube;
 float4 _Color, _ReflectColor;
 float _FresnelPower, _FogNear, _FogFar;
 
 struct Input {
     float2 uv_BumpMap1;
     float2 uv_BumpMap2;
     float3 worldRefl;
     float3 viewDir;
     INTERNAL_DATA
 };
 
 void surf (Input IN, inout SurfaceOutput o) 
 {
     half4 c = _Color;
     
     half3 bump1 = UnpackNormal(tex2D( _BumpMap1, IN.uv_BumpMap1)).rgb;
     half3 bump2 = UnpackNormal(tex2D( _BumpMap2, IN.uv_BumpMap2)).rgb;
     half3 bump = (bump1 + bump2) * 0.5;
     o.Normal = bump;
     
     half3 worldReflVec = WorldReflectionVector(IN, o.Normal);    
     half4 reflcol = texCUBE(_Cube, worldReflVec);
     
     // FRESNEL CALCS
     float fcbias = 0.20373;
     float facing = saturate(1.0 - max(dot( normalize(IN.viewDir.xyz), normalize(o.Normal)), 0.0));
     float refl2Refr = max(fcbias + (1.0-fcbias) * pow(facing, _FresnelPower), 0);            
     
     o.Albedo =  reflcol.rgb * _ReflectColor.rgb + c.rgb;
     o.Emission = o.Albedo * 0.25;
     float d = length(IN.viewDir);
     float l = saturate((d - _FogNear) / (_FogFar - _FogNear));
     o.Alpha = refl2Refr * l; 
 }
 ENDCG
 
 }
 
 FallBack "Reflective/VertexLit"
 } 



Any ideas how I could solve the issue?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Owen-Reynolds · Apr 02, 2013 at 01:42 AM

If it only works on a cube, it has to have something to do with cubes always having a back face. I'd guess there's something wrong with the face= calculation and viewDir, giving 0 alpha on front faces, but working on backs. A flipped sign(?)

Take a look at the back of a plane and see if it works there.

Comment
Add comment · Show 5 · 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 Azial · Apr 02, 2013 at 08:56 AM 0
Share

No, the cubes from Unity are also backface culled. On the other side from the plane there's nothing like it should.

Even If I delete the "refl2Refr" in the o.Alpha... line, the non-cubes have the issue.

avatar image Owen-Reynolds · Apr 02, 2013 at 11:03 PM 0
Share

Just tried it -- looks the same on a plane and cube to me, just looking at alpha. But, the alpha fades IN in the distance. One $$anonymous$$us EL would fade out.

The refl2Refr doesn't seem to do anything (just uniform less opaque.)

Can be tricky to adjust _FogNear/Far, since you can't see the numbers. Sometimes easier to make them not sliders.

avatar image Azial · Apr 03, 2013 at 08:58 AM 0
Share

That's strange. For me, alpha fades out and refl2refr works. I use a mac, an open gl issue?

avatar image Owen-Reynolds · Apr 03, 2013 at 01:43 PM 0
Share

Look at el = (distance-near)/(far-near), which sets the alpha. That number is 0 at the near value and 1 at the far. So increases alpha as you move away. If it doesn't, something funny is going on.

avatar image Azial · Apr 03, 2013 at 03:46 PM 0
Share

I made a new scene and now the shader works perfect on both cube and plane. The names for "fog near" and "fog far" are choosen this way, that the slider in the editor makes bigger values in right direction than left, so your "fog far" slider must be on the right hand from the "fog near" slider value. For me it looks this: http://www.imagebanana.com/view/jnvn01xs/WaterShader.jpg (can't embed pictures here some reason, the "accept" button does nothing)

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

11 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

Related Questions

Cube botttom transparent with shader? 1 Answer

Problem with alpha coloured shader 1 Answer

Fade-In/Out shadows for a specific object 0 Answers

How to force the compilation of a shader in Unity? 5 Answers

transparent mode does not work at alpha=255 1 Answer


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