Search Unity

Shader On Entire Object Instead of Faces

Discussion in 'Shaders' started by Cronnoc, Jul 15, 2019.

  1. Cronnoc

    Cronnoc

    Joined:
    Jul 6, 2018
    Posts:
    2
    So this is probably a really noob question, and it has probably been answered before, but to be honest I simply didn't know what to google for this.

    So, I'm really new to shaders and I've just made a basic colour change shader, but it is not doing what I wanted it to do. What I want is for the shader to start at the bottom of the object, then uniformly move up the cube on all sides to the top. Unfortunately, it is working as though each face of the cube is a separate object.

    Shader on all faces.gif
    As I said, this is probably a really common question, I just didn't know what to look up. Thank you for your time.
     

    Attached Files:

  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    6,994
    If you can, post your shader code (using code tags) and we'll be able to help out more.

    But the main thing is you're probably using a Surface Shader and driving the color "edge" using the IN.uv_MainTex or similar value. A mesh's UVs, or texture coordinates, are used to map a 2D space onto the 3D surface of the mesh, but there's no guaranteed correlation between the mesh's position in the UV as the UV coordinate can be completely arbitrary. If you want to make across a mesh's 3D space then you need to be working with 3D coordinates. The easiest to access when using a Surface Shader is the world position, which Unity will provide to the surf function if you add float3 worldPos; to the input struct. However that gets you the world position of each pixel that is rendered, but not the relative position within the mesh. So if you use the world position you'd need to transform the world position back into object space (aka model space, or the untransformed mesh space), or manually set a lot of data on the material to define where in world space the start and end positions for the transition are. Alternatively you could use a custom vertex function and input struct value to pass the original object space vertex positions, which will result in the surf function getting the object space position in that struct value.
    Code (csharp):
    1. struct Input
    2. {
    3.     // ... other stuff
    4.     float3 worldPos; // special variable name the shader auto fills
    5. };
    6.  
    7. void surf (Input IN, inout SurfaceOutputStandard o)
    8. {
    9.     float3 objectSpacePos = mul(unity_WorldToObject, float4(IN.worldPos, 1.0));
    10.     ...
    Code (csharp):
    1. #pragma surface surf Standard vertex:vert
    2.  
    3. struct Input
    4. {
    5.     // ... other stuff
    6.     float3 objectPos; // custom value
    7. };
    8.  
    9. void vert (inout appdata_full v, out Input o)
    10. {
    11.     o.objectPos = v.vertex.xyz; // accessible as IN.objectPos in the surf function
    12. }
    However, be wary that even if you do have object space positions, that doesn't mean you have a nice 0.0 to 1.0 value you can drive from. UVs tend to be in that nice range just because texcoords are a normalized range where 0.0, 0.0 is the bottom left corner of the texture and 1.0, 1.0 is the top right. Mesh vertex positions are also arbitrarily positioned relative to the mesh's pivot. Additionally at no stage do shaders ever have access to the mesh's full vertex data, generally only 1, or in special cases 3 vertices at a time. This means the shader doesn't know the extents of the mesh. i.e.: it doesn't know where the top and the bottom of the mesh are. So you still need to set that manually on the mesh itself, or use meshes that fit to the range you want your shader to use. But at least multiple copies of the same mesh won't need separate world space coordinates.


    One last gotcha. Unity uses mesh batching by default. Game objects marked as static will use static batching, and multiple simple meshes using the same material may get dynamically batched. What this means is Unity combines multiple meshes into one single mesh to make them faster to render. This also pre-applies their object to world transforms so as far as the shader is concerned the "object space" position and "world space" position are identical for batched meshes. This messes with everything I've written above. One option is to use unique materials on each object, or to disable batching for the project, or on the shader itself using the "DisableBatching"="True" tag. However there's one other option that skips all of the above, and doesn't require any manual material property setting at all. Use UVs.

    This might seem counter intuitive consider at the start of this I said UVs were your problem, but you're using the UVs on an existing mesh which has UVs setup to be used with a texture. I also said UVs are arbitrary. So the trick is instead to use custom meshes with an additional UV set that simply starts at the bottom of your mesh and ends at the top (or vice versa, or whatever arbitrary orientation you want). Then in the shader you use that uv instead of the main UV or the vertex position. For UVs not tied to a texture property, Surface shaders will get very upset, so you'll still need to use a custom Input value and vertex function.
    Code (csharp):
    1. struct Input
    2. {
    3.     // ... other stuff
    4.     float myValue;
    5. };
    6.  
    7. void vert(inout appdata_full v, out Input o)
    8. {
    9.     o.myValue = v.texcoord1.x; // texcoord is zero based, so texcoord1 == the second UV set
    10. }
    Know that the second UV set is used for lightmap coordinates by Unity's shaders, so if this is a static game object you'll want to use the third (also used for dynamic light mapping) or fourth (used by nothing built in) UV set. The mesh UVs are also technically a float4 value that Unity defaults to only using the first two components of, so you could use the third component to store your value if you do it from a script. Unity will not import anything but the first two components from meshes read from .fbx or .blend files.
     
  3. Cronnoc

    Cronnoc

    Joined:
    Jul 6, 2018
    Posts:
    2
    Thank you for your very detailed response. Your answer did provide me with everything I was after and I was able to successfully adapt the code to my project.