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 Thanks!
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.
Something like this? (using the builtin cube game object) Code (csharp): Shader "cube map for cubes" { Properties { _Cube("cube map", Cube) = "" {} } SubShader { Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag uniform samplerCUBE _Cube; struct vertexInput { float4 vertex : POSITION; }; struct vertexOutput { float4 pos : SV_POSITION; float3 texDir : TEXCOORD0; }; vertexOutput vert(vertexInput input) { vertexOutput output; output.texDir = input.vertex; output.pos = mul(UNITY_MATRIX_MVP, input.vertex); return output; } float4 frag(vertexOutput input) : COLOR { return texCUBE(_Cube, input.texDir); } ENDCG } } } (adapted from http://en.wikibooks.org/wiki/Cg_Programming/Unity/Reflecting_Surfaces )
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
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): Shader "RenderFX/Skybox" { Properties { _Tint ("Tint Color", Color) = (.5, .5, .5, .5) _FrontTex ("Front (+Z)", 2D) = "white" {} _BackTex ("Back (-Z)", 2D) = "white" {} _LeftTex ("Left (+X)", 2D) = "white" {} _RightTex ("Right (-X)", 2D) = "white" {} _UpTex ("Up (+Y)", 2D) = "white" {} _DownTex ("down (-Y)", 2D) = "white" {} } SubShader { Tags { "Queue"="Background" "RenderType"="Background" } Cull Off ZWrite Off Fog { Mode Off } CGINCLUDE #include "UnityCG.cginc" fixed4 _Tint; struct appdata_t { float4 vertex : POSITION; float2 texcoord : TEXCOORD0; }; struct v2f { float4 vertex : POSITION; float2 texcoord : TEXCOORD0; }; v2f vert (appdata_t v) { v2f o; o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); o.texcoord = v.texcoord; return o; } fixed4 skybox_frag (v2f i, sampler2D smp) { fixed4 tex = tex2D (smp, i.texcoord); fixed4 col; col.rgb = tex.rgb + _Tint.rgb - unity_ColorSpaceGrey; col.a = tex.a * _Tint.a; return col; } ENDCG Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma fragmentoption ARB_precision_hint_fastest sampler2D _FrontTex; fixed4 frag (v2f i) : COLOR { return skybox_frag(i,_FrontTex); } ENDCG } Pass{ CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma fragmentoption ARB_precision_hint_fastest sampler2D _BackTex; fixed4 frag (v2f i) : COLOR { return skybox_frag(i,_BackTex); } ENDCG } Pass{ CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma fragmentoption ARB_precision_hint_fastest sampler2D _LeftTex; fixed4 frag (v2f i) : COLOR { return skybox_frag(i,_LeftTex); } ENDCG } Pass{ CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma fragmentoption ARB_precision_hint_fastest sampler2D _RightTex; fixed4 frag (v2f i) : COLOR { return skybox_frag(i,_RightTex); } ENDCG } Pass{ CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma fragmentoption ARB_precision_hint_fastest sampler2D _UpTex; fixed4 frag (v2f i) : COLOR { return skybox_frag(i,_UpTex); } ENDCG } Pass{ CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma fragmentoption ARB_precision_hint_fastest sampler2D _DownTex; fixed4 frag (v2f i) : COLOR { return skybox_frag(i,_DownTex); } ENDCG } } SubShader { Tags { "Queue"="Background" "RenderType"="Background" } Cull Off ZWrite Off Fog { Mode Off } Color [_Tint] Pass { SetTexture [_FrontTex] { combine texture +- primary, texture * primary } } Pass { SetTexture [_BackTex] { combine texture +- primary, texture * primary } } Pass { SetTexture [_LeftTex] { combine texture +- primary, texture * primary } } Pass { SetTexture [_RightTex] { combine texture +- primary, texture * primary } } Pass { SetTexture [_UpTex] { combine texture +- primary, texture * primary } } Pass { SetTexture [_DownTex] { combine texture +- primary, texture * primary } } } }
SOLVED UPDATE: SOLVED Code (csharp): Shader "Custom/Cubemap from textures" { Properties { _FrontTex ("Front (+Z)", 2D) = "white" {} _BackTex ("Back (-Z)", 2D) = "white" {} _LeftTex ("Left (+X)", 2D) = "white" {} _RightTex ("Right (-X)", 2D) = "white" {} _UpTex ("Up (+Y)", 2D) = "white" {} _DownTex ("down (-Y)", 2D) = "white" {} } SubShader { Tags { "Queue"="Background" "RenderType"="Background" } Cull Front ZWrite Off Fog { Mode Off } Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma exclude_renderers flash #include "UnityCG.cginc" sampler2D _FrontTex; sampler2D _BackTex; sampler2D _LeftTex; sampler2D _RightTex; sampler2D _UpTex; sampler2D _DownTex; struct appdata { float4 vertex : POSITION; float3 normal : NORMAL; }; struct v2f { float4 pos : SV_POSITION; float3 n : TEXCOORD1; }; v2f vert (appdata v) { v2f o; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); o.n = mul((float3x3)_Object2World,v.normal); return o; } half4 frag( v2f i ) : COLOR { float3 n = i.n; float3 an= abs(n); float3 xyz; xyz.x = (an.x>max(an.y,an.z)) ? 1 : 0; xyz.y = (an.y>max(an.x,an.z)) ? 1 : 0; xyz.z = (an.z>max(an.x,an.y)) ? 1 : 0; xyz = (n>0) ? xyz : -xyz; float3 satxyz = saturate(-xyz); float3 natxyz = saturate(xyz); float2 xc =-(float2(n.zy)/n.x)*0.5+0.5; float2 yc =-(float2(n.zx)/n.y)*0.5+0.5; float2 zc = (float2(n.xy)/n.z)*0.5+0.5; float3 side1 = tex2D(_FrontTex,xc).xyz; float3 side4 = tex2D(_DownTex,yc).xyz; float3 side6 = tex2D(_LeftTex,zc).xyz; xc.y = 1-xc.y; yc.y = 1-yc.y; zc.y = 1-zc.y; float3 side2 = tex2D(_BackTex,xc).xyz; float3 side3 = tex2D(_UpTex,yc).xyz; float3 side5 = tex2D(_RightTex,zc).xyz; float3 cube = side1 * satxyz.x + side2 * natxyz.x + side3 * satxyz.y + side4 * natxyz.y + side5 * satxyz.z + side6 * natxyz.z; return half4(cube,0); } ENDCG } } }
@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.
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.