I want to create just simple rectangular grid with shaders. I tried to search but haven’t found how to’s or examples, not for shader’s code itself (internet is full of such examples), but how to apply that shader in defold.
Am I understand it right. That instead of adding a sprite and material to it, I just should create texture in code with resourse.create_texture() and then somehow apply material to this texture (haven’t found how). Or I’m heading in the wrong direction?
Any example?
Just a frame grid with transparent squares and only visible grid
Maybe it is simpler to just create it with a factory and a sprite of 1 square frame? But then I’m thinking of changing the transparency of it on runtime, so double frames would be half as transparent right?
So this approach would require more memory, but faster at runtime?
A material with a shader program that is used by the model component
A shader program that draws a grid
1. Mesh to draw on
Here you can for instance add a model component to a game object and use the /builtins/assets/meshes/quad.dae which is a basic rectangle
2. A material with a shader program that is used by the model component
Copy the default model.material and model.vp and model.fp from /builtins/materials. Edit your copy of the model.material so that it uses your copied model.vp and model.fp. In the example below I decided to name my material grid.material:
One additional thing to consider is how the mesh should be rendered. You can keep the default projection in the render script and simply stretch the game object with the mesh so that it covers the entire screen. The mesh will be 1x1 pixel but if you stretch the game object to the size of the display it will obviously cover the entire screen. The other option is to add a perspective camera and position it so that your mesh is rendered properly.
Here’s me using the default projection and simply stretching the game object with the mesh:
Not exactly a model, but there will be an underlying set of triangles that are drawn using the sprite shader which will be sampling pixels from a texture, optionally applying a tint to the pixel color.
@britzl Could I disturb one more time?
I have a code for a grid (it’s not exactly what I want, but for now it draws some grid). But to work futher seems like I can’t make it transparent whatever I do. I have a scene with this model and some sprite with an image. And it always draws red grid, but the image isn’t visible. Here is grid.fp
#version 330
in vec4 var_position;
in vec2 var_texcoord0;
out vec4 out_color;
void main()
{
// How often to draw lines
float interval = .1;
// Line thiccness
float thicc = .003;
vec4 color = vec4(0, 0, 0, 0);
// Draw gridlines based on UV (float offset used to center the grid)
float offset = (thicc / 2) - ((1 - interval) / 2);
if(mod(var_texcoord0.x + offset, interval) < thicc || mod(var_texcoord0.y + offset, interval) < thicc) {
color.r = 1;
color.a = 1;
}
out_color = vec4(color.rgb * color.a, color.a);
}
Since you are drawing a mesh/model there’s some extra tricks to transparency. I’m honestly not sure what the “correct” approach is, but you can try discarding pixels with an alpha value below a threshold, instead of setting alpha to 0. Here’s me doing the same in a noise shader: