Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by cclaypool1 · May 15, 2013 at 04:50 AM · shadercolorheightglsldisplacement

Shader that changes color based on displacement from radius (height shader for deformed sphere).

I have create a GLSL shader that is supposed to find the vertex coordinates of a deformed sphere and compare that to the original radius to create a displacement value that can then be used to alter the color of a point based on its displacement from the normal. For some reason, in the following shader, displacement is always equal to zero and the object is white along all points of varying displacement. Is there anything that I am doing wrong and can fix to achieve the desired effect? Is it possible that I need to use Unity's coordinate system for the radius as the object is first created as a 0.5^3 cube and then normalized to make a sphere then scaled to the radius? (The Radius value is taken from the object by using renderer.material.SetFloat("_radius", radius) but can be changed manually.)

 Shader "Custom/SphereShader" {
 
 
     Properties
     {
     
         _radius("Radius", Float) = 50
         
     }
         
    SubShader {
       Pass {
          GLSLPROGRAM
  
          #ifdef VERTEX
   
          varying vec3 position;
          varying float displacement;
          uniform float _radius;
         varying float radius;
          varying float _length;
          varying vec4 _color;
          
             
  
          void main()
          {
              radius = _radius;
              position = gl_Vertex;
              _length = length(position);
              displacement = ((_length - radius)/radius);
              _color = ((1.0 - displacement), (displacement + 1.0), 1.0, 1.0);
             gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
             
          }
  
          #endif
  
          #ifdef FRAGMENT
  
          varying vec4 _color;
  
          void main()
          {
              //RGBA
             gl_FragColor = _color;
 
          }
  
          #endif
  
          ENDGLSL
       }
    }
 }
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 comomomo · Nov 27, 2015 at 10:14 AM 0
Share

Hi, did you ever figure this out? @cclaypool1

I'm currently trying to do something really similar and I'm having trouble finding reliable methods.

avatar image cclaypool1 comomomo · Nov 28, 2015 at 05:41 AM 0
Share

Yeah I eventually figured it out. This was quite a while ago and I can't remember what I was struggling with in this shader but I basically made a bunch of if statements in the frag section that if the displacement value from the normalized point was within a certain range it would return a certain color.

avatar image comomomo cclaypool1 · Nov 28, 2015 at 09:46 PM 0
Share

Nice! I was having trouble getting to the normalized point in the shader and ended up writing vertex coloring into my program (using distance from the original vertex to the displaced vertex) working from here: http://catlikecoding.com/unity/tutorials/noise-derivatives/

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

14 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

Related Questions

Material doesn't have a color property '_Color' 4 Answers

Cg Language incompatibility Desktop VS GLSL Android 0 Answers

vertex displacement shader and ssao 0 Answers

Changing Eye Colour (Colour only non-white parts of a texture?) 2 Answers

Color white does nothing consider mobile... really? 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