It looks like you're new here. If you want to get involved, click one of these buttons!
I'm working on a platform game with large, vertically scrolling levels and wanted a simple parallax sky effect in the background. Using a large texture would be impractical as the levels are going to be considerably taller than 2048 pixels. So I wrote a shader for shading between multiple colours. You set 5 colours, the percentage points (from scale 0.0 to 1.0) at which you'd like one to fade in the next, the centre point at which the gradient starts, and the shape of the gradient (circle, elliptical, or linear gradients are possible). I thought I'd share it here in case someone else has a use for it. This could be adapted for all sorts of things, eg to make colourful text etc.
The image below is my attempt at a sunset sky. It would look pretty good (in a cartoony way) with some clouds in front of it.
--# Main
-- MultiStop Colour Gradients
function setup()
print ("Drag your finger to set the gradient centre")
print ("Pull down this output window to access more settings")
Gradient.setup()
parameter.number("GradientAspect", 0, 2, 0.5)
parameter.number("Step1", 0, 1, 0.9)
parameter.number("Step2", 0, 1, 0.3)
parameter.number("Step3", 0, 1, 0.2)
parameter.number("Step4", 0, 1, 0.1)
parameter.number("Step5", 0, 1, 0.05)
parameter.color("Color1",
color(31, 27, 53, 255))
parameter.color("Color2",
color(0, 198, 255, 255))
parameter.color("Color3",
color(228, 207, 225, 255))
parameter.color("Color4",
color(254, 0, 0, 255))
parameter.color("Color5",
color(255, 255, 1, 255))
Centre = vec2(0,-0.4)
end
function draw()
background(40, 40, 50)
Gradient.mesh.shader.centre = Centre
Gradient.mesh.shader.aspect = vec2(GradientAspect, 1)
Gradient.mesh.shader.colors = {Color1, Color2, Color3, Color4, Color5}
Gradient.mesh.shader.step = {Step1, Step2, Step3, Step4, Step5}
Gradient.mesh:draw()
end
function touched(t)
Centre = vec2(-0.5 + t.x/ WIDTH , -0.5 + t.y/HEIGHT)
end
--# Gradient
Gradient = {}
function Gradient.setup()
Gradient.mesh = mesh()
Gradient.mesh:addRect(WIDTH/2, HEIGHT/2, WIDTH, HEIGHT)
Gradient.mesh.shader = shader(
[[
uniform mat4 modelViewProjection;
uniform lowp vec2 aspect; // set to (1,1) for circular gradient, (0,1) or (1,0) for linear, or fractional, eg (1, 0.5) for oval
attribute vec4 position;
attribute vec4 color;
attribute vec2 texCoord;
// varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
void main()
{
// vColor = color;
vTexCoord = (texCoord - vec2(0.5,0.5)) * aspect;
gl_Position = modelViewProjection * position;
}
]],
[[
precision highp float;
const int no = 5; //the number of gradation points you want to have
// uniform lowp sampler2D texture;
uniform lowp vec2 centre; // centre of gradient
uniform float step[no]; // transition points between colours, 0.0 - 1.0
uniform lowp vec4 colors[no]; // colours to grade between
//varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
varying float vDistance;
void main()
{
float dist = distance( vTexCoord, centre);
lowp vec4 col = colors[0];
for (int i=1; i<no; ++i) {
col = mix(col, colors[i], smoothstep(step[i-1], step[i], dist));
}
gl_FragColor = col; //texture2D( texture, vTexCoord ) * col;
}
]]
)
end
Comments
Here's a variant for shading text:
Ah, the wonderful days of the 80's. Personally, I think that gradient text is hard to read.
Please just don't write code for flashing text. [-O<
Yes, I wouldn't typeset a novel like this
( instead I'd use this: http://codea.io/talk/discussion/6710/markdown-codea-rich-text-formatting#latest )
This is for attention-grabbing. Hi-score tables and the like.
beautiful Colour Gradients text
@yojimbo2000 been messing about with your first gradient shader. Looking to see if I can go for some varying backdrops for my ghost slicing game. Here's a graveyard scene
That's awesome! Love how it's all procedural. There's some very atmospheric games around which are just silhouettes over a gradient. One on iOS called Volt that I like.
Although, there's no need to draw the moon with an ellipse, do it with the shader. Try this:
Of course - didn't think of that!
Replace draw with the two functions below for a nice outline effect on the graveyard, which follows the direction of the moon
(This is a bit like Photoshop tennis, where you take a drawing and modify it, in turns).
Nice effect, though it cancels out the subtle fade out of the objects which are further in the distance.
In an action game, I'm not sure subtlety is a factor!
)
does it need the newest Codea version and ios or global replace?
it shows:shader compile error,attempt to redeclare 'step' as variable,attempt to use 'step' as variable... how to make it?
@firewolf - there are four versions of the code. Which one?
i tried again,the same errors in four versions
Which version of iOS and Codea are you using? Did you make any changes to the code before you saw that error?
I wonder whether older versions of open gl had a step function (in 2.0 it's
edge
andsmoothstep
), which would make step not available as a variable name (similar to the issue where the reflect function was added).thanks
Now with optional mist and rain
Love it!
Nice! I would halve the size of the rain sprites, though, they look a bit blocky.
Yes @Ignatz I agree they do look a bit chunky.
Here's a spooky forest using the same shader. There is a small bug where the top of a tree sometimes appears thicker and some work could be done to unify the palettes - it'll look a bit odd if you change the sky colour - the trees should be tinted from the same base, but I'm tired and off to bed!
These are very cool. You're the master of 2D procedural art! One thing though, I think the moon has too hard an edge with step2 set at 0.0501, I can see aliasing. I prefer giving it a slightly softer edge, around 0.052 looks good to me (ie an approximately 2-pixel fall-off at the edge of the moon).
Thanks - with the vector based art I like the sharp edges of the moon - but with your approach it allows it to be easily changed
i wanna make the rain drop effect like 3d matrixworld screensaver, how to make the gradient color when text() chars vertically on screen?
Here's a version that does vertical text, right-to-left. Note that using
gsub(".")
only works with single-byte characters (The smiley at the end won't display). If you want to parse multi-byte text, you should use Lua 5.3's UTF8 library.EDIT: changed the name of the shader variable from step to gradStep
i get this error
That's interesting. GLES does have a built in function called
step
I just realised, so it's probably a reserved word. But it's interesting that it works on my setup and not yours (different versions of GLES between iOS versions? Are you iOS 7 still?)@Jmv38 in the above code, I changed the name of the variable from step to gradStep, could you see if you still get the error with this version?
ios7. Now it runs. but the text is completely changed to a text that is meaningless. Strange...
It's vertical, and right-to-left
here is what i get ???

That's what it's meant to do. I know, not very useful.
@yojimbo2000, @West--
I overall just want to say that these rock. Like layer tennis, as @Ignatz said.
In playing with it I think I've found an error and I wonder if you can tell me how to correct it.
Setting the aspect parameter of the shader to account for the screen dimensions seems to make the circle become drawn off-center.
If you set the aspect to 1, 1, and the centre parameter to 0.5, 0.5, the ellipse is drawn with its center right in the corner, as it should be.
But with the same centre setting, if you set the aspect to (WIDTH/HEIGHT, 1), which is what I think you do in this code, the ellipse is no longer centered in the corner. It seems to get drawn somewhat to the right of where it should get drawn. In effect, the centre parameter no longer determines the actual center.
Can either of you guys advise me how to adjust it so it centers around the centre? I don't understand from looking at the shader code what exactly is going on.
@UberGoober I had a quick look at it - it seems that the issue is to do with the way Codea handles screen width at setup.
I suspect that it all works ok if you have the sidebar present but when you move to displayMode(FULLSCREEN) you get the issue you describe.
If you set the aspect ratio to be (749/HEIGHT,1) then this should resolve the problem (assuming a normal fullscreen width of 1024). Horribly clunky but will work.
749 is the width returned (in my iPad) when I first start Codea - it is also the width of the drawing area with the sidebar present.
I suspect that when the shader is set up it is reading in the width of the texture area to be the fullscreen width minus the sidebar (in vTexCoord).
However, my shader knowledge is extremely limited and I looked at this a fair while ago
@Simeon @John Can you shed any light on what's going on? Feels like a bug
Here is a slightly modified version of the original - swap in and out the displayMode and gradientAspect values.
Change the displayMode to OVERLAY and comment out the GradientAspect in draw and the slider for GradientAspect should work after you center it image.
You guys seem to have hit it exactly, in my testing.
@yojimbo2000, @West:
Below is my attempt to generalize the code to make it an all-purpose ellipse drawer, please give it a run and let me know what you think.
for
loops...For a little while now, I've been trying to do it myself, but I'm coming up against the whole thing where shaders are in a different language.
When I inspect the shader code, it seems like all you'd have to do is take the
no
which is defined asconst int no= 3
and make it a different number. It seems like it should all work if you could manually change that number.Trying that gets me some the error messages, and trying to adjust for those messages gets me some other error messages, from all of which I've learned these two things:
no
changeable, it can't be declaredconst
anymoreno
function as a parameter in an array declaration, it must be declaredconst
....so solving the catch-22 there is beyond me. This seems like it must be possible to do, but I'm stymied. Any help?