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
3
Question by DaveA · Aug 25, 2011 at 03:57 PM · shaderfunctionruntimeheightmapdisplacement

Using displacementmap on-the-fly (shader or function)?

Anyone know offhand any shader that takes a displacement map? Seems like it wouldn't be hard for a shader to derive a normal map from that, or whatever other map(s) it needs.

Short of that, anyone have a handy function for converting height maps to normal maps? I know this is done by the texture importer, but I need this for run-time. Thanks.

Comment
Add comment · Show 15
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 Waz · Aug 25, 2011 at 04:14 PM 0
Share

All the parallax shaders take a displacement map, but that's not really your question. You really need the second thing.

avatar image Waz · Aug 26, 2011 at 01:26 AM 1
Share

Come on DaveA, pony up, answer your own question for the greater good now... :)

avatar image Joshua · Aug 26, 2011 at 08:51 PM 1
Share

Are you really going to make me write it even though you already know the solution? (for each pixel take delta of the two next and above it. Construct a vector orthogonal to both. If no two next, take the one next to it and the delta with himself)

Don't be lazy, you do it! :p

avatar image Peter G · Aug 27, 2011 at 12:04 PM 2
Share

Ok, I've thought about it, and I still think its a bad idea to do in a shader, but I have an idea. Firstly, let me give a quick explanation of vertex and fragment shaders. The vertex shader is called per vertex on the object. Generally, this positions the vertex based on its position and rotation relative to the cameras position, rotation, and view frustum.

   //that's this line:
   output.vertex = $$anonymous$$ul(input.vertex, UNITY_$$anonymous$$ATRIX_$$anonymous$$VP);

You also do per-vertex lighting, and anything else that you can do with vertex information such as vertex colors or extrusion along normals.

The fragment/pixel shader is basically per fragment. Slightly simplified, that's approximately per visible pixel. Texturing and per pixel lighting is done in the fragment shader. Normal mapping is also done here as well as texture combining such as splat map. Values sent from the vertex shader to the pixel shader are interpolated based on the closeness to a given vertex.

It sounds like it will be too expensive if you run it that many times. Now, let me get to my idea. I think Joshua and Warwick have been hinting at this. Have you tried a render texture. You could copy the source texture to a render texture through Graphics.Blit() with a material that converts your height map to a normal map. This would let you put it outside of a shader and hence controlling how frequently its called. But, this is only relevant once you write the shader:

  var heightmap : Texture2D;
  var normal$$anonymous$$ap : RenderTexture;
  var converter$$anonymous$$at : $$anonymous$$aterial;
  //This material has a shader that converts the height map to a normal map;

  function Update () [
       if(Application.frameCount % 15 == 0) {
       //only update the normal map every 15 frames.
              Graphics.Blit(height$$anonymous$$ap, normal$$anonymous$$ap, converter$$anonymous$$at);
       }
  }
avatar image Waz · Aug 27, 2011 at 12:15 PM 1
Share

Yes, what Peter G says. (I call such things filter shaders, no idea where I got that name from, it's how you do "SetPixel" stuff but in realtime, and it's how the Image Effects like blur, SSAO, FXAA3, etc. are implemented; technically it's the same as any other rendering, it just happens to render a quad over the whole output and the shader of the material of that quad just happens to do something interesting with it's input texture and parameters).

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

8 People are following this question.

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

Related Questions

Tessellation/displacement is transparent, shouldn't be 2 Answers

Dynamically animating mesh according to heightmap image sequence 1 Answer

How to make trees in a terrain transparent in runtime? 0 Answers

Pass data from "v2f vert(appdata_vert v){}" to "float4 frag(v2f i){}" 0 Answers

A way to move Texture's offset in runtime? 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