• 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 The-Oddler · Aug 11, 2012 at 03:25 PM · vertexshaderlab

Shader height lines

Hi,

I'm trying to make a shader that shows height lines (like on a map.) Something like this: alt text

I can't seem to get the world-height of my vertex right though. Here's my shader:

     Shader "Custom/Height"
 {
  Properties 
  {
  _Color ("Color", Color) = (0.5, 0.5, 0.5, 0.5)
  _Step ("Step", Float) = 50.0
  }
  SubShader
  {
  //Tags {"Queue" = "Opaque"}
  ZWrite On
         
         //GrabPass { }
  Pass
  {
  Fog { Mode Off }
  Blend SrcAlpha OneMinusSrcAlpha
 
  CGPROGRAM
 // Upgrade NOTE: excluded shader from Xbox360; has structs without semantics (struct v2f members position)
 #pragma exclude_renderers xbox360
 
  #pragma vertex vert
  #pragma fragment frag
  #include "UnityCG.cginc"
  
  fixed4 _Color;
  float _Step;
 
  struct appdata
  {
  float4 vertex : POSITION;
  };
  struct v2f
  {
  float4 pos : SV_POSITION;
  float4 uv : TEXCOORD0;
  float3 worldPos;
  };
  
  v2f vert (appdata v)
  {
  v2f o;
  o.worldPos = mul(_Object2World, v.vertex).xyz;
  o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  o.uv = o.pos;
  return o;
  }
  half4 frag(v2f i) : COLOR
  {
  return _Color * fixed4(
  fixed3(
  1.0 - pow(
  (float)((int)i.worldPos.y % (int)_Step) / _Step,
  4)
  ),
  1);
  }
  ENDCG
  }
  }
 }

And this show my lines, like I want it to. Though when I rotate my camera the lines change :s

Side view, here the lines are like I want them:

alt text

And a top view, as you can see the lines moved:

alt text

Anyone any idea what's wrong? I use the worldPos to calculate the colour, so this one might be wrong? I don't know.

Ps. I'm still very new to this shader stuff, so extra info is always welcome ;)

Thanks a lot!

-Pablo

Comment
Add comment · Show 3
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 The-Oddler · Aug 11, 2012 at 03:28 PM 0
Share

Srr for the badly formatted code, can't get it to show better :s In the preview it looks good, then when I save the edit it's all different...

avatar image Owen-Reynolds · Aug 11, 2012 at 04:22 PM 0
Share

The stripes seem to be going off of screen y. Looks like Object2World is giving screenPos, ins$$anonymous$$d of worldPos.

A test might be to skip it (it accounts for rotations and movements, and your terrain does neither.) Just: o.worldPos=v.vertex; and see if that manages to keep the stripes on world y.

avatar image The-Oddler · Aug 11, 2012 at 05:23 PM 0
Share

That gives the same result :s Also tried changing the screen size, and that indeed seems to affect the stripes too. So you might be right that it is the screen y it's using. Though why? :P

I looked around some more, and I found that maybe the _Object2World is only available in unity pro. $$anonymous$$aybe you have any idea how to do the same thing in indie? Without using _Object2World?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by The-Oddler · Aug 11, 2012 at 06:01 PM

Ok, I seem to have fixed the problem. I'm still not entirely sure why yet though.

What I did is take a different shader as a base, and change it to fit my needs. Here's the final shader:

 Shader "Custom/Height"
 {
  Properties 
  {
  _Color ("Color", Color) = (0.5, 0.5, 0.5, 0.5)
  _Step ("Step", Float) = 50.0
  }
 
  SubShader
  {
      Pass
      {
  CGPROGRAM
 // Upgrade NOTE: excluded shader from Xbox360; has structs without semantics (struct v2f members worldPos)
 #pragma exclude_renderers xbox360
  #pragma vertex vert
  #pragma fragment frag
  #include "UnityCG.cginc"
  
  fixed4 _Color;
  float _Step;
  
  struct v2f
  {
      float4 pos : SV_POSITION;
      float3 worldPos;
  };
  
  v2f vert (appdata_base v)
  {
      v2f o;
      o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
      o.worldPos = mul(_Object2World, v.vertex).xyz;
      return o;
  }
  
  half4 frag (v2f i) : COLOR
  {
      return _Color * fixed4(
  fixed3(
  1.0 - pow(
  (float)((int)i.worldPos.y % (int)_Step) / _Step,
  2)
  ),
  1);
  }
  ENDCG
      }
  }
 }
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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Implementing specular in a shader on iOS 1 Answer

How to move vertices up/down randomly in shader. 1 Answer

Why doesn't this vertex+fragment shader work? 0 Answers

Having trouble implementing edge collapse operation with half-edge data structure 0 Answers

Combining Textures and Shaders 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