Search Unity

Question Problems importing geometry shader to new project.

Discussion in 'Shaders' started by ColePlusPlus, Jan 7, 2023.

  1. ColePlusPlus

    ColePlusPlus

    Joined:
    Feb 16, 2016
    Posts:
    7
    Hello, I've created a geometry shader for grass rendering which worked perfectly up until I imported it to a different project. No code was changed but now the grass offsets itself when I move the object its on. If the object is at world coordinates (0, 0, 0) with a scale of (1, 1, 1), the grass is in the correct place but as soon as the object is moved or scaled, the grass moves and scales around 2x or 3x what it should.
    Here's when the plane is at (0, 0, 0):
    2.PNG
    And when I translate the plane up:
    1.PNG

    I have no idea what could be causing this. All I did was create a new shader and material and copy the source code from the old shader into the new one. Any suggestions would be great! :)
    Thank you!
    I'm in URP btw.
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,043
    Share the shader.
    If you set the position of the grass in the shader, note that by default shaders are object space. So you don't need to change the position of vertexes in the shader with the position of the object
     
  3. ColePlusPlus

    ColePlusPlus

    Joined:
    Feb 16, 2016
    Posts:
    7
    I think I solved it. I believe I was using TransformObjectToWorld incorrectly in the geometry vertex shader.

    Before I was setting the vertex pos like this:

    Code (CSharp):
    1.  
    2.       OUT.vertex = float4(TransformObjectToWorld(IN.vertex), 1.0f);
    3.  
    And I changed it to:
    Code (CSharp):
    1.  
    2.       OUT.vertex = IN.vertex;
    3.  
    I understand why it wasnt working now but I still dont understand why it worked in the previous project. Oh well.
     
    Last edited: Jan 9, 2023
  4. ColePlusPlus

    ColePlusPlus

    Joined:
    Feb 16, 2016
    Posts:
    7
    Actually I was wrong. This did solve the positions, but the geometry shader geometry still scales with the object.