Search Unity

Address only certain vertices of an object

Discussion in 'Shaders' started by Mikehere, Jan 14, 2020.

  1. Mikehere


    Nov 27, 2015
    Hello everyone,
    I have been struggeling with a problem for a while now even though I can't imagine that it's too complicated.

    I have an object (let's say it's a cube) and I want to address only the right vertices of that object. So if we have an object where the middle (in object coordinates, where the bottom left corner would be 0,0,0 and the right top corner would be 1,1,1) is at 0.5,0.5,0.5, I want to adress all vertices with x > 0.5 .

    I attached a picture to clarify what I mean. How can I do that? I would like to change those vertex positions or their color. I appreciate any kind of input.

    Best regards

    Attached Files:

  2. Invertex


    Nov 7, 2013
    Object space vertex positions go both negative and positive relative to the object's pivot point. So, if you've made sure your mesh has its pivot point modeled so it's evenly between left/right, then you can ensure you only affect the right side vertices by checking
    if(v.vertex.x > 0) { //do stuff }
    in your vertex shader.

    As for knowing if it's top right corner or not... Well, the vertex positions aren't constrained to -1/1. So unless you know beforehand that your mesh is 2 world space unity wide, then 1,1 will not be the top right of the mesh. If your pivot is centered, then you could feed the .bounds.extents of the object's Renderer component into a property in your shader and use that to divide the vertex position value with to get a 0-1 range value for the right side no matter the modeled size of the object though.
    Last edited: Jan 14, 2020