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 Kilomoana83 · Nov 15, 2014 at 04:07 PM · c#shadercolortransparencytint

surface shader homeworld (space) colorize transparent

Hi,

I have a problem regarding a fragment-shader. I tried to port a homeworld shader to unity which is working very nice except one problem remaining.

The teamcolors should only be shown on the transparent areas of the texture. Actually the whole ship is colorized.

Here is the unity part:

 Shader "Kilomoana/s_ship" {
     Properties {
         _MainTex ("Base (RGB)", 2D) = "white" {}
         _SpecSI ("SpecSI (RGB)", 2D) = "white" {}
         _Shininess ("Shininess", Range (0.01, 10)) = 0.078125
         _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
         _SelfIlluminationBlue("Glow Blue", Range (0, 1)) = 0.5
         _ColorA("Team Color A", Color) = (1,0.8,0,1)
         _ColorB("Team Color B", Color) = (0,0,1,1)
     }
     SubShader {
         Tags { "RenderType"="Opaque" }
         LOD 200
         
         CGPROGRAM
         #pragma surface surf BlinnPhong
         sampler2D _MainTex;
         sampler2D _SpecSI;
         float _Shininess;
         float _SelfIlluminationBlue;
         float4 _ColorA;
         float4 _ColorB;
         
         struct Input {
             float2 uv_MainTex;
             float3 worldRefl;
         };
 
         void surf (Input IN, inout SurfaceOutput o)
         {
             // diffuse color
             half4 c  = tex2D (_MainTex, IN.uv_MainTex);
             
             // specular & glow strength
             half4 c2 = tex2D (_SpecSI , IN.uv_MainTex);
 
             // two colors
             float4 teamBaseCol   = (c + 0.5f) * _ColorA + (c - 0.5f);
             float4 teamStripeCol = (c + 0.5f) * _ColorB + (c - 0.5f);
             
             // team color amount is defined by alpha values
             float teamBaseAmount   = 1 - c.a;
             float teamStripeAmount = 1 - c2.a;
 
             // compute final base color
             float4 base = float4(1,1,1,1);
 
             base.rgb = lerp( teamBaseCol.rgb  , c.rgb   , teamBaseAmount   );
             base.rgb = lerp( teamStripeCol.rgb, base.rgb, teamStripeAmount );
     
             half spec     = c2.b;
             half selfIlluBlue = c2.g;
             half selfIlluChangedBlue = selfIlluBlue * _SelfIlluminationBlue;
                         
             o.Gloss    = spec;
             o.Specular = _Shininess;
             
             o.Emission = (selfIlluChangedBlue) * c;
             
             o.Albedo   = lerp( base.rgb, float3(0,0,0), selfIlluChangedBlue);
             o.Alpha    = base.a;
         }
         ENDCG
     } 
     FallBack "Diffuse"
 }
 

This is the original shader:

 static Texture $diffuse
 static Texture $glow
 
 simple base(Texture $diffuse, Texture $glow, Colour $teambase, Colour $teamstripe, Colour $fogColour, Colour $addColour, Colour $shadowColour)
 {
     setCap depthBufferCap true
     setCap gouraudCap true
     setCap cullCap true
     setCap texture0Cap true
     setCap lightingCap true
 
     fillMode solidFill
     cullMode backCull
 
     material ambient   1 1 1 1
     material diffuse   1 1 1 1
     material specular  1 1 1 1
     material shininess 96 96 96 96
     setVertexColour    1 1 1 1
 
     textureBind 0 $diffuse
 //    textureMode    replaceMode
 }
 
 simple light(Texture $diffuse, Texture $glow, Colour $teambase, Colour $teamstripe, Colour $fogColour, Colour $addColour, Colour $shadowColour)
 {
     setCap depthBufferCap true
     setCap gouraudCap true
     setCap cullCap true
     setCap texture0Cap false
     setCap texture1Cap false
     setCap lightingCap true
     setCap blendCap true
 
     srcBlend    destColourBlend
     destBlend    zeroBlend
 
     fillMode solidFill
     cullMode backCull
 
     material ambient   1 1 1 1
     material diffuse   1 1 1 1
     material specular  1 1 1 1
     material shininess 96 96 96 96
     setVertexColour    1 1 1 1
 }
 
 
 simple fog(Texture $diffuse, Texture $glow, Colour $teambase, Colour $teamstripe, Colour $fogColour, Colour $addColour, Colour $shadowColour)
 {
     setCap depthBufferCap true
     setCap gouraudCap true
     setCap blendCap true
     setCap cullCap        true
 
     srcBlend srcAlphaBlend
     destBlend invSrcAlphaBlend
 
     fillMode solidFill
     cullMode backCull
 
     setVertexColour    $fogColour    
 }
 
 compound ship()
 {
     addPass base
     //addPass light
     addPass fog
 }
 

Tinted in a dark gray it already looks nicely:

alt text

When tinted in red you can see the wrong effect:

alt text

Hope someone could help me out.

h2.png (204.3 kB)
h1.png (272.6 kB)
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 Kilomoana83 · Nov 21, 2014 at 06:36 PM

Finally solved it by myself. alt text

Don't know why I did not saw it:

   float teamBaseAmount   = 1 - c.a + c2.a;
   float teamStripeAmount = 1 - c2.a + c.a;

h1.jpg (280.9 kB)
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

2 People are following this question.

avatar image avatar image

Related Questions

How do you add a Color Tint to a shader? 1 Answer

Additive particle shader with higher max alpha? 0 Answers

How to FadeIn 3dObject using alpha color? 1 Answer

Unity Standard Shader Detail Albedo x2 Own Colour 0 Answers

Color tint on a shader. 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