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 kilian277 · Jun 05, 2014 at 10:17 AM · shaderalphamapalpha-channel

Shader question

Hi,

i've made a small shader that takes a color map with RGBA channels and also takes 4 diffuse textures as channels and multiply each color channel on the map with each diffuse texture to blend them together on a piece of mesh terrain.

Now the problem is i also want to use the alpha channel for the 4th texture. Now i've come to the problem that the map.a is applying the texture to the opacity value of 1 and not that of 0.

Here's a picture of it:

alt text

Now if a create a float value for the alpha channel and do

 float a = m.a - 1;

It applies to the correct regions directed by the color map. But the texture itself is screwed in a way:

alt text

How can i fix this problem shown in pic2 ?

Here's the shader code:

 Shader "Exile/terrain/exile_4lyr_terrain" {
     Properties {
         _Color ("Main Color", Color) = (1,1,1,1)
         _Layer1 ("Layer1", 2D) = "white" {}
         _Layer2 ("Layer2", 2D) = "white" {}
         _Layer3 ("Layer3", 2D) = "white" {}
         _Layer4 ("Layer4", 2D) = "white" {}
         _Map ("Map", 2D) = "white" {}
     }
     SubShader {
         Tags { "RenderType"="Opaque" }
         LOD 200
         
         CGPROGRAM
         #pragma surface surf Lambert
 
         sampler2D _Layer1;
         sampler2D _Layer2;
         sampler2D _Layer3;
         sampler2D _Layer4;
         sampler2D _Map;
         float4 _Color;
 
         struct Input {
             float2 uv_Layer1;
             float2 uv_Layer2;
             float2 uv_Layer3;
             float2 uv_Layer4;
             float2 uv_Map;
         };
 
         void surf (Input IN, inout SurfaceOutput o) {
             half4 l1 = tex2D (_Layer1, IN.uv_Layer1);
             half4 l2 = tex2D (_Layer2, IN.uv_Layer2);
             half4 l3 = tex2D (_Layer3, IN.uv_Layer3);
             half4 l4 = tex2D (_Layer4, IN.uv_Layer4);
             half4 m = tex2D (_Map, IN.uv_Map);
             
             float a = m.a - 1;
 
             
             o.Albedo = ((l1.rgb * m.r) + (l2.rgb * m.g) + (l3.rgb * m.b) + (l4.rgb * a)) * _Color.rgb;
         }
         ENDCG
     } 
     FallBack "Diffuse"
 }
 
problem1.png (207.9 kB)
problem2.png (310.5 kB)
Comment
Add comment · Show 6
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 GrahamReeves ♦♦ · Jun 05, 2014 at 01:36 PM 0
Share

what is in the alpha channel of the map texture? all 0? because if it's all zero, you're applying an alpha of -1 (0-1)... and -1 isn't a good alpha to apply and causes this effect

avatar image GrahamReeves ♦♦ · Jun 05, 2014 at 01:57 PM 1
Share

I'm not sure what effect you're trying to achieve, perhaps you just want to invert the alpha? float a = 1 - m.a;

avatar image kilian277 · Jun 05, 2014 at 02:37 PM 0
Share

Yes i want to invert the alpha but it gives me that that particular channel is very bright like this :

alt text

alt text

problem4.png (301.1 kB)
avatar image GrahamReeves ♦♦ · Jun 05, 2014 at 05:18 PM 0
Share

I'm afraid we can't see the image you've attached, but the colour will start looking more and more white/bright the more colour you add together. If that channel is adding too much colour, just reduce it with float a = 1 - m.a; a *= 0.6;

avatar image kilian277 · Jun 05, 2014 at 06:17 PM 0
Share

i' ve edited it , and that seems logical but when i add the float (also with slider) it basically stays bright but yea only the intensity of how much is shown of that particular texture changes.

Show more comments

1 Reply

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

Answer by GrahamReeves · Jun 06, 2014 at 01:44 PM

Everytime you add more colour from the different layers you're adding brightness, so it will ONLY get brighter and brighter.

Perhaps this will work if you want to "merge" the different layers as they appear. This method should mean if you only have one layer in an area, it will be full colour and not merged with the others and I think should have a similar brightness all over.

 void surf (Input IN, inout SurfaceOutput o) {
          half4 l1 = tex2D (_Layer1, IN.uv_Layer1);
          half4 l2 = tex2D (_Layer2, IN.uv_Layer2);
          half4 l3 = tex2D (_Layer3, IN.uv_Layer3);
          half4 l4 = tex2D (_Layer4, IN.uv_Layer4);
          half4 m = tex2D (_Map, IN.uv_Map);
 
          // not sure if you want this or not:
          // m.a = 1 - m.a;
 
          // don't let intensity of all layers go over 1
          float AlphaWeight = m.r + m.g + m.b + m.a;
          if ( AlphaWeight > 1 )
                   m *= 1.0 / AlphaWeight;
  
          o.Albedo = ((l1.rgb * m.r) + (l2.rgb * m.g) + (l3.rgb * m.b) + (l4.rgb * m.a)) * _Color.rgb;
        }
Comment
Add comment · Show 1 · 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 kilian277 · Jun 06, 2014 at 06:52 PM 0
Share

thanks man ! the alphaweight did it.

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

22 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 avatar image avatar image avatar image avatar image

Related Questions

Water shader with alpha 0 Answers

sprite.associatedAlphaSplitTexture with RGB Crunched ETC1 0 Answers

Shader to render RenderTexture plus alpha? 1 Answer

Shader that draws alpha independent of transparency? 2 Answers

Cutout Shader getting Opaque when turning alpha off and on again 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