Search Unity

Using pre-transformed verticies

Discussion in 'Shaders' started by Mortom, Oct 30, 2014.

  1. Mortom

    Mortom

    Joined:
    Aug 21, 2014
    Posts:
    20
    I'm trying to render an object using it's uv's as coordinates. I want the object to appears as a 2D overlay.

    I can make the object render, however it is a flat, unwrapped object, but still in 3D space around the origin.

    I've tried writing the position out to clip space but nothing renders. I'm also wanting to do this for pre-transformed vertices, so I store a pre-transformed vertex in clip space [-1,-1] to [1,1] and just pass this through the vert program. However I just can't make it rendering anything.

    Code (CSharp):
    1. Shader "Custom/Flat"
    2. {
    3.     Properties
    4.     {
    5.     }
    6.  
    7.     SubShader
    8.     {
    9.         Tags { "RenderType"="Opaque" }
    10.  
    11.         Pass
    12.         {
    13.             CGPROGRAM
    14.  
    15.             #pragma vertex vert
    16.             #pragma fragment frag
    17.             #include "UnityCG.cginc"
    18.  
    19.             struct v2f
    20.             {
    21.                 float4 pos : SV_POSITION;
    22.             };
    23.  
    24.             v2f vert (appdata_base v)
    25.             {
    26.                 v2f o;
    27.              
    28.                 float4 pos;
    29.              
    30.                 pos = float4(v.texcoord.x,v.texcoord.y, 0.0, 1.0);
    31.              
    32.                 o.pos = pos;
    33.                 //o.pos = mul (UNITY_MATRIX_MVP, pos);
    34.  
    35.                 return o;
    36.             }
    37.  
    38.             half4 frag (v2f i) : COLOR
    39.             {
    40.                 return half4(0.0,0.0,1.0,1.0);
    41.             }
    42.             ENDCG
    43.  
    44.         }
    45.     }
    46.     FallBack "Diffuse"
    47. }
    48.  
    If I comment in the matrix multiple line UNITY_MATRIX_MVP, then it will render a sphere object as a flat grid, but still in 3D as a flat plane. I want to output a clip space, or screen space position, so it acts as an overlay.

    I kind of expected it to appear in one of quadrants of the screen as the uv's go from 0 to 1. I'll obviously need to do some additional math to convert it into clip space, but I can't even get a single triangle to appear yet.

    I suspect I have the Z or W component wrongly set but I've tried a load of combination and still can't make it work.

    How do I go about this in Unity? What space should the vertex shader been outputing?
     
    Last edited: Oct 30, 2014
  2. Mortom

    Mortom

    Joined:
    Aug 21, 2014
    Posts:
    20
    Never mind...

    It does work. It just doesn't display in the Editor window. With the UNITY_MATRIX_MVP line added in the Scene window does show the flattened objected, just in 3D space at the origin. Without it, the editor doesn't draw anything for the sphere.

    However if I select the Camera, then the Camera Preview window does show the flattened object as I would expect, drawn in the top right quadrant of the screen. This also obviously works in the Game window.

    I would still however be interested to know why the Scene window doesn't work? How does Unity prevent the shader from rendering to the screen in the Scene view. That is odd.