hello
i’m currently working on fashion project
and i want to users allow to add some stickers to there cloth
i create material plugin for this
and my project work with only hard coded shaders
like this
if (shaderType === "fragment") {
return {
CUSTOM_FRAGMENT_DEFINITIONS: `
uniform sampler2D sticker_0_txtr;
`,
CUSTOM_FRAGMENT_BEFORE_LIGHTS: `
surfaceAlbedo = baseColor;
float halfSize = sticker_0_size / 2.0;
vec2 uv = vAlbedoUV;
uv.x *= 1.0 / sticker_0_size;
uv.y *= 1.0 / sticker_0_size;
// todo this is not dynamic yet
uv.x += 0.5;
uv.y += 0.0;
if ((sticker_0_uvof.x - halfSize) < vAlbedoUV.x && vAlbedoUV.x < (sticker_0_uvof.x + halfSize)) {
if ((sticker_0_uvof.y - halfSize) < vAlbedoUV.y && vAlbedoUV.y < (sticker_0_uvof.y + halfSize)) {
vec4 sticker = texture2D(sticker_0_txtr,uv);
surfaceAlbedo = mix(surfaceAlbedo, sticker.rgb, sticker.a);
}
}
`,
};
}
and that’s work find but i want to be able to add or remove stickers
so i change my code to something like this
if (shaderType === "fragment") {
if (this.stickers === undefined) return;
let CUSTOM_FRAGMENT_DEFINITIONS = "";
this.stickers.forEach(
(_sticker, i) =>
(CUSTOM_FRAGMENT_DEFINITIONS += `uniform sampler2D sticker_${i}_txtr;`)
);
let CUSTOM_FRAGMENT_BEFORE_LIGHTS: string = "surfaceAlbedo = baseColor;";
this.stickers.forEach(
(_sticker, i) =>
(CUSTOM_FRAGMENT_BEFORE_LIGHTS += `
float halfSize = sticker_${i}_size / 2.0;
vec2 uv_${i} = vAlbedoUV;
uv_${i}.x *= 1.0 / sticker_${i}_size;
uv_${i}.y *= 1.0 / sticker_${i}_size;
// todo this is not dynamic yet
uv_${i}.x += 0.5;
uv_${i}.y += 0.0;
if ((sticker_${i}_uvof.x - halfSize) < vAlbedoUV.x && vAlbedoUV.x < (sticker_${i}_uvof.x + halfSize)) {
if ((sticker_${i}_uvof.y - halfSize) < vAlbedoUV.y && vAlbedoUV.y < (sticker_${i}_uvof.y + halfSize)) {
vec4 sticker = texture2D(sticker_${i}_txtr,uv);
surfaceAlbedo = mix(surfaceAlbedo, sticker.rgb, sticker.a);
}
}
`)
);
return {
CUSTOM_FRAGMENT_DEFINITIONS,
CUSTOM_FRAGMENT_BEFORE_LIGHTS,
};
}
return null;
}
and pass stickers to materialPlugin class
but i think “getCustomCode” method is calling in “super” functions and “this.stickers” is undefined
i disable plugin and enable it in setTimeout but still material plugin not working
“getCustomCode” is calling again and this.stickers are avaliable but shader code is not recompiled i guss
is there any way to force material plugin (or mareial ) to recompile??
thanks for your time