Move and die (shader/material/model weirdness) (SOLVED)

I’ve been playing around with the very cool bezier toy that roccosaienz contributed to the forum.

I tried to implement similar functionality to the project I’m working on but failed. After a few hours of futzing with it, I discovered that the object must reside at 0,0 to work. Otherwise the curve is not drawn.

Here is a minimal example that works. Just run it. To see the problem, move the “root” GO (in the main.collection) and then run it again. Poof! The curve disappears.

So I think I can get the thing to work in my game, but I REALLY want to know why changing the GO’s position destroys the image.

If you change the model’s position slightly, by 1 pixel, the image deforms. So I suppose there’s some multiplicative effect based on the model’s location, but I’m otherwise in the dark.

Bezier example.zip (996.4 KB)

2 Likes

Hey @Musenik, yes, the shader works only when the go is at (0,0). This is a limitation that was okay for my game.

I am quite sure that the shader may be rewritten to work in any position, but at the moment I am quite busy.

Anyway you may still use it: the initial and final points must be given in world coordinates and also the two tangents are in world coordinates. Should you be in trouble create an empty go, move it freely as you need and then use go.get_world_postion() to get its world coordinates and pass them to the vertex constants of the go with the bezier shader (that is fixed at (0,0)). I hope this is somehow clear… it is just a stupid suggestion and clearly not optimazed.

However, I am happy you found the bezier shader worth considering.

EDIT: if you want to know why the go must be at (0,0) looks at the vertex shader bezier.vp. At line 21 there is one (quite obscure, yes!) comment: // remap [-1, 1] -> [0, 1]. The shader wants the points of the model to form a square with vertices (1,1), (-1,1), (-1,-1) and (1,-1) with the two horizontal sides divided in many segments (the shader does not care about the number of such segments). So if you move the go then the square has different vertices and this spoils the coordinate computations in the vertex shader.

Edit2: you may try to use “local coordinates” in the material. I am not sure and I cannot try It now myself. But when I wrote the shader, that option was not possible with Defold… :slight_smile:

Ciao, Rocco.

4 Likes

Setting the material to use local coordinates prevented the image from disappearing after moving the model’s position. Thanks!

I have a much better feel for what’s happening in the shader.

cheers
Keith

3 Likes