Defold shader template for porting ShaderToy code.
This is a basic Defold project template for porting ShaderToy shaders.
To learn how to port ShaderToy code to Defold, check out the Official Defold ShaderToy Tutorial.
See DefFX, Defold Shader Examples, Defold Shaders, DefFragments, Pixel Planets, and Glitch Defold for Defold-specific shaders implemented with best practices.
The goal of this project is to provide a template for porting and playing with code, not reimplement every ShaderToy input.
This is in development, not well tested, and suffers issues. Many shaders require fine tuning for resolution and input parity. Feel free to open an issue for any problems you encounter or improvements.
- iResolution
- iTime
- iTimeDelta
- iFrame
- iMouse
- iChannel0
TO DO...
Please see the shader.fp Fragment Program for a basic implementation.
Ensure all Fragment Constants are used (ex: vec4 mouse = iMouse;
), otherwise Defold will throw errors.
The shader.material defines constants used by the shader.
Texture Samplers defined within the Material will automatically appear in the Model's properties.
- Set additional shader parameters as Fragment Constants.
The shader renders onto the shader_model
Model Component within the ShaderFold
GameObject in main.collection.
- Set
iChannel0
as the texture to render onto the model.
Updates to Fragment Constants are pushed from the fold.script Script Component within the ShaderFold
GameObject in main.collection.
-
Set the
Mesh Size
as the size, in pixels, of theshader_model
mesh. The default mesh is the built-in 2x2 quad. -
Set the
Use Ratio
flag to define theiResolution
behaviour. If true,iResolution
will use size ratios (ex:(1.78, 1.)
), otherwise uses the scaled mesh size, in pixels (ex:mesh_size.xy * go_scale.xy
).
-
[x]
iTime
The total time, in seconds. -
[y]
iTimeDelta
The time elapsed since the previous frame, in seconds. -
[z]
iFrame
The frame count. -
[w]
unused
time = iTimeFrame.x; timeDelta = iTimeFrame.y; frame = iTimeFrame.z;
-
[x]
iResolution.x
TheShaderFold
GameObject width, in pixels. IfUse Ratio
is set, outputs as the width ratio. -
[y]
iResolution.y
TheShaderFold
GameObject height, in pixels. IfUse Ratio
is set, outputs as the height ratio. -
[z]
unused
-
[w]
unused
resolution = iResolution.xy;
-
[x]
iMouse.x
The Mouse x-position while the Mouse Button is held. -
[y]
iMouse.y
The Mouse y-position while the Mouse Button is held. -
[z]
iMouse.z
Contains both the Mouse Click y-position and the Mouse Down state.-
sign(iMouse.z)
The Mouse Down state. -
abs(iMouse.z)
The Mouse Click y-position.
-
-
[w]
iMouse.w
Contains both the Mouse Click x-position and the Mouse Click state. ShaderToy Click = Defoldaction.pressed
.-
sign(iMouse.w)
The Mouse Click state. -
abs(iMouse.w)
The Mouse Click x-position.
if (sign(iMouse.w) > 0.) color.rg *= abs(iMouse.wz) / iResolution.x;
-
-
[xy]
in fragCoord
The input fragment position. Supplied to the Fragment Program by the Vertex Program.vec2 uv = var_texcoord0.xy * iResolution.xy - 0.5
-
out fragColor
The output fragment color. -
[x]
red
The output red channel value. -
[y]
green
The output green channel value. -
[z]
blue
The output blue channel value. -
[w]
alpha
The output color transparency.gl_FragColor = sampler2D(iChannel0, uv);
ShaderFold iMouse
is based on Input - Mouse by iq (Inigo Quilez) and tuto: new mouse events by FabriceNeyret2.