Hello all!
I take advantage of Cocos Creator model 3.8.3.
I wish to write a shader. On this shader I want to have the ability to decide the place of a fraction relative to the geometry it belongs to. Whether or not this place can be in normalized coordinates from 0 to 1 or in a price from 0 to the width/peak of the sprite – 1, it’s not so vital to me. I want the shader solely for the UI, it will likely be used with the Sprite element.
I haven’t labored with shaders in Cocos Creator earlier than. Right here they’re referred to as results. I created my impact file check from the contents of the file at “inner/results/for2d/builtin-sprite.impact”. I created a check materials and used the check impact in it.
I created a 200×400 picture with this look:
Added it to the mission, positioned it on the scene. If you don’t set CustomMaterial within the Sprite element, then altering the Grayscale flag is mirrored within the look of the sprite.
However as quickly as I set my check materials in CustomMaterial, all the sprite turns white. Usually, I’ve to allow the USE TEXTURE discipline within the materials itself. Equally, I’ve to allow the IS GRAY discipline myself. I perceive that relying on the preprocessor definitions, there are various variants of the identical shader. Apparently, the Sprite element itself selects the specified variant from the usual ones. Properly, is there any technique to make it choose the specified variant from my shader? Finally, I don’t change the construction or the listing of preprocessor definitions – every part stays as is.
Usually, to this point I’ve solely checked the USE TEXTURE field within the check materials.
If we discuss in regards to the technique of acquiring the fragment coordinate, then when utilizing a texture, I might use UV. So far as I keep in mind, the middle of the sprite could be the fragment at coordinates (0.5, 0.5). I made a decision so as to add the next traces of code to the shader:
if (uv0.x > 0.45 && uv0.x 0.45 && uv0.y
In fact, that is provided that there’s a texture:
The anticipated outcome:
However that is within the editor. And when you run it within the browser, you will note:
Why is that?
I modified the paint situation to:
if (uv0.y > 0.5) {
Experimented with different footage and seen that if the image is in an atlas, the outcome could also be incorrect. Apparently, within the first case, my sprite was pasted someplace when launched within the browser.
Then the query is, how do I take note of within the shader if the sprite is within the atlas? That’s, how do I get native UV?
That’s the UV factor. Can’t Cocos Creator output a sprite with out a texture in any respect? Doesn’t it simply create two triangles for them? At the start of the fragment shader I wrote:
return vec4(0.25, 0.5, 1, 1);
Nevertheless, nothing was output once I cleared the Sprite Body discipline of the Sprite element (the dimensions of the UITransform element was 200×400).
Usually, how else can you discover out from a fraction shader in regards to the place of a fraction relative to the geometry to which it belongs?