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
0
Question by Shchvova · Sep 17, 2011 at 03:26 AM · shadergradient

Shader gradient moving with object

Shader gradient in object coordinates

How do I make a shader which will draw gradient colour (turning darker and brighter). Also, I want it to be surface shader Here, what I got so far:

 Shader "Gradient Shader" {
     Properties {
         _Color ("Main Color", Color) = (1,.5,.5,1)
     }
     SubShader {
         Tags { "RenderType"="Opaque" }
         CGPROGRAM
         #pragma surface surf Lambert
         struct Input {
             float3 worldPos;
         };
 
         float4 _Color;
           void surf (Input IN, inout SurfaceOutput o) {
             float f = mul(_Object2World, float4(IN.worldPos,0))[2];
             o.Albedo = _Color.rgb*(0.7+ cos(f*100)*0.3 );
             o.Alpha = 1;
         }
         ENDCG
     } 
     FallBack "Diffuse"
 }

now, problem is gradient is staying in world when object is moving. I expect that _Object2World multiplication should fix it, but it don't.

How do I make it stick to an object?

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
Best Answer

Answer by Owen-Reynolds · Sep 17, 2011 at 03:18 PM

worldPos isn't "stuck" to the model, so can't be used to make an effect which is stuck to the model. The two things that are stuck to the model are uv_MainTex and the local vertex coords (the original ones, before the object ever moves.) The uv-coords tend to have odd patterns, so aren't very useful for gradient-type effects (for example, a leg might be uv-mapped upside-down, to make it fit better in the texture.)

Even though getting the original model coords is the single easiest thing to do, the surface shader doesn't provide them in Input(bottom of http://unity3d.com/support/documentation/Components/SL-SurfaceShaders.html) If you look at the bottom of the examples section, it says how to add a vertex shader to compute this for you (the very last paragraph says to use this trick.) I didn't see where appdata_full was defined, but the last two examples have enough info. To get model vertex data can use:

 struct Input {
   float3 pos; // unused name, to hold local, raw, vert pos
 };
 
 void vert (inout appdata_full v, out Input o)) {
       // send raw vert coords, unmodified by world, into pixel shader:
       o.pos = v.vertex.xyz;
   }

 // in surf:
 float f = o.pos.xy;  // or yz or xz

Not tested (but I have written a few surface shaders.)

I believe that Object2World has already been used. The built-in part takes the raw vert coords and mults by obj2world to know where to place the verts. It seems like you could use world2Object (if such a thing exists) or Object2world_invert(?) to reverse direction. But then you'd be doing lots of multiplying and dividing to get back the original numbers. Using a vert shader is more typing, but runs faster and can do more tricks.

Comment
Add comment · Show 1 · 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 Shchvova · Sep 18, 2011 at 03:13 AM 0
Share

I figured that out. Thanks! Here is my shader

   Shader "CoinShader" {
       Properties {
           _Color ("$$anonymous$$ain Color", Color) = (1,.5,.5,1)
       }
       SubShader {
           Tags { "RenderType"="Opaque" }
   
           
           CGPROGRA$$anonymous$$
           #pragma surface surf Lambert vertex:vert
   
           float4 _Color;
   
           struct Input {
               float coord;
           };
   
         void vert (inout appdata_full v, out Input o) {
             o.coord = v.vertex[2];
         }
   
   
           void surf (Input IN, inout SurfaceOutput o) {
               float f = IN.coord;//mul(_World2Object, float4(IN.worldPos,0))[2];
               o.Albedo = _Color.rgb*(0.7+ cos(f*100)*0.3 );
               o.Alpha = 1;
           }
           ENDCG
       } 
       FallBack "Diffuse"
   }

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Shader not working in OpenGL (Working in DirectX) 0 Answers

is there a way to make a gradient material, white to black, in Unity3D? 2 Answers

Getting horizontal gradient in unlit sprite shader even sprite is rotated 0 Answers

How to create a shader for a dynamic linear gradient 0 Answers

Newbie shader question for displacement/ gradient of a texture 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