Search Unity

What is this matrix!?

Discussion in 'Scripting' started by hpjohn, Feb 27, 2013.

  1. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    I need to know what the values of the "_Projector" matrix is.
    In the standard projectors assets there are 2 basic shaders for projectors (blob shadows), and also around the net eg http://en.wikibooks.org/wiki/Cg_Programming/Unity/Projectors
    They use the (aparently) built-in matrix
    uniform float4x4 _Projector;
    I really want to know what the values are, and how unity calculates them. Despite some advice from the IRC, I cannot fathom what it's values are.

    For example, the most obvious matrix would be something like

    Code (csharp):
    1. Projector P = GetComponent<Projector>();
    2. Matrix4x4 M = Matrix4x4.Perspective(P.fieldOfView, P.aspectRatio, P.nearClipPlane, P.farClipPlane);
    3. M *= transform.worldToLocalMatrix;
    4. P.material.SetMatrix("_ReplacementProjector", M);
    But that gives the second pic (First is what it's supposed to look like, using the builtin _Projector)

    So maybe its something fancy that cameras can do, so add a camera component and use

    Code (csharp):
    1. P.material.SetMatrix("_ReplacementProjector", camera.projectionMatrix);
    But that gives the third.

    So I'm proper stumped

     
    Last edited: Mar 27, 2013
    PawelGruntowski likes this.
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    The result is the same regardless of using shaderlab
    Code (csharp):
    1. Shader "Projector Shaderlab" {
    2.     Properties {
    3.         _ShadowTex ("Cookie", 2D) = "gray" { TexGen ObjectLinear }
    4.     }
    5.    
    6.     Subshader {
    7.         Pass {
    8.             Blend Zero SrcAlpha                  
    9.             Offset -1, -1
    10.             SetTexture [_ShadowTex] {
    11.                 combine texture, ONE - texture
    12.                 Matrix [_Projector]
    13.             }
    14.         }
    15.     }
    16. }
    Or cg

    Code (csharp):
    1. Shader "Projector cg shader test" {
    2.     Properties {
    3.         _ShadowTex ("Projected Image", 2D) = "white" {}
    4.     }
    5.     SubShader {
    6.         Pass {     
    7.             Blend Zero OneMinusSrcAlpha
    8.             CGPROGRAM
    9.             #pragma vertex vert  
    10.             #pragma fragment frag
    11.             #pragma debug
    12.             uniform sampler2D _ShadowTex;
    13.             uniform float4x4 _ReplacementProjector;
    14.            
    15.             struct vertexInput {
    16.                 float4 vertex : POSITION;
    17.                 float3 normal : NORMAL;
    18.             };
    19.            
    20.             struct vertexOutput {
    21.                 float4 pos : SV_POSITION;
    22.                 float4 posProj : TEXCOORD0;
    23.             };
    24.  
    25.             vertexOutput vert(vertexInput input) {
    26.                 vertexOutput output;
    27.                 output.posProj = mul(_ReplacementProjector, input.vertex);
    28.                 output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
    29.                 return output;
    30.             }
    31.            
    32.             float4 frag(vertexOutput input) : COLOR {
    33.                 return tex2D(_ShadowTex , float2(input.posProj) / input.posProj.w);
    34.             }
    35.            
    36.             ENDCG
    37.         }
    38.     }
    39. }
     
  3. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Today i tried implemeting the matrix construction according to this paper...
    http://csc.lsu.edu/~kooima/pdfs/gen-perspective.pdf
    ...and while the method is somewhat different, the result is still not appropriate to replace _Projector

    I would really appreciate input from somene in the know at UT, surely this is a simple question for the people that wrote this functionality?
     
  5. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Bump!
     
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    3-Week bump
    I've done some other stuff, come back to this problem, still can't get it to work.
    I get the impression that, although the shader only take a single 4x4 matrix, its actually calculating a different matrix for every GO that the projector shines on, meaning that I can't simply calculate a single projection matrix for the projector GO, which would suck. Alot

    The end result I'm trying to achieve is having the projectors project using custom matrices like this

    So althoguh the projector is non-rotated, pointing along the world Z, by controlling the matrix as directed in the paper linked above, I control where the projector shines.
     
  7. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    eh bump
    If it's impossible, someone could at least say so :(
     
  8. PawelGruntowski

    PawelGruntowski

    Joined:
    Dec 6, 2013
    Posts:
    7
  9. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    That's right. The projector matrix is function of the game object transform and the projector transform, therefore it is different for every GameObjects.
     
  10. PawelGruntowski

    PawelGruntowski

    Joined:
    Dec 6, 2013
    Posts:
    7
    Code (CSharp):
    1. Matrix4x4 P = Matrix4x4.Perspective (projector.fieldOfView, projector.aspectRatio, projector.nearClipPlane, projector.farClipPlane);
    2. Matrix4x4 V = Matrix4x4.TRS (new Vector3 (0, 0, 0), Quaternion.identity, new Vector3 (1, 1, -2)) * projector.transform.worldToLocalMatrix;
    3. projector.material.SetMatrix("_PVMatrix", P*V);
    and shader
    Code (CSharp):
    1.  
    2. Shader"Projector cg shader" {
    3. Properties {
    4. _ShadowTex ("Projected Image", 2D) = "white" {}
    5. }
    6. SubShader {
    7. Pass {
    8. Blend Zero OneMinusSrcAlpha
    9. CGPROGRAM
    10. #pragma vertex vert
    11. #pragma fragment frag
    12.  
    13. uniform sampler2D _ShadowTex;
    14. uniform float4x4 _PVMatrix;
    15.  
    16. struct vertexInput {
    17. float4 vertex : POSITION;
    18. float3 normal : NORMAL;
    19. };
    20.  
    21. struct vertexOutput {
    22. float4 pos : SV_POSITION;
    23. float4 posProj : TEXCOORD0;
    24. };
    25.  
    26. vertexOutput vert(vertexInput input) {
    27. vertexOutput output;
    28. output.posProj = mul(mul(_PVMatrix,_Object2World), input.vertex);
    29. output.posProj.xy = output.posProj.xy / output.posProj.w + 0.5;
    30. output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
    31. return output;
    32. }
    33.  
    34. float4 frag(vertexOutput input) : COLOR {
    35. return tex2D(_ShadowTex , input.posProj.xy);
    36. }
    37.  
    38. ENDCG
    39. }
    40. }
    41. }
    42.  
     
    Last edited: Apr 20, 2016