• 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 Hamza Hasan · Jan 16, 2016 at 04:26 PM · shadersshaderlab

Why I cannot assign fixed4 value in vertex method?

I'm old in Game Development but very new in shader writing. I just want a shader that transform from 1 texture to another. So I did start learning shader writing and wrote my desired shader. But during writing one thing I cannot understand as I am very dumb in shader writing that if I assign fixed4 color with COLOR semantic in vertex method then compiler gives the following error.

Shader error in 'Hamza/TwoTextures': cannot map expression to vs_4_0 instruction set at line 39 (on glcore)

Compiling Vertex program Platform defines: UNITY_ENABLE_REFLECTION_BUFFERS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING SHADER_API_DESKTOP UNITY_TEXTURE_ALPHASPLIT_ALLOWED

Here is the not working shader code,

 Shader "Hamza/TwoTextures"{
     Properties{
         [NoScaleOffset]
         _Texture1 ("Texture 1",2D) = "white"{}
 
         [NoScaleOffset]
         _Texture2 ("Texture 2",2D) = "white"{}
 
         _Mixer ("Mixer", Range(0,1)) = 0
     }
 
     SubShader{
         Pass{
             CGPROGRAM
 
             #pragma vertex vert
             #pragma fragment frag
 
             sampler2D     _Texture1;
             sampler2D     _Texture2;
             float         _Mixer;
 
             struct appdata{
                 fixed4 vertex     : POSITION;
                 fixed2 tex         : TEXCOORD0;
             };
 
             struct v2f{
                 fixed4 pos         : SV_POSITION;
                 fixed2 tex         : TEXCOORD0;
                 fixed4 col         : COLOR;
             };
 
             v2f vert(appdata i){
 
                 v2f o;
                 o.pos = mul(UNITY_MATRIX_MVP,i.vertex);
                 o.tex = i.tex;
 
                 // Giving error on this line
                 o.col = (tex2D(_Texture1,i.tex) * _Mixer) + (tex2D(_Texture2,i.tex) * (1 - _Mixer));
                 return o;
             }
 
             fixed4 frag(v2f v) : COLOR
             {
                 return v.col;
             }
 
             ENDCG
         }
     }
 }

And if I write same expression to throw it as return fixed4 value in fragment method then everything is working fine.

Here is working shader code,

 Shader "Hamza/TwoTextures"{
         Properties{
         [NoScaleOffset]
         _Texture1 ("Texture 1",2D) = "white"{}
     [NoScaleOffset]
     _Texture2 ("Texture 2",2D) = "white"{}

     _Mixer ("Mixer", Range(0,1)) = 0
 }

 SubShader{
     Pass{
         CGPROGRAM

         #pragma vertex vert
         #pragma fragment frag

         sampler2D     _Texture1;
         sampler2D     _Texture2;
         float         _Mixer;

         struct appdata{
             fixed4 vertex     : POSITION;
             fixed2 tex         : TEXCOORD0;
         };

         struct v2f{
             fixed4 pos         : SV_POSITION;
             fixed2 tex         : TEXCOORD0;
         };

         v2f vert(appdata i){

             v2f o;
             o.pos = mul(UNITY_MATRIX_MVP,i.vertex);
             o.tex = i.tex;
             return o;
         }

         fixed4 frag(v2f i) : COLOR
         {
             // It is giving me no error
             return (tex2D(_Texture1,i.tex) * _Mixer) + (tex2D(_Texture2,i.tex) * (1 - _Mixer));
         }

         ENDCG
     }
 }
 }

Can anyone explain me that why it is happening? And what is the difference between them?

As I mentioned that I did start learning only two days before. I'm sort of noob in this.

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 _dns_ · Jan 16, 2016 at 06:03 PM

I think it's because you are using tex2D in a vertex shader (only works in pixel/surface shader). You should use tex2Dlod. Here is a post to explain that : http://forum.unity3d.com/threads/how-to-use-vertex-texture-fetch.63231/ . Note that you should also have to specify a "#pragma target 3.0" as this only works on shader model 3+

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

_MainTex_ST Redefinition error 1 Answer

Scene Depth Node, find depth value from x/y position of camera frame 1 Answer

Weird shader error X5204 - read of uninitialized components 1 Answer

Uniform variable operations not for every vertex and fragment. 0 Answers

Why does this code work without any "Pass{}" expressions? And is there a way to add shadow collections? 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