Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Question (Video) Can't figure out how to get World Position on a custom version of URP/2D/Sprite-Lit-Default

Discussion in '2D Experimental Preview' started by Jamez0r, Feb 22, 2021.

  1. Jamez0r

    Jamez0r

    Joined:
    Jul 29, 2019
    Posts:
    190
    Is there a special way to get "world space" in a customized version of the URP/2D/Sprite-Lit-Default shader?

    I'm converting a shader I made that wasn't lit (just a basic vert/frag shader) over to a "lit" version by starting with the URP/2D/Sprite-Lit-Default shader and just adding/changing things as needed. Everything seems to be working fine except that my world-space position doesn't seem correct.

    This is how I got world space in my original shader (in the vertex function):

    Code (CSharp):
    1. OUT.worldPos = mul(unity_ObjectToWorld, IN.vertex);
    And this is what I have in the new customized 2D/sprite-lit-default one:

    Code (CSharp):
    1. o.worldPos = mul(unity_ObjectToWorld, v.positionOS);
    The result with the new shader is almost as though the world position is 'moving along with the camera'. Recorded a video explaining whats going on. The issue is at the 1:00 mark.



    My fragment code is exactly the same for both. I'm 99.9% the issue I'm seeing is due to this world position calculation. Maybe something is different with the 2D Renderer ? Or "v.positionOS" isn't the same thing as what would be "IN.vertex" (from vertex functions that take appdata_t)?

    @castor76 I saw you mention in another thread that you are also doing reflections, did you happen to run into this same issue?

    Thanks for any help!!
     
  2. Armadous

    Armadous

    Joined:
    Apr 6, 2013
    Posts:
    4
    Have you seen float3 TransformObjectToWorld(float3 positionOS) in the core package? There is a whole slew of space transforms SpaceTransforms.hlsl used all over the place by the sprite lit package. Worth trying?
     
  3. Jamez0r

    Jamez0r

    Joined:
    Jul 29, 2019
    Posts:
    190
    @Armadous Thanks, you're a lifesaver - the TransformObjectToWorld(v.positionOS) works great.

    I guess the "positionOS" (probably stands for "Position - Object Space"?) is different than the IN.vertex value that I was using on the old shader.

    Thanks again for the help!