(Solved) Shader output is black

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.

vertex shader:

uniform mediump mat4 view_proj;

// positions are in world space
attribute mediump vec4 position;
attribute mediump vec2 texcoord0;

varying mediump vec2 var_texcoord0;

void main()
{
    gl_Position = view_proj * vec4(position.xyz, 1.0);
    var_texcoord0 = texcoord0;
}

fragment shader

// The texture coordinate for the fragment
varying mediump vec2 var_texcoord0;

// Texture sampler input
uniform lowp sampler2D DIFFUSE_TEXTURE;
uniform lowp vec4 tint;

void main()
{
	 // Pre-multiply alpha since all runtime textures already are
    lowp vec4 tint_pm = vec4(tint.xyz * tint.w, tint.w);
    gl_FragColor = texture2D(DIFFUSE_TEXTURE, var_texcoord0.xy) * tint_pm;
}

In the builtin render script i’ve then added my new predicate in init()

self.tex_pred = render.predicate({"test"})

and draw it in the update function

render.draw(self.tex_pred)

The material is attached to a sprite but all i’m getting is black object when I run the game.
what am I missing?

btw. my material and shader files are located in main/level

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?

Try to set the “tile” tag on your material. If that works then it is most likely the render script. Would you mind posting the render script?

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?

Is this actually true @sicher? Does the changes actually get included when you build and launch?

I’m testing here and it seems like edits to the built in render script does not get picked up.

I know that changes in builtins was possible earlier. Has this changed @Ragnar_Svensson?

Here’s an issue on this: https://github.com/defold/editor2-issues/issues/231

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.

4 Likes

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 :slight_smile:

Edit: it works \o/

Note that the manuals are for Editor 1. Editor 2 is not yet ready and there is a design issue for this, linked above.

2 Likes

Oh right, silly me. I knew that already.