cocos shader敌人受到攻击改变颜色
视频 [视频](https://www.bilibili.com/video/BV1SMpXzTErd/?
https://www.bilibili.com/video/BV1SMpXzTErd/?vd_source=fcaf9af78115af41b55fe1f11da57759vd_source=fcaf9af78115af41b55fe1f11da57759)
将前面的shader简单改下
取消根据dist画光柱 还是判断图片是否透明
o.rgb = original.rgb + glowEffect ;这个取消
换成glowcolor.rgb 用选择的颜色称为受到攻击后变得颜色
然后价格脚本 子弹碰撞后再敌人node下shouji脚本有个将material中shader uniform变量切换的
在shader中写了glowspread超过0会将图片本色换成受到攻击颜色
在脚本中发生碰撞后将0超过1
然后再0.4秒后回到1
otherCollider.node.getComponent(direnshouji).direnshouji();
子弹碰撞后调用othercollider中的敌人node下的脚本
onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact |null){//console.log("ziji",selfCollider.node.name);//console.log("zhuang",otherCollider.node.name);const guaiwu=otherCollider.node;this.showlizi(selfCollider.node.position);guaiwu.getComponent(enemyhit).shoushang(this.danfahit);const bullet=selfCollider.node;if (bullet && bullet.isValid) {// 先从数组中移除const index = this.fashezidansz.indexOf(bullet);if (this.bulletsToRemove.indexOf(bullet) === -1) {this.bulletsToRemove.push(bullet);}let xue=otherCollider.node.getComponent(enemyhit).getxue();otherCollider.node.getComponent(direnshouji).direnshouji();if(xue<=0){//console.log('huan',this.guai.name);}// 标记需要销毁的子弹,而不是立即销毁// 然后回收子弹}
import { _decorator, Component, Node, Sprite } from 'cc';
const { ccclass, property } = _decorator;@ccclass('direnshouji')
export class direnshouji extends Component {q:number=0;qo:number=0;qoo:number=0;start() {}update(deltaTime: number) {}
direnshouji(){const me=this.node.getComponent(Sprite);this.q=(this.q+0.1)%19;this.qoo=(this.qoo+0.001)%0.2;const time = Date.now();this.qo=(this.qo+0.1)%1;//console.log(this.q);me.material.setProperty('glowSpread',this.qo);this.qo=0;this.scheduleOnce(()=>{me.material.setProperty('glowSpread',this.qo);},0.4);
}
}
vec4 frag () {vec4 o = texture(cc_spriteTexture,uv);vec4 original=o;// 固定光柱宽度和移动速度float beamPos = mod(glowSpread -0.3, 3.0) ;float dist = abs(uv.x - beamPos);//if(dist < glowIntensity) { // 固定光柱宽度为0.1if(o.a>0.0){
// 计算光柱强度 (中心最强,边缘最弱)float strength = 1.0 - dist / glow;// 从白色渐变到 glowColorvec3 glowEffect = mix(vec3(1.0), glowColor.rgb, uv.x / glowIntensity);// 应用发光效果if(glowSpread>0.0){o.rgb=glowColor.rgb;//o.rgb = original.rgb + glowEffect ;//o.a = max(original.a, strength);}}//}return o;}