MultTextures.ts 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. //*//
  2. import { __private, approx, assert, BaseRenderData, BitmapFont, cclegacy, Component, Director, director, EPSILON, Game, game, gfx, ImageAsset, Label, Material, MeshBuffer, MotionStreak, murmurhash2_32_gc, Node, renderer, resources, Root, Sprite, SpriteFrame, StencilManager, Texture2D, TiledLayer, TiledRenderData, UIMeshRenderer, UIRenderer } from 'cc';
  3. import { DEBUG, EDITOR, JSB } from 'cc/env';
  4. const VER = "2.0.2";
  5. //最大纹理,固定8张
  6. const MAX_TEX = 8
  7. //原生开关,根据需要开启或关闭
  8. const SUPPORT_NATIVE = true;
  9. //@ts-ignore
  10. gfx.Texture.prototype.texID = -1; //当前纹理id
  11. //@ts-ignore
  12. Material.prototype.isMult = false; //多纹理材质的标记
  13. //@ts-ignore
  14. Component.prototype.useMult = false; //组件多纹理开关
  15. export const MultBatch2D: any = {
  16. native: !SUPPORT_NATIVE && JSB,
  17. enable: false,
  18. parent: null,
  19. incID: 0,
  20. count: 0,
  21. hash: 0,
  22. reset: function () {
  23. this.incID += this.count;
  24. this.count = 0;
  25. }
  26. };
  27. const _image = new ImageAsset({
  28. width: 1,
  29. height: 1,
  30. _compressed: false,
  31. format: gfx.Format.RGBA32F,
  32. _data: new Float32Array(4).fill(0),
  33. });
  34. const Texture = new Texture2D();
  35. Texture.setFilters(1, 1);
  36. Texture.image = _image;
  37. Texture.addRef();
  38. //预加载多纹理材质
  39. const loadMultTextures = function () {
  40. MultBatch2D.enable = false;
  41. resources.load("multTextures/Mult-material", Material, (err, material) => {
  42. if (!err) {
  43. let mat = cclegacy.builtinResMgr.get('ui-sprite-material');
  44. if (mat) {
  45. mat._hash = MultBatch2D.hash = Material.getHash(mat);
  46. MultBatch2D.parent = material;
  47. MultBatch2D.enable = true;
  48. material.addRef();
  49. }
  50. }
  51. });
  52. }
  53. //填补原生纹理数据
  54. const endBatcher = function () {
  55. if (!JSB) return;
  56. let batcher: any = director.root?.batcher2D;
  57. if (batcher && batcher.isMult) {
  58. let mat = batcher._currMaterial;
  59. if (mat && MultBatch2D.count > 0) {
  60. let tid = Texture.getGFXTexture();//?.objectID;
  61. let cache = batcher.cacheTextures;
  62. for (let i = MultBatch2D.count; i < 8; i++) {
  63. if (cache[i] !== tid) {
  64. mat.setProperty("texture" + i, Texture);
  65. cache[i] = tid;
  66. }
  67. }
  68. }
  69. }
  70. }
  71. //多纹理材质缓存队列
  72. let _cacheUseCount: number = 0;
  73. let _cacheMaterials: Array<Material> = [];
  74. const getMultMaterial = function (oldMat: any, rd: any = null) {
  75. let MB = MultBatch2D;
  76. endBatcher();
  77. MB.reset();
  78. if (!MB.enable || !oldMat || !rd || !rd.isMult) {
  79. return oldMat;
  80. }
  81. if (!MB.parent || !MB.parent.isValid) {
  82. loadMultTextures();
  83. return oldMat;
  84. }
  85. let newMat: any = _cacheMaterials[_cacheUseCount++];
  86. if (!newMat || !newMat.isValid) {
  87. let material = { parent: MB.parent };
  88. newMat = new renderer.MaterialInstance(material);
  89. _cacheMaterials[_cacheUseCount - 1] = newMat;
  90. newMat['cacheTextures'] = [];
  91. newMat['isMult'] = true;
  92. newMat.addRef();
  93. }
  94. return newMat;
  95. }
  96. //游戏启动前,务必加载多纹理材质
  97. game.once(Game.EVENT_GAME_INITED, () => {
  98. if (EDITOR || MultBatch2D.native) return; //|| JSB
  99. loadMultTextures();
  100. });
  101. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  102. // 多纹理合批,sprite , label , renderdata ,等其他组件的重写和监听
  103. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  104. const inject_Renderdata = function () {
  105. const RenderData = cclegacy.UI.RenderData.prototype;
  106. RenderData.texID = -1;
  107. RenderData.isMult = false;
  108. RenderData.matDirty = true;
  109. RenderData.texDirty = true;
  110. RenderData.dataDirty = 0x0;
  111. //兼容多纹理hash计算
  112. RenderData.updateHash = function () {
  113. if (this.isMult && MultBatch2D.enable) {
  114. const bid = this.chunk ? this.chunk.bufferId : -1;
  115. const hashString = `${bid}${this.layer}` + '98k';
  116. this.dataHash = murmurhash2_32_gc(hashString, 666);
  117. this.hashDirty = false;
  118. } else {
  119. const bid = this.chunk ? this.chunk.bufferId : -1;
  120. const hashString = `${bid}${this.layer} ${this.textureHash}`;
  121. this.dataHash = murmurhash2_32_gc(hashString, 666);
  122. this.hashDirty = false;
  123. }
  124. this.matDirty = false;
  125. }
  126. //监听纹理的变更
  127. Object.defineProperty(RenderData, "textureDirty", {
  128. get: function () {
  129. return this.texDirty;
  130. },
  131. set: function (val: boolean) {
  132. this.texDirty = val;
  133. if (val === true) {
  134. this.texID = -1;
  135. }
  136. }
  137. });
  138. //检测是否支持多纹理合批
  139. const isMultTextures = function (rd: any, uir: UIRenderer) {
  140. rd.isMult = false;
  141. let material: any = uir.getRenderMaterial(0)!
  142. if (!material || !MultBatch2D.enable) return;
  143. //@ts-ignore
  144. //组件控制开关 useMult: 可以开启自定义组件参与多纹理
  145. //|| uir instanceof Sprite || uir instanceof Label
  146. if (uir.useMult) {
  147. material._hash = material.hash || material._hash;
  148. if (!material._hash) {
  149. material._hash = Material.getHash(material);
  150. }
  151. rd.isMult = (MultBatch2D.hash == material._hash);
  152. }
  153. }
  154. //监听pass变更,检测是否多纹理支持
  155. const updatePass = RenderData.updatePass;
  156. RenderData.updatePass = function (comp: UIRenderer): void {
  157. isMultTextures(this, comp);
  158. updatePass.call(this, comp);
  159. }
  160. //监听pass变更,检测是否多纹理支持
  161. const updateRenderData = RenderData.updateRenderData;
  162. RenderData.updateRenderData = function (comp: UIRenderer, frame: any): void {
  163. if (this.passDirty) {
  164. isMultTextures(this, comp);
  165. }
  166. //isMultTextures(this, comp);
  167. updateRenderData.call(this, comp, frame);
  168. }
  169. }
  170. const inject_Label = function () {
  171. //@ts-ignore
  172. Label.prototype.useMult = true;
  173. //监听 Label 的 uv 变更
  174. const label = Label.Assembler;
  175. if (label) {
  176. const getAssembler = label.getAssembler;
  177. label.getAssembler = function (comp: Label) {
  178. const assembler = getAssembler.call(this, comp);
  179. if (assembler.changeUV == undefined) {
  180. assembler.changeUV = function (s: any) {
  181. let rd = s.renderData;
  182. rd && (rd.dataDirty = 1);
  183. };
  184. const UVs = assembler.updateUVs;
  185. if (UVs) {
  186. if (comp.font instanceof BitmapFont) {
  187. assembler.updateUVs = function (comp: Label) {
  188. UVs.call(this, comp);
  189. this.changeUV(comp);
  190. }
  191. } else if (comp.cacheMode === Label.CacheMode.CHAR) {
  192. assembler.updateUVs = function (comp: Label) {
  193. UVs.call(this, comp);
  194. this.changeUV(comp);
  195. }
  196. } else {
  197. assembler.updateUVs = function (comp: Label) {
  198. UVs.call(this, comp);
  199. const renderData = comp.renderData;
  200. if (!renderData || !comp.ttfSpriteFrame) {
  201. return;
  202. }
  203. this.changeUV(comp);
  204. }
  205. }
  206. }
  207. }
  208. return assembler;
  209. }
  210. }
  211. }
  212. const inject_Sprite = function () {
  213. //@ts-ignore
  214. Sprite.prototype.useMult = true;
  215. //监听 sprite 的 uv 变更
  216. const sprite = Sprite.Assembler;
  217. if (sprite) {
  218. const getAssembler = sprite.getAssembler;
  219. sprite.getAssembler = function (comp: Sprite) {
  220. const assembler = getAssembler.call(this, comp);
  221. if (assembler.changeUV == undefined) {
  222. assembler.changeUV = function (s: any) {
  223. let rd = s.renderData;
  224. rd && (rd.dataDirty = 1);
  225. };
  226. const UVs = assembler.updateUVs;
  227. if (UVs) {
  228. if (comp.type == Sprite.Type.FILLED) {
  229. if (comp.fillType != Sprite.FillType.RADIAL) {
  230. assembler.updateUVs = function (s: any, f0: number, f1: number) {
  231. UVs.call(this, s, f0, f1);
  232. this.changeUV(s);
  233. }
  234. }
  235. } else {
  236. if (comp.type != Sprite.Type.TILED) {
  237. assembler.updateUVs = function (s: any) {
  238. UVs.call(this, s);
  239. if (s.spriteFrame)
  240. this.changeUV(s);
  241. }
  242. }
  243. }
  244. }
  245. if (JSB) {
  246. const wUV = assembler.updateWorldUVData;
  247. if (wUV) {
  248. assembler.updateWorldUVData = function (s: any) {
  249. wUV.call(this, s);
  250. this.changeUV(s);
  251. }
  252. }
  253. }
  254. const verUV = assembler.updateWorldVertexAndUVData;
  255. if (verUV) {
  256. assembler.updateWorldVertexAndUVData = function (s: any, c: any) {
  257. verUV.call(this, s, c);
  258. this.changeUV(s);
  259. }
  260. }
  261. }
  262. return assembler;
  263. }
  264. }
  265. }
  266. const inject_MotionStreak = function () {
  267. if (MotionStreak) {
  268. const motionStreak: any = MotionStreak.prototype;
  269. motionStreak.useMult = true; //参与多纹理合批
  270. const lateUpdate = motionStreak.lateUpdate;
  271. motionStreak.lateUpdate = function (dt: number) {
  272. lateUpdate.call(this, dt);
  273. if (this._assembler) {
  274. if (this.points.length >= 2) {
  275. let rd = this.renderData;
  276. //全局标记刷新纹理uv
  277. rd && (rd.dataDirty = 1);
  278. }
  279. }
  280. }
  281. }
  282. }
  283. const inject_TiledLayer = function () {
  284. if (TiledLayer && !JSB) {
  285. const Tiled: any = TiledLayer.prototype;;
  286. Tiled.useMult = true; //参与多纹理合批
  287. Tiled.dataDirty = false; //全局标记刷新纹理uv
  288. const setUserNodeDirty = Tiled.setUserNodeDirty
  289. Tiled.setUserNodeDirty = function (dirty: boolean) {
  290. setUserNodeDirty.call(this, dirty);
  291. if (!dirty) {
  292. //全局标记刷新纹理uv
  293. this.dataDirty = true;
  294. }
  295. }
  296. Tiled._render = function (ui: any): void {
  297. const layer = this.node.layer;
  298. for (let i = 0, j = 0; i < this._tiledDataArray.length; i++) {
  299. this._tiledDataArrayIdx = i;
  300. const m = this._tiledDataArray[i];
  301. const info = this._drawInfoList[j];
  302. if (m.subNodes) {
  303. // 提前处理 User Nodes
  304. m.subNodes.forEach((c: any) => {
  305. if (c) {
  306. ui.walk(c.node);
  307. j++;
  308. }
  309. });
  310. } else {
  311. const td = m as TiledRenderData;
  312. if (td.texture) {
  313. let isDirty = false;
  314. let rd: any = td.renderData!;
  315. rd.material = this.getRenderMaterial(0);
  316. if (rd.texture !== td.texture) {
  317. rd.texture = td.texture;
  318. // isDirty = true;
  319. }
  320. if (rd.layer !== layer) {
  321. rd.layer = layer;
  322. isDirty = true;
  323. }
  324. rd.isMult = true; //强制参与多纹理
  325. // if (JSB) rd._renderDrawInfo = info;
  326. //更新renderdata hash
  327. isDirty && rd.updateHash();
  328. if (this.dataDirty) rd.dataDirty = 1;
  329. // NOTE: 由于 commitComp 只支持单张纹理, 故分多次提交
  330. ui.commitComp(this, td.renderData, td.texture, this._assembler, null);
  331. j++;
  332. }
  333. }
  334. }
  335. this.dataDirty = false;
  336. this.node._static = true;
  337. }
  338. }
  339. }
  340. game.once(Game.EVENT_ENGINE_INITED, () => {
  341. if (EDITOR || MultBatch2D.native) return; //|| JSB
  342. inject_Label();
  343. inject_Sprite();
  344. inject_Renderdata();
  345. inject_TiledLayer();
  346. inject_MotionStreak();
  347. director.on(Director.EVENT_AFTER_DRAW, (dt) => {
  348. MultBatch2D.reset();
  349. _cacheUseCount = 0;
  350. });
  351. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  352. // 多纹理合批,合批核心过程修改
  353. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  354. const Batcher2D: any = cclegacy.internal.Batcher2D.prototype;
  355. Batcher2D.isMult = false; //多纹理标记
  356. Batcher2D.isNative = JSB; //原生的开关
  357. Batcher2D.cacheTextures = []; //纹理缓存数据
  358. Batcher2D.currMaterial = null;//当前指定材质
  359. Object.defineProperty(Batcher2D, "_currMaterial", {
  360. get: function () {
  361. return this.currMaterial;
  362. },
  363. set: function (material: any) {
  364. // if (this.currMaterial === material) return;
  365. //检测多纹理材质,接替 _currMaterial
  366. let rd = this._currRenderData; //重置检测
  367. if (material == this._emptyMaterial) rd = null;
  368. this.currMaterial = getMultMaterial(material, rd);
  369. this.isMult = false;
  370. if (MultBatch2D.enable) {
  371. if (this.currMaterial && this.currMaterial.isMult) {
  372. this.cacheTextures = this.currMaterial.cacheTextures;
  373. this.isMult = true; //当前 batcher 多纹理标记
  374. }
  375. }
  376. }
  377. });
  378. const Stage_ENTER_LEVEL = 2;
  379. const Stage_ENTER_LEVEL_INVERTED = 6;
  380. //@ts-ignore
  381. type TextureBase = __private._cocos_asset_assets_texture_base__TextureBase;
  382. Batcher2D.commitComp = function (comp: UIRenderer, renderData: BaseRenderData | null, frame: TextureBase | SpriteFrame | null, assembler: any, transform: Node | null) {
  383. let dataHash = 0;
  384. let mat: any = null;
  385. let bufferID = -1;
  386. if (renderData && renderData.chunk) {
  387. if (!renderData.isValid()) return;
  388. dataHash = renderData.dataHash;
  389. mat = renderData.material;
  390. bufferID = renderData.chunk.bufferId;
  391. }
  392. // Notice: A little hack, if it is for mask, not need update here, while control by stencilManger
  393. if (comp.stencilStage === Stage_ENTER_LEVEL || comp.stencilStage === Stage_ENTER_LEVEL_INVERTED) {
  394. this._insertMaskBatch(comp);
  395. } else {
  396. comp.stencilStage = StencilManager.sharedManager!.stage;
  397. }
  398. const depthStencilStateStage = comp.stencilStage;
  399. let texID = -1;
  400. let texture = null;
  401. let MB = MultBatch2D;
  402. let flushBatch = false;
  403. let isNative = this.isNative;
  404. //@ts-ignore
  405. if (MB.enable && renderData && renderData.isMult) {
  406. if (frame && frame.isValid)
  407. texture = frame.getGFXTexture();
  408. if (texture) {
  409. //@ts-ignore
  410. if (texture.texID === undefined) texture.texID = -1;
  411. //@ts-ignore
  412. texID = texture.texID - MB.incID;
  413. flushBatch = texID < 0 && MB.count >= MAX_TEX;
  414. if (this.isMult) mat = this._currMaterial;
  415. }
  416. }
  417. if (flushBatch
  418. || this._currHash !== dataHash || dataHash === 0 || this._currMaterial !== mat
  419. || this._currDepthStencilStateStage !== depthStencilStateStage) {
  420. // Merge all previous data to a render batch, and update buffer for next render data
  421. this.autoMergeBatches(this._currComponent!);
  422. if (!isNative && renderData && !renderData._isMeshBuffer) {
  423. this.updateBuffer(renderData.vertexFormat, bufferID);
  424. }
  425. this._currRenderData = renderData;
  426. this._currHash = renderData ? renderData.dataHash : 0;
  427. this._currComponent = comp;
  428. this._currTransform = transform;
  429. this._currMaterial = comp.getRenderMaterial(0)!;
  430. this._currDepthStencilStateStage = depthStencilStateStage;
  431. this._currLayer = comp.node.layer;
  432. if (frame) {
  433. if (DEBUG) {
  434. assert(frame.isValid, 'frame should not be invalid, it may have been released');
  435. }
  436. this._currTexture = frame.getGFXTexture();
  437. this._currSampler = frame.getGFXSampler();
  438. this._currTextureHash = frame.getHash();
  439. this._currSamplerHash = this._currSampler.hash;
  440. } else {
  441. this._currTexture = null;
  442. this._currSampler = null;
  443. this._currTextureHash = 0;
  444. this._currSamplerHash = 0;
  445. }
  446. }
  447. if (!isNative) assembler.fillBuffers(comp, this);
  448. if (texture) {
  449. if (texID < 0 || MB.count === 0) {
  450. texID = MB.count++;
  451. //@ts-ignore
  452. //let id = texture.objectID;
  453. //@ts-ignore
  454. texture.texID = texID + MB.incID;
  455. let caches = this.cacheTextures;
  456. if (caches[texID] !== texture) {
  457. caches[texID] = texture;
  458. //@ts-ignore
  459. texture = frame.texture;
  460. if (!texture) texture = frame;
  461. this._currMaterial.setProperty("texture" + texID, texture);
  462. }
  463. }
  464. this.fillTextureID(renderData, texID);
  465. if (isNative) {
  466. renderData!.renderDrawInfo.setMaterial(this._currMaterial);
  467. }
  468. }
  469. }
  470. //填充多纹理 id 到顶点数据
  471. Batcher2D.fillTextureID = function (renderData: any, texID: number) {
  472. // if (!renderData) return;
  473. let vbuf = renderData.chunk.vb;
  474. let uvX = 0, length = vbuf.length;
  475. if (renderData.dataDirty === 1) {
  476. for (let i = 0; i < length; i += 9) {
  477. uvX = ~~(vbuf[i + 3] * 100000);
  478. vbuf[i + 3] = uvX * 10 + texID;
  479. }
  480. } else {
  481. if (renderData.texID !== texID) {
  482. for (let i = 0; i < length; i += 9) {
  483. uvX = ~~(vbuf[i + 3] * 0.1);
  484. vbuf[i + 3] = uvX * 10 + texID;
  485. }
  486. }
  487. }
  488. renderData.dataDirty = 0;
  489. renderData.texID = texID;
  490. };
  491. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  492. // 多纹理合批,原生平台支持的修改
  493. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  494. if (!EDITOR && JSB) {
  495. const rootProto: any = Root.prototype;
  496. const oldFrameMove = rootProto.frameMove;
  497. rootProto.frameMove = function (deltaTime: number) {
  498. //@ts-ignore
  499. director.root?.batcher2D.update();
  500. //director.root?.batcher2D.reset();
  501. oldFrameMove.call(this, deltaTime, director.getTotalFrames());
  502. };
  503. Batcher2D.update = function update() {
  504. const screens = this._screens;
  505. for (let i = 0; i < screens.length; ++i) {
  506. const screen = screens[i];
  507. const scene = screen._getRenderScene();
  508. if (!screen.enabledInHierarchy || !scene) {
  509. continue;
  510. }
  511. // Reset state and walk
  512. this._opacityDirty = 0;
  513. this._pOpacity = 1;
  514. this._batchRootDepth = 0;
  515. this.walk(screen.node);
  516. this.resetRenderStates();
  517. }
  518. this._batches.clear();
  519. this.resetRenderStates();
  520. StencilManager.sharedManager!.reset();
  521. }
  522. Batcher2D.walk = function walk(node: Node, level = 0): void {
  523. if (!node.activeInHierarchy) {
  524. return;
  525. }
  526. const children = node.children;
  527. const uiProps = node._uiProps;
  528. const render = uiProps.uiComp as UIRenderer;
  529. // Save opacity
  530. const parentOpacity = this._pOpacity;
  531. let opacity = parentOpacity;
  532. // TODO Always cascade ui property's local opacity before remove it
  533. const selfOpacity = render && render.color ? render.color.a / 255 : 1;
  534. this._pOpacity = opacity *= selfOpacity * uiProps.localOpacity;
  535. // TODO Set opacity to ui property's opacity before remove it
  536. //@ts-ignore
  537. //uiProps.setOpacity(opacity);
  538. uiProps._opacity = opacity;
  539. if (!approx(opacity, 0, EPSILON)) {
  540. // Render assembler update logic
  541. if (render && render.enabledInHierarchy) {
  542. render.fillBuffers(this);// for rendering
  543. }
  544. if (children.length > 0 && !node._static) {
  545. for (let i = 0; i < children.length; ++i) {
  546. const child = children[i];
  547. this.walk(child, level);
  548. }
  549. }
  550. }
  551. // Restore opacity
  552. this._pOpacity = parentOpacity;
  553. // Post render assembler update logic
  554. // ATTENTION: Will also reset colorDirty inside postUpdateAssembler
  555. if (render && render.enabledInHierarchy) {
  556. render.postUpdateAssembler(this);
  557. if ((render.stencilStage === Stage_ENTER_LEVEL || render.stencilStage === Stage_ENTER_LEVEL_INVERTED)
  558. && (StencilManager.sharedManager!.getMaskStackSize() > 0)) {
  559. this.autoMergeBatches(this._currComponent!);
  560. this.resetRenderStates();
  561. StencilManager.sharedManager!.exitMask();
  562. }
  563. }
  564. level += 1;
  565. }
  566. Batcher2D._insertMaskBatch = function (comp: UIRenderer | UIMeshRenderer): void {
  567. this.autoMergeBatches(this._currComponent!);
  568. this.resetRenderStates();
  569. //this._createClearModel();
  570. //this._maskClearModel!.node = this._maskClearModel!.transform = comp.node;
  571. const _stencilManager = StencilManager.sharedManager!;
  572. _stencilManager.pushMask(1);//not need object,only use length
  573. //_stencilManager.clear(comp); //invert
  574. _stencilManager.enableMask();
  575. }
  576. Batcher2D.commitModel = function (comp: UIMeshRenderer | UIRenderer, model: any, mat: Material | null): void {
  577. // if the last comp is spriteComp, previous comps should be batched.
  578. if (this._currMaterial !== this._emptyMaterial) {
  579. this.autoMergeBatches(this._currComponent!);
  580. this.resetRenderStates();
  581. }
  582. if (mat) {
  583. // Notice: A little hack, if it is for mask, not need update here, while control by stencilManger
  584. if (comp.stencilStage === Stage_ENTER_LEVEL || comp.stencilStage === Stage_ENTER_LEVEL_INVERTED) {
  585. this._insertMaskBatch(comp);
  586. } else {
  587. //@ts-ignore
  588. comp._stencilStage = StencilManager.sharedManager!.stage;
  589. }
  590. }
  591. }
  592. Batcher2D.commitIA = function (renderComp: UIRenderer, ia: any, tex?: TextureBase, mat?: Material, transform?: Node): void {
  593. // if the last comp is spriteComp, previous comps should be batched.
  594. if (this._currMaterial !== this._emptyMaterial) {
  595. this.autoMergeBatches(this._currComponent!);
  596. this.resetRenderStates();
  597. }
  598. if (renderComp) {
  599. //@ts-ignore
  600. renderComp._stencilStage = StencilManager.sharedManager!.stage;
  601. }
  602. }
  603. Batcher2D.commitMiddleware = function (comp: UIRenderer,
  604. meshBuffer: MeshBuffer,
  605. indexOffset: number,
  606. indexCount: number,
  607. tex: TextureBase,
  608. mat: Material,
  609. enableBatch: boolean,
  610. ): void {
  611. this.autoMergeBatches(this._currComponent!);
  612. this.resetRenderStates();
  613. this._currIsMiddleware = true;
  614. }
  615. Batcher2D.autoMergeBatches = function (renderComp?: UIRenderer): void {
  616. if (this._currIsMiddleware) {
  617. // this.mergeBatchesForMiddleware(renderComp!);
  618. if (renderComp) {
  619. //@ts-ignore
  620. renderComp._stencilStage = StencilManager.sharedManager!.stage;
  621. }
  622. this._currIsMiddleware = false;
  623. this._middlewareBuffer = null;
  624. }
  625. }
  626. // if (Graphics) {
  627. // const graphics: any = Graphics.prototype;
  628. // graphics.debugDraw = false;
  629. // const _render = graphics._render;
  630. // graphics._render = function (render: any): void {
  631. // if (this.debugDraw) {
  632. // this._isNeedUploadData = false;
  633. // }
  634. // _render.call(this, render);
  635. // }
  636. // }
  637. // if (sp.Skeleton) {
  638. // const Skeleton: any = sp.Skeleton.prototype;
  639. // const _updateDebugDraw = Skeleton._updateDebugDraw;
  640. // Skeleton._updateDebugDraw = function () {
  641. // _updateDebugDraw.call(this);
  642. // if (this._debugRenderer) {
  643. // this._debugRenderer.debugDraw = true;
  644. // }
  645. // }
  646. // }
  647. }
  648. });
  649. //*/