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 /
  • Help Room /
This question was closed Oct 12, 2017 at 10:14 AM by Gregor2211 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Gregor2211 · Oct 11, 2017 at 05:36 PM · shadershaderlabshader writingsurface shaderparsing error

Additive Blending Shader for Mobile - parse error

Hello!

I'm working on custom surface shader, which blending two textures into one additive image with transparency (alpha channel).
I have very low knowledge about shaders and I mostly improvise.
Generally I've done it by mixing some parts of Unity built-in shaders. And here comes the problem:

  1. When I'm using only CGPROGAM instructions without Pass, shader works in editor and it effects are visible in Game viewport, even in runtime. BUT, if I hit Play button on other scene, then switch it (in-game) to that with shader - shader aren't working anymore. Object is invisible.

  2. When I'm using CGPROGRAM instructions with Pass and SetTexture functions (CG instructions are outside Pass brackets) then it works. Unfortunately, in that case I lost blending, so this isn't what I want.

  3. When I'm put CGPROGRAM instructions into Pass brackets I receive error: Parse error: syntax error, unexpected TOK_PASS, expecting TOK_SETTEXTURE or '}' at line 38

I don't know what I'm doing wrong.
Below is my code.
Unity version: 2017.1.0f3
Target platform: Android


 Shader "Custom/AdditiveTransparentBlending"
 {
     Properties
     {
         _MainTex("Texture 1", 2D) = "white" {}
         _Texture2("Texture 2", 2D) = "white" {}
         _Blend("Blend", Range(0, 1)) = 0.5
     }
     
     SubShader
     {
         Pass
         {
             Tags
             {
                 "Queue" = "Transparent"
                 "IgnoreProjector" = "True"
                 "RenderType" = "Transparent"
             }
             Blend SrcAlpha One
             Cull Off
             Lighting Off
             ZWrite Off
             Fog{ Color(0,0,0,0) }
 
             BindChannels
             {
                 Bind "Color", color
                 Bind "Vertex", vertex
                 Bind "TexCoord", texcoord
             }
 
             CGPROGRAM
             #pragma target 2.0
             #include "UnityCG.cginc"
             //#pragma exclude_renderers flash
             #pragma surface surf Lambert vertex:vert noforwardadd keepalpha
 
             struct Input
             {
                 float2 uv_MainTex;
                 float2 uv_Texture2;
                 float4 color : Color;
             };
 
             float _Blend;
             sampler2D _MainTex;
             sampler2D _Texture2;
 
             void vert(inout appdata_full v, out Input o)
             {
                 UNITY_INITIALIZE_OUTPUT(Input,o);
                 o.color = v.color;
             }
 
             void surf(Input IN, inout SurfaceOutput o)
             {
                 fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
                 fixed4 c2 = tex2D(_Texture2, IN.uv_Texture2);
 
                 o.Albedo = (c.rgb * IN.color.rgb) * _Blend + (c2.rgb * IN.color.rgb) * (1 - _Blend);
                 o.Alpha = (c.a * IN.color.a) * _Blend + (c2.a * IN.color.a) * (1 - _Blend);
 
                 o.Albedo *= 2;
             }
 
             ENDCG
             /*
             Pass
             {
                 SetTexture[_MainTex]
                 SetTexture[_Texture2]
                 {
                     //combine texture * primary
                     ConstantColor(0,0,0,[_Blend])
                     Combine texture Lerp(constant) previous
                 }
             }*/
         }
     }
     //Fallback "Diffuse"
 }



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

  • Sort: 
avatar image
1
Best Answer

Answer by Gregor2211 · Oct 12, 2017 at 10:12 AM

Okay, I figured it out by changing approach. I've looked better into Unity documentation and code of built-in shaders, to reveal that I don't even need that whole CG part of code, because Legacy Texture Combiners are doing all the work. Earlier I didn't know that combiners can compute color and alpha separately (see documentation >> link).

Below is current code (now is short and clear).


 Shader "Custom/AdditiveBlending"
 {
     Properties
     {
         _MainTex("Texture 1", 2D) = "white" {}
         _Texture2("Texture 2", 2D) = "white" {}
         _Blend("Blend", Range(0, 1)) = 0.5
     }
     
     SubShader
     {
         Tags
         {
             "Queue" = "Transparent"
             "IgnoreProjector" = "True"
             "RenderType" = "Transparent"
         }
         Blend SrcAlpha One
         Cull Off
         Lighting Off
         ZWrite Off
         Fog{ Color(0,0,0,0) }
             
         BindChannels
         {
             Bind "Color", color
             Bind "Vertex", vertex
             Bind "TexCoord", texcoord
         }
         
         Pass
         {
             SetTexture[_MainTex]
             SetTexture[_Texture2]
             {
                 ConstantColor(0,0,0,[_Blend])
                 combine texture Lerp(constant) previous, texture Lerp(constant) previous
             }
         }
     }
 }
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

Follow this Question

Answers Answers and Comments

159 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

Related Questions

Custom Shader Giving Unexpected token error for period ' . ' 1 Answer

Operate the shader, rotate the material 90 degrees 0 Answers

[Unity 5 shader] Fake specular with surface shader, is it doable?,Fake Specular with surface shader, is it doable? 0 Answers

Fog not working in my own Vertext Shader 2 Answers

Translucent Transparent Diffuse Sprite Shader Edit 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