Shader doesn't work on brower, or run it in the editor

[GradientSprite.ts]

@ccclass("GraidentSprite")
@executeInEditMode(true)
export class GraidentSprite extends Component {
@property
  private _startColor: Color = new Color(255, 0, 0, 255);
  @property({ type: Color })
  set startColor(value: Color) {
    this._startColor = value;
    this.updateMaterial();
  }
  get startColor(): Color {
    return this._startColor;
  }

  // endColor, angle, edge1, and edge2 were declared in the same way as startColor.

private updateMaterial() {
    if (this.sprite && this.sprite.customMaterial) {
      const material = this.sprite.getMaterialInstance(0);

      material.setProperty(
        "startColor",
        new Vec4(
          this._startColor.r / 255,
          this._startColor.g / 255,
          this._startColor.b / 255,
          this._startColor.a / 255
        )
      );
      material.setProperty(
        "endColor",
        new Vec4(
          this._endColor.r / 255,
          this._endColor.g / 255,
          this._endColor.b / 255,
          this._endColor.a / 255
        )
      );
      material.setProperty("angle", this._angle);
      material.setProperty("edge1", this._edge1);
      material.setProperty("edge2", this._edge2);

      this.sprite.setMaterialInstance(material, 0);
      this.sprite.markForUpdateRenderData(true);
  }  
}

[gradient_2colors.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:
        startColor: { value: [1.0, 1.0, 1.0, 1.0], editor: {type: color} }
        endColor: { value: [1.0, 1.0, 1.0, 1.0], editor: {type: color} }
        angle: { value: 45 }
        edge1: { value: 0.1 }
        edge2: { value: 0.3 }
}%

CCProgram sprite-vs %{
  precision highp float;
  #include <builtin/uniforms/cc-global>
  #if USE_LOCAL
    #include <builtin/uniforms/cc-local>
  #endif
  #if SAMPLE_FROM_RT
    #include <common/common-define>
  #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 <builtin/internal/embedded-alpha>
  #include <builtin/internal/alpha-test>

  in vec4 color;

  in vec2 uv0;
  #pragma builtin(local)
  layout(set = 2, binding = 11) uniform sampler2D cc_spriteTexture;

  uniform ARGS{
    vec4 startColor;
    vec4 endColor;
    float angle;
    float edge1;
    float edge2;
  };

  vec4 frag () {
    vec4 o = vec4(1, 1, 1, 1); 
    o *= CCSampleWithAlphaSeparated(cc_spriteTexture, uv0);

    float rad = radians(angle);
    float diagCoord = uv0.x * cos(rad) + uv0.y * sin(rad);
    
    o.rgb *= mix(startColor.rgb, endColor.rgb, smoothstep(edge1, edge2, diagCoord));
    o *= color;

    ALPHA_TEST(o);
    return o;
  }
}%

[topic]
When I use above codes, the shaders I apply to the sprite work fine when I’m in the editor, or when I run it in an emulator.

But when I run it in the brower, or when I run it in the editor and view it, the shaders don’t get applied.

I’ve tried dozens of ways to get it to work, but I can’t figure out why it’s not working. Can someone help me with my issue?

Cocos 3.8.3
VSCode 1.90.2

Check the error messages in browser console, please

1 Like

OMG… I made a very basic mistake.

I didn’t get any error messages when I ran it in the editor, so I didn’t look at it in the browser, assuming there was nothing there.
Thanks for the help.

But I got an error saying asset not found. I’m new to cocos creator so I don’t know a solution.

ERROR : The asset 346ed7e7-bcb7-4ada-89da-c9f87571895b@fab0c is missing!
ERROR : The asset 346ed7e7-bcb7-4ada-89da-c9f87571895b is missing!

Given the poor representation of the game screen, I’m guessing the asset my browser isn’t finding is a shader or material… or both? What can I do to fix this?

Not sure what asset is that. It could be anything.

You can copy its ID and search it in editor @ asset browser.

Good luck and have fun :blush:

1 Like

I ended up not finding the asset with the UUID I mentioned in my last question, and everything else was fine without it.
Just to be on the safe side, I deleted all assets and reloaded them, but I’m still having the same problem.

I’m wondering if I should give up on using materials and shaders and just make them images.

I’ve been looking for a solution for 4 days now and can’t seem to figure it out. Anyway, thanks for your help.

First try this:
image

You can also do this:

Just paste the UUID into search bar in VSC and you will see some info.

After that scroll up and you will findout Node’s name like this
image

2 Likes

Thank you so much for telling me how to find that component, but it’s too bad it’s not related to the original problem, which is the shader. ;-(

It’s simply an error log caused by a missing asset in some component.

Applying a gradient shader with 5 colors to the label works fine, but applying the same shader to the spirte doesn’t work, and the 2-color graident in the code I posted earlier doesn’t work either.

Is there a difference between applying the material to the sprite and the label?


I wondered if the shader was not working properly because of the setProperty code, so I applied a graident shader with 5 colors that I never change in my code.

Unsurprisingly, the 2-color gradient that I parameterize in the script doesn’t work either.

They both have a color in common that corresponds to the start color, and when I adjust the edge value of the smoothstep, the entire thing is covered by the mixed middle value.

The problem was the texture setting in atlas.
I was using a non-power-of-two atlas texture, with Filter Mode set to Bilinear and Wrap Mode set to Clamp.

After changing the settings in ATLAS for other issues, I realized that it was working correctly.

For now, I changed the settings to Neartest(None) and Wrap, and it’s working fine. I also changed the settings of my atlas texture to make it a POT.
Just in case anyone else is having similar issues, I thought I’d share.

1 Like

Maybe this may help

1 Like