- Home /
Shader does not take tiling into account
So I am using this shader provided by Unity to make a 360 player. It works great for Mono videos, however when used for Stereo it is not working because the tiling values are not taken into account.
Here is the shader:
Shader "360/Equirectangular"
{
Properties
{
_MainTex ("Diffuse (RGB) Alpha (A)", 2D) = "gray" {}
}
SubShader
{
Pass
{
Tags {"LightMode" = "Always"}
Cull Front
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 3.0
#include "UnityCG.cginc"
struct v2f
{
float4 pos : SV_POSITION;
float3 normal : TEXCOORD0;
};
v2f vert(float4 vertex : POSITION, float3 normal : NORMAL)
{
v2f outCoords = { UnityObjectToClipPos(vertex), normal };
return outCoords;
}
#define ONE_OVER_PI .31830988618379067154F
inline float2 ToRadialCoords(float3 coords)
{
float3 normalizedCoords = normalize(coords);
float latitude = acos(normalizedCoords.y);
float longitude = atan2(normalizedCoords.z, normalizedCoords.x);
float2 sphereCoords = float2(longitude, latitude) * ONE_OVER_PI;
return float2(0.5F - sphereCoords.x * 0.5F, 1.0F - sphereCoords.y);
}
sampler2D _MainTex;
fixed4 frag(v2f i) : SV_Target
{
float2 equirectangularUV = ToRadialCoords(i.normal);
return tex2D(_MainTex, equirectangularUV);
}
ENDCG
}
}
FallBack "VertexLit"
}
Any ideas how to solve the tiling problem?
When you say 360, is it 360 like a 360 picture kind or 360 as in viewving a textured object from all angles kind? Also, stereo? Be more specific and add some details to your context. Not everyone that can help you knows what you are talking about.
Answer by Bunny83 · Sep 24, 2018 at 10:52 PM
Well you have to take the tiling and offset into account yourself. Usually the incoming uv coordinates are modified through the TRANSFORM_TEX macro, but since it's your shader it's up to you how you want to affect the uvs. Keep in mind that TRANSFORM_TEX is just a macro and you have to declare the required variable yourself. See this question for reference
The macro is defined inside the Unity.cginc file (inside \Editor\Data\CGIncludes) and simply looks like this:
#define TRANSFORM_TEX(tex,name) (tex.xy * name##_ST.xy + name##_ST.zw)
So all it actually does is multiplying the given coordinate by the "xy" component and adding the zw component. The xy is the scaling while the zw contains the offset. So in your case you need to declare this variable:
float4 _MainTex_ST;
and do this:
float2 equirectangularUV = TRANSFORM_TEX(ToRadialCoords(i.normal), _MainTex);
Which will actually translate to
float2 equirectangularUV = ToRadialCoords(i.normal).xy * _MainTex_ST.xy + _MainTex_ST.zw;
Your answer
Follow this Question
Related Questions
360 video resolution (Gear VR) 1 Answer
360 video stitching bad when using Sphere Mesh and VideoClip 2 Answers
How Can I Combine Stereo Screen? 0 Answers