Reference for converting GLSL to HLSL - Unity 3d ShaderToy


  • Replace iGlobalTime shader input (“shader playback time in seconds”) with _Time.y
  • Replace iResolution.xy (“viewport resolution in pixels”) with _ScreenParams.xy
  • Replace vec2 types with float2mat2 with float2x2 etc.
  • Replace vec3(1) shortcut constructors in which all elements have same value with explicit float3(1,1,1)
  • Replace Texture2D with Tex2D
  • Replace atan(x,y) with atan2(y,x) <- Note parameter ordering!
  • Replace mix() with lerp()
  • Replace *= with mul()
  • Replace signed integer vector ive2, ivec3, ivec4 with int2, int3,int4
  • Replace iTime with _Time
  • Remove third (bias) parameter from Texture2D lookups
  • mainImage(out vec4 fragColor, in vec2 fragCoord) is the fragment shader function, equivalent to float4 mainImage(float2 fragCoord : SV_POSITION) : SV_Target
  • UV coordinates in GLSL have 0 at the top and increase downwards, in HLSL 0 is at the bottom and increases upwards, so you may need to use uv.y = 1 – uv.y at some point.
  • Replace fract with frac
  • Replace function dFdx with ddx
  • Replace function dfdy with ddy
  • fragCoord in GLSL
    Default Unity shader naming :

    HLSL - Shader Lab in Unity
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};

In frag method:

fixed4 frag (v2f i) : SV_Target

{

   // GLSL - fragCoord, fragCoord.xy = i.vertex.xy

    i.vertex.position 

}

In GLSL we would have method:
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{

}



  • Replace mod with fmod





I took some information from https://alastaira.wordpress.com/2015/08/07/unity-shadertoys-a-k-a-converting-glsl-shaders-to-cghlsl/

Comments