I'm trying to make a game with periodic boundary conditions (so basically if you are on the right edge of the map, and you look right, you should see the whole map again).
I made a geometry shader which takes one triangle and outputs the 27 triangles i need (3*3*3 periodic box), and it works perfectly while the base object is on the screen. As soon as the base object leaves the screen all the copies disappear too.
So i think unity does not even call my shaders on the vertices which is behind my camera (which is totally fine for optimization), but with my current solution i need a call on every object of the game.
Is it possible to force unity not to discard any objects before rendering, or should i look for a different solution? If i have to do something else, do you have any ideas?
Here's my current code. (I'm new with shaders, so it might be stupid...)
Shader "Unlit/Geom_shader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma geometry geom
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
float2 uv : TEXCOORD0;
};
struct v2g {
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
};
struct g2f {
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2g vert (appdata v)
{
v2g o;
o.vertex = v.vertex;
o.uv = v.uv;
return o;
}
[maxvertexcount(3*3*3*3)]
void geom(triangle v2g input[3], inout TriangleStream<g2f> tristream) {
g2f o = (g2f)0;
for (int x = 0; x < 3; x++)
{
float x_shift = (x - 1) * 2*_SinTime[2];
for (int y = 0; y < 3; y++)
{
float y_shift = (y - 1) * 2 * _SinTime[2];
for (int z = 0; z < 3; z++)
{
float z_shift = (z - 1) * 2 * _CosTime[2];
for (int i = 0; i < 3; i++)
{
o.vertex = UnityObjectToClipPos(input[i].vertex + float4(x_shift, y_shift, z_shift, 0));
o.uv = TRANSFORM_TEX(input[i].uv, _MainTex);
tristream.Append(o);
}
tristream.RestartStrip();
}
}
}
}
fixed4 frag (g2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
return col;
}
ENDCG
}
}
}
I'm not trying to use it for lights but for whole objects and i'm sure they are handled differently. So the reason why it is happening is the same, but the solution must be different. (Why it's not a duplicate of: Unity3D - Light deactivated when facing opposite direction )
It's not the vertices being discarded, it's the entire mesh being culled as a whole.
Unity implements View Frustum Culling based on the bounding box of your mesh. You can change the bounding box of a mesh manually by assigning a new value to Mesh.bounds
.