Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

unity_ObjectToWorld does not contain translation?

Discussion in 'Shaders' started by fredlllll, Jan 11, 2019.

  1. fredlllll


    Nov 20, 2014
    Me again,

    now this might only be me being blind again, but i have the suspicion that unity_ObjectToWorld does not contain any translation information.

    so here is a minimal shader to test that: (also great that the forum does not have a codetag for shaders)
    Code (csharp):
    1. Shader "Custom/TranslationTest"
    2. {
    3.     Properties
    4.     {
    5.     }
    6.     SubShader
    7.     {
    8.         Tags { "RenderType"="Opaque" }
    9.         LOD 200
    11.         CGPROGRAM
    12.         #pragma surface surf Standard fullforwardshadows
    14.         struct Input
    15.         {
    16.             float3 worldPos;
    17.         };
    19.         void surf (Input IN, inout SurfaceOutputStandard o)
    20.         {
    21.             o.Albedo = mul(unity_ObjectToWorld, float3(0.1, 0.1, 0.1));
    22.         }
    23.         ENDCG
    24.     }
    25.     FallBack "Diffuse"
    26. }
    so you see all i do is transform a small vector with the worldmatrix. i would expect the translation to also come to play here, but nope, results look like this

    the two colored ones have scale 2 on x and y, so that channel gets scaled, rotation also gets applied (color changes with rotation when scale is applied). but with the front cube u see that the x part of the vector does not get translated (or any other axis for that matter). now what am i doing wrong? where can i get the translation info? im running 2018.3.0f2
  2. bgolus


    Dec 7, 2012
    That's because unity_ObjectToWorld is a 4x4 matrix, and you're multiplying a float3 which ignores the position. You're only seeing scale and rotation have any effect because that's all you're extracting from it.

    o.Albedo = mul(unity_ObjectToWorld, float4(0.1, 0.1, 0.1, 1.0)).xyz;

    Note you may still not see any color change if you view this in the game view. That's because of batching.
  3. fredlllll


    Nov 20, 2014
    thanks it worked, i knew it had to be a small detail.