MultTextures.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. //*//
  2. import { BaseRenderData, Director, Game, Material, MotionStreak, Node, ParticleSystem2D, Sprite, SpriteFrame, StencilManager, TiledLayer, TransformBit, UIRenderer, VERSION, __private, assert, cclegacy, director, game, gfx, murmurhash2_32_gc, renderer, resources } from 'cc';
  3. import { DEBUG, EDITOR, JSB } from 'cc/env';
  4. export const MultBatch2D: any = {
  5. enable: false,
  6. parent: null,
  7. textures: [],
  8. incID:0,
  9. count:0,
  10. hash: 0,
  11. reset: function () {
  12. // this.textures.length = 0;
  13. this.incID+=this.count;
  14. this.count = 0;
  15. }
  16. };
  17. const loadMultTextures = function () {
  18. MultBatch2D.enable = false; //提前加载多纹理材质
  19. resources.load("multTextures/Mult-material", Material, (err, material) => {
  20. if (!err) {
  21. let mat = cclegacy.builtinResMgr.get('ui-sprite-material');
  22. if (mat) {
  23. MultBatch2D.hash = Material.getHash(mat);
  24. MultBatch2D.parent = material;
  25. MultBatch2D.enable = true;
  26. material.addRef();
  27. }
  28. }
  29. });
  30. }
  31. let _cacheUseCount: number = 0;
  32. let _cacheMaterials: Array<Material> = [];
  33. //@ts-ignore
  34. Material.prototype.isMultTextures = false;
  35. const getMultMaterial = function (oldMat: any) {
  36. MultBatch2D.reset();
  37. if (!MultBatch2D.enable ||
  38. !oldMat || !oldMat.isMultTextures) {
  39. return oldMat;
  40. }
  41. if (!MultBatch2D.parent
  42. || !MultBatch2D.parent.isValid) {
  43. loadMultTextures();
  44. return oldMat;
  45. }
  46. //if (!MultBatch2D.enable) return null;
  47. let newMat: any = _cacheMaterials[_cacheUseCount++];
  48. if (!newMat || !newMat.isValid) {
  49. const material = { parent: MultBatch2D.parent };
  50. newMat = new renderer.MaterialInstance(material);
  51. _cacheMaterials[_cacheUseCount - 1] = newMat;
  52. newMat['isMultTextures'] = true;
  53. newMat['cacheTextures'] = [-1];
  54. newMat.addRef();
  55. }
  56. return newMat;
  57. }
  58. //如果有异常组件,或者自定义组件,
  59. //在这进行添加排除,不参与多纹理合批
  60. const excludeMaterial = function (uir: UIRenderer, material: any) {
  61. if (!material) return;
  62. let enable: boolean = true;
  63. if (enable && TiledLayer) {
  64. enable = !(uir instanceof TiledLayer);
  65. }
  66. if (enable && MotionStreak) {
  67. enable = !(uir instanceof MotionStreak);
  68. }
  69. if (enable && ParticleSystem2D) {
  70. enable = !(uir instanceof ParticleSystem2D);
  71. }
  72. material['isMultTextures'] = false;
  73. if (enable && MultBatch2D.hash == material.hash) {
  74. material['isMultTextures'] = true;
  75. }
  76. }
  77. game.once(Game.EVENT_GAME_INITED, () => {
  78. if (EDITOR || JSB) return;
  79. loadMultTextures();
  80. const UIR: any = UIRenderer.prototype;
  81. const updateMaterial: any = UIR.updateMaterial;
  82. UIR.updateMaterial = function () {
  83. updateMaterial.call(this); //this.getSharedMaterial(0);
  84. excludeMaterial(this, this.customMaterial || this.material);
  85. }
  86. });
  87. game.once(Game.EVENT_ENGINE_INITED, () => {
  88. if (EDITOR || JSB) return;
  89. director.on(Director.EVENT_AFTER_DRAW, (dt) => {
  90. _cacheUseCount = 0;
  91. MultBatch2D.reset();
  92. });
  93. const RenderData = cclegacy.UI.RenderData.prototype;
  94. RenderData.texID = -1;
  95. RenderData.isSpr = false;
  96. RenderData.texDirty = true;
  97. RenderData.dataDirty = 0x0;
  98. RenderData.updateHash = function() {
  99. if(this.material && this.material.isMultTextures){
  100. const bid = this.chunk ? this.chunk.bufferId : 100000;
  101. this.dataHash = bid*1000000000 + this.layer;
  102. this.hashDirty = false;
  103. }else{
  104. const bid = this.chunk ? this.chunk.bufferId : -1;
  105. const hashString = `${bid}${this.layer} ${this.textureHash}`;
  106. this.dataHash = murmurhash2_32_gc(hashString, 666);
  107. this.hashDirty = false;
  108. }
  109. }
  110. Object.defineProperty(RenderData, "vertDirty", {
  111. get: function () {
  112. return this._vertDirty;
  113. },
  114. set: function (val: boolean) {
  115. this._vertDirty = val;
  116. if (val === true && !this.isSpr) {
  117. this.dataDirty |= 1;
  118. }
  119. if (this._renderDrawInfo && val) {
  120. this._renderDrawInfo.setVertDirty(val);
  121. }
  122. }
  123. });
  124. Object.defineProperty(RenderData, "textureDirty", {
  125. get: function () {
  126. return this.texDirty;
  127. },
  128. set: function (val: boolean) {
  129. this.texDirty = val;
  130. if (val === true) {
  131. this.texID = -1;
  132. }
  133. }
  134. });
  135. let str = (VERSION.concat());
  136. str = str.replace('.','').replace('.','');
  137. if( parseInt( str ) < 384) { //修复 renderManager
  138. const UIR: any = UIRenderer.prototype;
  139. const markForUpdateRenderData = UIR.markForUpdateRenderData;
  140. UIR.markForUpdateRenderData = function(enable = true){
  141. markForUpdateRenderData.call(this,enable);
  142. if(enable && this.renderData) {
  143. if(!this.renderData.isSpr)
  144. this.renderData.dataDirty |= 2;
  145. }
  146. }
  147. const updateRenderer = UIR.updateRenderer;
  148. UIR.updateRenderer = function(){
  149. updateRenderer.call(this);
  150. if(this.renderData){
  151. if(!this.renderData.isSpr)
  152. this.renderData.dataDirty &=(~2);
  153. }
  154. }
  155. }
  156. let SPRA: any = cclegacy.UI.spriteAssembler;
  157. if (SPRA) {
  158. const spriteAssembler = SPRA.getAssembler;
  159. SPRA.getAssembler = function (sprite: Sprite) {
  160. const spr = spriteAssembler.call(this, sprite);
  161. if(spr.changeUV == undefined){
  162. spr.changeUV = function(s:any){
  163. let rd = s.renderData;
  164. if(rd){
  165. rd.dataDirty = 1;
  166. rd.isSpr = true;
  167. }
  168. };
  169. const UVs = spr.updateUVs;
  170. if(UVs){
  171. if(sprite.type == Sprite.Type.FILLED &&
  172. sprite.fillType != Sprite.FillType.RADIAL){
  173. spr.updateUVs = function(s: any, f0: number, f1: number){
  174. UVs.call(this,s,f0,f1);
  175. this.changeUV(s);
  176. }
  177. }else{
  178. spr.updateUVs = function(s:any){
  179. UVs.call(this,s);
  180. this.changeUV(s);
  181. }
  182. }
  183. }
  184. const verUV = spr.updateWorldVertexAndUVData;
  185. if(verUV){
  186. spr.updateWorldVertexAndUVData = function(s:any, c:any){
  187. verUV.call(this,s, c);
  188. this.changeUV(s);
  189. }
  190. }
  191. }
  192. return spr;
  193. }
  194. }
  195. cclegacy.internal.Batcher2D.prototype.cacheTextures = [];
  196. cclegacy.internal.Batcher2D.prototype.currMaterial = null;
  197. cclegacy.internal.Batcher2D.prototype.isMultTextures = false;
  198. Object.defineProperty(cclegacy.internal.Batcher2D.prototype, "_currMaterial", {
  199. get: function () {
  200. return this.currMaterial;
  201. },
  202. set: function (metrial: any) {
  203. if (this.currMaterial === metrial) return;
  204. this.currMaterial = getMultMaterial(metrial);
  205. if (MultBatch2D.enable) {
  206. this.isMultTextures = false;
  207. if (this.currMaterial && this.currMaterial.isMultTextures) {
  208. this.cacheTextures = this.currMaterial.cacheTextures;
  209. this.isMultTextures = true;
  210. }
  211. }
  212. }
  213. });
  214. const MAX_TEX = 8;
  215. const _texture = {
  216. texture: new cclegacy.SimpleTexture(),
  217. defalut: new cclegacy.SimpleTexture(),
  218. setFrame(frame: any) {
  219. this.texture['_gfxSampler'] = frame.getGFXSampler();
  220. this.texture['_gfxTextureView'] = frame.getGFXTexture();
  221. }
  222. };
  223. //@ts-ignore
  224. gfx.Texture.prototype['texID'] = -1;
  225. const Stage_ENTER_LEVEL = 2;
  226. const Stage_ENTER_LEVEL_INVERTED = 6;
  227. //@ts-ignore
  228. type TextureBase = __private._cocos_asset_assets_texture_base__TextureBase;
  229. cclegacy.internal.Batcher2D.prototype.commitComp = function (comp: UIRenderer, renderData: BaseRenderData | null, frame: TextureBase | SpriteFrame | null, assembler: any, transform: Node | null) {
  230. let dataHash = 0;
  231. let mat: any;
  232. let bufferID = -1;
  233. if (renderData && renderData.chunk) {
  234. if (!renderData.isValid()) return;
  235. dataHash = renderData.dataHash;
  236. mat = renderData.material;
  237. bufferID = renderData.chunk.bufferId;
  238. }
  239. // Notice: A little hack, if it is for mask, not need update here, while control by stencilManger
  240. if (comp.stencilStage === Stage_ENTER_LEVEL || comp.stencilStage === Stage_ENTER_LEVEL_INVERTED) {
  241. this._insertMaskBatch(comp);
  242. } else {
  243. comp.stencilStage = StencilManager.sharedManager!.stage;
  244. }
  245. const depthStencilStateStage = comp.stencilStage;
  246. let texID = -1;
  247. let texture = null;
  248. let MB = MultBatch2D;
  249. let flushBatch = false;
  250. let isMultTextures = false;
  251. // let textures = MB.textures;
  252. if ( MB.enable && mat && mat.isMultTextures) {
  253. if(frame && frame.isValid)
  254. texture = frame.getGFXTexture();
  255. if (texture) {
  256. isMultTextures = true;
  257. //@ts-ignore
  258. texID = texture.texID - MB.incID;
  259. flushBatch = texID < 0 && MB.count >= MAX_TEX;
  260. if(this.isMultTextures) mat = this._currMaterial;
  261. }
  262. }
  263. if (flushBatch
  264. || this._currHash !== dataHash || dataHash === 0 || this._currMaterial !== mat
  265. || this._currDepthStencilStateStage !== depthStencilStateStage) {
  266. // Merge all previous data to a render batch, and update buffer for next render data
  267. this.autoMergeBatches(this._currComponent!);
  268. if (renderData && !renderData._isMeshBuffer) {
  269. this.updateBuffer(renderData.vertexFormat, bufferID);
  270. }
  271. texID = -1;
  272. this._currRenderData = renderData;
  273. this._currHash = renderData ? renderData.dataHash : 0;
  274. this._currComponent = comp;
  275. this._currTransform = transform;
  276. this._currMaterial = comp.getRenderMaterial(0)!;
  277. this._currDepthStencilStateStage = depthStencilStateStage;
  278. this._currLayer = comp.node.layer;
  279. if (frame) {
  280. if (DEBUG) {
  281. assert(frame.isValid, 'frame should not be invalid, it may have been released');
  282. }
  283. this._currTexture = frame.getGFXTexture();
  284. this._currSampler = frame.getGFXSampler();
  285. this._currTextureHash = frame.getHash();
  286. this._currSamplerHash = this._currSampler.hash;
  287. } else {
  288. this._currTexture = null;
  289. this._currSampler = null;
  290. this._currTextureHash = 0;
  291. this._currSamplerHash = 0;
  292. }
  293. }
  294. assembler.fillBuffers(comp, this);
  295. if (isMultTextures) {
  296. if (texID < 0 || MB.count === 0) { //|| MB.count === 0
  297. texID = MB.count++;
  298. //@ts-ignore
  299. let id = texture.objectID;
  300. //@ts-ignore
  301. texture.texID = texID + MB.incID;
  302. let caches = this.cacheTextures;
  303. if (texID > 0 && caches[texID] !== id) {
  304. caches[texID] = id;
  305. _texture.setFrame(frame);
  306. const name = "texture" + texID;
  307. this._currMaterial.setProperty(name, _texture.texture);
  308. }
  309. }
  310. this._fillDatas(renderData, texID);
  311. }
  312. }
  313. cclegacy.internal.Batcher2D.prototype["_fillDatas"] = function (renderData: any, texID: number) {
  314. if (!renderData) return;
  315. let uvX = 0;
  316. let vbuf = renderData.chunk.vb;
  317. if (renderData.dataDirty == 1) {
  318. renderData.dataDirty = 0;
  319. for (let i = 0, length = vbuf.length; i < length; i += 9) {
  320. uvX = ~~(vbuf[i + 3] * 100000);
  321. vbuf[i + 3] = uvX * 10 + texID;
  322. }
  323. } else {
  324. if (renderData.texID != texID) {
  325. for (let i = 0, length = vbuf.length; i < length; i += 9) {
  326. uvX = ~~(vbuf[i + 3] * 0.1);
  327. vbuf[i + 3] = uvX * 10 + texID;
  328. }
  329. }
  330. }
  331. renderData.texID = texID;
  332. };
  333. });
  334. //*/