Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Vertex Texture Fetch in Unity Free?

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

  1. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    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

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    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

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    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

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    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

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

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
  7. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    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

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

    Attached Files:

  9. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    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

    Joined:
    Apr 7, 2010
    Posts:
    2,051
    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

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    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

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

    carllooper

    Joined:
    Aug 8, 2009
    Posts:
    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

    Joined:
    Oct 11, 2011
    Posts:
    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

    Joined:
    Jun 11, 2011
    Posts:
    927
  16. dreamerflyer

    dreamerflyer

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

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    AnyOne Can help?