Monday, August 22, 2022
HomeGame DevelopmentSet vertex colour of sprite in 3.5 - Cocos Creator

Set vertex colour of sprite in 3.5 – Cocos Creator


Hiya,

I couldn’t discover an updated instance of the best way to set the vertices colours of a sprite in 3.5.
Can somebody clarify to me the way it’s carried out, or hyperlink me an instance/doc that I might need missed ?
Thanks prematurely.


If I adapt the older examples I discovered for the three.5 API, it provides one thing like this :

@ccclass("ColorSprite")
export class ColorSprite extends SpriteComponent {
  @property({ sort: [Color] })
  non-public colours: Shade[] = [];


  protected _updateColor() {
    tremendous._updateColor();

    const vData = this.renderData.vertexFormat;
    let colorOffset = 5;
    for (let i = 0; i < 4; i++)  this.colour;
      const colorR = colour.r / 255;
      const colorG = colour.g / 255;
      const colorB = colour.b / 255;
      const colorA = this.node._uiProps.opacity;
      vData![colorOffset] = colorR;
      vData![colorOffset + 1] = colorG;
      vData![colorOffset + 2] = colorB;
      vData![colorOffset + 3] = colorA;
      colorOffset += 9;
    
  }
}

However this provides an error :

Uncaught TypeError: Can’t learn properties of undefined (studying ‘format’)
at updateOpacity (utils.ts:83:35)
at Batcher2D.stroll (batcher-Second.ts:670:13)
at Batcher2D.stroll (batcher-Second.ts:680:22)
at Batcher2D.stroll (batcher-Second.ts:680:22)
at Batcher2D.replace (batcher-Second.ts:204:18)
at Root.frameMove (root.ts:437:31)
at Director.tick (director.ts:715:25)
at callback (sport.ts:838:26)

By the way in which, the explanation I want it’s because I need to apply a shader to a sprite that takes its sprite body by way of an atlas. Due to the atlas, I not have entry to uv coordinates from 0 to 1 to be able to know the place I’m within the sprite.
My thought to counter this was to make use of vertex colour interpolation to simulates a 0 to 1 coordinate.
If somebody has a greater thought for this, let me know.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments