Search Unity

Cubemap on a cube as texture

Discussion in 'Shaders' started by warka0OO, Sep 25, 2012.

  1. warka0OO

    warka0OO

    Joined:
    Feb 21, 2012
    Posts:
    6
    Hi!!

    I'm not really experimented with shaders. Does exists a ressources about a cubemap as a texture on a cube ? :)
    (with a six faces texture)

    If someone can help me on this, it could be nice :D

    Thanks!
     
  2. MADmarine

    MADmarine

    Joined:
    Aug 31, 2010
    Posts:
    627
    Cubemaps aren't really designed for this, they're mainly designed for things like reflections.

    You might be able to find some way to extract the textures or provide some kind of direction vector to the shader to make it appear the way you want on a cube, but it wouldn't be a great way of doing it. The end result would just be a complicated un-optimised mess.

    Just UV map your cube correctly, provide a texture to match, and everything will be much easier.
     
  3. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    Something like this? (using the builtin cube game object)

    Code (csharp):
    1.  
    2. Shader "cube map for cubes" {
    3.    Properties {
    4.       _Cube("cube map", Cube) = "" {}
    5.    }
    6.    SubShader {
    7.       Pass {  
    8.          CGPROGRAM
    9.  
    10.          #pragma vertex vert  
    11.          #pragma fragment frag
    12.  
    13.          uniform samplerCUBE _Cube;  
    14.  
    15.          struct vertexInput {
    16.             float4 vertex : POSITION;
    17.          };
    18.          struct vertexOutput {
    19.             float4 pos : SV_POSITION;
    20.             float3 texDir : TEXCOORD0;
    21.          };
    22.  
    23.          vertexOutput vert(vertexInput input)
    24.          {
    25.             vertexOutput output;
    26.  
    27.             output.texDir = input.vertex;
    28.             output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
    29.             return output;
    30.          }
    31.  
    32.          float4 frag(vertexOutput input) : COLOR
    33.          {
    34.             return texCUBE(_Cube, input.texDir);
    35.          }
    36.  
    37.          ENDCG
    38.       }
    39.    }
    40. }
    41.  
    (adapted from http://en.wikibooks.org/wiki/Cg_Programming/Unity/Reflecting_Surfaces )
     
  4. warka0OO

    warka0OO

    Joined:
    Feb 21, 2012
    Posts:
    6
    But, in fact, i've done some boolean operations in unity (with cubes, spheres and custom mesh).

    I've never programmed shaders by the past (except with several modeling process and procedural generations with hardware instantiation).

    For my problem, i searched to apply differents textures (on each face) on a custom cube model, like trixel in Fez (i guess). It's like a projection from each face on the mesh.

    In fact, it's like a UV map but by compting the all face of the mesh.

    If you have an idea or some tutorial to see to learn something about it, i'll see it :)

    thx you for answering :D
     
  5. warka0OO

    warka0OO

    Joined:
    Feb 21, 2012
    Posts:
    6
    Waouh, i didn't refresh the page when you post this oO
    thx

    I gonna see this !
     
  6. tabor

    tabor

    Joined:
    Nov 29, 2011
    Posts:
    42
    Hey Martin, thanks for that useful bit of code. Do you have any idea how to modify this to allow individual inputs for each face, instead of using a cube map? Compare the default shaders RenderFX/SkyboxCubed and RenderFX/Skybox to see the inputs I am looking for.

    The goal is to get around the res limit of 2048 per face when using a cubemap.

    Thanks!


    Here is the default shader with the properties I am referring to for reference:
    Code (csharp):
    1.  
    2. Shader "RenderFX/Skybox" {
    3. Properties {
    4.     _Tint ("Tint Color", Color) = (.5, .5, .5, .5)
    5.     _FrontTex ("Front (+Z)", 2D) = "white" {}
    6.     _BackTex ("Back (-Z)", 2D) = "white" {}
    7.     _LeftTex ("Left (+X)", 2D) = "white" {}
    8.     _RightTex ("Right (-X)", 2D) = "white" {}
    9.     _UpTex ("Up (+Y)", 2D) = "white" {}
    10.     _DownTex ("down (-Y)", 2D) = "white" {}
    11. }
    12.  
    13. SubShader {
    14.     Tags { "Queue"="Background" "RenderType"="Background" }
    15.     Cull Off ZWrite Off Fog { Mode Off }
    16.    
    17.     CGINCLUDE
    18.     #include "UnityCG.cginc"
    19.  
    20.     fixed4 _Tint;
    21.    
    22.     struct appdata_t {
    23.         float4 vertex : POSITION;
    24.         float2 texcoord : TEXCOORD0;
    25.     };
    26.     struct v2f {
    27.         float4 vertex : POSITION;
    28.         float2 texcoord : TEXCOORD0;
    29.     };
    30.     v2f vert (appdata_t v)
    31.     {
    32.         v2f o;
    33.         o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    34.         o.texcoord = v.texcoord;
    35.         return o;
    36.     }
    37.     fixed4 skybox_frag (v2f i, sampler2D smp)
    38.     {
    39.         fixed4 tex = tex2D (smp, i.texcoord);
    40.         fixed4 col;
    41.         col.rgb = tex.rgb + _Tint.rgb - unity_ColorSpaceGrey;
    42.         col.a = tex.a * _Tint.a;
    43.         return col;
    44.     }
    45.     ENDCG
    46.    
    47.     Pass {
    48.         CGPROGRAM
    49.         #pragma vertex vert
    50.         #pragma fragment frag
    51.         #pragma fragmentoption ARB_precision_hint_fastest
    52.         sampler2D _FrontTex;
    53.         fixed4 frag (v2f i) : COLOR { return skybox_frag(i,_FrontTex); }
    54.         ENDCG
    55.     }
    56.     Pass{
    57.         CGPROGRAM
    58.         #pragma vertex vert
    59.         #pragma fragment frag
    60.         #pragma fragmentoption ARB_precision_hint_fastest
    61.         sampler2D _BackTex;
    62.         fixed4 frag (v2f i) : COLOR { return skybox_frag(i,_BackTex); }
    63.         ENDCG
    64.     }
    65.     Pass{
    66.         CGPROGRAM
    67.         #pragma vertex vert
    68.         #pragma fragment frag
    69.         #pragma fragmentoption ARB_precision_hint_fastest
    70.         sampler2D _LeftTex;
    71.         fixed4 frag (v2f i) : COLOR { return skybox_frag(i,_LeftTex); }
    72.         ENDCG
    73.     }
    74.     Pass{
    75.         CGPROGRAM
    76.         #pragma vertex vert
    77.         #pragma fragment frag
    78.         #pragma fragmentoption ARB_precision_hint_fastest
    79.         sampler2D _RightTex;
    80.         fixed4 frag (v2f i) : COLOR { return skybox_frag(i,_RightTex); }
    81.         ENDCG
    82.     }  
    83.     Pass{
    84.         CGPROGRAM
    85.         #pragma vertex vert
    86.         #pragma fragment frag
    87.         #pragma fragmentoption ARB_precision_hint_fastest
    88.         sampler2D _UpTex;
    89.         fixed4 frag (v2f i) : COLOR { return skybox_frag(i,_UpTex); }
    90.         ENDCG
    91.     }  
    92.     Pass{
    93.         CGPROGRAM
    94.         #pragma vertex vert
    95.         #pragma fragment frag
    96.         #pragma fragmentoption ARB_precision_hint_fastest
    97.         sampler2D _DownTex;
    98.         fixed4 frag (v2f i) : COLOR { return skybox_frag(i,_DownTex); }
    99.         ENDCG
    100.     }
    101. }  
    102.  
    103. SubShader {
    104.     Tags { "Queue"="Background" "RenderType"="Background" }
    105.     Cull Off ZWrite Off Fog { Mode Off }
    106.     Color [_Tint]
    107.     Pass {
    108.         SetTexture [_FrontTex] { combine texture +- primary, texture * primary }
    109.     }
    110.     Pass {
    111.         SetTexture [_BackTex]  { combine texture +- primary, texture * primary }
    112.     }
    113.     Pass {
    114.         SetTexture [_LeftTex]  { combine texture +- primary, texture * primary }
    115.     }
    116.     Pass {
    117.         SetTexture [_RightTex] { combine texture +- primary, texture * primary }
    118.     }
    119.     Pass {
    120.         SetTexture [_UpTex]    { combine texture +- primary, texture * primary }
    121.     }
    122.     Pass {
    123.         SetTexture [_DownTex]  { combine texture +- primary, texture * primary }
    124.     }
    125. }
    126. }
     
  7. tabor

    tabor

    Joined:
    Nov 29, 2011
    Posts:
    42
    SOLVED

    UPDATE: SOLVED
    Code (csharp):
    1.  
    2. Shader "Custom/Cubemap from textures" {
    3. Properties {
    4.     _FrontTex ("Front (+Z)", 2D) = "white" {}
    5.     _BackTex ("Back (-Z)", 2D) = "white" {}
    6.     _LeftTex ("Left (+X)", 2D) = "white" {}
    7.     _RightTex ("Right (-X)", 2D) = "white" {}
    8.     _UpTex ("Up (+Y)", 2D) = "white" {}
    9.     _DownTex ("down (-Y)", 2D) = "white" {}
    10. }
    11.  
    12.  
    13.  
    14.  
    15. SubShader {
    16.     Tags { "Queue"="Background" "RenderType"="Background" }
    17.     Cull Front ZWrite Off Fog { Mode Off }
    18.     Pass {
    19. CGPROGRAM
    20. #pragma vertex vert
    21. #pragma fragment frag
    22. #pragma exclude_renderers flash
    23. #include "UnityCG.cginc"
    24. sampler2D _FrontTex;
    25. sampler2D _BackTex;
    26. sampler2D _LeftTex;
    27. sampler2D _RightTex;
    28. sampler2D _UpTex;
    29. sampler2D _DownTex;
    30.  
    31.  
    32.  
    33.  
    34. struct appdata {
    35.     float4 vertex   : POSITION;
    36.     float3 normal   : NORMAL;
    37. };
    38. struct v2f {
    39.     float4 pos  : SV_POSITION;
    40.     float3 n    : TEXCOORD1;
    41. };
    42. v2f vert (appdata v) {
    43.     v2f o;
    44.     o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    45.     o.n = mul((float3x3)_Object2World,v.normal);
    46.     return o;
    47. }
    48. half4 frag( v2f i ) : COLOR {
    49.  
    50.  
    51.  
    52.  
    53.     float3 n = i.n;
    54.     float3 an= abs(n);
    55.  
    56.  
    57.  
    58.  
    59.     float3 xyz;
    60.     xyz.x = (an.x>max(an.y,an.z)) ? 1 : 0;
    61.     xyz.y = (an.y>max(an.x,an.z)) ? 1 : 0;
    62.     xyz.z = (an.z>max(an.x,an.y)) ? 1 : 0;
    63.     xyz = (n>0) ? xyz : -xyz;
    64.  
    65.  
    66.  
    67.  
    68.     float3 satxyz = saturate(-xyz);
    69.     float3 natxyz = saturate(xyz);
    70.  
    71.  
    72.  
    73.  
    74.     float2 xc =-(float2(n.zy)/n.x)*0.5+0.5;
    75.     float2 yc =-(float2(n.zx)/n.y)*0.5+0.5;
    76.     float2 zc = (float2(n.xy)/n.z)*0.5+0.5;
    77.     float3 side1 = tex2D(_FrontTex,xc).xyz;
    78.     float3 side4 = tex2D(_DownTex,yc).xyz;
    79.     float3 side6 = tex2D(_LeftTex,zc).xyz;
    80.     xc.y = 1-xc.y;
    81.     yc.y = 1-yc.y;
    82.     zc.y = 1-zc.y;
    83.     float3 side2 = tex2D(_BackTex,xc).xyz;
    84.     float3 side3 = tex2D(_UpTex,yc).xyz;
    85.     float3 side5 = tex2D(_RightTex,zc).xyz;
    86.    
    87.     float3 cube =
    88.         side1 * satxyz.x + side2 * natxyz.x +
    89.         side3 * satxyz.y + side4 * natxyz.y +
    90.         side5 * satxyz.z + side6 * natxyz.z;
    91.     return half4(cube,0);
    92. }
    93. ENDCG
    94.         }
    95.     }
    96. }
    97.  
    98.  
     
    Last edited: Jul 9, 2013
  8. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    @tabor Could I ask what you were trying to accomplish exactly with the shader? I wasn't exactly sure based on what I saw in this thread, but I think I may have had a similar issue.
     
  9. khos85

    khos85

    Joined:
    Jul 21, 2013
    Posts:
    541
    I tried the shader code but my cubes seem to be transparent, did anyone else notice this? I'd like to use this shader to assign texture per face/side if poss.