@safe-engine/cocos 2.5.13 → 2.5.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,5 +10,5 @@ export declare class TiledSpriteNode extends cc.Sprite {
10
10
  updateShaderUniforms(): void;
11
11
  setContentSize(w: any, h: any): void;
12
12
  }
13
- export declare function createTiledSprite(src: string, totalW: number, totalH: number): TiledSpriteNode;
13
+ export declare function createTiledSprite(src: string, totalW: number, totalH: number): cc.Sprite;
14
14
  //# sourceMappingURL=TiledSprite.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"TiledSprite.d.ts","sourceRoot":"","sources":["../../src/render/TiledSprite.ts"],"names":[],"mappings":"AAEA,qBAAa,eAAgB,SAAQ,EAAE,CAAC,MAAM;IAC5C,QAAQ,EAAE,EAAE,CAAC,SAAS,CAAA;IACtB,aAAa,EAAE,oBAAoB,CAAA;IACnC,WAAW,EAAE,oBAAoB,CAAA;IACjC,OAAO,EAAE,oBAAoB,CAAA;IAC7B,SAAS,EAAE,oBAAoB,CAAA;IAC/B,QAAQ,EAAE,oBAAoB,CAAA;gBAElB,IAAI,KAAA;IAMhB,UAAU;IAuBV,oBAAoB;IAkBpB,cAAc,CAAC,CAAC,KAAA,EAAE,CAAC,KAAA;CAIpB;AAED,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,mBAS5E"}
1
+ {"version":3,"file":"TiledSprite.d.ts","sourceRoot":"","sources":["../../src/render/TiledSprite.ts"],"names":[],"mappings":"AAEA,qBAAa,eAAgB,SAAQ,EAAE,CAAC,MAAM;IAC5C,QAAQ,EAAE,EAAE,CAAC,SAAS,CAAA;IACtB,aAAa,EAAE,oBAAoB,CAAA;IACnC,WAAW,EAAE,oBAAoB,CAAA;IACjC,OAAO,EAAE,oBAAoB,CAAA;IAC7B,SAAS,EAAE,oBAAoB,CAAA;IAC/B,QAAQ,EAAE,oBAAoB,CAAA;gBAElB,IAAI,KAAA;IAMhB,UAAU;IAsBV,oBAAoB;IAkBpB,cAAc,CAAC,CAAC,KAAA,EAAE,CAAC,KAAA;CAIpB;AAED,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,aAkC5E"}
@@ -28,23 +28,23 @@ export class TiledSpriteNode extends cc.Sprite {
28
28
  this._texLoc = program.getUniformLocationForName('u_texture');
29
29
  this._scaleLoc = program.getUniformLocationForName('u_scale');
30
30
  this._sizeLoc = program.getUniformLocationForName('u_size');
31
- this.scheduleUpdate();
31
+ // this.scheduleUpdate()
32
32
  }
33
33
  updateShaderUniforms() {
34
34
  if (!this._program || !this.texture || !this.texture.isLoaded())
35
35
  return;
36
36
  const texW = this.texture.width;
37
37
  const texH = this.texture.height;
38
- const size = this.getContentSize();
39
- console.log('TiledSprite updateShaderUniforms', size, texW, texH);
40
- const scaleX = size.width / texW;
41
- const scaleY = size.height / texH;
38
+ const { height, width } = this.getContentSize();
39
+ // console.log('TiledSprite updateShaderUniforms', { height, width }, texW, texH)
40
+ const scaleX = width / texW;
41
+ const scaleY = height / texH;
42
42
  this._program.use();
43
43
  this._program.setUniformLocationWith2f(this._tileScaleLoc, scaleX, scaleY);
44
44
  this._program.setUniformLocationWith2f(this._texSizeLoc, texW, texH);
45
45
  this._program.setUniformLocationWith1i(this._texLoc, 0);
46
46
  this._program.setUniformLocationWith2f(this._scaleLoc, scaleX, scaleY);
47
- this._program.setUniformLocationWith2f(this._sizeLoc, size.width, size.height);
47
+ this._program.setUniformLocationWith2f(this._sizeLoc, width, height);
48
48
  }
49
49
  setContentSize(w, h) {
50
50
  super.setContentSize(w, h);
@@ -52,14 +52,38 @@ export class TiledSpriteNode extends cc.Sprite {
52
52
  }
53
53
  }
54
54
  export function createTiledSprite(src, totalW, totalH) {
55
- const tileSprite = new TiledSpriteNode(src);
56
- function afterLoaded() {
57
- tileSprite.setContentSize(totalW, totalH);
58
- console.log('createTiledSprite', src, totalW, totalH, tileSprite);
55
+ const tileSprite = new cc.Sprite(src);
56
+ // lấy kích thước gốc của texture
57
+ const tileW = tileSprite.texture.width;
58
+ const tileH = tileSprite.texture.height;
59
+ const program = new cc.GLProgram();
60
+ program.initWithString(tiledVsh, tieldFsh);
61
+ // program.initWithVertexShaderByteArray(vert, frag);
62
+ program.addAttribute(cc.ATTRIBUTE_NAME_POSITION, cc.VERTEX_ATTRIB_POSITION);
63
+ program.addAttribute(cc.ATTRIBUTE_NAME_COLOR, cc.VERTEX_ATTRIB_COLOR);
64
+ program.addAttribute(cc.ATTRIBUTE_NAME_TEX_COORD, cc.VERTEX_ATTRIB_TEX_COORDS);
65
+ if (!program.link()) {
66
+ console.error('Failed to link shader program');
67
+ return;
59
68
  }
69
+ program.updateUniforms();
70
+ program.setUniformLocationWith1i(program.getUniformLocationForName('u_texture'), 0);
71
+ const tileScaleLoc = program.getUniformLocationForName('u_tileScale');
72
+ const texSizeLoc = program.getUniformLocationForName('u_texSize');
73
+ const scaleX = totalW / tileW;
74
+ const scaleY = totalH / tileH;
60
75
  if (tileSprite.texture._textureLoaded)
61
76
  afterLoaded();
62
77
  else
63
78
  tileSprite.texture.addLoadedEventListener(afterLoaded);
79
+ function afterLoaded() {
80
+ // program.use()
81
+ program.setUniformLocationWith2f(texSizeLoc, tileW, tileH);
82
+ program.setUniformLocationWith2f(tileScaleLoc, scaleX, scaleY);
83
+ tileSprite.setShaderProgram(program);
84
+ // nếu dùng batch node hoặc spriteframe atlas, đảm bảo texture unit đúng
85
+ }
86
+ // tileSprite.setContentSize(totalW, totalH)
87
+ tileSprite.setScale(scaleX, scaleY);
64
88
  return tileSprite;
65
89
  }
@@ -1,3 +1,3 @@
1
1
  export declare const tieldFsh = "\n#ifdef GL_ES\nprecision mediump float;\n#endif\n\nvarying vec2 v_texCoord;\nvarying vec4 v_fragmentColor;\n\nuniform sampler2D u_texture;\nuniform vec2 u_tileScale;\nuniform vec2 u_texSize;\n\nvoid main()\n{\n // scale UV to tile space\n vec2 uv = v_texCoord * u_tileScale;\n\n // wrap using fract\n vec2 uvw = fract(uv);\n\n // avoid linear-filter seams: push UVs slightly away from exact 0.0/1.0 edges\n // compute texel size in uv-space\n vec2 texel = 1.0 / u_texSize;\n\n // shrink usable range slightly to avoid sampling border when linear filter is used\n // the factor 1.0 - 2.0*texel moves the uv into [texel, 1-texel]\n uvw = uvw * (1.0 - 2.0 * texel) + texel;\n\n vec4 color = texture2D(u_texture, uvw);\n\n gl_FragColor = color * v_fragmentColor;\n}\n";
2
- export declare const tiledVsh = "\nattribute vec4 a_position;\nattribute vec2 a_texCoord;\nattribute vec4 a_color;\n\n#ifdef GL_ES\nvarying mediump vec2 v_texCoord;\nvarying lowp vec4 v_fragmentColor;\n#else\nvarying vec2 v_texCoord;\nvarying vec4 v_fragmentColor;\n#endif\nuniform vec2 u_scale;\nuniform vec2 u_size;\n\nvoid main() {\n vec2 pivot = u_size * 0.5;\n vec4 pos = a_position;\n // pos.xy *= u_scale; // scale h\u00ECnh \u1EA3nh\n pos.xy = (pos.xy - pivot) * u_scale + pivot;\n gl_Position = CC_PMatrix * pos;\n v_texCoord = a_texCoord;\n v_fragmentColor = a_color;\n}\n";
2
+ export declare const tiledVsh = "\nattribute vec4 a_position;\nattribute vec2 a_texCoord;\nattribute vec4 a_color;\n\n#ifdef GL_ES\nvarying mediump vec2 v_texCoord;\nvarying lowp vec4 v_fragmentColor;\n#else\nvarying vec2 v_texCoord;\nvarying vec4 v_fragmentColor;\n#endif\nuniform vec2 u_scale;\nuniform vec2 u_size;\n\nvoid main() {\n gl_Position = CC_PMatrix * a_position;\n v_texCoord = a_texCoord;\n v_fragmentColor = a_color;\n}\n";
3
3
  //# sourceMappingURL=shader.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"shader.d.ts","sourceRoot":"","sources":["../../src/render/shader.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,QAAQ,qyBAgCpB,CAAA;AACD,eAAO,MAAM,QAAQ,ikBAwBpB,CAAA"}
1
+ {"version":3,"file":"shader.d.ts","sourceRoot":"","sources":["../../src/render/shader.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,QAAQ,qyBAgCpB,CAAA;AACD,eAAO,MAAM,QAAQ,iaAoBpB,CAAA"}
@@ -47,11 +47,7 @@ uniform vec2 u_scale;
47
47
  uniform vec2 u_size;
48
48
 
49
49
  void main() {
50
- vec2 pivot = u_size * 0.5;
51
- vec4 pos = a_position;
52
- // pos.xy *= u_scale; // scale hình ảnh
53
- pos.xy = (pos.xy - pivot) * u_scale + pivot;
54
- gl_Position = CC_PMatrix * pos;
50
+ gl_Position = CC_PMatrix * a_position;
55
51
  v_texCoord = a_texCoord;
56
52
  v_fragmentColor = a_color;
57
53
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@safe-engine/cocos",
3
- "version": "2.5.13",
3
+ "version": "2.5.14",
4
4
  "description": "safe-engine with cocos renderer support",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",