cocosCreator3.X 如何动态设定Shader?

如题:


通过代码来改变shader,参数是改变了,但是程序并没有效果
NISA8U12_Q%4MLA4_F%G)J
下面是shader的effect:


CCEffect %{
techniques: 
  - passes: 
      - vert: sprite-vs:vert
        frag: sprite-fs:frag
        depthStencilState: 
          depthTest: false
          depthWrite: false
        blendState: 
          targets: 
            - blend: true
              blendSrc: src_alpha
              blendDst: one_minus_src_alpha
              blendDstAlpha: one_minus_src_alpha
        rasterizerState: 
          cullMode: none
        properties: 
          alphaThreshold: 
            value: 0.5
          uIntensity: 
            value: 4.37
          uSeed: 
            value: 0.1
}%
        
CCProgram sprite-vs %{
  precision highp float;
  #include <cc-global>
  #if USE_LOCAL
    #include <cc-local>
  #endif
  #if SAMPLE_FROM_RT
    #include <common>
  #endif
  in vec3 a_position;
  in vec2 a_texCoord;
  in vec4 a_color;
  out vec4 color;
  out vec2 uv0;
  vec4 vert () {
    vec4 pos = vec4(a_position, 1);
    #if USE_LOCAL
      pos = cc_matWorld * pos;
    #endif
    #if USE_PIXEL_ALIGNMENT
      pos = cc_matView * pos;
      pos.xyz = floor(pos.xyz);
      pos = cc_matProj * pos;
    #else
      pos = cc_matViewProj * pos;
    #endif
    uv0 = a_texCoord;
    #if SAMPLE_FROM_RT
      CC_HANDLE_RT_SAMPLE_FLIP(uv0);
    #endif
    color = a_color;
    return pos;
  }
}%

CCProgram sprite-fs %{
  precision highp float;
  #include <embedded-alpha>
  #include <alpha-test>
  #include <cc-global>
  in vec4 color;
  #if USE_TEXTURE
    in vec2 uv0;
    #pragma builtin(local)
    layout(set = 2, binding = 10) uniform sampler2D cc_spriteTexture;
  #endif

  uniform Constant {
		float uIntensity;
		float uSeed;
	};

vec2 c_UV0_main_4() {
	    return uv0;
	}
	float c_BurnOut_fr_3(vec2 c, float seed) 
	{
	return fract(43.0 * sin(c.x + 7.0 * c.y) * seed);
	}
	float c_BurnOut_fn_3(vec2 p, float seed)
	{
	    vec2 i = floor(p), w = p - i, j = vec2(1.0, 0.0);
	    w = w * w * (3.0 - w - w);
	    return mix(mix(c_BurnOut_fr_3(i, seed), c_BurnOut_fr_3(i + j, seed), w.x), mix(c_BurnOut_fr_3(i + j.yx, seed), c_BurnOut_fr_3(i + 1.0, seed), w.x), w.y);
	}
	float c_BurnOut_fa_3(vec2 p, float seed) 
	{
	    float m = 0.0, f = 2.0;
	    for (int i = 0; i < 9; i++) {
	    
	m += c_BurnOut_fn_3(f * p, seed) / f; 
	    
	f += f;
	    }
	    return m;
	}
	vec4 c_BurnOut_main_3(vec4 MainColor, vec2 UV, float Intensity, float Seed) 
	{
	    float t = fract(Intensity * 0.9999);
	    float cc = smoothstep(t / 1.2, t + 0.1, c_BurnOut_fa_3(3.5 * UV, Seed));
	    vec4 c = vec4(cc);
	    c = MainColor * c;
	    c.r = mix(c.r, c.r * 15.0 * (1.0 - c.a) * 8.0, Intensity);
	    c.g = mix(c.g, c.g * 10.0 * (1.0 - c.a) * 4.0, Intensity);
	    c.b = mix(c.b, c.b * 5.0 * (1.0 - c.a), Intensity);
	    return vec4(c.rgb, c.a);
	}
	vec4 c_Output_main_1(vec4 Color) { 
	    return Color; 
	}
	vec4 shaderfx()                                    
	{                                              
	    vec2 out_UV_UV0_4 = c_UV0_main_4();
	    vec4 out_Output_BurnOut_3 = c_BurnOut_main_3(texture(cc_spriteTexture, uv0), vec2(out_UV_UV0_4), uIntensity, uSeed);
	    return c_Output_main_1(vec4(out_Output_BurnOut_3));
	}                                              
	
  vec4 frag () {
     vec4 o = shaderfx();
       o *= color;
     ALPHA_TEST(o);
     return o;
   }
 }%

通过控制uIntensity,或者uSeed都能达到效果,现在改变了uIntensity,但是对象没有跟着变化,是出了什么问题?是需要调用update吗?材质代码中好戏没有相关的api,求解。

给你个例子,变量名自己修改

this.renderer.material.setProperty('tileAndType', v2(this._index, this._state));

感谢大神,3.x这样写是可以的

sp.renderData.material.setProperty("uIntensity",value)

谢谢

该主题在最后一个回复创建后14天后自动关闭。不再允许新的回复。