MultToNative.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { getMultMaterial, MultBatch2D } from "./MultTextures";
  2. //仅原生需要,发布H5或者小游戏,可以删除本脚本
  3. if (CC_JSB) {
  4. //@ts-ignore
  5. let RenderFlow = cc.RenderFlow;
  6. RenderFlow.FLAG_REORDER_CHILDREN = 1 << 29;
  7. RenderFlow.FLAG_WORLD_TRANSFORM_CHANGED = 1 << 30;
  8. RenderFlow.FLAG_OPACITY_CHANGED = 1 << 31;
  9. let _dirtyTargets = [];
  10. let _dirtyWaiting = [];
  11. let _rendering = false;
  12. var director = cc.director;
  13. RenderFlow.render = function (scene, dt, camera = null) {
  14. _rendering = true;
  15. RenderFlow.validateRenderers();
  16. for (let i = 0, l = _dirtyTargets.length; i < l; i++) {
  17. let node = _dirtyTargets[i];
  18. node._inJsbDirtyList = false;
  19. let comp = node._renderComponent;
  20. if (!comp) continue;
  21. let assembler = comp._assembler;
  22. if (!assembler) continue;
  23. let flag = node._dirtyPtr[0];
  24. if (flag & RenderFlow.FLAG_UPDATE_RENDER_DATA) {
  25. node._dirtyPtr[0] &= ~RenderFlow.FLAG_UPDATE_RENDER_DATA;
  26. assembler._updateRenderData && assembler._updateRenderData();
  27. }
  28. if (flag & RenderFlow.FLAG_COLOR) {
  29. node._dirtyPtr[0] &= ~RenderFlow.FLAG_COLOR;
  30. comp._updateColor && comp._updateColor();
  31. }
  32. }
  33. _dirtyTargets.length = 0;
  34. this.visitBegin(scene);
  35. dt = dt || 0;
  36. this._nativeFlow.render(scene._proxy, dt, camera);
  37. _dirtyTargets = _dirtyWaiting.slice(0);
  38. _dirtyWaiting.length = 0;
  39. _rendering = false;
  40. };
  41. RenderFlow.renderCamera = function (camera, scene) {
  42. RenderFlow.render(scene, 0, camera);
  43. }
  44. RenderFlow.init = function (nativeFlow) {
  45. cc.EventTarget.call(this);
  46. this._nativeFlow = nativeFlow;
  47. };
  48. RenderFlow.register = function (target) {
  49. if (target._inJsbDirtyList) return;
  50. if (_rendering) {
  51. _dirtyWaiting.push(target);
  52. } else {
  53. _dirtyTargets.push(target);
  54. }
  55. target._inJsbDirtyList = true;
  56. }
  57. const empty_cullingMask = 1;
  58. const empty_node = new cc.Node();
  59. const empty_material = new cc.Material();
  60. const Renderer = {
  61. node: empty_node,
  62. material: empty_material,
  63. cullingMask: empty_cullingMask,
  64. _dummyNode: empty_node,
  65. reset: function () {
  66. this.node = empty_node;
  67. this.empty_material = empty_material;
  68. this.cullingMask = empty_cullingMask;
  69. this.material = getMultMaterial(empty_material);
  70. },
  71. flush: function (node) {
  72. this.cullingMask = node._cullingMask;
  73. this.material = getMultMaterial(empty_material);
  74. }
  75. };
  76. cc.Node.prototype["realOpacity"] = 1;
  77. const FLAG_RENDER = RenderFlow.FLAG_RENDER;
  78. const FLAG_DONOTHING = RenderFlow.FLAG_DONOTHING;
  79. const FLAG_POST_RENDER = RenderFlow.FLAG_POST_RENDER;
  80. const buildTree = function (node) {
  81. let opacity = node.realOpacity;
  82. let renderFlag = node._renderFlag;
  83. if (opacity == 0 || !node._activeInHierarchy || renderFlag & FLAG_DONOTHING) return;
  84. let comp = node._renderComponent;
  85. if (comp) {
  86. if (renderFlag & FLAG_POST_RENDER) Renderer.flush(node);
  87. else if (renderFlag & FLAG_RENDER) {
  88. comp._checkBacth(Renderer, node._cullingMask);
  89. }
  90. }
  91. let children = node._children;
  92. for (let i = 0, n = children.length; i < n; i++) {
  93. let c = children[i];
  94. c.realOpacity = opacity * (~~c._opacity / 255);
  95. buildTree(c);
  96. }
  97. if (comp) {
  98. //cc.Mask Armature Skeleton
  99. if (renderFlag & FLAG_POST_RENDER) Renderer.flush(node);
  100. }
  101. };
  102. RenderFlow.visitBegin = function (node) {
  103. if(MultBatch2D.enable){
  104. buildTree(node);
  105. Renderer.reset();
  106. }
  107. };
  108. }