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
iChannel0as 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 Sizeas the size, in pixels, of theshader_modelmesh. The default mesh is the built-in 2x2 quad. -
Set the
Use Ratioflag to define theiResolutionbehaviour. If true,iResolutionwill use size ratios (ex:(1.78, 1.)), otherwise uses the scaled mesh size, in pixels (ex:mesh_size.xy * go_scale.xy).
-
[x]
iTimeThe total time, in seconds. -
[y]
iTimeDeltaThe time elapsed since the previous frame, in seconds. -
[z]
iFrameThe frame count. -
[w]
unusedtime = iTimeFrame.x; timeDelta = iTimeFrame.y; frame = iTimeFrame.z;
-
[x]
iResolution.xTheShaderFoldGameObject width, in pixels. IfUse Ratiois set, outputs as the width ratio. -
[y]
iResolution.yTheShaderFoldGameObject height, in pixels. IfUse Ratiois set, outputs as the height ratio. -
[z]
unused -
[w]
unusedresolution = iResolution.xy;
-
[x]
iMouse.xThe Mouse x-position while the Mouse Button is held. -
[y]
iMouse.yThe Mouse y-position while the Mouse Button is held. -
[z]
iMouse.zContains 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.wContains 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 fragCoordThe input fragment position. Supplied to the Fragment Program by the Vertex Program.vec2 uv = var_texcoord0.xy * iResolution.xy - 0.5
-
out fragColorThe output fragment color. -
[x]
redThe output red channel value. -
[y]
greenThe output green channel value. -
[z]
blueThe output blue channel value. -
[w]
alphaThe 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.
