dd
This commit is contained in:
126
assets/resources/multTextures/Mult-effect.effect
Normal file
126
assets/resources/multTextures/Mult-effect.effect
Normal file
@@ -0,0 +1,126 @@
|
||||
// Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd.
|
||||
CCEffect %{
|
||||
techniques:
|
||||
- passes:
|
||||
- vert: sprite-vs:vert
|
||||
frag: sprite-fs:frag
|
||||
depthStencilState:
|
||||
depthTest: false
|
||||
depthWrite: false
|
||||
blendState:
|
||||
targets:
|
||||
- blend: true
|
||||
blendSrc: src_alpha
|
||||
blendDst: one_minus_src_alpha
|
||||
blendDstAlpha: one_minus_src_alpha
|
||||
rasterizerState:
|
||||
cullMode: none
|
||||
properties:
|
||||
alphaThreshold: { value: 0.5 }
|
||||
texture1: { value: white}
|
||||
texture2: { value: white}
|
||||
texture3: { value: white}
|
||||
texture4: { value: white}
|
||||
texture5: { value: white}
|
||||
texture6: { value: white}
|
||||
texture7: { value: white}
|
||||
}%
|
||||
|
||||
CCProgram sprite-vs %{
|
||||
precision highp float;
|
||||
#include <builtin/uniforms/cc-global>
|
||||
#if USE_LOCAL
|
||||
#include <builtin/uniforms/cc-local>
|
||||
#endif
|
||||
#if SAMPLE_FROM_RT
|
||||
#include <common/common-define>
|
||||
#endif
|
||||
in vec3 a_position;
|
||||
in vec2 a_texCoord;
|
||||
in vec4 a_color;
|
||||
|
||||
out vec4 color;
|
||||
out vec3 uv0;
|
||||
|
||||
vec4 vert () {
|
||||
vec4 pos = vec4(a_position, 1);
|
||||
|
||||
#if USE_LOCAL
|
||||
pos = cc_matWorld * pos;
|
||||
#endif
|
||||
|
||||
#if USE_PIXEL_ALIGNMENT
|
||||
pos = cc_matView * pos;
|
||||
pos.xyz = floor(pos.xyz);
|
||||
pos = cc_matProj * pos;
|
||||
#else
|
||||
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;
|
||||
|
||||
#if SAMPLE_FROM_RT
|
||||
CC_HANDLE_RT_SAMPLE_FLIP(uv0.xy);
|
||||
#endif
|
||||
color = a_color;
|
||||
|
||||
return pos;
|
||||
}
|
||||
}%
|
||||
|
||||
CCProgram sprite-fs %{
|
||||
precision highp float;
|
||||
#include <builtin/internal/embedded-alpha>
|
||||
#include <builtin/internal/alpha-test>
|
||||
|
||||
in vec4 color;
|
||||
|
||||
#if USE_TEXTURE
|
||||
in vec3 uv0;
|
||||
#pragma builtin(local)
|
||||
layout(set = 2, binding = 12) uniform sampler2D cc_spriteTexture;
|
||||
uniform sampler2D texture1;
|
||||
uniform sampler2D texture2;
|
||||
uniform sampler2D texture3;
|
||||
uniform sampler2D texture4;
|
||||
uniform sampler2D texture5;
|
||||
uniform sampler2D texture6;
|
||||
uniform sampler2D texture7;
|
||||
#endif
|
||||
|
||||
vec4 frag () {
|
||||
vec4 o = vec4(1, 1, 1, 1);
|
||||
|
||||
#if USE_TEXTURE
|
||||
if(uv0.z<0.5)
|
||||
o *= CCSampleWithAlphaSeparated(cc_spriteTexture, uv0.xy);
|
||||
else if(uv0.z<1.5)
|
||||
o *= CCSampleWithAlphaSeparated(texture1, uv0.xy);
|
||||
else if(uv0.z<2.5)
|
||||
o *= CCSampleWithAlphaSeparated(texture2, uv0.xy);
|
||||
else if(uv0.z<3.5)
|
||||
o *= CCSampleWithAlphaSeparated(texture3, uv0.xy);
|
||||
else if(uv0.z<4.5)
|
||||
o *= CCSampleWithAlphaSeparated(texture4, uv0.xy);
|
||||
else if(uv0.z<5.5)
|
||||
o *= CCSampleWithAlphaSeparated(texture5, uv0.xy);
|
||||
else if(uv0.z<6.5)
|
||||
o *= CCSampleWithAlphaSeparated(texture6, uv0.xy);
|
||||
else
|
||||
o *= CCSampleWithAlphaSeparated(texture7, uv0.xy);
|
||||
|
||||
#if IS_GRAY
|
||||
float gray = 0.2126 * o.r + 0.7152 * o.g + 0.0722 * o.b;
|
||||
o.r = o.g = o.b = gray;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
o *= color;
|
||||
ALPHA_TEST(o);
|
||||
return o;
|
||||
}
|
||||
}%
|
||||
11
assets/resources/multTextures/Mult-effect.effect.meta
Normal file
11
assets/resources/multTextures/Mult-effect.effect.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"ver": "1.7.1",
|
||||
"importer": "effect",
|
||||
"imported": true,
|
||||
"uuid": "add9fcaa-8475-4521-917b-7de307b81c4d",
|
||||
"files": [
|
||||
".json"
|
||||
],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
31
assets/resources/multTextures/Mult-material.mtl
Normal file
31
assets/resources/multTextures/Mult-material.mtl
Normal file
@@ -0,0 +1,31 @@
|
||||
{
|
||||
"__type__": "cc.Material",
|
||||
"_name": "",
|
||||
"_objFlags": 0,
|
||||
"__editorExtras__": {},
|
||||
"_native": "",
|
||||
"_effectAsset": {
|
||||
"__uuid__": "add9fcaa-8475-4521-917b-7de307b81c4d",
|
||||
"__expectedType__": "cc.EffectAsset"
|
||||
},
|
||||
"_techIdx": 0,
|
||||
"_defines": [
|
||||
{
|
||||
"USE_TEXTURE": true
|
||||
}
|
||||
],
|
||||
"_states": [
|
||||
{
|
||||
"rasterizerState": {},
|
||||
"depthStencilState": {},
|
||||
"blendState": {
|
||||
"targets": [
|
||||
{}
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
"_props": [
|
||||
{}
|
||||
]
|
||||
}
|
||||
11
assets/resources/multTextures/Mult-material.mtl.meta
Normal file
11
assets/resources/multTextures/Mult-material.mtl.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"ver": "1.0.21",
|
||||
"importer": "material",
|
||||
"imported": true,
|
||||
"uuid": "490db6c2-4dd4-4050-98ec-c1c62ccaae16",
|
||||
"files": [
|
||||
".json"
|
||||
],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
306
assets/resources/multTextures/MultTextures.ts
Normal file
306
assets/resources/multTextures/MultTextures.ts
Normal file
@@ -0,0 +1,306 @@
|
||||
//*//
|
||||
import { BaseRenderData, Director, Game, Material, Node, ParticleSystem2D, Sprite, SpriteFrame, StencilManager, UIRenderer, __private, _decorator, assert, cclegacy, director, game, renderer, resources } from 'cc';
|
||||
import { DEBUG, EDITOR, JSB } from 'cc/env';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
|
||||
export const MultBatch2D: any = {
|
||||
enable: false,
|
||||
parent: null,
|
||||
textures: [],
|
||||
hash: 0,
|
||||
reset: function () {
|
||||
this.textures.length = 0;
|
||||
}
|
||||
};
|
||||
|
||||
let _cacheUseCount: number = 0;
|
||||
let _cacheMaterials: Array<Material> = [];
|
||||
|
||||
const getMultMaterial = function () {
|
||||
|
||||
if (!MultBatch2D.enable) return null;
|
||||
|
||||
let material: any = _cacheMaterials[_cacheUseCount++];
|
||||
if (!material) {
|
||||
const mat = { parent: MultBatch2D.parent };
|
||||
material = new renderer.MaterialInstance(mat);
|
||||
material['isMultTextures'] = true;
|
||||
_cacheMaterials.push(material);
|
||||
}
|
||||
|
||||
return material;
|
||||
}
|
||||
|
||||
let 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();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
const UIR: any = UIRenderer.prototype;
|
||||
const updateMaterial: any = UIR.updateMaterial;
|
||||
UIR.updateMaterial = function () {
|
||||
updateMaterial.call(this);
|
||||
//this.getSharedMaterial(0);
|
||||
let material = this.customMaterial || this.material;
|
||||
if (material) {
|
||||
material['isMultTextures'] = false;
|
||||
if (MultBatch2D.hash == material.hash) {
|
||||
material['isMultTextures'] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const PS2D: any = ParticleSystem2D.prototype;
|
||||
const _updateMaterial = PS2D._updateMaterial;
|
||||
PS2D._updateMaterial = function () {
|
||||
_updateMaterial.call(this);
|
||||
let material = this.customMaterial || this.material;
|
||||
if (material) {
|
||||
material['isMultTextures'] = false;
|
||||
if (MultBatch2D.hash == material.hash) {
|
||||
material['isMultTextures'] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
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;
|
||||
|
||||
Object.defineProperty(cclegacy.UI.RenderData.prototype, "vertDirty", {
|
||||
get: function () {
|
||||
return this._vertDirty;
|
||||
},
|
||||
set: function (val: boolean) {
|
||||
this._vertDirty = val;
|
||||
if (val === true) {
|
||||
this.dataDirty = true;
|
||||
}
|
||||
if (this._renderDrawInfo && val) {
|
||||
this._renderDrawInfo.setVertDirty(val);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Object.defineProperty(cclegacy.UI.RenderData.prototype, "textureDirty", {
|
||||
get: function () {
|
||||
return this.texDirty;
|
||||
},
|
||||
set: function (val: boolean) {
|
||||
this.texDirty = val;
|
||||
if (val === true) {
|
||||
this.texID = -1;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const Spr: any = Sprite.prototype;
|
||||
Spr.flagChangedVersion = -1;
|
||||
Object.defineProperty(Spr, "_flagChangedVersion", {
|
||||
get: function () {
|
||||
return this.flagChangedVersion;
|
||||
},
|
||||
set: function (val: number) {
|
||||
if (this.flagChangedVersion != val) {
|
||||
this.flagChangedVersion = val;
|
||||
let rd = this.renderData;
|
||||
let type = this.type;
|
||||
if (rd && type == Sprite.Type.TILED
|
||||
|| (type == Sprite.Type.FILLED && Sprite.FillType.RADIAL)) {
|
||||
rd.dataDirty = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
director.on(Director.EVENT_AFTER_DRAW, (dt) => {
|
||||
cclegacy.internal.Batcher2D._rdHash = -1;
|
||||
MultBatch2D.reset();
|
||||
_cacheUseCount = 0;
|
||||
});
|
||||
|
||||
|
||||
cclegacy.internal.Batcher2D.prototype.currMaterial = null;
|
||||
Object.defineProperty(cclegacy.internal.Batcher2D.prototype, "_currMaterial", {
|
||||
get: function () {
|
||||
return this.currMaterial;
|
||||
},
|
||||
set: function (metrial: any) {
|
||||
if (this.currMaterial === metrial) return;
|
||||
this.currMaterial = metrial;
|
||||
MultBatch2D.reset();
|
||||
if (metrial && metrial.isMultTextures) {
|
||||
let mat = getMultMaterial();
|
||||
if (mat) {
|
||||
this.currMaterial = mat;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
const Stage_ENTER_LEVEL = 2;
|
||||
const Stage_ENTER_LEVEL_INVERTED = 6;
|
||||
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 bufferID = -1;
|
||||
if (renderData && renderData.chunk) {
|
||||
if (!renderData.isValid()) return;
|
||||
dataHash = renderData.dataHash;
|
||||
mat = renderData.material;
|
||||
bufferID = renderData.chunk.bufferId;
|
||||
// as RenderData;
|
||||
let rd: any = renderData;
|
||||
rdHash = bufferID << 16 | rd.layer;
|
||||
|
||||
}
|
||||
// Notice: A little hack, if it is for mask, not need update here, while control by stencilManger
|
||||
if (comp.stencilStage === Stage_ENTER_LEVEL || comp.stencilStage === Stage_ENTER_LEVEL_INVERTED) {
|
||||
this._insertMaskBatch(comp);
|
||||
} else {
|
||||
comp.stencilStage = StencilManager.sharedManager!.stage;
|
||||
}
|
||||
const depthStencilStateStage = comp.stencilStage;
|
||||
|
||||
|
||||
|
||||
let texID = -1;
|
||||
let texture = null;
|
||||
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 (flushBatch
|
||||
|| this._currHash !== dataHash || dataHash === 0 || this._currMaterial !== mat
|
||||
|| this._currDepthStencilStateStage !== depthStencilStateStage) {
|
||||
// Merge all previous data to a render batch, and update buffer for next render data
|
||||
this.autoMergeBatches(this._currComponent!);
|
||||
if (renderData && !renderData._isMeshBuffer) {
|
||||
this.updateBuffer(renderData.vertexFormat, bufferID);
|
||||
}
|
||||
|
||||
this._rdHash = rdHash;
|
||||
this._currRenderData = renderData;
|
||||
this._currHash = renderData ? renderData.dataHash : 0;
|
||||
this._currComponent = comp;
|
||||
this._currTransform = transform;
|
||||
this._currMaterial = comp.getRenderMaterial(0)!;
|
||||
this._currDepthStencilStateStage = depthStencilStateStage;
|
||||
this._currLayer = comp.node.layer;
|
||||
if (frame) {
|
||||
if (DEBUG) {
|
||||
assert(frame.isValid, 'frame should not be invalid, it may have been released');
|
||||
}
|
||||
this._currTexture = frame.getGFXTexture();
|
||||
this._currSampler = frame.getGFXSampler();
|
||||
this._currTextureHash = frame.getHash();
|
||||
this._currSamplerHash = this._currSampler.hash;
|
||||
} else {
|
||||
this._currTexture = null;
|
||||
this._currSampler = null;
|
||||
this._currTextureHash = 0;
|
||||
this._currSamplerHash = 0;
|
||||
}
|
||||
}
|
||||
|
||||
assembler.fillBuffers(comp, this);
|
||||
|
||||
if (isMultTextures) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
this._fillDatas(renderData, texID);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
cclegacy.internal.Batcher2D.prototype["_fillDatas"] = function (renderData: any, texID: number) {
|
||||
|
||||
if (!renderData) return;
|
||||
|
||||
let uvX = 0;
|
||||
let vbuf = renderData.chunk.vb;
|
||||
if (renderData.dataDirty) {
|
||||
renderData.dataDirty = false;
|
||||
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) {
|
||||
for (let i = 0, length = vbuf.length; i < length; i += 9) {
|
||||
uvX = ~~(vbuf[i + 3] * 0.1);
|
||||
vbuf[i + 3] = uvX * 10 + texID;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
renderData.texID = texID;
|
||||
};
|
||||
|
||||
});
|
||||
|
||||
//*/
|
||||
9
assets/resources/multTextures/MultTextures.ts.meta
Normal file
9
assets/resources/multTextures/MultTextures.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "bd7e7bed-a237-4c50-9383-95bb886ff622",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
Reference in New Issue
Block a user