• 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 Jiexi · Oct 04, 2013 at 09:14 AM · shadertexturevertex

Texture based on vertex height

Hi there,

I have been toying with shaders for a couple of weeks now and feel like i'm making no progress, every step forward gives 2 steps back.

I can put 2 textures in my shader without any issues, i can blend them together by doing:

 half4 c = tex2D(_MainTex, IN.uv_MainTex);
 half4 d = tex2D(_SecTex, IN.uv_SecTex);
 o.Albedo = c.rgb * d.rgb;

But now i'd like to select the texture based on the vertex height. Any way to achieve this?

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 Hoeloe · Oct 04, 2013 at 09:31 AM 0
Share

You might be able to do it by adding a variable in your Input struct, and writing the y coordinate of the vertex as part of a vertex shader. Then you can access the y coordinate in the fragment shader, for use with blending.

avatar image Jiexi · Oct 04, 2013 at 09:44 AM 0
Share

Hi, thanks for the quick reply, I am trying that as we speak, the only thing is that i'm not sure how the syntax should be, and before i had issues that it wasn't initialized.

but this is how i'm trying to achieve that:

 struct Input {
             float4 pos : POSITION;
             float2 uv_$$anonymous$$ainTex : TEXCOORD0 ;
             float2 uv_SecTex : TEXCOORD1 ;
         };
         
         Input vert (inout appdata_full v)
         {
             Input o;
             o.pos = round(mul(_Object2World, v.vertex) );
             return o;
         }
 
         void surf (Input i, inout SurfaceOutput o) {
             half4 c = tex2D (_$$anonymous$$ainTex, i.uv_$$anonymous$$ainTex);
             half4 d = tex2D (_SecTex, i.uv_SecTex);
             if(i.pos.y > 1.0f)
             {
                 o.Albedo = c.rgb;
             }
             else
             {
                 o.Albedo = d.rgb;
             }
             o.Alpha = c.a;
         }

but this is giving: variable/member "pos" has semantic "POSITION0" which is not visible in this profile

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Oct 04, 2013 at 09:44 AM

You just need to calculate a Lerp factor to blend the two textures together. You should define a blending area. For example everything below the height of 20 should take the first texture, above the second one. In addition to the 20 you define a blendAreaSize of 2.5 for example.

The Lerp value would be something like

 L = clamp((pos.y- blend height - blendAreaSize*0.5) / blendAreaSize)

I'm writing on my tablet, so I can't tell got the exact syntax, but this should do the trick.

L will be a value that is 0 for y values below 18.75 and 1 for everything above 21.25.

In between it would linearly blend from 0 to 1. Now you just need to use Lerp with your two color values

Comment
Add comment · Show 4 · 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 Bunny83 · Oct 04, 2013 at 09:48 AM 0
Share

BTW since it seems you write a surface shader you might want to look at this:

http://docs.unity3d.com/Documentation/Components/SL-SurfaceShaders.html

avatar image Bunny83 · Oct 04, 2013 at 09:55 AM 0
Share

Just in case you missed the example page:

http://docs.unity3d.com/Documentation/Components/SL-SurfaceShaderExamples.html

avatar image Jiexi · Oct 04, 2013 at 10:48 AM 0
Share

I keep getting the error: variable/member "pos" has semantic "POSITION0" which is not visible in this profile I'm not sure what this means or how to solve it.

Oh just like my parser, i'm hopelessly lost.

avatar image Bunny83 · Oct 05, 2013 at 02:55 AM 0
Share

Have you took a look at the surface shader example "Slices via World Space Position" ?

It also uses the world space position

   struct Input {
       // ...
       float3 worldPos;
       // ...
   };

Again, surface shaders works differently from normal vertex / fragment shaders. You should not use semantics in your input structure, unity will deter$$anonymous$$e them itself based on the var name. Again, see Surface Shader input structure

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

17 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

Related Questions

Reveal texture at position 0 Answers

Simple mesh deformation : Mesh.vertices or vertex shader with displacement texture ? 1 Answer

Vertex Texture Lookup Breaking in Unity 3.5 0 Answers

Curved + Diffuse Shader??? 0 Answers

Cutout Material is not showing texture transparency? 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