Commit 2a62f6d9 authored by haiyoucuv's avatar haiyoucuv

init

parent db5f8677
......@@ -1514,7 +1514,7 @@
"__prefab": null,
"_contentSize": {
"__type__": "cc.Size",
"width": 106,
"width": 58,
"height": 58
},
"_anchorPoint": {
......
......@@ -283,6 +283,7 @@ export class Snake extends Component {
);
this.head.setPosition(newHeadPos);
this.head.setScale(this.scale, this.scale);
this.head.setSiblingIndex(this.bodyArr.length);
const space = ~~(this.SEGMENT_SPACING * this.scale);
......
......@@ -39,8 +39,8 @@ CCProgram sprite-vs %{
in vec2 a_texCoord;
in vec4 a_color;
out vec4 color;
out vec3 uv0;
out lowp vec4 color;
out mediump vec3 uv0;
vec4 vert () {
vec4 pos = vec4(a_position, 1);
......@@ -57,11 +57,11 @@ CCProgram sprite-vs %{
pos = cc_matViewProj * pos;
#endif
// uv0 = a_texCoord;
float id = mod(a_texCoord.x,10.0);
uv0.x = (a_texCoord.x - id)*0.000001;
uv0.y = a_texCoord.y;
uv0.z = id;
uv0.z = mod(a_texCoord.x,10.0);
//uv0.z = fract(a_texCoord.x*0.1);
//uv0.z = floor(uv0.z*10.0+0.1);
uv0.x = (a_texCoord.x-uv0.z)*0.000001;
#if SAMPLE_FROM_RT
CC_HANDLE_RT_SAMPLE_FLIP(uv0.xy);
......@@ -73,14 +73,14 @@ CCProgram sprite-vs %{
}%
CCProgram sprite-fs %{
precision highp float;
precision mediump float;
#include <builtin/internal/embedded-alpha>
#include <builtin/internal/alpha-test>
in vec4 color;
in lowp vec4 color;
#if USE_TEXTURE
in vec3 uv0;
in mediump vec3 uv0;
#pragma builtin(local)
layout(set = 2, binding = 12) uniform sampler2D cc_spriteTexture;
uniform sampler2D texture1;
......@@ -96,19 +96,19 @@ CCProgram sprite-fs %{
vec4 o = vec4(1, 1, 1, 1);
#if USE_TEXTURE
if(uv0.z<0.5)
if(uv0.z == 0.0)
o *= CCSampleWithAlphaSeparated(cc_spriteTexture, uv0.xy);
else if(uv0.z<1.5)
else if(uv0.z == 1.0)
o *= CCSampleWithAlphaSeparated(texture1, uv0.xy);
else if(uv0.z<2.5)
else if(uv0.z == 2.0)
o *= CCSampleWithAlphaSeparated(texture2, uv0.xy);
else if(uv0.z<3.5)
else if(uv0.z == 3.0)
o *= CCSampleWithAlphaSeparated(texture3, uv0.xy);
else if(uv0.z<4.5)
else if(uv0.z == 4.0)
o *= CCSampleWithAlphaSeparated(texture4, uv0.xy);
else if(uv0.z<5.5)
else if(uv0.z == 5.0)
o *= CCSampleWithAlphaSeparated(texture5, uv0.xy);
else if(uv0.z<6.5)
else if(uv0.z == 6.0)
o *= CCSampleWithAlphaSeparated(texture6, uv0.xy);
else
o *= CCSampleWithAlphaSeparated(texture7, uv0.xy);
......
//*//
import {
__private,
assert,
BaseRenderData,
BitmapFont,
cclegacy,
Color,
Component,
Director,
director,
Game,
game,
gfx,
Label,
Material,
MotionStreak,
murmurhash2_32_gc,
Node,
ParticleSystem2D,
renderer,
resources,
Sprite,
SpriteFrame,
StencilManager,
TiledLayer,
TiledRenderData,
UIRenderer,
__private,
_decorator,
assert,
cclegacy,
director,
game,
renderer,
resources
} from 'cc';
import { DEBUG, EDITOR, JSB } from 'cc/env';
VERSION
} from "cc";
import { DEBUG, EDITOR, JSB } from "cc/env";
const {ccclass, property} = _decorator;
const MAX_TEX = 8; //最大纹理
//@ts-ignore
gfx.Texture.prototype.texID = -1; //当前纹理id
//@ts-ignore
Material.prototype.isMultTextures = false; //多纹理材质标记
export const MultBatch2D: any = {
enable: false,
parent: null,
textures: [],
incID: 0,
count: 0,
hash: 0,
reset: function () {
this.textures.length = 0;
this.incID += this.count;
this.count = 0;
}
};
const loadMultTextures = function () {
MultBatch2D.enable = false; //提前加载多纹理材质
resources.load("MultTextures/Mult-material", Material, (err, material) => {
if (!err) {
let mat = cclegacy.builtinResMgr.get("ui-sprite-material");
if (mat) {
mat._hash = MultBatch2D.hash = Material.getHash(mat);
MultBatch2D.parent = material;
MultBatch2D.enable = true;
material.addRef();
}
}
});
};
let _cacheUseCount: number = 0;
let _cacheMaterials: Array<Material> = [];
const getMultMaterial = function (oldMat: any) {
MultBatch2D.reset();
if (!MultBatch2D.enable ||
!oldMat || !oldMat.isMultTextures) {
return oldMat;
}
//if (!MultBatch2D.enable) return null;
if (!MultBatch2D.parent
|| !MultBatch2D.parent.isValid) {
loadMultTextures();
return oldMat;
}
let newMat: any = _cacheMaterials[_cacheUseCount++];
if (!newMat) {
const material = {parent: MultBatch2D.parent};
if (!newMat || !newMat.isValid) {
const material = { parent: MultBatch2D.parent };
newMat = new renderer.MaterialInstance(material);
newMat['isMultTextures'] = true;
_cacheMaterials.push(newMat);
_cacheMaterials[_cacheUseCount - 1] = newMat;
newMat["isMultTextures"] = true;
newMat["cacheTextures"] = [-1];
newMat.addRef();
}
return newMat;
}
};
//如果有异常组件,或者自定义组件,
//在这进行添加排除,不参与多纹理合批
//@ts-ignore
Component.prototype.useMult = false;
//组件控制开关 useMult: 可以开启自定义组件是不参与多纹理
const excludeMaterial = function (uir: UIRenderer, material: any) {
if (!material) return;
let enable: boolean = true;
if (enable && TiledLayer) {
enable = !(uir instanceof TiledLayer);
}
if (enable && MotionStreak) {
enable = !(uir instanceof MotionStreak);
}
if (enable && ParticleSystem2D) {
enable = !(uir instanceof ParticleSystem2D);
}
material['isMultTextures'] = false;
if (enable && MultBatch2D.hash == material.hash) {
material['isMultTextures'] = true;
material["isMultTextures"] = false;
//@ts-ignore
if (uir.useMult || uir instanceof Sprite || uir instanceof Label) {
if (MultBatch2D.hash == material.hash) {
material["isMultTextures"] = true;
}
}
}
};
game.once(Game.EVENT_GAME_INITED, () => {
if (EDITOR || JSB) return;
//提前加载多纹理材质
resources.load("MultTextures/Mult-material", Material, (err, material) => {
if (!err) {
let mat = cclegacy.builtinResMgr.get('ui-sprite-material');
MultBatch2D.hash = Material.getHash(mat);
MultBatch2D.parent = material;
MultBatch2D.enable = true;
}
});
loadMultTextures();
const UIR: any = UIRenderer.prototype;
const updateMaterial: any = UIR.updateMaterial;
UIR.updateMaterial = function () {
updateMaterial.call(this); //this.getSharedMaterial(0);
excludeMaterial(this, this.customMaterial || this.material);
}
};
});
......@@ -108,18 +128,41 @@ game.once(Game.EVENT_GAME_INITED, () => {
game.once(Game.EVENT_ENGINE_INITED, () => {
if (EDITOR || JSB) return;
cclegacy.UI.RenderData.prototype.texID = -1;
cclegacy.UI.RenderData.prototype.texDirty = true;
cclegacy.UI.RenderData.prototype.dataDirty = true;
director.on(Director.EVENT_AFTER_DRAW, (dt) => {
Object.defineProperty(cclegacy.UI.RenderData.prototype, "vertDirty", {
MultBatch2D.reset();
_cacheUseCount = 0;
});
const RenderData = cclegacy.UI.RenderData.prototype;
RenderData.texID = -1;
RenderData.sprite = false;
RenderData.texDirty = true;
RenderData.dataDirty = 0x0;
RenderData.updateHash = function () {
if (this.material && this.material.isMultTextures) {
const bid = this.chunk ? this.chunk.bufferId : 100000;
this.dataHash = bid * 1000000000 + this.layer;
this.hashDirty = false;
// console.log("updateHash:" + this.chunk.bufferId );
} else {
const bid = this.chunk ? this.chunk.bufferId : -1;
const hashString = `${bid}${this.layer} ${this.textureHash}`;
this.dataHash = murmurhash2_32_gc(hashString, 666);
this.hashDirty = false;
}
};
Object.defineProperty(RenderData, "vertDirty", {
get: function () {
return this._vertDirty;
},
set: function (val: boolean) {
this._vertDirty = val;
if (val === true) {
this.dataDirty = true;
if (val === true && !this.sprite) {
this.dataDirty |= 1;
}
if (this._renderDrawInfo && val) {
this._renderDrawInfo.setVertDirty(val);
......@@ -127,7 +170,7 @@ game.once(Game.EVENT_ENGINE_INITED, () => {
}
});
Object.defineProperty(cclegacy.UI.RenderData.prototype, "textureDirty", {
Object.defineProperty(RenderData, "textureDirty", {
get: function () {
return this.texDirty;
},
......@@ -139,37 +182,82 @@ game.once(Game.EVENT_ENGINE_INITED, () => {
}
});
const UIR: any = UIRenderer.prototype;
const _updateColor: any = UIR._updateColor;
UIR._updateColor = function () {
let dataDirty = false;
let rd = this.renderData;
if (rd) dataDirty = rd.dataDirty;
_updateColor.call(this);
let str = (VERSION.concat());
str = str.replace(".", "").replace(".", "");
if (parseInt(str) < 384) { //修复 renderManager
const UIR: any = UIRenderer.prototype;
const markForUpdateRenderData = UIR.markForUpdateRenderData;
UIR.markForUpdateRenderData = function (enable = true) {
markForUpdateRenderData.call(this, enable);
if (enable && this.renderData) {
if (!this.renderData.sprite)
this.renderData.dataDirty |= 2;
}
};
const updateRenderer = UIR.updateRenderer;
UIR.updateRenderer = function () {
updateRenderer.call(this);
if (this.renderData) {
if (!this.renderData.sprite)
this.renderData.dataDirty &= (~2);
}
};
if (rd) {
rd.dataDirty = dataDirty;
}
}
const SPR: any = Sprite.prototype;
const _render = SPR._render;
SPR._render = function (render: any) {
if (this.node.hasChangedFlags) {
if (this.type === Sprite.Type.TILED
|| this.type === Sprite.Type.FILLED
&& this.fillType === Sprite.FillType.RADIAL) {
let rd = this.renderData;
rd && (rd.dataDirty = true);
let SPRA: any = cclegacy.UI.spriteAssembler;
if (SPRA) {
const spriteAssembler = SPRA.getAssembler;
SPRA.getAssembler = function (sprite: Sprite) {
const spr = spriteAssembler.call(this, sprite);
if (spr.changeUV == undefined) {
spr.changeUV = function (s: any) {
let rd = s.renderData;
if (rd) {
rd.dataDirty = 1;
rd.sprite = true;
}
};
const UVs = spr.updateUVs;
if (UVs) {
if (sprite.type == Sprite.Type.FILLED &&
sprite.fillType != Sprite.FillType.RADIAL) {
spr.updateUVs = function (s: any, f0: number, f1: number) {
UVs.call(this, s, f0, f1);
this.changeUV(s);
};
} else {
spr.updateUVs = function (s: any) {
UVs.call(this, s);
this.changeUV(s);
};
}
}
const verUV = spr.updateWorldVertexAndUVData;
if (verUV) {
spr.updateWorldVertexAndUVData = function (s: any, c: any) {
verUV.call(this, s, c);
this.changeUV(s);
};
}
}
}
_render.call(this, render);
return spr;
};
}
cclegacy.internal.Batcher2D.prototype.cacheTextures = [];
cclegacy.internal.Batcher2D.prototype.currMaterial = null;
cclegacy.internal.Batcher2D.prototype.isMultTextures = false;
Object.defineProperty(cclegacy.internal.Batcher2D.prototype, "_currMaterial", {
get: function () {
return this.currMaterial;
......@@ -177,35 +265,26 @@ game.once(Game.EVENT_ENGINE_INITED, () => {
set: function (metrial: any) {
if (this.currMaterial === metrial) return;
this.currMaterial = getMultMaterial(metrial);
if (MultBatch2D.enable) {
this.isMultTextures = false;
if (this.currMaterial && this.currMaterial.isMultTextures) {
this.cacheTextures = this.currMaterial.cacheTextures;
this.isMultTextures = true;
}
}
}
});
director.on(Director.EVENT_AFTER_DRAW, (dt) => {
cclegacy.internal.Batcher2D._rdHash = -1;
MultBatch2D.reset();
_cacheUseCount = 0;
});
const MAX_TEX = 8;
const _texture = {
texture: new cclegacy.SimpleTexture(),
defalut: new cclegacy.SimpleTexture(),
setFrame(frame: any) {
this.texture['_gfxSampler'] = frame.getGFXSampler();
this.texture['_gfxTextureView'] = frame.getGFXTexture();
}
};
const Stage_ENTER_LEVEL = 2;
const Stage_ENTER_LEVEL_INVERTED = 6;
//@ts-ignore
type TextureBase = __private._cocos_asset_assets_texture_base__TextureBase;
cclegacy.internal.Batcher2D.prototype._rdHash = -1;
cclegacy.internal.Batcher2D.prototype.commitComp = function (comp: UIRenderer, renderData: BaseRenderData | null, frame: TextureBase | SpriteFrame | null, assembler: any, transform: Node | null) {
let rdHash = -1;
let dataHash = 0;
let mat: any;
let mat: any = null;
let bufferID = -1;
if (renderData && renderData.chunk) {
......@@ -213,9 +292,6 @@ game.once(Game.EVENT_ENGINE_INITED, () => {
dataHash = renderData.dataHash;
mat = renderData.material;
bufferID = renderData.chunk.bufferId;
// as RenderData;
let rd: any = renderData;
rdHash = bufferID << 16 | rd.layer;
}
......@@ -230,26 +306,17 @@ game.once(Game.EVENT_ENGINE_INITED, () => {
let texID = -1;
let texture = null;
let MB = MultBatch2D;
let flushBatch = false;
let isMultTextures = false;
if (MultBatch2D.enable && mat && mat.isMultTextures) {
texture = frame && frame.getGFXTexture();
texID = MultBatch2D.textures.indexOf(texture);
isMultTextures = true;
if (texID < 0) {
if (MultBatch2D.textures.length == MAX_TEX) {
// MultBatch2D.textures.length = 0;
flushBatch = true;
}
}
if (this._currMaterial && this._currMaterial.isMultTextures) {
mat = this._currMaterial;
dataHash = this._currHash;
if (this._rdHash != rdHash) {
flushBatch = true;
texID = -1;
}
if (MB.enable && mat && mat.isMultTextures) {
if (frame && frame.isValid)
texture = frame.getGFXTexture();
if (texture) {
//@ts-ignore
texID = texture.texID - MB.incID;
flushBatch = texID < 0 && MB.count >= MAX_TEX;
if (this.isMultTextures) mat = this._currMaterial;
}
}
......@@ -263,7 +330,7 @@ game.once(Game.EVENT_ENGINE_INITED, () => {
this.updateBuffer(renderData.vertexFormat, bufferID);
}
this._rdHash = rdHash;
this._currRenderData = renderData;
this._currHash = renderData ? renderData.dataHash : 0;
this._currComponent = comp;
......@@ -273,7 +340,7 @@ game.once(Game.EVENT_ENGINE_INITED, () => {
this._currLayer = comp.node.layer;
if (frame) {
if (DEBUG) {
assert(frame.isValid, 'frame should not be invalid, it may have been released');
assert(frame.isValid, "frame should not be invalid, it may have been released");
}
this._currTexture = frame.getGFXTexture();
this._currSampler = frame.getGFXSampler();
......@@ -290,47 +357,149 @@ game.once(Game.EVENT_ENGINE_INITED, () => {
assembler.fillBuffers(comp, this);
if (isMultTextures) {
if (texture !== null) {
if (texID < 0 || MB.count === 0) {
if (texID < 0) {
texID = MultBatch2D.textures.length;
MultBatch2D.textures.push(texture);
if (texID > 0) {
_texture.setFrame(frame);
const name = "texture" + texID;
this._currMaterial.setProperty(name, _texture.texture);
texID = MB.count++;
//@ts-ignore
let id = texture.objectID;
//@ts-ignore
texture.texID = texID + MB.incID;
let caches = this.cacheTextures;
if (texID > 0 && caches[texID] !== id) {
caches[texID] = id;
this._currMaterial.setProperty("texture" + texID, texture);
}
}
this._fillDatas(renderData, texID);
}
}
};
cclegacy.internal.Batcher2D.prototype["_fillDatas"] = function (renderData: any, texID: number) {
if (!renderData) return;
// if (!renderData) return;
let uvX = 0;
let vbuf = renderData.chunk.vb;
if (renderData.dataDirty) {
renderData.dataDirty = false;
if (renderData.dataDirty === 1) {
renderData.dataDirty = 0;
for (let i = 0, length = vbuf.length; i < length; i += 9) {
uvX = ~~(vbuf[i + 3] * 100000);
vbuf[i + 3] = uvX * 10 + texID;
}
} else {
if (renderData.texID != texID) {
if (renderData.texID !== texID) {
for (let i = 0, length = vbuf.length; i < length; i += 9) {
uvX = ~~(vbuf[i + 3] * 0.1);
vbuf[i + 3] = uvX * 10 + texID;
}
}
}
renderData.dataDirty = 0;
renderData.texID = texID;
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (MotionStreak) {
//@ts-ignore
MotionStreak.prototype.useMult = true;
const motionStreak: any = MotionStreak.Assembler;
const motionAssembler = motionStreak.getAssembler;
motionStreak.getAssembler = function (comp: Label) {
let assembler = motionAssembler.call(this, comp);
if (!assembler.changDirty) {
assembler.changDirty = function (comp: MotionStreak) {
if (comp.points.length >= 2) {
let rd: any = comp.renderData;
//标记刷新纹理id数据
rd && (rd.dataDirty = 1);
}
};
const update = assembler.update;
assembler.update = function (comp: MotionStreak, dt: number) {
update.call(this, comp, dt);
this.changDirty(comp);
};
}
return assembler;
};
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (TiledLayer) {
const Tiled: any = TiledLayer.prototype;
;
Tiled.useMult = true;
Tiled.dataDirty = false;
const setUserNodeDirty = Tiled.setUserNodeDirty;
Tiled.setUserNodeDirty = function (dirty: boolean) {
setUserNodeDirty.call(this, dirty);
if (!dirty) {
//标记刷新纹理id数据
this.dataDirty = true;
}
};
Tiled._render = function (ui: any): void {
for (let i = 0; i < this._tiledDataArray.length; i++) {
this._tiledDataArrayIdx = i;
const m = this._tiledDataArray[i];
if (m.subNodes) {
// 提前处理 User Nodes
m.subNodes.forEach((c: any) => {
if (c) ui.walk(c.node);
});
} else {
const td = m as TiledRenderData;
if (td.texture) {
const data = this.tiledDataArray[this._tiledDataArrayIdx] as TiledRenderData;
let rd: any = data.renderData;
if (rd) {
//切换实际绘画renderdata
let isDirty = false;
rd.renderData = data.renderData;
rd.renderData.material = this.material;
if (rd.texture !== data.texture) {
rd.texture = data.texture;
isDirty = true;
}
if (rd.layer !== this.node.layer) {
rd.layer = this.node.layer;
isDirty = true;
}
//更新renderdata hash
isDirty && rd.updateHash();
if (this.dataDirty) {
rd.dataDirty = 1;
}
}
// NOTE: 由于 commitComp 只支持单张纹理, 故分多次提交
ui.commitComp(this, rd.renderData, rd.texture, this._assembler, null);
}
}
}
this.dataDirty = false;
this.node._static = true;
};
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
});
//*/
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment