CocosCreator 使用引擎自带材质

//-- 使用引擎自带材质
let mm = cc.Material.getBuiltinMaterial("2d-gray-sprite")     
node.getComponent(cc.Sprite).setMaterial(0, mm)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
新版Creator 2.1.2,终于支持 ShaderHelper 了 let ShaderProperty = cc . Class ({ name : 'ShaderProperty' , properties : { key : '' , value : '' , } }); cc . Class ({ extends : cc . Component , properties : { effect : cc . EffectAsset , //effect资源 speed : 0.01 , //控制动态Shader的time参数 props : [ ShaderProperty ], //shader参数 }, start () { if (! this . effect ) { return ; } //获取精灵组件 let sprite = this . node . getComponent ( cc . Sprite ); if (! sprite ) { return ; } //实例化一个材质对象 let material = new cc . Material (); //在材质对象上开启USE_TEXTURE定义 material . define ( 'USE_TEXTURE' , true ); //为材质设置effect,也是就绑定Shader了 material . effectAsset = this . effect ; material . name = this . effect . name ; //将材质绑定到精灵组件上,精灵可以绑定多个材质 //这我们替换0号默认材质 sprite . setMaterial ( 0 , material ); //从精灵组件上获取材质,这步很重要,不然没效果 this . material = sprite . getMaterial ( 0 ); //初始化参数 this . time = 0 ; this . props . forEach ( item => this . material . setProperty ( item . key , item . value )); }, /** * 在update事件中更新材质参数 * 不同的Shader这可能需要重写 */ update () { if (! this . material || ! this . speed ) { return ; } if ( this . flag ) { this . time += this . speed ; } else { this . time -= this . speed ; } if ( this . time >= 1.2 ) { this . flag = 0 ; } else if ( this . time <= - 0.2 ) { this . flag = 1 ; } //更新Shader代码中的time参数 this . material . setProperty ( 'time' , this . time ); }, });

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值