- Home /
2D Shader that bends vertices?
I've been researching an idea of a 2D game where the player is always centered (at least horizontally), the terrain is always flat (ie. no slopes, no jumping, etc.) and various sprites will be added right on top of the terrain.
Here's the tricky part: while the game will be completely flat/linear and you can only move forward/backward, I want to create the effect of the whole world being a sphere, ie. the terrain and all sprites that are not at the center (horizontally) will get bent downwards, creating the illusion of moving on a spherical terrain. Make any sense?
The reason why I thought this should be much easier with a shader is because I want sprites (think houses, trees, etc.) to look like they're aligned to the terrain even if the terrain looks bent/spherical.
I hope this makes sense and I hope that someone will be able to at least point me in the right direction (I don't know much about shaders). If not, I'll have to draw a picture to demonstrate.
Answer by tanoshimi · Nov 01, 2013 at 09:19 PM
I'm not quite sure about the "2D" bit of your request, but I wrote this blog article a few days ago about creating a curved world shader a la Animal Crossing, with full surface shader code included. Does that help?
https://alastaira.wordpress.com/2013/10/25/animal-crossing-curved-world-shader/
Answer by Dracorat · Nov 01, 2013 at 06:01 PM
If you keep the camera centered on the player you can do it by using a perspective camera and using a high FoV value to make it fish-eye.
This seems like it could work, but wouldn't adjusting the FoV cause some skewing/distortion, especially on sprites that would be near the edges of the camera?
Yes, but wouldn't you get that anyway if you were bending them?
I guess I would. I was hoping there'd be some sort of post-processing way of bending the entire camera without distortion. I'll have to try adjusting the FoV though, as the bending doesn't need to be drastic, so it's entirely possible that a regular user wouldn't even notice the distortion.
Answer by ElKrullo · Aug 13, 2015 at 08:50 AM
Hi!
I know this is an old thread, but i managed to modify Unitys default shader a bit and got this working. I'm sure it could be improved upon, but it's a start.
Shader "Sprites/Curved"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
_Curvature ("Curvature", Float) = 0.001
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Cull Off
Lighting Off
ZWrite Off
Blend One OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile _ PIXELSNAP_ON
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
half2 texcoord : TEXCOORD0;
};
fixed4 _Color;
uniform float _Curvature;
v2f vert(appdata_t IN)
{
v2f OUT;
float4 vv = mul( _Object2World, IN.vertex );
vv.xyz -= _WorldSpaceCameraPos.xyz;
//Curvature and taper effects are calculated here
vv = float4( (vv.x * vv.y) * _Curvature), (vv.x * vv.x) * - _Curvature, 0.0f, 0.0f );
//Use this instead if you don't want the taper effect
//vv = float4( 0.0f, (vv.x * vv.x) * - _Curvature, 0.0f, 0.0f );
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex) + mul(_World2Object, vv);
OUT.texcoord = IN.texcoord;
OUT.color = IN.color * _Color;
#ifdef PIXELSNAP_ON
OUT.vertex = UnityPixelSnap (OUT.vertex);
#endif
return OUT;
}
sampler2D _MainTex;
fixed4 frag(v2f IN) : SV_Target
{
fixed4 c = tex2D(_MainTex, IN.texcoord) * IN.color;
c.rgb *= c.a;
return c;
}
ENDCG
}
}
}
Your answer
Follow this Question
Related Questions
Terraria / Starbound Style Lighting 3 Answers
one shader for multiple sprites 0 Answers
2D Sprite Highlight 0 Answers
Having an array of points(vector2), how do I create a 2d terrain? (new to unity) 2 Answers