Alright i’ve been trying to understand how shaders work in Defold, I know CG and have used GLSL before in another engine but I don’t see how to apply it here. I’ve read all three pages which are involved in the shader process (material, shader, and rendering) but I don’t think they do a good job in combining those into an actual use case. You only see bits and pieces of code and a few images from the material window but nothing about connecting your shader to the rendering process?
From what I understand I need to connect my material to the rendering pipeline. Does this imply that I add my material in the builtins-> default.render_script or do I make a new render script?
Atm. I’m trying to replicate the builtin sprite mat.
I have initialised the constants in the material and set a tag.
Hmmm… I don’t see any immediate problems. A few things:
The builtins are read-only. In Editor 2 you can do temporary changes to those files, build and see the results. However, your changes will not be saved to disk.
Does this mean that you see the object but it’s not textured? What happens if you set the frag color to a constant:
// red
gl_FragColor =vec4(1.0, 0.0, 0.0, 1.0);
File locations have no impact at all. The references from the material file to shader programs needs to be set correctly, but once they are set you can move files and the references will update automatically.
Not exactly. The material has a set of tags that “classifies” what it draws from a rendering point of view. The render script chooses which tags to draw when, by grouping tags into predicates. Typical tags for different scenarios would be “shadow-caster”, “glowing-sprite”, etc. You only need to use a custom render script if you want to draw tags differently then the default does. If you introduce a new material with a tag already being drawn, and you assign that material to something that exists in the game world, those shaders will run without you having to do anything else.
You need to debug it, like @sicher described. Try to narrow down the problem by elimination. Change the shader, if you can hard-code the color and see it on screen, it would for example mean the texture sampling is giving you black pixels.
Ah so I do have to make a new render script then, where to I put that and how to connect it?
No It displays nothing. Nothing happens if I set the color constant either.
The thing is, it’s an exact copy of the sprite.material, sprite.vp, sprite.fp files except the names are different and they are placed in my main/level folder.
There are still sources for errors. For example, a common mistake is to forget to relink the shaders in the material so it still points to the builtin sprite shaders instead of your new ones.
In Defold you are free to arrange your files any way you like. We do not require anything from the folder structure except the requirement of a game.project file in the root of the project. Maybe put the render script in a folder named render and name the file custom.render_script? Next step would be to create a .render file (perhaps custom.render) and let that point to custom.render_script, and finally in game.project, in the bootstrap section, you point to custom.render.
My material is referenced correctly, so that’s not the problem.
I tried using one of the already defined tags (tile) and now it works, so it must be something with the render script. You say it’s not possible to save changed in the builtin but you can test, so shouldn’t it work?
I did and it works now so it must be the render script.
I’m still using the builtin, so i’ve basically just added
function init(self)
...
self.tex_pred = render.predicate({"test"})
...
and my update
function update(self,dt)
...
render.set_projection(vmath.matrix4_orthographic(0, render.get_width(), 0, render.get_height(), -1, 1))
render.draw(self.tile_pred)
render.draw(self.particle_pred)
render.draw(self.tex_pred) -- my new material
render.draw_debug3d()
...
as far as I can see, that should be enough to get it working?
That’s not really a “released feature”, it’s better to do it properly in user files. Even if it worked for render scripts, making temporary changes in builtin-files could be a source of confusion and errors.
Okay that would be nice to have that clarified in the manual. I find it confusing to be able to edit the builtin files if you’re not supposed to. I like having access to them but there should be a popup or some notice that these files are read-only.
I’ll make a new render and render script and hopefully that’ll make it work