Search code examples
unity-game-engineshadercg

Unity/CG: Scale object in vertex shader


I'm trying to write a vertex shader that scales the size of the object. I've found many "solutions" to this problem, all to the tune of "expanding vertices along the normals" like so:

v2f vert(appdata_base v)
{
    v2f OUT;
    float3 norm = normalize(v.normal); //Unity 5 fix

    v.vertex.xyz += norm * _Scaling;
    OUT.pos = mul(UNITY_MATRIX_MVP, v.vertex);

    OUT.normals = v.normal;
    return OUT;
}

However, this does not work for objects with sharp edges, such as cubes. In the case of a cube it just makes the sides move directly out but the sides stay the same size thus you get an "exploded" cube:

enter image description here

Can someone tell me what I might be missing to get any object to scale properly in the shader?


Solution

  • you have the center, you also have the verticies position vector relative to that. You can find the magnitude of that and then scale that.