Cocos Shader入門學(xué)習(xí)案例,消融效果

根據(jù)對 來點花樣!使用噪聲圖實現(xiàn)不規(guī)則溶解效果 的學(xué)習(xí)。

在學(xué)習(xí)Shader的過程中做的小案例,嘗試在 Creator v2.4.10 使用噪聲圖實現(xiàn)物體的消融效果。

噪聲圖:

640.png

Shader的編寫:

CCEffect 中添加 propertie:

properties:
    dissolveMap: { value: white, editor: { tooltip: '噪音圖'} }
    dissolveThreshold: { value: 0.3, editor: { tooltip: '溶解閾值'} }
    edgeColor: { value: [1, 1, 1, 1] ,editor: { type: color, tooltip: '邊緣顏色'}}
    edgeWidth: { value: 0.05, editor: { tooltip: '邊緣寬度'} }

修改片段著色器fs:

聲明部分:

  #if USE_TEXTURE
  uniform sampler2D dissolveMap; // 噪音圖
  #endif
 
  uniform Dissolve {  // 消融相關(guān)
    vec4 edgeColor;
    float edgeWidth;
    float dissolveThreshold;
  };

入口函數(shù):

void main () {
    vec4 o = vec4(1, 1, 1, 1);
 
    #if USE_TEXTURE
      vec4 dissolveValue = texture2D(dissolveMap, v_uv0);
 
      if (dissolveValue.r < dissolveThreshold) { // 溶解
        discard;
      }
      
      o = texture2D(texture, v_uv0);
    #endif
 
    o *= v_color;
    if (dissolveValue.r < dissolveThreshold + edgeWidth && o.a > 0.0) { // 邊緣
      float edge = smoothstep(dissolveThreshold - edgeWidth, dissolveThreshold + edgeWidth, dissolveValue.r);
      vec4 finalColor = mix(o, edgeColor, edge);
      o = finalColor;
    }
 
    gl_FragColor = o.rgba;
  }

使用腳本控制節(jié)點逐漸消融:

const {ccclass, property} = cc._decorator;
 
@ccclass
export default class DissolveEffect extends cc.Component {
 
    @property
    dissolveInterval: number = 0.01;
    @property
    dissolveStep: number = 0.01;
 
    start () {
        const material = this.getComponent(cc.Sprite).getMaterial(0);
 
        material.setProperty('dissolveThreshold', 0);
        
        this.schedule(()=>{
 
            let dissolveThreshold = material.getProperty('dissolveThreshold', 0);
            dissolveThreshold += this.dissolveStep;
            material.setProperty('dissolveThreshold', dissolveThreshold);
 
            // console.log(dissolveThreshold);
            
        }, this.dissolveInterval, 1/this.dissolveStep);
    }
}

最終效果:

shader.gif
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容