Vertex Texture Fetch in Unity Free?

Discussion in 'ShaderLab' started by imaginaryhuman, Mar 2, 2012.

  1. imaginaryhuman

    imaginaryhuman

    Member

    Joined:
    Mar 21, 2010
    Messages:
    3,470
    I would like to do a vertex texture fetch in the vertex shader, but I am using Unity Free, not Pro. Is there ANY way to get it to read ANY kind of texture in ANY format, within Unity Free, or does it require a floating-point render texture only available in Unity Pro?
  2. Farfarer

    Farfarer

    Member

    Joined:
    Aug 17, 2010
    Messages:
    2,161
    You could technically bake a texture to the vertex colours of a mesh?

    I'm not sure you can sample textures in the vertex shader in any version of Unity...
  3. Dreamora

    Dreamora

    Member

    Joined:
    Apr 5, 2008
    Messages:
    26,586
    Yes VTF works, it does not require any render textures. But if I remember right, you can't use surface shaders, you must use standard cg fragment and vertex functions.

    Question naturally is what you want to fetch, if its a RT or something calculated in realtime then Pro is required.

    Also, there is no difference in texture formats available in free or pro. they are at maximum ARGB32 on both ends, there is no A32R32G32B32 support or anything alike in Unity (and as long as OSX is not cut from the support thats not gonna change till 10.6 support is phased out as you simply can't work with them there), its limited to http://unity3d.com/support/documentation/ScriptReference/TextureFormat.html
  4. Jessy

    Jessy

    Member

    Joined:
    Jun 7, 2007
    Messages:
    7,173
    Works fine in GLSL. Which is kind of a problem, because it will run under iOS device emulation, but not work on iOS!
  5. Dreamora

    Dreamora

    Member

    Joined:
    Apr 5, 2008
    Messages:
    26,586
    Thats funny cause any 3GS+ gpu supports VTF so I would guess it might be related to something else
  6. Jessy

    Jessy

    Member

    Joined:
    Jun 7, 2007
    Messages:
    7,173
  7. imaginaryhuman

    imaginaryhuman

    Member

    Joined:
    Mar 21, 2010
    Messages:
    3,470
    Great, you made my day. So you're saying in a vertex shader you can read one or more textures (up to how many texture units?) and those textures can be any of the supported unity formats including a regular 8-bit per component rgb, rgba, alpha8 etc?

    Also how is the speed compared to reading texture in the fragment shader? I heard its a lot slower (but that on directx 10/shader model 4? It's using the exact same hardware as texture fetch in the fragment shader?)

    Thanks
    Last edited: Mar 3, 2012
  8. Jessy

    Jessy

    Member

    Joined:
    Jun 7, 2007
    Messages:
    7,173
    I have no idea about the specifics. Try it out for yourself!

    Attached Files:

  9. imaginaryhuman

    imaginaryhuman

    Member

    Joined:
    Mar 21, 2010
    Messages:
    3,470
    Thanks, though this example doesn't seem to be actually accessing the texture, just generating some vertex colors?
    Last edited: Mar 3, 2012
  10. Noisecrime

    Noisecrime

    Member

    Joined:
    Apr 7, 2010
    Messages:
    1,265
    No idea, seems fast enough to me though, certainly much faster than manipulating the vertices on the cpu and uploading them.




    Just remember you need to use SM3.0 in your shader.
  11. Jessy

    Jessy

    Member

    Joined:
    Jun 7, 2007
    Messages:
    7,173
    It "generates vertex colors" by accessing the texture. That's what this thread is about! Ask me if you have any questions about what the shader is doing. I tried to make something really simple for illustration. You were asking about multiple texture lookups though, so I included 12.
  12. imaginaryhuman

    imaginaryhuman

    Member

    Joined:
    Mar 21, 2010
    Messages:
    3,470
    Oh I see, I'll definitely have to go back and look at it more then. Thanks
  13. carllooper

    carllooper

    New Member

    Joined:
    Aug 8, 2009
    Messages:
    64
    I'm on Windows.

    The following works for me. Unity automatically added the slightly misleading upgrade note.

    The difference between the following two versions is that

    1. the first one has if defined conditioning in the vert program whereas the second one doesn't.
    2. the second one uses #pragma glsl whereas the first one doesn't

    All else is the same.


    On Windows in default DirectX mode:

    Code (csharp):
    1. Shader "Custom/VertDisplace" {
    2.         Properties {
    3.             _MainTex ("Base (RGB)", 2D) = "white" {}
    4.         }
    5.         SubShader {
    6.             Tags { "RenderType"="Opaque" }
    7.             LOD 200
    8.            
    9.             CGPROGRAM
    10. // Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it does not contain a surface program or both vertex and fragment programs.
    11. #pragma exclude_renderers gles
    12.  
    13.             #pragma target 3.0
    14.             #pragma vertex vert
    15.             #pragma surface surf BlinnPhong
    16.            
    17.             sampler2D _MainTex;
    18.      
    19.             struct Input {
    20.                 float2 uv_MainTex;
    21.             };
    22.      
    23.             void vert (inout appdata_full v) {
    24.                 #if !defined(SHADER_API_OPENGL)
    25.                 float4 tex = tex2Dlod (_MainTex, float4(v.texcoord.xy,0,0));
    26.                 v.vertex.y += tex.r * 2.0;
    27.                 #endif
    28.             }  
    29.            
    30.             void surf (Input IN, inout SurfaceOutput o) {
    31.                 half4 tex = tex2D(_MainTex, IN.uv_MainTex);
    32.                 o.Albedo = tex.rgb;
    33.                 o.Alpha = tex.a;
    34.             }        
    35.            
    36.             ENDCG
    37.         }
    38.         FallBack "Diffuse"
    39.     }

    On Windows in forced OpenGL mode:

    Code (csharp):
    1. Shader "Custom/VertDisplace" {
    2.         Properties {
    3.             _MainTex ("Base (RGB)", 2D) = "white" {}
    4.         }
    5.         SubShader {
    6.             Tags { "RenderType"="Opaque" }
    7.             LOD 200
    8.            
    9.             CGPROGRAM
    10. // Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it does not contain a surface program or both vertex and fragment programs.
    11. #pragma exclude_renderers gles
    12.             #pragma glsl
    13.             #pragma target 3.0
    14.             #pragma vertex vert
    15.             #pragma surface surf BlinnPhong
    16.            
    17.             sampler2D _MainTex;
    18.      
    19.             struct Input {
    20.                 float2 uv_MainTex;
    21.             };
    22.      
    23.             void vert (inout appdata_full v) {
    24.                 float4 tex = tex2Dlod (_MainTex, float4(v.texcoord.xy,0,0));
    25.                 v.vertex.y += tex.r * 2.0;
    26.             }  
    27.            
    28.             void surf (Input IN, inout SurfaceOutput o) {
    29.                 half4 tex = tex2D(_MainTex, IN.uv_MainTex);
    30.                 o.Albedo = tex.rgb;
    31.                 o.Alpha = tex.a;
    32.             }        
    33.            
    34.             ENDCG
    35.         }
    36.         FallBack "Diffuse"
    37.     }
    Last edited: Mar 7, 2012
  14. niks

    niks

    New Member

    Joined:
    Oct 11, 2011
    Messages:
    16
    Hello Carllooper...

    I am new in shaders with unity.

    i want to create a shader for texturing my vertex of a cube with a predefined radius...
    can you please tell me how i can do the same.???

    Sorry for my bad english and Thanks in advance

    Thank!
    Niks
  15. dreamerflyer

    dreamerflyer

    Member

    Joined:
    Jun 11, 2011
    Messages:
    227
  16. dreamerflyer

    dreamerflyer

    Member

    Joined:
    Jun 11, 2011
    Messages:
    227
    Since VTF can run on IOS ,So R2VB can too??
  17. dreamerflyer

    dreamerflyer

    Member

    Joined:
    Jun 11, 2011
    Messages:
    227
    AnyOne Can help?