Creator 版本: 3.8.2
目标平台: Android
我在Web平台可以生效 Shader, 执行的 Spine 变色, 调用 shaderChange 函数设置 Shader, 调用 shaderColor 来改变 Shader 参数, 但在原生平台, JSB 里的代码是使用原生不行, 看论坛加的, 现在是 Spine 在原生设置 Shader 直接就消失了~~>_<
/**
* 设置 Shader
* 在安卓机上有问题, 会导致 spine 不显示
*
* @param shader
*/
public shaderChange(shader: EffectAsset | null) {
if (shader === GSpine.spineDef || shader === null) this.shaderData = { type: GSpineShaderType.Def }
if (shader) {
const material = new Material()
material.reset({ effectAsset: shader })
this.material = material
if (JSB) {
this.setSharedMaterial(material, 0)
// @ts-ignore
// this._renderComponent._updateMaterial()// 报错崩溃
} else {
let cache = this['_materialCache']
for (let key in cache) {
if (cache[key].effectAsset !== material.effectAsset) {
// @ts-ignore
cache[key] = material
// $g.log(`[Shader Bug]重置了材质, 请检查是否有问题`)
}
}
// this.setSharedMaterial(GSpine.spineColor, 0)
// this.updateMaterial(GSpine.spineColor)
// this.activateMaterial(GSpine.spineColor)
}
} else {
if (JSB) {
const material = new Material()
material.reset({ effectAsset: GSpine.spineDef })
this.setSharedMaterial(material, 0)
// @ts-ignore
// this._renderComponent._updateMaterial() // 报错崩溃
} else {
this.material = null
}
}
}
/** 执行变色 */
public shaderColor(color: Color, rate: number) {
let change = false
if (this.shaderData.type !== GSpineShaderType.Color) {
change = true
this.shaderData = { type: GSpineShaderType.Color, color, rate }
this.shaderChange(GSpine.spineColor)
}
const c_run = !this.shaderData.color.equals(color)
const r_run = this.shaderData.rate !== rate
if (change || c_run || r_run) {
this.shaderData.color = color
this.shaderData.rate = rate
if (JSB) {
let m = this.getSharedMaterial(0)
if (m) {
if (change || c_run) m.setProperty('u_color', color)
if (change || r_run) m.setProperty('u_rate', rate)
}
} else {
let matCaches = this['_materialCache']
for (let key in matCaches) {
let m = matCaches[key]
if (change || c_run) m.setProperty('u_color', color)
if (change || r_run) m.setProperty('u_rate', rate)
}
}
}
}