Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Yuta · May 30, 2011 at 10:39 AM · shadertexture2dtextures

How to mix textures with multipass shader on Unity ?

How to mix textures with multipass shader on Unity ?

I want to mix textures with multipass shader on Unity as alpha-blending. But I don't know how to get the first pass's result with shader on Unity.

I've tried the variable "previous" and "primary", but they couldn't be used.

This is my shader.

 Shader "Unlit/MixTextures" {
     Properties {
       _MainTex ("Main Base(RGB) Alpha(A)", 2D) = "white" {}
       _MainTex2 ("Main2 Base(RGB) Alpha(A)", 2D) = "white" {}
     }
     SubShader {
         Tags{"Queue"="Transparent"}
         Pass{
             // Base        
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityCG.cginc"
             
             sampler2D _MainTex;
                         
             struct v2f{
                 float4 pos : SV_POSITION;
                 float2 uv : TEXCOORD0;
             };
             
             float4 _MainTex_ST;
             
             v2f vert(appdata_base v)
             {
                 v2f o;
                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
                 return o;
             }
 
             float4 frag( v2f i ) : COLOR {
                 return tex2D(_MainTex, i.uv);
             }
 
             ENDCG
         }
         
         // pass 2
         Pass{
             // Add parts
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityCG.cginc"
             
             sampler2D _MainTex2;
             float4 _TextureOffsetAndSize2;
                         
             struct v2f{
                 float4 pos : SV_POSITION;
                 float2 uv : TEXCOORD0;
                 float4 color : COLOR;
             };
             
             float4 _MainTex2_ST;
             
             v2f vert(appdata_base v)
             {
                 v2f o;
                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex2);
                 return o;
             }
 
             float4 frag( v2f i ) : COLOR {
                 
                 // * I want to use 1st pass result here * //
                 
                 float4 tex = tex2D(_MainTex2, i.uv);
                 tex.x *= tex.w;
                 tex.y *= tex.w;
                 tex.z *= tex.w;
                 return tex;
             }
             
             ENDCG
         }
     } 
     Fallback "Diffuse"
 }
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 Owen-Reynolds · May 30, 2011 at 05:22 PM 0
Share

For this particular shader, it seems easier to do it all in one pass. Just move the line from frag#1 into frag#2 (rename a var) and lose pass#1. Even if you have two unwraps (isn't TRANSFOR$$anonymous$$_TEX mostly o.uv=v.uv;?) you can have the vertex shader pass the second along in TEXCOORD1. Your shader, it's creating "alpha blended" colors?

avatar image Yuta · Jun 01, 2011 at 07:19 AM 0
Share

Thank you for your replying! You're all right. I can realize this function with one pass. $$anonymous$$y concrete shader will create "alpha blended" colors. So I want to know how to get the result of previous pass.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Peter Bruun-Rasmussen · May 30, 2011 at 12:26 PM

You should use the Blend-tag to blend the result of two passes. Something like this:

 Shader "Unlit/MixTextures" {
         Properties {
           _MainTex ("Main Base(RGB) Alpha(A)", 2D) = "white" {}
           _MainTex2 ("Main2 Base(RGB) Alpha(A)", 2D) = "white" {}
         }
         SubShader {
             Tags{"Queue"="Transparent"}
     
             Pass{
                 Lighting Off
                 SetTexture [_MainTex] {
                     combine texture, texture
                 }
             }
     
             Pass{
                 Blend DstColor Zero 
                 Lighting Off
                 SetTexture [_MainTex2] {
                     combine texture, texture
                 }
            }
         } 
         Fallback "Diffuse"
     }

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 Yuta · Jun 01, 2011 at 07:23 AM 0
Share

Thank you for your replying. I tried your code. But there is a problem. On my scene, the color of 1st pass turned to be darker than that I don't apply multi-passes ...

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

Why is Unity generating grayscale normal maps? 0 Answers

Effects of Graphics.Blit to rendertexture are temporary 2 Answers

Assigning a texture for object in inspector. Is it loaded right away? 0 Answers

Why is my terrain so dark? 1 Answer

Converting a RenderTexture to a Texture2D for use in a shader 2 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