Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by drillbit · Nov 24, 2016 at 02:37 AM · c#shadersunity5texture2dmixing

Mix Shaders Question -- Multiple Textures, Recolorable?

I'm pretty new to writing shaders so I'm having some difficulty making something work. Basically, as I've shown in this diagram:

alt text

I want to be able to get a final texture that's a combination of a base color, a flat-color pattern that can be set to whatever color, and a fur texture that overlays everything. I cannot figure out how I should be recoloring the layered-on texture, nor if I should be doing this via a shader or by modifying a new texture outside the shader. I've tried messing around with mix shaders, but I just can't figure out how to get the results I want. I want to be able to do this with multiple layered-on patterns with colors defined by external code. Any help would be greatly appreciated!

mix-shader-question.png (111.7 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
1
Best Answer

Answer by Namey5 · Nov 24, 2016 at 04:40 AM

 Shader "Custom/Shader" {
     Properties {
         _BaseColor ("Base Color", Color) = "white"
         _PatternColor ("Pattern Color", Color) = "white"
         _Pattern ("Pattern", 2D) = "white" {}
         _Overlay ("Overlay", 2D) = "white" {}
     }
     SubShader {
         Pass {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma target 3.0
             #include "UnityCG.cginc"
 
             struct v2f {
                 float4 pos : SV_POSITION;
                 float2 uv : TEXCOORD0;
             };
 
             v2f vert (appdata_base v) {
                 v2f o;
                 o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
                 o.uv = v.texcoord.xy;
                 return o;
             }
 
             sampler2D _Pattern;
             float4 _Pattern_ST;
             sampler2D _Overlay;
             float4 _Overlay_ST;
             fixed4 _BaseColor;
             fixed4 _PatternColor;
 
             half4 frag (v2f i) : SV_Target
             {
                 half4 c = lerp (_BaseColor, _PatternColor, tex2D (_Pattern, TRANSFORM_TEX (i.uv, _Pattern)).r) * tex2D (_Overlay, TRANSFORM_TEX (i.uv, _Overlay));
                 c.a = 1.0;
                 return c;
             }
             ENDCG
         }
     }
 }

This should fit your original proposal. If you need to add more textures, etc. you can dissect the elements of this shader,

Comment
Add comment · Show 6 · 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 drillbit · Nov 24, 2016 at 05:10 AM 0
Share

This is excellent! $$anonymous$$y only problem is that I'm honestly not sure how to add additional patterns. I feel like I understand it until the lerp part.... at that point I'm not sure how to add the secondary texture (like I said, very new with shaders).

Here's what I have so far:

 Shader "Custom/$$anonymous$$onsterShader" {
  Properties {
          _BaseColor ("Base Color", Color) = (1,1,1,1) 
          _PatternColor ("Pattern Color", Color) = (1,1,1,1)
          _Pattern2Color ("Pattern2 Color", Color) = (1,1,1,1)
          _Pattern ("Pattern", 2D) = "white" {}
          _Pattern2 ("Pattern2", 2D) = "white" {}
          _Overlay ("Overlay", 2D) = "white" {}
      }
      SubShader {
          Pass {
              CGPROGRA$$anonymous$$
              #pragma vertex vert
              #pragma fragment frag
              #pragma target 3.0
              #include "UnityCG.cginc"
  
              struct v2f {
                  float4 pos : SV_POSITION;
                  float2 uv : TEXCOORD0;
              };
  
              v2f vert (appdata_base v) {
                  v2f o;
                  o.pos = mul (UNITY_$$anonymous$$ATRIX_$$anonymous$$VP, v.vertex);
                  o.uv = v.texcoord.xy;
                  return o;
              }
  
              sampler2D _Pattern;
              float4 _Pattern_ST;
              sampler2D _Pattern2;
              float4 _Pattern2_ST;
              sampler2D _Overlay;
              float4 _Overlay_ST;
              fixed4 _BaseColor;
              fixed4 _PatternColor;
              fixed4 _Pattern2Color;
  
              half4 frag (v2f i) : SV_Target
              {
                  half4 c = lerp (_BaseColor, _PatternColor, tex2D (_Pattern, TRANSFOR$$anonymous$$_TEX (i.uv, _Pattern)).r) * tex2D (_Overlay, TRANSFOR$$anonymous$$_TEX (i.uv, _Overlay));
                  c.a = 1.0;
                  return c;
              }
              ENDCG
          }
      }
  }
avatar image Namey5 drillbit · Nov 24, 2016 at 06:39 AM 1
Share

You've got it pretty much right. What the lerp function does is it takes a value between 0 and 1 as the third input (in this case we use the red channel of the pattern texture to keep it grayscale), and transitions between the first and second value. 0 for the third value will return entirely the first value (colour in this case), whilst 1 will return the second, and anything inbetween will morph. In this case, because lerp only takes 2 values and a transition value, we have to do something a bit different.

 half4 c = lerp (_BaseColor, _PatternColor, tex2D (_Pattern, TRANSFOR$$anonymous$$_TEX (i.uv, _Pattern)).r);
 half4 c2 = lerp (c, _Pattern2Color, tex2D (_Pattern2, TRANSFOR$$anonymous$$_TEX (i.uv, _Pattern2)).r);
 return c2 * tex2D (_Overlay, TRANSFOR$$anonymous$$_TEX (i.uv, _Overlay));

I have split it up into more variables to make it easier to understand. In this case, we blend between the base colour and the first pattern colour, and store it in a variable. Then, we blend between that variable and the second pattern colour. It's a bit confusing because I've used the 'TRANSFOR$$anonymous$$TEX' macro, which isn't necessary, but what it does is it allows for the texture controls in the inspector (in the shader known as '$$anonymous$$yTexture_ST') to distort the texture coordinates.

avatar image drillbit Namey5 · Nov 24, 2016 at 04:52 PM 0
Share

Okay, yeah, I think I understand now. The logic makes sense to me, but the specific syntax for shaders kind of feels really new to me.

One final question-- I notice that when I use this code, the second pattern only shows up where the first pattern is, like it's masked by it. I think an extra step might have to be taken to combine their alphas?

Show more comments
avatar image Vadol · Aug 02, 2019 at 08:34 AM 0
Share

Parse error: syntax error, unexpected TVAL_STRING, expecting '(' at line 5

avatar image Namey5 Vadol · Aug 02, 2019 at 01:24 PM 0
Share

Replace these lines;

 _BaseColor ("Base Color", Color) = "white"
 _PatternColor ("Pattern Color", Color) = "white"

with this;

 _BaseColor ("Base Color", Color) = (1,1,1,1)
 _PatternColor ("Pattern Color", Color) = (1,1,1,1)

I probably fixed that in one of the old comments, but UA has since made most comments more than a year old unreadable for some reason.

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

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

Related Questions

Texture offset not working with shader 0 Answers

Return transform to normal rotation after rotation key pressed? 0 Answers

In what situation Component.gameObject method throws NullReferenceException? 2 Answers

OnTriggerEnter2D not working 1 Answer

Material doesn't have a texture property '_CameraDepthTexture' 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