Hi everyone,
I’m trying to convert this distortion shader in Unity to Unreal Engine and I’ve been pulling my hair since last night.What I did so far is converting the math operations in the shader to UE material.Below you can see the current state of the material and the cg shader source code.
SubShader
{
//Pass to grab everything behind the object
GrabPass
{
Name "BASE"
Tags { "LightMode" = "Always" }
}
//Render Pass
Pass
{
Name "BASE"
Tags { "LightMode" = "Always" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
uniform half _StrengthX;
uniform half _StrengthY;
uniform float4 _DistMap_ST;
uniform sampler2D _DistMap;
uniform half _DistScrollSpeedY;
uniform half _DistScrollSpeedX;
struct VertexInput
{
float4 vertex : POSITION;
float2 texcoord: TEXCOORD0;
};
struct VertexOutput
{
float4 vertex : POSITION;
float2 uvmain : TEXCOORD0;
float4 uvgrab : TEXCOORD1;
};
//Sampler for grab pass
sampler2D _GrabTexture;
//Vertex Shader Function
VertexOutput vert (VertexInput v)
{
//Resultant vertex
VertexOutput output;
//Model Space to Projection space
output.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
//Flip UV based on OpenGL/DirectX
#if UNITY_UV_STARTS_AT_TOP
float scale = -1.0;
#else
float scale = 1.0;
#endif
output.uvgrab.xy = (float2(output.vertex.x, output.vertex.y*scale) + output.vertex.w)* 0.5;
output.uvgrab.z = output.vertex.z;
output.uvgrab.w = output.vertex.w;
output.uvmain = TRANSFORM_TEX( v.texcoord, _DistMap );
return output;
}
//Fragment Shader Function
half4 frag(VertexOutput i ) : COLOR
{
//Scroll the distortion map
half2 mapoft = half2(_Time.y*_DistScrollSpeedX, _Time.y*_DistScrollSpeedY);
//get distortion data with uv scroll
half4 offsetColor = tex2D(_DistMap, i.uvmain + mapoft);
//Calculate offset
half oftX = offsetColor.r * _StrengthX;
half oftY = offsetColor.g * _StrengthY;
//Update to grab data
i.uvgrab.x += oftX;
i.uvgrab.y += oftY;
//return the grab texture with modified data
return tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
}
ENDCG
}
}
The resultant effect in Unity 3D
And here is the screenshot of the material in progress
The panning of the distortion texture seems fine but there is something wrong with manipulating the vertex data to the screen color UV
Any help is appreciated.
Thanks