Search Unity

3D Textures on Android?

Discussion in 'Shaders' started by Harrison_Perry, Jan 3, 2018.

  1. Harrison_Perry

    Harrison_Perry

    Joined:
    Jan 16, 2017
    Posts:
    9
    Hey Everyone,

    I'm running into issues trying to get 3D Textures working on Android. Pink (error) materials are appearing on objects that are using this shader.

    Below is the 3D Texture shader. This works fine in windows builds and in the editor. But not with Android.

    Please let me know if you need any more information. My hunch is that Android doesn't support 3D textures. (https://docs.unity3d.com/Manual/class-Texture3D.html) Though, I've tried searching for proof and cannot seem to find anything. I'm no Android / OpenGL / Shader whizz so I may be missing something simple!

    Is there any reason the below shader wouldn't work on Android?

    Thanks in advance,
    Harrison

    Code (CSharp):
    1. // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
    2.  
    3. Shader "Unlit/_3DTextureTest_Trans"
    4. {
    5.     Properties
    6.     {
    7.         _MainTex("Texture", 3D) = "white" {}
    8.         _Alpha("Alpha", Range(0, 1)) = 0.2
    9.        
    10.     }
    11.         SubShader
    12.     {
    13.         Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" }
    14.         ZWrite Off
    15.         Blend SrcAlpha OneMinusSrcAlpha
    16.         LOD 100
    17.  
    18.         Pass
    19.         {
    20.             CGPROGRAM
    21.             #pragma vertex vert
    22.             #pragma fragment frag
    23.             // make fog work
    24.             #pragma multi_compile_fog
    25.  
    26.             #include "UnityCG.cginc"
    27.  
    28.             struct appdata
    29.             {
    30.                 float4 vertex : POSITION;
    31.                 float2 uv : TEXCOORD0;
    32.             };
    33.  
    34.             struct v2f
    35.             {
    36.                 float2 uv : TEXCOORD0;
    37.                 UNITY_FOG_COORDS(1)
    38.                 float4 vertex : SV_POSITION;
    39.                 float3 wpos : TEXCOORD1;
    40.             };
    41.  
    42.             sampler3D _MainTex;
    43.             float4 _MainTex_ST;
    44.             float _Alpha;
    45.  
    46.             v2f vert(appdata v)
    47.             {
    48.                
    49.                 v2f o;
    50.                 o.vertex = UnityObjectToClipPos(v.vertex);
    51.                 float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
    52.                 o.wpos = worldPos;
    53.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    54.                 UNITY_TRANSFER_FOG(o,o.vertex);
    55.                 return o;
    56.             }
    57.  
    58.             fixed4 frag(v2f i) : SV_Target
    59.             {
    60.                 // sample the texture
    61.                 float3 texcoord = i.wpos.xzy;
    62.                 //texcoord.xy = texcoord.yx;
    63.                 texcoord.z += 1.5f;
    64.                 texcoord.z = texcoord.z / (3*3);
    65.  
    66.                 texcoord.x /= 13.6875;
    67.                 texcoord.y /= 10;
    68.  
    69.                 texcoord.x /= 30;
    70.                 texcoord.y /= 30;
    71.  
    72.                 fixed4 col = tex3D(_MainTex, texcoord  );
    73.  
    74.                 if (texcoord.x > 1)
    75.                     col = float4(0, 0, 0, 1);
    76.                 if (texcoord.x < 0)
    77.                     col = float4(0, 0, 0, 1);
    78.  
    79.                 if (texcoord.y > 1)
    80.                     col = float4(0, 0, 0, 1);
    81.                 if (texcoord.y < 0)
    82.                     col = float4(0, 0, 0, 1);
    83.  
    84.                 col.a = _Alpha;
    85.  
    86.                 // apply fog
    87.                 UNITY_APPLY_FOG(i.fogCoord, col);
    88.                 return col;
    89.         }
    90.     ENDCG
    91. }
    92.     }
    93. }
    94.  
     
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,014
    Hi!

    Android devices running OpenGL ES 3 should support 3D textures, as well as ES 2 devices with an GL_OES_texture_3D extension.

    Check the device logs, they should contain the actual reason.
     
  3. Harrison_Perry

    Harrison_Perry

    Joined:
    Jan 16, 2017
    Posts:
    9
    Hey aleksandrk,

    Thanks for getting back so quickly. I've made an empty project with just a cube, material, and the shader. It works on Android! Probably should have tried that first... I'll have to do some more digging in the actual application. Annoyingly, the device logs aren't printing anything useful.

    Thanks again!

    EDIT:

    It's working! I had to change Graphics API levels! Thanks so much!

     
    Last edited: Jan 3, 2018