Compare commits
51 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a855400c93 | |||
| 3aeb193d42 | |||
| c4e4790ee6 | |||
| 143af30cf2 | |||
| d6ff23fed4 | |||
| 84e8d4ebf7 | |||
| f2b9936033 | |||
| 129eb437ad | |||
| c58c893334 | |||
| ab982aec68 | |||
| 2d2bbe6731 | |||
| 187387b03f | |||
| 661253a521 | |||
| b6d0f02e88 | |||
| dd1b155d3a | |||
| df96ee85f3 | |||
| 708a343ce8 | |||
| 393203c2f8 | |||
| f3d11f017b | |||
| 09c201017c | |||
| ce6107d930 | |||
| ab8792686b | |||
| 44ef76855b | |||
| 8878e7aade | |||
| 621880a123 | |||
| 2eb268bf9a | |||
| 2e6cc9b127 | |||
| 98917ff2a7 | |||
| dc29d2f8f8 | |||
| eec2a1693b | |||
| 632f9b442f | |||
| 8ce19b658e | |||
| 07f2c5fb43 | |||
| 9b17fd6165 | |||
| e2795f9ecd | |||
| 89a412e0d6 | |||
| aef31099d8 | |||
| 89f8768e57 | |||
| 424be5d0b2 | |||
| e5183b796a | |||
| 32478c1a9c | |||
| 74e632a3d6 | |||
| 26883ba6f4 | |||
| 055c70f8d2 | |||
| 99f6db401c | |||
| 9390ee6bc0 | |||
| 7f258dac58 | |||
| 5c99570616 | |||
| 821928733b | |||
| f567861ff6 | |||
| a7a20b64ab |
@@ -28,6 +28,7 @@ jobs:
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
- run: npm ci --ignore-scripts
|
||||
- run: node scripts/spread-pal.cjs
|
||||
- run: npm run build:debug-infos
|
||||
- name: Download external
|
||||
run: |
|
||||
|
||||
@@ -46,3 +46,11 @@ platforms/runtime/platforms/vivo-mini-game/ral.min.js
|
||||
@types/consts.d.ts
|
||||
|
||||
__TS_ENGINE__/
|
||||
|
||||
# pal 由 @cocos/engine-pal 在 postinstall 挂载(软链/复制),不入库
|
||||
/pal
|
||||
|
||||
# platforms 源码已抽到 @cocos/engine-platforms;其预打包 adapter 由 postinstall 的
|
||||
# spread-adapter.cjs 复制到 bin/adapter(/bin 已忽略)。platforms 源码不再入库。
|
||||
/platforms
|
||||
|
||||
|
||||
@@ -382,70 +382,70 @@
|
||||
"test": "context.buildTimeConstants && context.buildTimeConstants.HTML5",
|
||||
"isVirtualModule": true,
|
||||
"overrides": {
|
||||
"pal/minigame": "pal/minigame/non-minigame.ts",
|
||||
"pal/audio": "pal/audio/web/player.ts",
|
||||
"pal/system-info": "pal/system-info/web/system-info.ts",
|
||||
"pal/screen-adapter": "pal/screen-adapter/web/screen-adapter.ts",
|
||||
"pal/input": "pal/input/web/index.ts",
|
||||
"pal/env": "pal/env/web/env.ts",
|
||||
"pal/pacer": "pal/pacer/pacer-web.ts",
|
||||
"pal/wasm": "pal/wasm/wasm-web.ts"
|
||||
"pal/minigame": "pal/minigame/non-minigame.js",
|
||||
"pal/audio": "pal/audio/web/player.js",
|
||||
"pal/system-info": "pal/system-info/web/system-info.js",
|
||||
"pal/screen-adapter": "pal/screen-adapter/web/screen-adapter.js",
|
||||
"pal/input": "pal/input/web/index.js",
|
||||
"pal/env": "pal/env/web/env.js",
|
||||
"pal/pacer": "pal/pacer/pacer-web.js",
|
||||
"pal/wasm": "pal/wasm/wasm-web.js"
|
||||
}
|
||||
},
|
||||
{
|
||||
"test": "context.buildTimeConstants && context.buildTimeConstants.NATIVE",
|
||||
"isVirtualModule": true,
|
||||
"overrides": {
|
||||
"pal/minigame": "pal/minigame/non-minigame.ts",
|
||||
"pal/audio": "pal/audio/native/player.ts",
|
||||
"pal/system-info": "pal/system-info/native/system-info.ts",
|
||||
"pal/screen-adapter": "pal/screen-adapter/native/screen-adapter.ts",
|
||||
"pal/input": "pal/input/native/index.ts",
|
||||
"pal/env": "pal/env/native/env.ts",
|
||||
"pal/pacer": "pal/pacer/pacer-native.ts",
|
||||
"pal/wasm": "pal/wasm/wasm-native.ts"
|
||||
"pal/minigame": "pal/minigame/non-minigame.js",
|
||||
"pal/audio": "pal/audio/native/player.js",
|
||||
"pal/system-info": "pal/system-info/native/system-info.js",
|
||||
"pal/screen-adapter": "pal/screen-adapter/native/screen-adapter.js",
|
||||
"pal/input": "pal/input/native/index.js",
|
||||
"pal/env": "pal/env/native/env.js",
|
||||
"pal/pacer": "pal/pacer/pacer-native.js",
|
||||
"pal/wasm": "pal/wasm/wasm-native.js"
|
||||
}
|
||||
},
|
||||
{
|
||||
"test": "context.buildTimeConstants && context.buildTimeConstants.MINIGAME",
|
||||
"isVirtualModule": true,
|
||||
"overrides": {
|
||||
"pal/minigame": "pal/minigame/{{context.platform.toLowerCase()}}.ts",
|
||||
"pal/audio": "pal/audio/minigame/player.ts",
|
||||
"pal/system-info": "pal/system-info/minigame/system-info.ts",
|
||||
"pal/screen-adapter": "pal/screen-adapter/minigame/screen-adapter.ts",
|
||||
"pal/input": "pal/input/minigame/index.ts",
|
||||
"pal/env": "pal/env/minigame/env.ts",
|
||||
"pal/pacer": "pal/pacer/pacer-minigame.ts",
|
||||
"pal/wasm": "pal/wasm/wasm-minigame.ts"
|
||||
"pal/minigame": "pal/minigame/{{context.platform.toLowerCase()}}.js",
|
||||
"pal/audio": "pal/audio/minigame/player.js",
|
||||
"pal/system-info": "pal/system-info/minigame/system-info.js",
|
||||
"pal/screen-adapter": "pal/screen-adapter/minigame/screen-adapter.js",
|
||||
"pal/input": "pal/input/minigame/index.js",
|
||||
"pal/env": "pal/env/minigame/env.js",
|
||||
"pal/pacer": "pal/pacer/pacer-minigame.js",
|
||||
"pal/wasm": "pal/wasm/wasm-minigame.js"
|
||||
}
|
||||
},
|
||||
{
|
||||
"test": "context.buildTimeConstants && context.buildTimeConstants.RUNTIME_BASED",
|
||||
"isVirtualModule": true,
|
||||
"overrides": {
|
||||
"pal/minigame": "pal/minigame/runtime.ts",
|
||||
"pal/audio": "pal/audio/minigame/player.ts",
|
||||
"pal/system-info": "pal/system-info/minigame/system-info.ts",
|
||||
"pal/screen-adapter": "pal/screen-adapter/minigame/screen-adapter.ts",
|
||||
"pal/input": "pal/input/minigame/index.ts",
|
||||
"pal/env": "pal/env/runtime/env.ts",
|
||||
"pal/pacer": "pal/pacer/pacer-minigame.ts",
|
||||
"pal/wasm": "pal/wasm/wasm-minigame.ts"
|
||||
"pal/minigame": "pal/minigame/runtime.js",
|
||||
"pal/audio": "pal/audio/minigame/player.js",
|
||||
"pal/system-info": "pal/system-info/minigame/system-info.js",
|
||||
"pal/screen-adapter": "pal/screen-adapter/minigame/screen-adapter.js",
|
||||
"pal/input": "pal/input/minigame/index.js",
|
||||
"pal/env": "pal/env/runtime/env.js",
|
||||
"pal/pacer": "pal/pacer/pacer-minigame.js",
|
||||
"pal/wasm": "pal/wasm/wasm-minigame.js"
|
||||
}
|
||||
},
|
||||
{
|
||||
"test": "context.buildTimeConstants && context.buildTimeConstants.NODEJS",
|
||||
"isVirtualModule": true,
|
||||
"overrides": {
|
||||
"pal/minigame": "pal/minigame/non-minigame.ts",
|
||||
"pal/audio": "pal/audio/nodejs/player.ts",
|
||||
"pal/system-info": "pal/system-info/nodejs/system-info.ts",
|
||||
"pal/screen-adapter": "pal/screen-adapter/nodejs/screen-adapter.ts",
|
||||
"pal/input": "pal/input/nodejs/index.ts",
|
||||
"pal/env": "pal/env/nodejs/env.ts",
|
||||
"pal/pacer": "pal/pacer/pacer-nodejs.ts",
|
||||
"pal/wasm": "pal/wasm/wasm-nodejs.ts"
|
||||
"pal/minigame": "pal/minigame/non-minigame.js",
|
||||
"pal/audio": "pal/audio/nodejs/player.js",
|
||||
"pal/system-info": "pal/system-info/nodejs/system-info.js",
|
||||
"pal/screen-adapter": "pal/screen-adapter/nodejs/screen-adapter.js",
|
||||
"pal/input": "pal/input/nodejs/index.js",
|
||||
"pal/env": "pal/env/nodejs/env.js",
|
||||
"pal/pacer": "pal/pacer/pacer-nodejs.js",
|
||||
"pal/wasm": "pal/wasm/wasm-nodejs.js"
|
||||
}
|
||||
},
|
||||
{
|
||||
@@ -470,7 +470,15 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"test": "context.buildTimeConstants.SPINE_3_8",
|
||||
"test": "context.buildTimeConstants.SPINE_3_8 && context.buildTimeConstants.SPINE_4_2",
|
||||
"isVirtualModule": false,
|
||||
"overrides": {
|
||||
"cocos/spine/lib/spine-version.ts": "cocos/spine/lib/spine-version-dynamic.ts",
|
||||
"cocos/spine/lib/spine-instantiate.ts": "cocos/spine/lib/spine-instantiate-dynamic.ts"
|
||||
}
|
||||
},
|
||||
{
|
||||
"test": "context.buildTimeConstants.SPINE_3_8 && !context.buildTimeConstants.SPINE_4_2",
|
||||
"isVirtualModule": false,
|
||||
"overrides": {
|
||||
"cocos/spine/lib/spine-version.ts": "cocos/spine/lib/spine-version-3.8.ts",
|
||||
@@ -478,7 +486,7 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"test": "context.buildTimeConstants.SPINE_4_2",
|
||||
"test": "context.buildTimeConstants.SPINE_4_2 && !context.buildTimeConstants.SPINE_3_8",
|
||||
"isVirtualModule": false,
|
||||
"overrides": {
|
||||
"cocos/spine/lib/spine-version.ts": "cocos/spine/lib/spine-version-4.2.ts",
|
||||
@@ -877,10 +885,10 @@
|
||||
"cocos/asset/asset-manager/weak-cache.ts",
|
||||
"cocos/2d/framework/ui-skew-utils.ts",
|
||||
"cocos/spine/lib/spine-binary.ts",
|
||||
"pal/input/minigame/gamepad-input.ts",
|
||||
"pal/input/minigame/handheld-input.ts",
|
||||
"pal/input/minigame/handle-input.ts",
|
||||
"pal/input/minigame/hmd-input.ts"
|
||||
"pal/input/minigame/gamepad-input.js",
|
||||
"pal/input/minigame/handheld-input.js",
|
||||
"pal/input/minigame/handle-input.js",
|
||||
"pal/input/minigame/hmd-input.js"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -637,13 +637,13 @@ export class Graphics extends UIRenderer {
|
||||
const vertexBuffer = gfxDevice.createBuffer(new BufferInfo(
|
||||
BufferUsageBit.VERTEX | BufferUsageBit.TRANSFER_DST,
|
||||
MemoryUsageBit.DEVICE,
|
||||
65535 * stride,
|
||||
65536 * stride, // Uint16 can address 65536 vertices (indices 0–65535)
|
||||
stride,
|
||||
));
|
||||
const indexBuffer = gfxDevice.createBuffer(new BufferInfo(
|
||||
BufferUsageBit.INDEX | BufferUsageBit.TRANSFER_DST,
|
||||
MemoryUsageBit.DEVICE,
|
||||
65535 * Uint16Array.BYTES_PER_ELEMENT * 2,
|
||||
65536 * Uint16Array.BYTES_PER_ELEMENT * 2, // Uint16 can address 65536 vertices (indices 0–65535)
|
||||
Uint16Array.BYTES_PER_ELEMENT,
|
||||
));
|
||||
|
||||
|
||||
@@ -64,10 +64,10 @@ function _calculateCircle (center: Vec3, radius: Vec3, segments: number): Vec3[]
|
||||
*/
|
||||
export enum MaskType {
|
||||
/**
|
||||
* @en Rect mask.
|
||||
* @en Rect mask, supports rounded corners by setting radius.
|
||||
*
|
||||
* @zh
|
||||
* 使用矩形作为遮罩。
|
||||
* 使用矩形作为遮罩,可通过 radius 设置圆角。
|
||||
*/
|
||||
GRAPHICS_RECT = 0,
|
||||
|
||||
@@ -215,6 +215,32 @@ export class Mask extends Component {
|
||||
this._updateGraphics();
|
||||
}
|
||||
|
||||
/**
|
||||
* @en
|
||||
* The radius for rounded corners of rect mask.
|
||||
*
|
||||
* @zh
|
||||
* 矩形遮罩的圆角半径。
|
||||
*/
|
||||
@visible(function (this: Mask) {
|
||||
return this.type === MaskType.GRAPHICS_RECT;
|
||||
})
|
||||
@tooltip('i18n:mask.radius')
|
||||
@range([0, Number.POSITIVE_INFINITY])
|
||||
get radius (): number {
|
||||
return this._radius;
|
||||
}
|
||||
|
||||
set radius (value) {
|
||||
const radius = clamp(value, 0, Number.POSITIVE_INFINITY);
|
||||
if (this._radius === radius) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._radius = radius;
|
||||
this._updateGraphics();
|
||||
}
|
||||
|
||||
/**
|
||||
* @en
|
||||
* The mask image.
|
||||
@@ -289,6 +315,9 @@ export class Mask extends Component {
|
||||
@serializable
|
||||
protected _segments = 64;
|
||||
|
||||
@serializable
|
||||
protected _radius = 0;
|
||||
|
||||
@serializable
|
||||
protected _alphaThreshold = 0.1;
|
||||
|
||||
@@ -350,7 +379,25 @@ export class Mask extends Component {
|
||||
testPt.y += ap.y * h;
|
||||
|
||||
let result = false;
|
||||
if (this.type === MaskType.GRAPHICS_RECT || this.type === MaskType.GRAPHICS_STENCIL || this.type === MaskType.SPRITE_STENCIL) {
|
||||
if (this.type === MaskType.GRAPHICS_RECT) {
|
||||
result = testPt.x >= 0 && testPt.y >= 0 && testPt.x <= w && testPt.y <= h;
|
||||
const radius = Math.min(this._radius, w / 2, h / 2);
|
||||
if (result && radius > 0) {
|
||||
let dx = 0;
|
||||
let dy = 0;
|
||||
if (testPt.x < radius) {
|
||||
dx = testPt.x - radius;
|
||||
} else if (testPt.x > w - radius) {
|
||||
dx = testPt.x - (w - radius);
|
||||
}
|
||||
if (testPt.y < radius) {
|
||||
dy = testPt.y - radius;
|
||||
} else if (testPt.y > h - radius) {
|
||||
dy = testPt.y - (h - radius);
|
||||
}
|
||||
result = dx * dx + dy * dy <= radius * radius;
|
||||
}
|
||||
} else if (this.type === MaskType.GRAPHICS_STENCIL || this.type === MaskType.SPRITE_STENCIL) {
|
||||
result = testPt.x >= 0 && testPt.y >= 0 && testPt.x <= w && testPt.y <= h;
|
||||
} else if (this.type === MaskType.GRAPHICS_ELLIPSE) {
|
||||
const rx = w / 2;
|
||||
@@ -423,7 +470,12 @@ export class Mask extends Component {
|
||||
const x = -width * ap.x;
|
||||
const y = -height * ap.y;
|
||||
if (this._type === MaskType.GRAPHICS_RECT) {
|
||||
graphics.rect(x, y, width, height);
|
||||
const radius = Math.min(this._radius, width / 2, height / 2);
|
||||
if (radius > 0) {
|
||||
graphics.roundRect(x, y, width, height, radius);
|
||||
} else {
|
||||
graphics.rect(x, y, width, height);
|
||||
}
|
||||
} else if (this._type === MaskType.GRAPHICS_ELLIPSE) {
|
||||
const center = new Vec3(x + width / 2, y + height / 2, 0);
|
||||
const radius = new Vec3(width / 2, height / 2, 0);
|
||||
|
||||
@@ -567,7 +567,7 @@ export class Sprite extends UIRenderer {
|
||||
if (this.spriteFrame && this.spriteFrame.texture instanceof RenderTexture) {
|
||||
const rtMatName = `rt-${mat.name}`;
|
||||
let rtMat = builtinResMgr.get(rtMatName) as Material | null;
|
||||
if (!rtMat) {
|
||||
if (!rtMat || !rtMat.passes.length) {
|
||||
rtMat = new Material(rtMatName);
|
||||
rtMat.copy(mat, { defines: { SAMPLE_FROM_RT: true } });
|
||||
builtinResMgr.addAsset(rtMatName, rtMat);
|
||||
|
||||
@@ -536,7 +536,7 @@ export class UIRenderer extends Renderer {
|
||||
const mat = this._updateBuiltinMaterial();
|
||||
this.setSharedMaterial(mat, 0);
|
||||
if (this.stencilStage === Stage.ENTER_LEVEL || this.stencilStage === Stage.ENTER_LEVEL_INVERTED) {
|
||||
this.getMaterialInstance(0)!.recompileShaders({ USE_ALPHA_TEST: true });
|
||||
this.getMaterialInstance(0)?.recompileShaders({ USE_ALPHA_TEST: true });
|
||||
}
|
||||
this._updateBlendFunc();
|
||||
}
|
||||
@@ -600,16 +600,24 @@ export class UIRenderer extends Renderer {
|
||||
*/
|
||||
public _updateBlendFunc (): void {
|
||||
// todo: Not only Pass[0].target[0]
|
||||
let target = this.getRenderMaterial(0)!.passes[0].blendState.targets[0];
|
||||
const renderMat = this.getRenderMaterial(0);
|
||||
if (!renderMat || !renderMat.passes[0]) {
|
||||
return;
|
||||
}
|
||||
let target = renderMat.passes[0].blendState.targets[0];
|
||||
this._dstBlendFactorCache = target.blendDst;
|
||||
this._srcBlendFactorCache = target.blendSrc;
|
||||
if (this._dstBlendFactorCache !== this._dstBlendFactor || this._srcBlendFactorCache !== this._srcBlendFactor) {
|
||||
target = this.getMaterialInstance(0)!.passes[0].blendState.targets[0];
|
||||
const matInstance = this.getMaterialInstance(0);
|
||||
if (!matInstance || !matInstance.passes[0]) {
|
||||
return;
|
||||
}
|
||||
target = matInstance.passes[0].blendState.targets[0];
|
||||
target.blend = true;
|
||||
target.blendDstAlpha = BlendFactor.ONE_MINUS_SRC_ALPHA;
|
||||
target.blendDst = this._dstBlendFactor;
|
||||
target.blendSrc = this._srcBlendFactor;
|
||||
const targetPass = this.getMaterialInstance(0)!.passes[0];
|
||||
const targetPass = matInstance.passes[0];
|
||||
targetPass.blendState.setTarget(0, target);
|
||||
targetPass._updatePassHash();
|
||||
this._dstBlendFactorCache = this._dstBlendFactor;
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
*/
|
||||
|
||||
import { JSB } from 'internal:constants';
|
||||
import { Device, BufferUsageBit, MemoryUsageBit, Attribute, Buffer, BufferInfo, InputAssembler, InputAssemblerInfo, Feature, API } from '../../gfx';
|
||||
import { Device, BufferUsageBit, MemoryUsageBit, Attribute, Buffer, BufferInfo, InputAssembler, InputAssemblerInfo, Feature } from '../../gfx';
|
||||
import { getAttributeStride } from './vertex-format';
|
||||
import { sys, getError, warnID, assertIsTrue } from '../../core';
|
||||
import { NativeUIMeshBuffer } from './native-2d';
|
||||
@@ -251,11 +251,10 @@ export class MeshBuffer {
|
||||
|
||||
this.floatsPerVertex = getAttributeStride(attrs) >> 2;
|
||||
|
||||
var vDataCountLimit = 65536; // 2^16 - 1
|
||||
const glApi = device.gfxAPI;
|
||||
if (glApi === API.WEBGPU || glApi === API.WEBGL2 || glApi === API.WEBGL && device.hasFeature(Feature.ELEMENT_INDEX_UINT)) {
|
||||
vDataCountLimit = 4294967295; // 2^32 - 1
|
||||
}
|
||||
// Use the GPU's actual reported capability to determine max addressable vertex count.
|
||||
// Feature.ELEMENT_INDEX_UINT is set to true in: WebGL2, WebGPU, Vulkan, Metal, GLES3, GLES2+ext.
|
||||
// vDataCountLimit is a vertex COUNT limit (max index value + 1), not the max index value itself.
|
||||
const vDataCountLimit = device.hasFeature(Feature.ELEMENT_INDEX_UINT) ? 4294967296 : 65536; // Uint32 or Uint16 can address 2^32 or 2^16 vertices (indices 0–2^32-1 or 0–2^16-1)
|
||||
assertIsTrue(this._initVDataCount / this._floatsPerVertex < vDataCountLimit, getError(9005));
|
||||
|
||||
if (!this.vData || !this.iData) {
|
||||
|
||||
@@ -634,7 +634,7 @@ export class MeshRenderData extends BaseRenderData {
|
||||
const newVBytes = this._byteLength + vertexCount * this.stride;
|
||||
const newICount = this.indexCount + indexCount;
|
||||
|
||||
if (vertexCount + this.vertexCount > 65535) {
|
||||
if (vertexCount + this.vertexCount > 65536) { // Uint16 can address 65536 vertices (indices 0–65535)
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -697,13 +697,13 @@ export class MeshRenderData extends BaseRenderData {
|
||||
if (this._byteLength > vertexBuffer.size) {
|
||||
vertexBuffer.resize(this._byteLength);
|
||||
}
|
||||
vertexBuffer.update(verticesData);
|
||||
vertexBuffer.update(verticesData.buffer as ArrayBuffer, this._byteLength);
|
||||
|
||||
const indexBytes = indexCount << 1;
|
||||
if (indexBytes > this._indexBuffer.size) {
|
||||
this._indexBuffer.resize(indexBytes);
|
||||
}
|
||||
this._indexBuffer.update(indicesData);
|
||||
this._indexBuffer.update(indicesData.buffer as ArrayBuffer, indexBytes);
|
||||
}
|
||||
|
||||
public freeIAPool (): void {
|
||||
|
||||
@@ -165,6 +165,10 @@ export class StaticVBAccessor extends BufferAccessor {
|
||||
errorID(9004, byteLength);
|
||||
return null;
|
||||
}
|
||||
if (vertexCount > 65536) { // Vertex count must less than 65536 for index is uint16 (0 to 65535)
|
||||
errorID(9004, byteLength);
|
||||
return null;
|
||||
}
|
||||
let buf: MeshBuffer = null!;
|
||||
let freeList: IFreeEntry[];
|
||||
let bid = 0;
|
||||
@@ -176,9 +180,11 @@ export class StaticVBAccessor extends BufferAccessor {
|
||||
freeList = this._freeLists[i];
|
||||
// Loop entries
|
||||
for (let e = 0; e < freeList.length; ++e) {
|
||||
const freeEntry = freeList[e];
|
||||
const vertexOffset: number = freeEntry.offset / this.vertexFormatBytes;
|
||||
// Found suitable free entry
|
||||
if (freeList[e].length >= byteLength) {
|
||||
entry = freeList[e];
|
||||
if (freeEntry.length >= byteLength && vertexOffset + (vertexCount - 1) <= 65535) {
|
||||
entry = freeEntry;
|
||||
bid = i;
|
||||
eid = e;
|
||||
break;
|
||||
|
||||
@@ -508,7 +508,7 @@ export class Mesh extends Asset {
|
||||
let dstSize = idxView.length;
|
||||
if (dstStride === 4 && !gfxDevice.hasFeature(Feature.ELEMENT_INDEX_UINT)) {
|
||||
const vertexCount = this._struct.vertexBundles[prim.vertexBundelIndices[0]].view.count;
|
||||
if (vertexCount >= 65536) {
|
||||
if (vertexCount > 65536) { // Uint16 can address 65536 vertices (indices 0–65535)
|
||||
warnID(10001, vertexCount, 65536);
|
||||
continue; // Ignore this primitive
|
||||
} else {
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
import { CULL_MESHOPT, NATIVE_CODE_BUNDLE_MODE } from 'internal:constants';
|
||||
import { ensureWasmModuleReady, instantiateWasm } from 'pal/wasm';
|
||||
|
||||
import { sys, logID, error } from '../../core';
|
||||
import { sys, error, debugID } from '../../core';
|
||||
|
||||
import { game } from '../../game';
|
||||
import { NativeCodeBundleMode } from '../../misc/webassembly-support';
|
||||
@@ -53,7 +53,7 @@ function initDecoderASM (asm_factory: any): Promise<void> {
|
||||
MeshoptDecoder.decodeGltfBuffer = asm_factory.decodeGltfBuffer;
|
||||
MeshoptDecoder.useWorkers = asm_factory.useWorkers;
|
||||
MeshoptDecoder.decodeGltfBufferAsync = asm_factory.decodeGltfBufferAsync;
|
||||
logID(14202);
|
||||
debugID(14202);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ function initDecoderWASM (wasm_factory: any, wasm_url: string): Promise<void> {
|
||||
MeshoptDecoder.decodeGltfBuffer = wasm_factory.decodeGltfBuffer;
|
||||
MeshoptDecoder.useWorkers = wasm_factory.useWorkers;
|
||||
MeshoptDecoder.decodeGltfBufferAsync = wasm_factory.decodeGltfBufferAsync;
|
||||
logID(14203);
|
||||
debugID(14203);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -195,8 +195,9 @@ function define (className, baseClass, options): any {
|
||||
renderName = 'render_stage';
|
||||
}
|
||||
// 增加了 hidden: 开头标识,使它最终不会显示在 Editor inspector 的添加组件列表里
|
||||
|
||||
window.EditorExtends && window.EditorExtends.Component.addMenu(cls, `hidden:${renderName}/${className}`, -1);
|
||||
if (window.EditorExtends) {
|
||||
window.EditorExtends.Component.addMenu(cls, `hidden:${renderName}/${className}`, -1);
|
||||
}
|
||||
}
|
||||
|
||||
// Note: `options.ctor` should be the same as `cls` except if
|
||||
|
||||
@@ -185,7 +185,7 @@ export class WebGL2Swapchain extends Swapchain {
|
||||
self._canvas = info.windowHandle;
|
||||
|
||||
self._webGL2ContextLostHandler = self._onWebGLContextLost.bind(self);
|
||||
self._canvas.addEventListener(eventWebGLContextLost, self._onWebGLContextLost);
|
||||
self._canvas.addEventListener(eventWebGLContextLost, self._webGL2ContextLostHandler);
|
||||
|
||||
const { instance } = WebGL2DeviceManager;
|
||||
const { gl, capabilities } = instance;
|
||||
|
||||
@@ -233,8 +233,8 @@ export class ParticleSystem extends ModelRenderer {
|
||||
public startLifetime = new CurveRange();
|
||||
|
||||
/**
|
||||
* @en Particle system duration time.
|
||||
* @zh 粒子系统运行时间。
|
||||
* @en Particle system emitter duration time.
|
||||
* @zh 粒子系统发射器运行时间。
|
||||
*/
|
||||
@serializable
|
||||
@displayOrder(0)
|
||||
|
||||
@@ -776,6 +776,13 @@ export default class ParticleSystemRendererCPU extends ParticleSystemRendererBas
|
||||
_matInsInfo.subModelIdx = 0;
|
||||
}
|
||||
mat = mat || this._defaultTrailMat!;
|
||||
|
||||
const matIns: MaterialInstance | null = ps.getMaterialInstance(1);
|
||||
if (matIns === null || matIns === undefined) {
|
||||
ps.setSharedMaterial(mat.parent, 1); // set material[1] as trail material for ui-mesh-renderer to use
|
||||
ps.setMaterialInstance(mat, 1);
|
||||
}
|
||||
|
||||
const texture = mat.getProperty('mainTexture', 0) as Texture2D | null;
|
||||
if (texture && texture.isAlphaAtlas) {
|
||||
this._trailDefines[CC_USE_EMBEDDED_ALPHA] = true;
|
||||
|
||||
@@ -377,7 +377,10 @@ export default class TrailModule {
|
||||
if (this.lifeTime.getMax() < 1.0) {
|
||||
warnID(6036);
|
||||
}
|
||||
this._trailNum = Math.ceil(psTime * Math.ceil(this.lifeTime.getMax()) * 60 * (psRate * duration + burstCount));
|
||||
let pCount: number = psRate * duration; // potential particle count
|
||||
pCount = ps.prewarm ? pCount * 2 : pCount; // if prewarm we need double space
|
||||
pCount = pCount > ps.capacity ? ps.capacity : pCount; // max particle count is less/equal than capacity
|
||||
this._trailNum = Math.ceil(psTime * Math.ceil(this.lifeTime.getMax()) * 60 * (pCount + burstCount));
|
||||
this._trailSegments = new Pool(
|
||||
(): TrailSegment => new TrailSegment(10),
|
||||
Math.ceil(psRate * duration),
|
||||
|
||||
@@ -141,7 +141,7 @@ export class b2PhysicsWorld implements IPhysicsWorld {
|
||||
|
||||
if (!v) {
|
||||
if (this._debugGraphics) {
|
||||
this._debugGraphics.node.parent = null;
|
||||
this._debugGraphics.clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -34,8 +34,8 @@ export class b2CircleShape extends b2Shape2D implements ICircleShape {
|
||||
|
||||
_worldPosition = new Vec2();
|
||||
get worldPosition (): Vec2 {
|
||||
const p = (this._shapes[0] as b2jsb.CircleShape).m_p;
|
||||
return this._worldPosition.set(p.x * PHYSICS_2D_PTM_RATIO, p.y * PHYSICS_2D_PTM_RATIO);
|
||||
const comp = this.collider as CircleCollider2D;
|
||||
return Vec2.transformMat4(this._worldPosition, comp.offset, comp.node.worldMatrix);
|
||||
}
|
||||
|
||||
_createShapes (scaleX: number, scaleY: number, relativePositionX: number, relativePositionY: number): any[] {
|
||||
|
||||
@@ -26,7 +26,7 @@ import { instantiateWasm, ensureWasmModuleReady } from 'pal/wasm';
|
||||
import { BUILD, LOAD_BOX2D_MANUALLY, NATIVE_CODE_BUNDLE_MODE } from 'internal:constants';
|
||||
|
||||
import { game } from '../../game';
|
||||
import { error, sys, IVec2Like, log } from '../../core';
|
||||
import { error, sys, IVec2Like, log, debug } from '../../core';
|
||||
import { NativeCodeBundleMode } from '../../misc/webassembly-support';
|
||||
|
||||
// eslint-disable-next-line import/no-mutable-exports
|
||||
@@ -148,7 +148,7 @@ function initWasm (wasmFactory, wasmUrl: string): Promise<void> {
|
||||
}).catch((err) => reject(errorMessage(err)));
|
||||
},
|
||||
}).then((Instance: any) => {
|
||||
log('[box2d]:box2d wasm lib loaded.');
|
||||
debug('[box2d]:box2d wasm lib loaded.');
|
||||
B2 = Instance;
|
||||
}).then(resolve).catch((err: any) => reject(errorMessage(err)));
|
||||
});
|
||||
@@ -158,7 +158,7 @@ function initAsm (asmFactory): Promise<void> {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const errorMessage = (err: any): string => `[box2d]: box2d asm lib load failed: ${err}`;
|
||||
asmFactory().then((instance: any) => {
|
||||
log('[box2d]:box2d asm lib loaded.');
|
||||
debug('[box2d]:box2d asm lib loaded.');
|
||||
B2 = instance;
|
||||
}).then(resolve).catch((err: any) => reject(errorMessage(err)));
|
||||
});
|
||||
|
||||
@@ -114,7 +114,7 @@ export class B2PhysicsWorld implements IPhysicsWorld {
|
||||
|
||||
if (!v) {
|
||||
if (this._debugGraphics) {
|
||||
this._debugGraphics.node.parent = null;
|
||||
this._debugGraphics.clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -37,8 +37,8 @@ export class B2CircleShape extends B2Shape2D implements ICircleShape {
|
||||
|
||||
_worldPosition = new Vec2();
|
||||
get worldPosition (): Vec2 {
|
||||
const p = B2.CircleShapeGetPosition(this._shapes[0]) as B2.Vec2;
|
||||
return this._worldPosition.set(p.x * PHYSICS_2D_PTM_RATIO, p.y * PHYSICS_2D_PTM_RATIO);
|
||||
const comp = this.collider as CircleCollider2D;
|
||||
return Vec2.transformMat4(this._worldPosition, comp.offset, comp.node.worldMatrix);
|
||||
}
|
||||
|
||||
_createShapes (scaleX: number, scaleY: number, relativePositionX: number, relativePositionY: number): number[] { //B2.CircleShape[]
|
||||
|
||||
@@ -100,7 +100,7 @@ export class b2PhysicsWorld implements IPhysicsWorld {
|
||||
|
||||
if (!v) {
|
||||
if (this._debugGraphics) {
|
||||
this._debugGraphics.node.parent = null;
|
||||
this._debugGraphics.clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -37,8 +37,8 @@ export class b2CircleShape extends b2Shape2D implements ICircleShape {
|
||||
|
||||
_worldPosition = new Vec2();
|
||||
get worldPosition (): Vec2 {
|
||||
const p = (this._shapes[0] as b2.CircleShape).m_p;
|
||||
return this._worldPosition.set(p.x * PHYSICS_2D_PTM_RATIO, p.y * PHYSICS_2D_PTM_RATIO);
|
||||
const comp = this.collider as CircleCollider2D;
|
||||
return Vec2.transformMat4(this._worldPosition, comp.offset, comp.node.worldMatrix);
|
||||
}
|
||||
|
||||
_createShapes (scaleX: number, scaleY: number, relativePositionX: number, relativePositionY: number): any[] {
|
||||
|
||||
@@ -27,7 +27,7 @@ import { EDITOR, DEBUG, TEST, EDITOR_NOT_IN_PREVIEW } from 'internal:constants';
|
||||
import { IRigidBody2D } from '../spec/i-rigid-body';
|
||||
import { IBoxShape, ICircleShape, IPolygonShape, IBaseShape } from '../spec/i-physics-shape';
|
||||
import { IPhysicsWorld } from '../spec/i-physics-world';
|
||||
import { errorID, log } from '../../core';
|
||||
import { debug, errorID, log } from '../../core';
|
||||
import { ECollider2DType, EJoint2DType } from './physics-types';
|
||||
import { IJoint2D, IDistanceJoint, ISpringJoint, IFixedJoint, IMouseJoint,
|
||||
IRelativeJoint, ISliderJoint, IWheelJoint, IHingeJoint } from '../spec/i-physics-joint';
|
||||
@@ -110,7 +110,7 @@ export interface IPhysicsSelector {
|
||||
}
|
||||
|
||||
function register (id: IPhysicsEngineId, wrapper: IPhysicsWrapperObject): void {
|
||||
if (!EDITOR && !TEST) log(`[PHYSICS2D]: register ${id}.`);
|
||||
if (!EDITOR && !TEST) debug(`[PHYSICS2D]: register ${id}.`);
|
||||
selector.backend[id] = wrapper;
|
||||
if (!selector.physicsWorld || selector.id === id) {
|
||||
const mutableSelector = selector as Mutable<IPhysicsSelector>;
|
||||
@@ -124,12 +124,12 @@ function switchTo (id: IPhysicsEngineId): void {
|
||||
const mutableSelector = selector as Mutable<IPhysicsSelector>;
|
||||
if (selector.physicsWorld && id !== selector.id && selector.backend[id] != null) {
|
||||
//selector.physicsWorld.destroy();//todo
|
||||
if (!TEST) log(`[PHYSICS2D]: switch from ${selector.id} to ${id}.`);
|
||||
if (!TEST) debug(`[PHYSICS2D]: switch from ${selector.id} to ${id}.`);
|
||||
mutableSelector.id = id;
|
||||
mutableSelector.wrapper = selector.backend[id];
|
||||
mutableSelector.physicsWorld = createPhysicsWorld();
|
||||
} else {
|
||||
if (!EDITOR && !TEST) log(`[PHYSICS2D]: using ${mutableSelector.id}.`);
|
||||
if (!EDITOR && !TEST) debug(`[PHYSICS2D]: using ${mutableSelector.id}.`);
|
||||
mutableSelector.physicsWorld = createPhysicsWorld();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
import { ensureWasmModuleReady, instantiateWasm } from 'pal/wasm';
|
||||
import { BUILD, LOAD_BULLET_MANUALLY, NATIVE_CODE_BUNDLE_MODE } from 'internal:constants';
|
||||
import { game } from '../../game';
|
||||
import { error, log, sys } from '../../core';
|
||||
import { debug, error, sys } from '../../core';
|
||||
import { NativeCodeBundleMode } from '../../misc/webassembly-support';
|
||||
import type { BulletCache } from './bullet-cache';
|
||||
|
||||
@@ -106,7 +106,7 @@ function initWASM (wasmFactory, wasmUrl: string): Promise<void> {
|
||||
}).catch((err) => reject(errorMessage(err)));
|
||||
},
|
||||
}).then((instance: any) => {
|
||||
log('[bullet]:bullet wasm lib loaded.');
|
||||
debug('[bullet]:bullet wasm lib loaded.');
|
||||
bt = instance as Bullet.instance;
|
||||
globalThis.Bullet = bt as any;
|
||||
}).then(resolve).catch((err: any) => reject(errorMessage(err)));
|
||||
@@ -117,7 +117,7 @@ function initASM (asmFactory): Promise<void> {
|
||||
if (asmFactory != null) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
||||
return asmFactory().then((instance: any) => {
|
||||
log('[bullet]:bullet asm lib loaded.');
|
||||
debug('[bullet]:bullet asm lib loaded.');
|
||||
bt = instance as Bullet.instance;
|
||||
});
|
||||
} else {
|
||||
|
||||
@@ -35,7 +35,7 @@ import {
|
||||
import { IPhysicsWorld } from '../spec/i-physics-world';
|
||||
import { IRigidBody } from '../spec/i-rigid-body';
|
||||
import { IBoxCharacterController, ICapsuleCharacterController } from '../spec/i-character-controller';
|
||||
import { errorID, IVec3Like, warn, cclegacy, log } from '../../core';
|
||||
import { errorID, IVec3Like, warn, cclegacy, debug } from '../../core';
|
||||
import { EColliderType, EConstraintType, ECharacterControllerType } from './physics-enum';
|
||||
import { PhysicsMaterial } from '.';
|
||||
|
||||
@@ -125,7 +125,7 @@ function updateLegacyMacro (id: string): void {
|
||||
}
|
||||
|
||||
function register (id: IPhysicsEngineId, wrapper: IPhysicsWrapperObject): void {
|
||||
if (!EDITOR && !TEST) log(`[PHYSICS]: register ${id}.`);
|
||||
if (!EDITOR && !TEST) debug(`[PHYSICS]: register ${id}.`);
|
||||
selector.backend[id] = wrapper;
|
||||
if (!selector.physicsWorld || selector.id === id) {
|
||||
updateLegacyMacro(id);
|
||||
@@ -147,13 +147,13 @@ function switchTo (id: IPhysicsEngineId): void {
|
||||
const mutableSelector = selector as Mutable<IPhysicsSelector>;
|
||||
if (selector.physicsWorld && id !== selector.id && selector.backend[id] != null) {
|
||||
selector.physicsWorld.destroy();
|
||||
if (!TEST) log(`[PHYSICS]: switch from ${selector.id} to ${id}.`);
|
||||
if (!TEST) debug(`[PHYSICS]: switch from ${selector.id} to ${id}.`);
|
||||
updateLegacyMacro(id);
|
||||
mutableSelector.id = id;
|
||||
mutableSelector.wrapper = selector.backend[id];
|
||||
mutableSelector.physicsWorld = createPhysicsWorld();
|
||||
} else {
|
||||
if (!EDITOR && !TEST) log(`[PHYSICS]: using ${id}.`);
|
||||
if (!EDITOR && !TEST) debug(`[PHYSICS]: using ${id}.`);
|
||||
mutableSelector.physicsWorld = createPhysicsWorld();
|
||||
}
|
||||
if (worldInitData) {
|
||||
@@ -185,7 +185,7 @@ export function constructDefaultWorld (data: IWorldInitData): void {
|
||||
if (!worldInitData) worldInitData = data;
|
||||
if (!selector.runInEditor) return;
|
||||
if (!selector.physicsWorld) {
|
||||
if (!TEST) log(`[PHYSICS]: using ${selector.id}.`);
|
||||
if (!TEST) debug(`[PHYSICS]: using ${selector.id}.`);
|
||||
const mutableSelector = selector as Mutable<IPhysicsSelector>;
|
||||
const world = mutableSelector.physicsWorld = createPhysicsWorld();
|
||||
world.setGravity(worldInitData.gravity);
|
||||
|
||||
@@ -21,12 +21,14 @@
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
import { DEBUG } from 'internal:constants';
|
||||
import { screenAdapter } from 'pal/screen-adapter';
|
||||
import { Orientation } from '../../../pal/screen-adapter/enum-type';
|
||||
import {
|
||||
TextureType, TextureUsageBit, Format, RenderPass, Texture, Framebuffer,
|
||||
RenderPassInfo, Device, TextureInfo, FramebufferInfo, Swapchain, SurfaceTransform, TextureFlagBit, TextureFlags,
|
||||
} from '../../gfx';
|
||||
import { warn } from '../../core/platform/debug';
|
||||
import { Root } from '../../root';
|
||||
import { Camera } from '../scene';
|
||||
|
||||
@@ -41,7 +43,10 @@ export interface IRenderWindowInfo {
|
||||
externalFlag?: TextureFlags; // external texture type normal or oes
|
||||
}
|
||||
|
||||
const orientationMap: Record<Orientation, SurfaceTransform> = {
|
||||
// NOTE: `Orientation` is a flag enum whose composite members (LANDSCAPE, AUTO) are never
|
||||
// actual runtime orientations, so this map intentionally only covers the physical ones.
|
||||
// It is therefore typed as Partial rather than a total Record<Orientation, ...>.
|
||||
const orientationMap: Partial<Record<Orientation, SurfaceTransform>> = {
|
||||
[Orientation.PORTRAIT]: SurfaceTransform.IDENTITY,
|
||||
[Orientation.LANDSCAPE_RIGHT]: SurfaceTransform.ROTATE_90,
|
||||
[Orientation.PORTRAIT_UPSIDE_DOWN]: SurfaceTransform.ROTATE_180,
|
||||
@@ -253,7 +258,11 @@ export class RenderWindow {
|
||||
*/
|
||||
public resize (width: number, height: number): void {
|
||||
if (this._swapchain) {
|
||||
this._swapchain.resize(width, height, orientationMap[screenAdapter.orientation]);
|
||||
const surfaceTransform = orientationMap[screenAdapter.orientation];
|
||||
if (DEBUG && surfaceTransform === undefined) {
|
||||
warn(`Unexpected screen orientation: ${screenAdapter.orientation}`);
|
||||
}
|
||||
this._swapchain.resize(width, height, surfaceTransform || SurfaceTransform.IDENTITY);
|
||||
this._width = this._swapchain.width;
|
||||
this._height = this._swapchain.height;
|
||||
} else {
|
||||
|
||||
@@ -615,10 +615,17 @@ class DeviceRenderQueue implements RecordingInterface {
|
||||
this._devicePass = devicePass;
|
||||
this._phaseID = cclegacy.rendering.getPhaseID(devicePass.passID, context.renderGraph.getLayout(id));
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Creates a blit descriptor for the given blit operation.
|
||||
* @zh 为给定的 blit 操作创建一个 blit 描述符。
|
||||
* @param blit @en The blit operation to create a descriptor for. @zh 要为其创建描述符的 blit 操作。
|
||||
*/
|
||||
createBlitDesc (blit: Blit): void {
|
||||
if (!this._blitDesc) {
|
||||
this._blitDesc = new BlitDesc(blit);
|
||||
}
|
||||
this._blitDesc.blit = blit;
|
||||
this._blitDesc.createScreenQuad();
|
||||
this._blitDesc.createStageDescriptor();
|
||||
}
|
||||
|
||||
@@ -817,11 +817,20 @@ export class LightResource {
|
||||
this.lightIndex.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Adds a light to the light buffer and returns its byte offset.
|
||||
* @zh 将光源添加到光源缓冲区,并返回其字节偏移量。
|
||||
* @param light @en The light to add. @zh 要添加的光源。
|
||||
* @param bHDR @en Whether HDR rendering is enabled. @zh 是否启用 HDR 渲染。
|
||||
* @param exposure @en The camera exposure value. @zh 相机曝光值。
|
||||
* @param shadowInfo @en The shadow settings, or null if shadows are disabled. @zh 阴影设置,若禁用阴影则为 null。
|
||||
* @returns @en The byte offset of the light in the light buffer. @zh 光源在光源缓冲区中的字节偏移量。
|
||||
*/
|
||||
addLight (light: Light, bHDR: boolean, exposure: number, shadowInfo: Shadows | null): number {
|
||||
// Already added
|
||||
const existingLightID = this.lightIndex.get(light);
|
||||
if (existingLightID !== undefined) {
|
||||
return existingLightID;
|
||||
return existingLightID * this.elementSize;
|
||||
}
|
||||
|
||||
if (!this.lightBuffer) {
|
||||
|
||||
@@ -126,7 +126,7 @@ export class RenderAdditiveLightQueue {
|
||||
private declare _lightBuffer: Buffer;
|
||||
private declare _firstLightBufferView: Buffer;
|
||||
private declare _lightBufferData: Float32Array;
|
||||
private _instancedQueues: RenderInstancedQueue[] = [];
|
||||
private _instancedQueues: (RenderInstancedQueue | undefined)[] = [];
|
||||
private _lightMeterScale = 10000.0;
|
||||
|
||||
constructor (pipeline: PipelineRuntime) {
|
||||
@@ -151,7 +151,9 @@ export class RenderAdditiveLightQueue {
|
||||
|
||||
public clear (): void {
|
||||
this._instancedQueues.forEach((instancedQueue) => {
|
||||
instancedQueue.clear();
|
||||
if (instancedQueue) {
|
||||
instancedQueue.clear();
|
||||
}
|
||||
});
|
||||
this._instancedQueues.length = 0;
|
||||
|
||||
@@ -196,7 +198,7 @@ export class RenderAdditiveLightQueue {
|
||||
|
||||
this._lightCulling(model, validPunctualLights);
|
||||
|
||||
if (!_lightIndices.length && validPunctualLights.length > 0) { continue; }
|
||||
if (!_lightIndices.length) { continue; }
|
||||
|
||||
for (let j = 0; j < subModels.length; j++) {
|
||||
const lightPassIdx = _lightPassIndices[j];
|
||||
@@ -219,15 +221,14 @@ export class RenderAdditiveLightQueue {
|
||||
public gatherLightPasses (camera: Camera, cmdBuff: CommandBuffer, passLayout = 'default'): void {
|
||||
this.clear();
|
||||
|
||||
const validPunctualLights = this._pipeline.pipelineSceneData.validPunctualLights;
|
||||
if (!validPunctualLights.length) {
|
||||
this._bindForwardAddLight(validPunctualLights, passLayout);
|
||||
return;
|
||||
}
|
||||
|
||||
this._updateUBOs(camera, cmdBuff);
|
||||
this._updateLightDescriptorSet(camera, cmdBuff);
|
||||
|
||||
const validPunctualLights = this._pipeline.pipelineSceneData.validPunctualLights;
|
||||
if (!validPunctualLights.length) { return; }
|
||||
|
||||
this._bindForwardAddLight(validPunctualLights, passLayout);
|
||||
|
||||
// only for instanced and batched, no light culling applied
|
||||
for (let l = 0; l < validPunctualLights.length; l++) {
|
||||
const light = validPunctualLights[l];
|
||||
@@ -236,17 +237,21 @@ export class RenderAdditiveLightQueue {
|
||||
}
|
||||
|
||||
this._instancedQueues.forEach((instancedQueue) => {
|
||||
instancedQueue.uploadBuffers(cmdBuff);
|
||||
if (instancedQueue) {
|
||||
instancedQueue.uploadBuffers(cmdBuff);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public recordCommandBuffer (device: Device, renderPass: RenderPass, cmdBuff: CommandBuffer): void {
|
||||
const globalDSManager: GlobalDSManager = this._pipeline.globalDSManager;
|
||||
for (let j = 0; j < this._instancedQueues.length; ++j) {
|
||||
const queue = this._instancedQueues[j];
|
||||
if (!queue) { continue; }
|
||||
const light = this._instancedLightPassPool.lights[j];
|
||||
_dynamicOffsets[0] = this._instancedLightPassPool.dynamicOffsets[j];
|
||||
const descriptorSet = globalDSManager.getOrCreateDescriptorSet(light);
|
||||
this._instancedQueues[j].recordCommandBuffer(device, renderPass, cmdBuff, descriptorSet, _dynamicOffsets);
|
||||
queue.recordCommandBuffer(device, renderPass, cmdBuff, descriptorSet, _dynamicOffsets);
|
||||
}
|
||||
|
||||
for (let i = 0; i < this._lightPasses.length; i++) {
|
||||
@@ -318,11 +323,14 @@ export class RenderAdditiveLightQueue {
|
||||
if (((visibility & model.node.layer) === model.node.layer)) {
|
||||
switch (batchingScheme) {
|
||||
case BatchingSchemes.INSTANCING: {
|
||||
const buffer = pass.getInstancedBuffer(l);
|
||||
const buffer = pass.getInstancedBuffer(lightIdx);
|
||||
buffer.merge(subModel, lightPassIdx);
|
||||
buffer.dynamicOffsets[0] = this._lightBufferStride;
|
||||
if (!this._instancedQueues[l]) { this._instancedQueues[l] = new RenderInstancedQueue(); }
|
||||
this._instancedQueues[l].queue.add(buffer);
|
||||
buffer.dynamicOffsets[0] = this._lightBufferStride * lightIdx;
|
||||
if (this._instancedQueues.length <= lightIdx || !this._instancedQueues[lightIdx]) {
|
||||
this._instancedQueues.length = lightIdx + 1;
|
||||
this._instancedQueues[lightIdx] = new RenderInstancedQueue();
|
||||
}
|
||||
this._instancedQueues[lightIdx]!.queue.add(buffer);
|
||||
} break;
|
||||
default:
|
||||
lp!.lights.push(light);
|
||||
@@ -370,9 +378,6 @@ export class RenderAdditiveLightQueue {
|
||||
this._shadowUBO[UBOShadowEnum.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 1] = packing;
|
||||
this._shadowUBO[UBOShadowEnum.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 2] = 0.0;
|
||||
this._shadowUBO[UBOShadowEnum.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 3] = 0.0;
|
||||
|
||||
// Reserve sphere light shadow interface
|
||||
Color.toArray(this._shadowUBO, shadowInfo.shadowColor, UBOShadowEnum.SHADOW_COLOR_OFFSET);
|
||||
break;
|
||||
}
|
||||
case LightType.SPOT: {
|
||||
@@ -437,8 +442,6 @@ export class RenderAdditiveLightQueue {
|
||||
this._shadowUBO[UBOShadowEnum.SHADOW_PROJ_INFO_OFFSET + 2] = 1.0 / matShadowProj.m00;
|
||||
this._shadowUBO[UBOShadowEnum.SHADOW_PROJ_INFO_OFFSET + 3] = 1.0 / matShadowProj.m05;
|
||||
|
||||
Color.toArray(this._shadowUBO, shadowInfo.shadowColor, UBOShadowEnum.SHADOW_COLOR_OFFSET);
|
||||
|
||||
// Spot light sampler binding
|
||||
if (shadowFrameBufferMap.has(light)) {
|
||||
const texture = shadowFrameBufferMap.get(light)?.colorTextures[0];
|
||||
@@ -463,15 +466,15 @@ export class RenderAdditiveLightQueue {
|
||||
this._shadowUBO[UBOShadowEnum.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 1] = packing;
|
||||
this._shadowUBO[UBOShadowEnum.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 2] = 0.0;
|
||||
this._shadowUBO[UBOShadowEnum.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 3] = 0.0;
|
||||
|
||||
// Reserve point light shadow interface
|
||||
Color.toArray(this._shadowUBO, shadowInfo.shadowColor, UBOShadowEnum.SHADOW_COLOR_OFFSET);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
}
|
||||
globalDSManager.update();
|
||||
cmdBuff.updateBuffer(descriptorSet.getBuffer(UBOShadow.BINDING)!, this._shadowUBO);
|
||||
Color.toArray(this._shadowUBO, shadowInfo.shadowColor, UBOShadowEnum.SHADOW_COLOR_OFFSET);
|
||||
|
||||
descriptorSet.update();
|
||||
|
||||
cmdBuff.updateBuffer(descriptorSet.getBuffer(UBOShadow.BINDING)!, this._shadowUBO.buffer, this._shadowUBO.byteLength);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -627,6 +630,6 @@ export class RenderAdditiveLightQueue {
|
||||
}
|
||||
}
|
||||
|
||||
cmdBuff.updateBuffer(this._lightBuffer, this._lightBufferData);
|
||||
cmdBuff.updateBuffer(this._lightBuffer, this._lightBufferData.buffer as ArrayBuffer, this._lightBufferData.byteLength);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
import { cclegacy } from './core/global-exports';
|
||||
import { DataPoolManager } from './3d/skeletal-animation/data-pool-manager';
|
||||
import { Device, deviceManager } from './gfx';
|
||||
import { settings, Settings, warnID, Pool, macro, log } from './core';
|
||||
import { settings, Settings, warnID, Pool, macro, log, debug } from './core';
|
||||
import { PipelineEventProcessor } from './rendering/pipeline-event';
|
||||
import type { Root as JsbRoot } from './root';
|
||||
|
||||
@@ -242,7 +242,7 @@ rootProto.setRenderPipeline = function (customPipeline: boolean) {
|
||||
if (customPipeline) {
|
||||
cclegacy.rendering.createCustomPipeline();
|
||||
ppl = oldSetPipeline.call(this, null);
|
||||
log(`Using custom pipeline: ${macro.CUSTOM_PIPELINE_NAME}`);
|
||||
debug(`Using custom pipeline: ${macro.CUSTOM_PIPELINE_NAME}`);
|
||||
} else {
|
||||
// pipeline should not be created in C++, ._ctor need to be triggered
|
||||
if (cclegacy.legacy_rendering) {
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
*/
|
||||
|
||||
import { USE_XR } from 'internal:constants';
|
||||
import { Pool, cclegacy, warnID, settings, macro, log, errorID, SettingsCategory } from './core';
|
||||
import { Pool, cclegacy, warnID, settings, macro, log, errorID, SettingsCategory, debug } from './core';
|
||||
import { DebugView } from './rendering/debug-view';
|
||||
import { Camera, CameraType, Light, Model, TrackingType } from './render-scene/scene';
|
||||
import type { DataPoolManager } from './3d/skeletal-animation/data-pool-manager';
|
||||
@@ -380,11 +380,11 @@ export class Root {
|
||||
isCreateDefaultPipeline = true;
|
||||
this._pipeline = this._customPipeline!;
|
||||
// Use default _pipelineEvent
|
||||
log(`Using custom pipeline: ${macro.CUSTOM_PIPELINE_NAME}`);
|
||||
debug(`Using custom pipeline: ${macro.CUSTOM_PIPELINE_NAME}`);
|
||||
} else {
|
||||
const rppl: (PipelineRuntime & IPipelineEvent) = legacy_rendering.createDefaultPipeline();
|
||||
isCreateDefaultPipeline = true;
|
||||
log(`Using legacy pipeline`);
|
||||
debug(`Using legacy pipeline`);
|
||||
|
||||
this._classicPipeline = rppl!;
|
||||
this._pipeline = this._classicPipeline;
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
Copyright (c) 2025 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
// Runtime-selectable spine instantiation. Used only when both spine-3.8 and spine-4.2 are compiled into
|
||||
// the engine (editor/preview): cc.config.json's moduleOverrides then replaces spine-instantiate.ts with
|
||||
// this file. Unlike the fixed-version files (spine-instantiate-3.8.ts / -4.2.ts), both the 3.8 and 4.2
|
||||
// external references are kept here (so both wasm/asm binaries are bundled), and the matching one is
|
||||
// executed at runtime according to the selected version.
|
||||
// The version comes from the preview boot (game-boot.js / engine-bootstrap.ts), which reads it from
|
||||
// cocos.config.json before engine init and writes globalThis._CC_SPINE_VERSION.
|
||||
|
||||
import { ensureWasmModuleReady } from 'pal/wasm';
|
||||
import { error } from '../../core';
|
||||
import { shouldUseWasmModule, initWasm, initAsmJS } from './spine-wasm-utils';
|
||||
// Import setSpineVersion through the overridden './spine-version' (= spine-version-dynamic) so it is the
|
||||
// same module instance whose SPINE_VERSION live binding is read by the shared spine-define.ts.
|
||||
import { setSpineVersion } from './spine-version';
|
||||
|
||||
function getSelectedSpineVersion (): string {
|
||||
const v = (globalThis as any)._CC_SPINE_VERSION;
|
||||
return v === '4.2' ? '4.2' : '3.8';
|
||||
}
|
||||
|
||||
export function waitForSpineWasmInstantiation (): Promise<void> {
|
||||
const errorReport = (msg: any): void => { error(msg); };
|
||||
// The version must be set before wasm instantiation and before spine-define patches prototypes.
|
||||
const version = getSelectedSpineVersion();
|
||||
setSpineVersion(version);
|
||||
return ensureWasmModuleReady().then((): Promise<void> => {
|
||||
//We should use static code here, import operation will cause file copy to cache folder.
|
||||
if (version === '4.2') {
|
||||
if (shouldUseWasmModule()) {
|
||||
return Promise.all([
|
||||
import('external:emscripten/spine/4.2/spine.wasm.js'),
|
||||
import('external:emscripten/spine/4.2/spine.wasm'),
|
||||
]).then(([
|
||||
{ default: wasmFactory },
|
||||
{ default: spineWasmUrl },
|
||||
]) => initWasm(wasmFactory, spineWasmUrl));
|
||||
} else {
|
||||
return Promise.all([
|
||||
import('external:emscripten/spine/4.2/spine.asm.js'),
|
||||
import('external:emscripten/spine/4.2/spine.js.mem'),
|
||||
]).then(([
|
||||
{ default: asmFactory },
|
||||
{ default: asmJsMemUrl },
|
||||
]) => initAsmJS(asmFactory, asmJsMemUrl));
|
||||
}
|
||||
} else {
|
||||
if (shouldUseWasmModule()) {
|
||||
return Promise.all([
|
||||
import('external:emscripten/spine/3.8/spine.wasm.js'),
|
||||
import('external:emscripten/spine/3.8/spine.wasm'),
|
||||
]).then(([
|
||||
{ default: wasmFactory },
|
||||
{ default: spineWasmUrl },
|
||||
]) => initWasm(wasmFactory, spineWasmUrl));
|
||||
} else {
|
||||
return Promise.all([
|
||||
import('external:emscripten/spine/3.8/spine.asm.js'),
|
||||
import('external:emscripten/spine/3.8/spine.js.mem'),
|
||||
]).then(([
|
||||
{ default: asmFactory },
|
||||
{ default: asmJsMemUrl },
|
||||
]) => initAsmJS(asmFactory, asmJsMemUrl));
|
||||
}
|
||||
}
|
||||
}).catch(errorReport);
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
Copyright (c) 2025 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
// Runtime-selectable spine version. Used only when both spine-3.8 and spine-4.2 are compiled into the
|
||||
// engine (editor/preview): cc.config.json's moduleOverrides then replaces spine-version.ts with this file.
|
||||
// Unlike the fixed-version files (spine-version-3.8.ts / -4.2.ts), SPINE_VERSION is a mutable `let`
|
||||
// (a live ES binding), so the shared layer (spine-define.ts / skeleton.ts) reads the runtime value and
|
||||
// keeps both `SPINE_VERSION === '3.8' / '4.2'` branches instead of tree-shaking to a single one.
|
||||
// The version is set by spine-instantiate-dynamic.ts before instantiation, sourced from the host-provided
|
||||
// globalThis._CC_SPINE_VERSION (injected by the preview boot from cocos.config.json).
|
||||
import { DataInput, SkeletonBinary } from './spine-binary';
|
||||
import { BUILD } from 'internal:constants';
|
||||
|
||||
// Typed as string so comparisons in the shared layer against the other version literal are not flagged by TS.
|
||||
export let SPINE_VERSION: string = '3.8';
|
||||
|
||||
/**
|
||||
* Sets the current spine version. Must be called before spine wasm instantiation and before spine-define runs.
|
||||
*/
|
||||
export function setSpineVersion (version: string): void {
|
||||
SPINE_VERSION = version === '4.2' ? '4.2' : '3.8';
|
||||
}
|
||||
|
||||
function isVersionCompatible (version: string | null): boolean {
|
||||
if (!BUILD) {
|
||||
if (SPINE_VERSION === '3.8') {
|
||||
if (!version || version === '3.8.75' || !version.startsWith('3.8.')) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (!version || !version.startsWith('4.2.')) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal Used only by editor.
|
||||
*/
|
||||
export function isBinaryCompatible (buffer: Uint8Array): boolean {
|
||||
if (!BUILD) {
|
||||
const input = new DataInput(buffer);
|
||||
// The binary hash header differs between versions: 3.8 uses one string, 4.2 uses two ints.
|
||||
if (SPINE_VERSION === '3.8') {
|
||||
SkeletonBinary.readString(input); // read hash
|
||||
} else {
|
||||
SkeletonBinary.readInt(input); // read hash
|
||||
SkeletonBinary.readInt(input); // read hash
|
||||
}
|
||||
const version = SkeletonBinary.readString(input);
|
||||
|
||||
return isVersionCompatible(version);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal Used only by editor.
|
||||
*/
|
||||
export function isJsonCompatible (json: JSON): boolean {
|
||||
if (!BUILD) {
|
||||
return isVersionCompatible(json['skeleton']['spine']);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -24,6 +24,12 @@
|
||||
|
||||
export const SPINE_VERSION = '3.8';
|
||||
|
||||
// Placeholder so spine-instantiate-dynamic.ts can import setSpineVersion from './spine-version' and type-check.
|
||||
// When building the engine with runtime-selectable spine, moduleOverrides replaces this file with
|
||||
// spine-version-dynamic.ts (the real implementation). In fixed single-version builds (spine-3.8 / spine-4.2)
|
||||
// spine-instantiate-dynamic is not used, so this no-op is never called.
|
||||
export function setSpineVersion (version: string): void {}
|
||||
|
||||
export function isBinaryCompatible (buffer: Uint8Array): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -878,7 +878,7 @@ export class TiledLayer extends UIRenderer {
|
||||
const axis = this._staggerAxis;
|
||||
|
||||
switch (this._layerOrientation) {
|
||||
// left top to right dowm
|
||||
// left top to right down
|
||||
case Orientation.ORTHO:
|
||||
col = Math.floor(x / maptw);
|
||||
row = Math.floor(y / mapth);
|
||||
@@ -889,7 +889,7 @@ export class TiledLayer extends UIRenderer {
|
||||
col = Math.floor(x / maptw2);
|
||||
row = Math.floor(y / mapth2);
|
||||
break;
|
||||
// left top to right dowm
|
||||
// left top to right down
|
||||
case Orientation.HEX:
|
||||
if (axis === StaggerAxis.STAGGERAXIS_Y) {
|
||||
row = Math.floor(y / (mapth - this._diffY1!));
|
||||
@@ -1017,7 +1017,7 @@ export class TiledLayer extends UIRenderer {
|
||||
}
|
||||
|
||||
switch (layerOrientation) {
|
||||
// left top to right dowm
|
||||
// left top to right down
|
||||
case Orientation.ORTHO:
|
||||
cullingCol = col;
|
||||
cullingRow = rows - row - 1;
|
||||
@@ -1039,7 +1039,7 @@ export class TiledLayer extends UIRenderer {
|
||||
left = maptw2 * cullingCol;
|
||||
bottom = mapth2 * cullingRow;
|
||||
break;
|
||||
// left top to right dowm
|
||||
// left top to right down
|
||||
case Orientation.HEX:
|
||||
diffX2 = (axis! === StaggerAxis.STAGGERAXIS_Y && row % 2 === 1) ? maptw2 * odd_even! : 0;
|
||||
diffY2 = (axis! === StaggerAxis.STAGGERAXIS_X && col % 2 === 1) ? mapth2 * -odd_even! : 0;
|
||||
|
||||
@@ -10,7 +10,7 @@ vec3 HDRToLDR(vec3 color)
|
||||
#endif
|
||||
{
|
||||
// linear exposure has already applied to light intensity
|
||||
#if CC_TONE_MAPPING_TYPE == HDR_TONE_MAPPING_ACES
|
||||
#if CC_TONE_MAPPING_TYPE == TONE_MAPPING_ACES
|
||||
color.rgb = ACESToneMap(color.rgb);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -79,8 +79,12 @@ vec4 saturate(vec4 value) { return clamp(value, vec4(0.0), vec4(1.0)); }
|
||||
#define IS_RANGED_DIRECTIONAL_LIGHT(light_type) equalf_lowp(light_type, LIGHT_TYPE_RANGED_DIRECTIONAL)
|
||||
|
||||
// CC_TONE_MAPPING_TYPE Values
|
||||
#define TONE_MAPPING_ACES 0
|
||||
#define TONE_MAPPING_LINEAR 1
|
||||
#ifndef TONE_MAPPING_ACES
|
||||
#define TONE_MAPPING_ACES 0
|
||||
#endif
|
||||
#ifndef TONE_MAPPING_LINEAR
|
||||
#define TONE_MAPPING_LINEAR 1
|
||||
#endif
|
||||
|
||||
// Material
|
||||
#define SURFACES_MAX_TRANSMIT_DEPTH_VALUE 999999.0
|
||||
|
||||
@@ -7,7 +7,7 @@ vec4 CCFragOutput (vec4 color) {
|
||||
#if CC_USE_RGBE_OUTPUT
|
||||
color = packRGBE(color.rgb);
|
||||
#elif !CC_USE_FLOAT_OUTPUT
|
||||
#if CC_USE_HDR && CC_TONE_MAPPING_TYPE == HDR_TONE_MAPPING_ACES
|
||||
#if CC_USE_HDR && CC_TONE_MAPPING_TYPE == TONE_MAPPING_ACES
|
||||
color.rgb = ACESToneMap(color.rgb);
|
||||
#endif
|
||||
color.rgb = LinearToSRGB(color.rgb);
|
||||
|
||||
|
Before Width: | Height: | Size: 8.4 KiB After Width: | Height: | Size: 6.8 KiB |
|
Before Width: | Height: | Size: 8.8 KiB After Width: | Height: | Size: 945 B |
|
Before Width: | Height: | Size: 9.4 KiB After Width: | Height: | Size: 5.2 KiB |
|
Before Width: | Height: | Size: 9.2 KiB After Width: | Height: | Size: 7.2 KiB |
|
Before Width: | Height: | Size: 9.4 KiB After Width: | Height: | Size: 6.0 KiB |
|
Before Width: | Height: | Size: 9.2 KiB After Width: | Height: | Size: 4.5 KiB |
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "1.2.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "6ef37a33-5db9-4231-9855-f72b8428f6fa",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"ver": "1.0.4",
|
||||
"importer": "texture-cube",
|
||||
"imported": true,
|
||||
"uuid": "a4063d6c-c46e-482a-bf84-99eb9fbb2789",
|
||||
"files": [
|
||||
".json"
|
||||
],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"wrapModeS": "repeat",
|
||||
"wrapModeT": "repeat",
|
||||
"minfilter": "linear",
|
||||
"magfilter": "linear",
|
||||
"mipfilter": "none",
|
||||
"anisotropy": 0,
|
||||
"isRGBE": false
|
||||
}
|
||||
}
|
||||
@@ -70,7 +70,9 @@ CCProgram smaa-edge-fs %{
|
||||
vec3 ToLDR(vec3 color) {
|
||||
#if CC_USE_HDR
|
||||
color *= cc_exposure.x * FP_SCALE_INV;
|
||||
color = ACESToneMap(color);
|
||||
#if CC_TONE_MAPPING_TYPE == TONE_MAPPING_ACES
|
||||
color = ACESToneMap(color);
|
||||
#endif
|
||||
color = LinearToSRGB(color);
|
||||
#endif
|
||||
return color;
|
||||
|
||||
@@ -55,7 +55,9 @@ CCProgram tonemap-fs %{
|
||||
vec3 ToLDR(vec3 color) {
|
||||
#if CC_USE_HDR
|
||||
color *= cc_exposure.x * FP_SCALE_INV;
|
||||
color = ACESToneMap(color);
|
||||
#if CC_TONE_MAPPING_TYPE == TONE_MAPPING_ACES
|
||||
color = ACESToneMap(color);
|
||||
#endif
|
||||
color = LinearToSRGB(color);
|
||||
#endif
|
||||
return color;
|
||||
|
||||
@@ -244,6 +244,20 @@
|
||||
"faceCount": 0.1
|
||||
}
|
||||
]
|
||||
},
|
||||
"legacyFbxImporter": false,
|
||||
"allowMeshDataAccess": true,
|
||||
"addVertexColor": false,
|
||||
"generateLightmapUVNode": false,
|
||||
"meshOptimizer": {
|
||||
"enable": false,
|
||||
"algorithm": "simplify",
|
||||
"simplifyOptions": {
|
||||
"targetRatio": 1,
|
||||
"enableSmartLink": true,
|
||||
"agressiveness": 7,
|
||||
"maxIterationCount": 100
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -779,6 +779,7 @@ module.exports = link(mixin({
|
||||
},
|
||||
mask: {
|
||||
type: 'The mask type',
|
||||
radius: 'The radius for rounded corners of rect mask',
|
||||
spriteFrame: 'The mask image',
|
||||
inverted: 'The Reverse mask (Not supported Canvas Mode)',
|
||||
alphaThreshold:
|
||||
|
||||
@@ -760,6 +760,7 @@ module.exports = link(mixin({
|
||||
},
|
||||
mask: {
|
||||
type: '遮罩类型',
|
||||
radius: '矩形遮罩的圆角半径',
|
||||
spriteFrame: '遮罩所需要的贴图',
|
||||
inverted: '反向遮罩(不支持 Canvas 模式)',
|
||||
alphaThreshold: 'Alpha 阈值,只有当模板的像素的 Alpha 大于 Alpha Threshold 时,才会绘制内容(不支持 Canvas 模式)',
|
||||
|
||||
@@ -58,13 +58,13 @@ void UIModelProxy::activeSubModels() {
|
||||
auto* vertexBuffer = _device->createBuffer({
|
||||
gfx::BufferUsageBit::VERTEX | gfx::BufferUsageBit::TRANSFER_DST,
|
||||
gfx::MemoryUsageBit::DEVICE,
|
||||
65535 * _stride,
|
||||
65536 * _stride, // Uint16 can address 65536 vertices (indices 0–65535)
|
||||
_stride,
|
||||
});
|
||||
auto* indexBuffer = _device->createBuffer({
|
||||
gfx::BufferUsageBit::INDEX | gfx::BufferUsageBit::TRANSFER_DST,
|
||||
gfx::MemoryUsageBit::DEVICE,
|
||||
65535 * sizeof(uint16_t) * 2,
|
||||
65536 * sizeof(uint16_t) * 2, // Uint16 can address 65536 vertices (indices 0–65535)
|
||||
sizeof(uint16_t),
|
||||
});
|
||||
gfx::BufferList vbReference;
|
||||
|
||||
@@ -530,7 +530,7 @@ void Mesh::initialize() {
|
||||
if (dstStride == 4) {
|
||||
uint32_t vertexCount = _struct.vertexBundles[prim.vertexBundelIndices[0]].view.count;
|
||||
#if CC_OPTIMIZE_MESH_DATA
|
||||
if (vertexCount < 65536) {
|
||||
if (vertexCount <= 65536) { // Uint16 can address 65536 vertices (indices 0–65535)
|
||||
dstStride >>= 1; // Reduce to short.
|
||||
dstSize >>= 1;
|
||||
} else if (!gfxDevice->hasFeature(gfx::Feature::ELEMENT_INDEX_UINT)) {
|
||||
@@ -538,7 +538,7 @@ void Mesh::initialize() {
|
||||
}
|
||||
#else
|
||||
if (!gfxDevice->hasFeature(gfx::Feature::ELEMENT_INDEX_UINT)) {
|
||||
if (vertexCount >= 65536) {
|
||||
if (vertexCount > 65536) { // Uint16 can address 65536 vertices (indices 0–65535)
|
||||
CC_LOG_WARNING("Device does not support UINT element index type and vertexCount (%u) is larger than ushort", vertexCount);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -185,7 +185,11 @@ Mesh::ICreateInfo MeshUtils::createMeshInfo(const IGeometry &geometry, const ccs
|
||||
|
||||
if (geometry.customAttributes.has_value()) {
|
||||
for (const auto &ca : geometry.customAttributes.value()) {
|
||||
const auto &info = gfx::GFX_FORMAT_INFOS[static_cast<uint32_t>(attr->format)];
|
||||
if (ca.values.empty()) {
|
||||
CC_LOG_WARNING("Custom attribute %s has no values, skipped.", ca.attr.name.c_str());
|
||||
continue;
|
||||
}
|
||||
const auto &info = gfx::GFX_FORMAT_INFOS[static_cast<uint32_t>(ca.attr.format)];
|
||||
attributes.emplace_back(ca.attr);
|
||||
vertCount = std::max(vertCount, static_cast<uint32_t>(std::floor(ca.values.size() / info.count)));
|
||||
channels.emplace_back(Channel{stride, ca.values, ca.attr});
|
||||
|
||||
@@ -68,6 +68,9 @@ unsigned int AudioEngine::sMaxInstances = MAX_AUDIOINSTANCES;
|
||||
AudioEngine::ProfileHelper *AudioEngine::sDefaultProfileHelper = nullptr;
|
||||
ccstd::unordered_map<int, AudioEngine::AudioInfo> AudioEngine::sAudioIDInfoMap;
|
||||
AudioEngineImpl *AudioEngine::sAudioEngineImpl = nullptr;
|
||||
// Decoder-only fallback instance, defined here to keep AudioDecoderManager
|
||||
// initialization separate from OpenAL initialization.
|
||||
AudioEngineImpl *AudioEngine::sDecoderImpl = nullptr;
|
||||
|
||||
float AudioEngine::sVolumeFactor = 1.0F;
|
||||
events::EnterBackground::Listener AudioEngine::sOnPauseListenerID;
|
||||
@@ -156,6 +159,9 @@ void AudioEngine::end() {
|
||||
delete sAudioEngineImpl;
|
||||
sAudioEngineImpl = nullptr;
|
||||
|
||||
delete sDecoderImpl;
|
||||
sDecoderImpl = nullptr;
|
||||
|
||||
delete sDefaultProfileHelper;
|
||||
sDefaultProfileHelper = nullptr;
|
||||
|
||||
@@ -587,12 +593,48 @@ bool AudioEngine::isEnabled() {
|
||||
return sIsEnabled;
|
||||
}
|
||||
|
||||
PCMHeader AudioEngine::getPCMHeader(const char *url) {
|
||||
lazyInit();
|
||||
return sAudioEngineImpl->getPCMHeader(url);
|
||||
AudioEngineImpl *AudioEngine::getDecoderImpl() {
|
||||
if (AudioEngine::sAudioEngineImpl != nullptr) {
|
||||
return AudioEngine::sAudioEngineImpl;
|
||||
}
|
||||
// Try full init first; if it succeeds we get OpenAL + decoder.
|
||||
AudioEngine::lazyInit();
|
||||
if (AudioEngine::sAudioEngineImpl != nullptr) {
|
||||
return AudioEngine::sAudioEngineImpl;
|
||||
}
|
||||
// On oalsoft platforms, PCM decoding via AudioDecoderManager works
|
||||
// independently of OpenAL — create a decoder-only fallback instance.
|
||||
// On Android/OpenHarmony and Apple, AudioEngineImpl::getPCMHeader/
|
||||
// getOriginalPCMBuffer dereferences _audioPlayerProvider/_engineEngine
|
||||
// which are only set up by init(). Returning nullptr here lets callers
|
||||
// fail gracefully instead of crashing.
|
||||
#if CC_PLATFORM == CC_PLATFORM_WINDOWS || CC_PLATFORM == CC_PLATFORM_OHOS || \
|
||||
CC_PLATFORM == CC_PLATFORM_LINUX || CC_PLATFORM == CC_PLATFORM_QNX
|
||||
if (AudioEngine::sDecoderImpl == nullptr) {
|
||||
AudioEngine::sDecoderImpl = ccnew AudioEngineImpl();
|
||||
AudioEngineImpl::initDecoder();
|
||||
}
|
||||
return AudioEngine::sDecoderImpl;
|
||||
#else
|
||||
return nullptr;
|
||||
#endif
|
||||
}
|
||||
|
||||
PCMHeader AudioEngine::getPCMHeader(const char *url) {
|
||||
AudioEngineImpl *impl = AudioEngine::getDecoderImpl();
|
||||
if (impl == nullptr) {
|
||||
CC_LOG_WARNING("AudioEngine::getPCMHeader: audio engine unavailable, url: %s", url);
|
||||
return {};
|
||||
}
|
||||
return impl->getPCMHeader(url);
|
||||
}
|
||||
|
||||
ccstd::vector<uint8_t> AudioEngine::getOriginalPCMBuffer(const char *url, uint32_t channelID) {
|
||||
lazyInit();
|
||||
return sAudioEngineImpl->getOriginalPCMBuffer(url, channelID);
|
||||
AudioEngineImpl *impl = AudioEngine::getDecoderImpl();
|
||||
if (impl == nullptr) {
|
||||
CC_LOG_WARNING("AudioEngine::getOriginalPCMBuffer: audio engine unavailable, url: %s", url);
|
||||
return {};
|
||||
}
|
||||
return impl->getOriginalPCMBuffer(url, channelID);
|
||||
}
|
||||
} // namespace cc
|
||||
|
||||
@@ -426,6 +426,12 @@ void AudioEngineImpl::onResume() {
|
||||
}
|
||||
}
|
||||
|
||||
bool AudioEngineImpl::initDecoder() {
|
||||
// On Android the decoder infrastructure is set up by init().
|
||||
// This method exists so AudioEngine.cpp compiles uniformly across all platforms.
|
||||
return true;
|
||||
}
|
||||
|
||||
PCMHeader AudioEngineImpl::getPCMHeader(const char *url) {
|
||||
PCMHeader header{};
|
||||
ccstd::string fileFullPath = FileUtils::getInstance()->fullPathForFilename(url);
|
||||
|
||||
@@ -55,6 +55,7 @@ public:
|
||||
~AudioEngineImpl() override;
|
||||
|
||||
bool init();
|
||||
static bool initDecoder();
|
||||
int play2d(const ccstd::string &filePath, bool loop, float volume);
|
||||
void setVolume(int audioID, float volume);
|
||||
void setLoop(int audioID, bool loop);
|
||||
|
||||
@@ -43,6 +43,7 @@ public:
|
||||
~AudioEngineImpl();
|
||||
|
||||
bool init();
|
||||
static bool initDecoder();
|
||||
int play2d(const ccstd::string &fileFullPath, bool loop, float volume);
|
||||
void setVolume(int audioID, float volume);
|
||||
void setLoop(int audioID, bool loop);
|
||||
|
||||
@@ -632,6 +632,12 @@ bool AudioEngineImpl::checkAudioIdValid(int audioID) {
|
||||
return _audioPlayers.find(audioID) != _audioPlayers.end();
|
||||
}
|
||||
|
||||
bool AudioEngineImpl::initDecoder() {
|
||||
// On Apple the decoder infrastructure is set up by init().
|
||||
// This method exists so AudioEngine.cpp compiles uniformly across all platforms.
|
||||
return true;
|
||||
}
|
||||
|
||||
PCMHeader AudioEngineImpl::getPCMHeader(const char *url){
|
||||
PCMHeader header {};
|
||||
auto itr = _audioCaches.find(url);
|
||||
|
||||
@@ -339,6 +339,19 @@ protected:
|
||||
static void pauseAll(ccstd::vector<int> *pausedAudioIDs);
|
||||
static void resumeAll(ccstd::vector<int> *pausedAudioIDs);
|
||||
|
||||
/**
|
||||
* @brief Get Audio Decoder
|
||||
*
|
||||
* Strategy:
|
||||
* 1. If the full impl (with OpenAL) is already alive, reuse it (cache + decoder both available).
|
||||
* 2. If OpenAL init has not been attempted yet, try lazyInit() so that a successful OpenAL
|
||||
* environment still gets the full impl (keeps _audioCaches coherent with play2d).
|
||||
* 3. Only fall back to the decoder-only sDecoderImpl when lazyInit() has failed
|
||||
* (alcOpenDevice returned null, headless / OHOS / CI environments).
|
||||
* @return The impl instance to use for PCM decoding.
|
||||
*/
|
||||
static AudioEngineImpl *getDecoderImpl();
|
||||
|
||||
struct ProfileHelper {
|
||||
AudioProfile profile;
|
||||
|
||||
@@ -382,6 +395,10 @@ protected:
|
||||
static ProfileHelper *sDefaultProfileHelper;
|
||||
|
||||
static AudioEngineImpl *sAudioEngineImpl;
|
||||
// Decoder-only fallback instance used when OpenAL is unavailable.
|
||||
// getPCMHeader / getOriginalPCMBuffer do not depend on OpenAL and use this
|
||||
// instance directly instead of going through lazyInit().
|
||||
static AudioEngineImpl *sDecoderImpl;
|
||||
|
||||
class AudioEngineThreadPool;
|
||||
static AudioEngineThreadPool *sThreadPool;
|
||||
|
||||
@@ -62,7 +62,7 @@
|
||||
// log, CC_LOG_DEBUG aren't threadsafe, since we uses sub threads for parsing pcm data, threadsafe log output
|
||||
// is needed. Define the following macros (ALOGV, ALOGD, ALOGI, ALOGW, ALOGE) for threadsafe log output.
|
||||
|
||||
//IDEA: Move _winLog, winLog to a separated file
|
||||
// IDEA: Move _winLog, winLog to a separate file
|
||||
static void _winLog(const char *format, va_list args) {
|
||||
static const int MAX_LOG_LENGTH = 16 * 1024;
|
||||
int bufferSize = MAX_LOG_LENGTH;
|
||||
@@ -153,31 +153,50 @@ AudioEngineImpl::~AudioEngineImpl() {
|
||||
AudioDecoderManager::destroy();
|
||||
}
|
||||
|
||||
bool AudioEngineImpl::initDecoder() {
|
||||
return AudioDecoderManager::init();
|
||||
}
|
||||
|
||||
bool AudioEngineImpl::init() {
|
||||
bool ret = false;
|
||||
do {
|
||||
sALDevice = alcOpenDevice(nullptr);
|
||||
|
||||
if (sALDevice) {
|
||||
alGetError();
|
||||
sALContext = alcCreateContext(sALDevice, nullptr);
|
||||
alcMakeContextCurrent(sALContext);
|
||||
|
||||
alGenSources(MAX_AUDIOINSTANCES, _alSources);
|
||||
auto alError = alGetError();
|
||||
if (alError != AL_NO_ERROR) {
|
||||
CC_LOG_ERROR("%s:generating sources failed! error = %x\n", __FUNCTION__, alError);
|
||||
break;
|
||||
}
|
||||
|
||||
for (unsigned int src : _alSources) {
|
||||
_alSourceUsed[src] = false;
|
||||
}
|
||||
|
||||
_scheduler = CC_CURRENT_ENGINE()->getScheduler();
|
||||
ret = AudioDecoderManager::init();
|
||||
CC_LOG_DEBUG("OpenAL was initialized successfully!");
|
||||
if (sALDevice == nullptr) {
|
||||
// alcGetError(nullptr) is valid when no device is open
|
||||
ALCenum alcErr = alcGetError(nullptr);
|
||||
CC_LOG_ERROR("%s: alcOpenDevice failed, ALC error = 0x%x", __FUNCTION__, alcErr);
|
||||
break;
|
||||
}
|
||||
|
||||
alGetError();
|
||||
sALContext = alcCreateContext(sALDevice, nullptr);
|
||||
if (sALContext == nullptr) {
|
||||
ALCenum alcErr = alcGetError(sALDevice);
|
||||
CC_LOG_ERROR("%s: alcCreateContext failed, ALC error = 0x%x", __FUNCTION__, alcErr);
|
||||
break;
|
||||
}
|
||||
|
||||
alcMakeContextCurrent(sALContext);
|
||||
|
||||
std::fill(std::begin(_alSources), std::end(_alSources), 0);
|
||||
|
||||
alGenSources(MAX_AUDIOINSTANCES, _alSources);
|
||||
auto alError = alGetError();
|
||||
if (alError != AL_NO_ERROR) {
|
||||
CC_LOG_ERROR("%s:generating sources failed! error = %x\n", __FUNCTION__, alError);
|
||||
alcMakeContextCurrent(nullptr);
|
||||
alcDestroyContext(sALContext);
|
||||
sALContext = nullptr;
|
||||
break;
|
||||
}
|
||||
|
||||
for (unsigned int src : _alSources) {
|
||||
_alSourceUsed[src] = false;
|
||||
}
|
||||
|
||||
_scheduler = CC_CURRENT_ENGINE()->getScheduler();
|
||||
ret = initDecoder();
|
||||
CC_LOG_DEBUG("OpenAL was initialized successfully!");
|
||||
} while (false);
|
||||
|
||||
return ret;
|
||||
@@ -264,7 +283,7 @@ int AudioEngineImpl::play2d(const ccstd::string &filePath, bool loop, float volu
|
||||
}
|
||||
|
||||
void AudioEngineImpl::play2dImpl(AudioCache *cache, int audioID) {
|
||||
//Note: It may bn in sub thread or main thread :(
|
||||
// Note: It may be in sub thread or main thread :(
|
||||
if (!*cache->_isDestroyed && cache->_state == AudioCache::State::READY) {
|
||||
_threadMutex.lock();
|
||||
auto playerIt = _audioPlayers.find(audioID);
|
||||
|
||||
@@ -45,6 +45,9 @@ public:
|
||||
~AudioEngineImpl() override;
|
||||
|
||||
bool init();
|
||||
// Initialize only the PCM decoder subsystem (AudioDecoderManager).
|
||||
// Does not require an OpenAL device. Safe to call when init() failed.
|
||||
static bool initDecoder();
|
||||
int play2d(const ccstd::string &filePath, bool loop, float volume);
|
||||
void setVolume(int audioID, float volume);
|
||||
void setLoop(int audioID, bool loop);
|
||||
|
||||
@@ -70,20 +70,37 @@ static bool js_assets_SimpleTexture_registerListeners(se::State &s) // NOLINT(re
|
||||
{
|
||||
auto *cobj = SE_THIS_OBJECT<cc::SimpleTexture>(s);
|
||||
SE_PRECONDITION2(cobj, false, "Invalid Native Object");
|
||||
auto *thisObj = s.thisObject();
|
||||
|
||||
cobj->on<cc::SimpleTexture::TextureUpdated>([thisObj](cc::SimpleTexture * /*emitter*/, cc::gfx::Texture *texture) {
|
||||
// NOTE: resolve the live JS wrapper from the native pointer at emit time instead of capturing
|
||||
// `s.thisObject()`. A native texture can outlive its JS wrapper (e.g. builtin textures kept alive
|
||||
// by builtinResMgr), so a captured raw se::Object* would dangle and a later event would call into a
|
||||
// dead object. Skip when no live wrapper exists. See registerPassesUpdatedListener below for details.
|
||||
cobj->on<cc::SimpleTexture::TextureUpdated>([](cc::SimpleTexture *emitter, cc::gfx::Texture *texture) {
|
||||
auto *se = se::ScriptEngine::getInstance();
|
||||
if (se == nullptr || !se->isValid() || se->isInCleanup() || se->isGarbageCollecting()) {
|
||||
return;
|
||||
}
|
||||
se::AutoHandleScope hs;
|
||||
se::Value arg0;
|
||||
nativevalue_to_se(texture, arg0, nullptr);
|
||||
se::ScriptEngine::getInstance()->callFunction(thisObj, "_onGFXTextureUpdated", 1, &arg0);
|
||||
se::Object *jsObject = se::NativePtrToObjectMap::findFirst(emitter);
|
||||
if (jsObject != nullptr) {
|
||||
se::Value arg0;
|
||||
nativevalue_to_se(texture, arg0, nullptr);
|
||||
se->callFunction(jsObject, "_onGFXTextureUpdated", 1, &arg0);
|
||||
}
|
||||
});
|
||||
|
||||
cobj->on<cc::SimpleTexture::AfterAssignImage>([thisObj](cc::SimpleTexture * /*emitter*/, cc::ImageAsset *image) {
|
||||
cobj->on<cc::SimpleTexture::AfterAssignImage>([](cc::SimpleTexture *emitter, cc::ImageAsset *image) {
|
||||
auto *se = se::ScriptEngine::getInstance();
|
||||
if (se == nullptr || !se->isValid() || se->isInCleanup() || se->isGarbageCollecting()) {
|
||||
return;
|
||||
}
|
||||
se::AutoHandleScope hs;
|
||||
se::Value arg0;
|
||||
nativevalue_to_se(image, arg0, nullptr);
|
||||
se::ScriptEngine::getInstance()->callFunction(thisObj, "_onAfterAssignImage", 1, &arg0);
|
||||
se::Object *jsObject = se::NativePtrToObjectMap::findFirst(emitter);
|
||||
if (jsObject != nullptr) {
|
||||
se::Value arg0;
|
||||
nativevalue_to_se(image, arg0, nullptr);
|
||||
se->callFunction(jsObject, "_onAfterAssignImage", 1, &arg0);
|
||||
}
|
||||
});
|
||||
|
||||
return true;
|
||||
@@ -94,12 +111,20 @@ static bool js_assets_TextureBase_registerGFXSamplerUpdatedListener(se::State &s
|
||||
{
|
||||
auto *cobj = SE_THIS_OBJECT<cc::TextureBase>(s);
|
||||
SE_PRECONDITION2(cobj, false, "Invalid Native Object");
|
||||
auto *thisObj = s.thisObject();
|
||||
cobj->on<cc::TextureBase::SamplerUpdated>([thisObj](cc::TextureBase * /*emitter*/, cc::gfx::Sampler *sampler) {
|
||||
// Resolve the live JS wrapper at emit time (see SimpleTexture/Material listeners above) so the
|
||||
// callback never targets a wrapper that has already been GC'd while the native object lives on.
|
||||
cobj->on<cc::TextureBase::SamplerUpdated>([](cc::TextureBase *emitter, cc::gfx::Sampler *sampler) {
|
||||
auto *se = se::ScriptEngine::getInstance();
|
||||
if (se == nullptr || !se->isValid() || se->isInCleanup() || se->isGarbageCollecting()) {
|
||||
return;
|
||||
}
|
||||
se::AutoHandleScope hs;
|
||||
se::Value arg0;
|
||||
nativevalue_to_se(sampler, arg0, nullptr);
|
||||
se::ScriptEngine::getInstance()->callFunction(thisObj, "_onGFXSamplerUpdated", 1, &arg0);
|
||||
se::Object *jsObject = se::NativePtrToObjectMap::findFirst(emitter);
|
||||
if (jsObject != nullptr) {
|
||||
se::Value arg0;
|
||||
nativevalue_to_se(sampler, arg0, nullptr);
|
||||
se->callFunction(jsObject, "_onGFXSamplerUpdated", 1, &arg0);
|
||||
}
|
||||
});
|
||||
|
||||
return true;
|
||||
@@ -110,10 +135,31 @@ static bool js_assets_Material_registerPassesUpdatedListener(se::State &s) // NO
|
||||
{
|
||||
auto *cobj = SE_THIS_OBJECT<cc::Material>(s);
|
||||
SE_PRECONDITION2(cobj, false, "Invalid Native Object");
|
||||
auto *thisObj = s.thisObject();
|
||||
cobj->on<cc::Material::PassesUpdated>([thisObj](cc::Material * /*emitter*/) {
|
||||
// NOTE: Do NOT capture the JS wrapper (`s.thisObject()`) here. A native Material can outlive
|
||||
// its JS wrapper -- e.g. builtin "rt-*" materials are kept alive by builtinResMgr's IntrusivePtr
|
||||
// while their JS wrapper is GC'd after the owning sprite is destroyed. A captured raw se::Object*
|
||||
// would then dangle, and a later PassesUpdated (emitted from Material::update()/doDestroy()) would
|
||||
// invoke `_onPassesUpdated` on a dead object, producing:
|
||||
// [SE_ERROR] _onPassesUpdated is not a function: undefined
|
||||
// Instead resolve the *current* live JS wrapper from the native pointer at emit time, and skip
|
||||
// when none exists (a freshly recreated wrapper syncs passes lazily via its `passes` getter).
|
||||
//
|
||||
// The engine-state guard below is also mandatory: PassesUpdated can be emitted from
|
||||
// Material::update()/copy()/doDestroy() while the script engine is being torn down (editor
|
||||
// engine restart / scene reload) or while a GC is in progress. In those windows `findFirst`
|
||||
// may still return an se::Object whose V8 persistent now points at a freed object, and calling
|
||||
// into it crashes inside ObjectWrap::handle() (`Local<Object>::New` on a dangling handle).
|
||||
// Never touch JS during cleanup or GC -- see native/cocos/bindings/docs/JSB2.0-learning.
|
||||
cobj->on<cc::Material::PassesUpdated>([](cc::Material *emitter) {
|
||||
auto *se = se::ScriptEngine::getInstance();
|
||||
if (se == nullptr || !se->isValid() || se->isInCleanup() || se->isGarbageCollecting()) {
|
||||
return;
|
||||
}
|
||||
se::AutoHandleScope hs;
|
||||
se::ScriptEngine::getInstance()->callFunction(thisObj, "_onPassesUpdated", 0, nullptr);
|
||||
se::Object *jsObject = se::NativePtrToObjectMap::findFirst(emitter);
|
||||
if (jsObject != nullptr) {
|
||||
se->callFunction(jsObject, "_onPassesUpdated", 0, nullptr);
|
||||
}
|
||||
});
|
||||
|
||||
return true;
|
||||
|
||||
@@ -146,10 +146,19 @@ bool jsb_register_all_modules() {
|
||||
se::ScriptEngine *se = se::ScriptEngine::getInstance();
|
||||
|
||||
se->addBeforeCleanupHook([se]() {
|
||||
#if (CC_PLATFORM == CC_PLATFORM_IOS || CC_PLATFORM == CC_PLATFORM_MACOS || CC_PLATFORM == CC_PLATFORM_ANDROID || CC_PLATFORM == CC_PLATFORM_OHOS)
|
||||
// REMOVED: se->garbageCollect()
|
||||
// Reason: Calling GC before Object::cleanup() causes MarkCompact to traverse
|
||||
// live JSTypedArrays whose external BackingStore data has already been freed
|
||||
// by AudioEngine::end(). The safe GC in ScriptEngine::cleanup() (step 5,
|
||||
// after Object::cleanup() releases all persistent handles) is sufficient.
|
||||
cc::DeferredReleasePool::clear();
|
||||
#else
|
||||
se->garbageCollect();
|
||||
cc::DeferredReleasePool::clear();
|
||||
se->garbageCollect();
|
||||
cc::DeferredReleasePool::clear();
|
||||
#endif // (CC_PLATFORM == CC_PLATFORM_IOS || CC_PLATFORM == CC_PLATFORM_MACOS || CC_PLATFORM == CC_PLATFORM_ANDROID || CC_PLATFORM == CC_PLATFORM_OHOS)
|
||||
});
|
||||
|
||||
se->addRegisterCallback(jsb_register_global_variables);
|
||||
|
||||
@@ -699,7 +699,7 @@ static bool js_Model_setInstancedAttribute(se::State &s) // NOLINT(readability-i
|
||||
value[i] = dataVal.toFloat();
|
||||
}
|
||||
|
||||
cobj->setInstancedAttribute(name, value);
|
||||
cobj->setInstancedAttribute(name, std::move(value));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2022-2025 Xiamen Yaji Software Co., Ltd.
|
||||
Copyright (c) 2022-2026 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
@@ -23,11 +23,11 @@ THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
#pragma once
|
||||
|
||||
#define COCOS_MAJOR_VERSION 3
|
||||
#define COCOS_MINJOR_VERSION 8
|
||||
#define COCOS_PATCH_VERSION 8
|
||||
#define COCOS_VERSION_STRING "3.8.8"
|
||||
#define COCOS_MAJOR_VERSION 4
|
||||
#define COCOS_MINJOR_VERSION 0
|
||||
#define COCOS_PATCH_VERSION 0
|
||||
#define COCOS_VERSION_STRING "4.0.0"
|
||||
#define COCOS_VERSION_DEFINED 1
|
||||
#define COCOS_VERSION 30808
|
||||
#define COCOS_VERSION 40000
|
||||
|
||||
// #define COCOS_PRE_RELEASE "release"
|
||||
// #define COCOS_PRE_RELEASE "alpha"
|
||||
|
||||
@@ -68,6 +68,7 @@ RenderAdditiveLightQueue::RenderAdditiveLightQueue(RenderPipeline *pipeline) : _
|
||||
void RenderAdditiveLightQueue::recordCommandBuffer(gfx::Device *device, scene::Camera *camera, gfx::RenderPass *renderPass, gfx::CommandBuffer *cmdBuffer) {
|
||||
const uint32_t offset = _pipeline->getPipelineUBO()->getCurrentCameraUBOOffset();
|
||||
for (uint32_t i = 0; i < _instancedQueues.size(); ++i) {
|
||||
if (!this->_instancedQueues[i]) { continue; }
|
||||
const auto *light = _instancedLightPass.lights[i];
|
||||
_dynamicOffsets[0] = _instancedLightPass.dynamicOffsets[i];
|
||||
auto *globalDescriptorSet = _pipeline->getGlobalDSManager()->getOrCreateDescriptorSet(light);
|
||||
@@ -109,13 +110,12 @@ void RenderAdditiveLightQueue::gatherLightPasses(const scene::Camera *camera, gf
|
||||
|
||||
clear();
|
||||
|
||||
_validPunctualLights = _pipeline->getPipelineSceneData()->getValidPunctualLights();
|
||||
|
||||
if (_validPunctualLights.empty()) return;
|
||||
|
||||
updateUBOs(camera, cmdBuffer);
|
||||
updateLightDescriptorSet(camera, cmdBuffer);
|
||||
|
||||
_validPunctualLights = _pipeline->getPipelineSceneData()->getValidPunctualLights();
|
||||
if (_validPunctualLights.empty()) return;
|
||||
|
||||
const auto &renderObjects = _pipeline->getPipelineSceneData()->getRenderObjects();
|
||||
for (const auto &renderObject : renderObjects) {
|
||||
const auto *const model = renderObject.model;
|
||||
@@ -154,20 +154,19 @@ void RenderAdditiveLightQueue::gatherLightPasses(const scene::Camera *camera, gf
|
||||
_instancedLightPass.dynamicOffsets.emplace_back(_lightBufferStride * l);
|
||||
}
|
||||
for (const auto &instancedQueue : _instancedQueues) {
|
||||
instancedQueue->uploadBuffers(cmdBuffer);
|
||||
if (instancedQueue) {
|
||||
instancedQueue->uploadBuffers(cmdBuffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void RenderAdditiveLightQueue::clear() {
|
||||
for (const auto &instancedQueue : _instancedQueues) {
|
||||
instancedQueue->clear();
|
||||
if (instancedQueue) {
|
||||
instancedQueue->clear();
|
||||
}
|
||||
}
|
||||
_instancedQueues.clear();
|
||||
|
||||
for (auto lightPass : _lightPasses) {
|
||||
lightPass.dynamicOffsets.clear();
|
||||
lightPass.lights.clear();
|
||||
}
|
||||
_lightPasses.clear();
|
||||
|
||||
_instancedLightPass.dynamicOffsets.clear();
|
||||
@@ -212,13 +211,14 @@ void RenderAdditiveLightQueue::addRenderQueue(scene::SubModel *subModel, const s
|
||||
if ((visibility & model->getNode()->getLayer()) == model->getNode()->getLayer()) {
|
||||
switch (batchingScheme) {
|
||||
case scene::BatchingSchemes::INSTANCING: {
|
||||
auto *buffer = pass->getInstancedBuffer(i);
|
||||
auto *buffer = pass->getInstancedBuffer(lightIdx);
|
||||
buffer->merge(subModel, lightPassIdx);
|
||||
buffer->setDynamicOffset(0, _lightBufferStride);
|
||||
if (i >= _instancedQueues.size()) {
|
||||
_instancedQueues.emplace_back(ccnew RenderInstancedQueue());
|
||||
buffer->setDynamicOffset(0, _lightBufferStride * lightIdx);
|
||||
if (_instancedQueues.size() <= lightIdx || !_instancedQueues[lightIdx]) {
|
||||
_instancedQueues.resize(lightIdx + 1);
|
||||
_instancedQueues[lightIdx] = ccnew RenderInstancedQueue();
|
||||
}
|
||||
_instancedQueues[i]->add(buffer);
|
||||
_instancedQueues[lightIdx]->add(buffer);
|
||||
} break;
|
||||
case scene::BatchingSchemes::NONE: {
|
||||
lightPass.lights.emplace_back(light);
|
||||
@@ -243,6 +243,7 @@ void RenderAdditiveLightQueue::updateUBOs(const scene::Camera *camera, gfx::Comm
|
||||
|
||||
size_t offset = 0;
|
||||
if (validLightCount > _lightBufferCount) {
|
||||
_firstLightBufferView->destroy();
|
||||
_lightBufferCount = nextPow2(static_cast<uint32_t>(validLightCount));
|
||||
_lightBuffer->resize(utils::toUint(_lightBufferStride * _lightBufferCount));
|
||||
_lightBufferData.resize(static_cast<size_t>(_lightBufferElementCount) * _lightBufferCount);
|
||||
@@ -432,7 +433,7 @@ void RenderAdditiveLightQueue::updateLightDescriptorSet(const scene::Camera *cam
|
||||
memcpy(_shadowUBO.data() + UBOShadow::MAT_LIGHT_VIEW_PROJ_OFFSET, matShadowViewProj.m, sizeof(matShadowViewProj));
|
||||
|
||||
// shadow info
|
||||
float shadowNFLSInfos[4] = {0.1F, spotLight->getRange(), 0.0F, 0.0F};
|
||||
float shadowNFLSInfos[4] = {0.01F, spotLight->getRange(), 0.0F, 0.0F};
|
||||
memcpy(_shadowUBO.data() + UBOShadow::SHADOW_NEAR_FAR_LINEAR_SATURATION_INFO_OFFSET, &shadowNFLSInfos, sizeof(shadowNFLSInfos));
|
||||
|
||||
const auto &shadowSize = shadowInfo->getSize();
|
||||
@@ -491,15 +492,17 @@ bool RenderAdditiveLightQueue::getLightPassIndex(const scene::Model *model, ccst
|
||||
bool hasValidLightPass = false;
|
||||
|
||||
for (const auto &subModel : model->getSubModels()) {
|
||||
int lightPassIndex = 0;
|
||||
int lightPassIndex = -1; // use -1 to indicate no valid light pass found
|
||||
int k = 0;
|
||||
for (const auto &pass : *(subModel->getPasses())) {
|
||||
if (pass->getPhase() == _phaseID) {
|
||||
lightPassIndex = k;
|
||||
hasValidLightPass = true;
|
||||
break;
|
||||
}
|
||||
++lightPassIndex;
|
||||
++k;
|
||||
}
|
||||
lightPassIndices->push_back(lightPassIndex);
|
||||
lightPassIndices->push_back(static_cast<uint32_t>(lightPassIndex));
|
||||
}
|
||||
|
||||
return hasValidLightPass;
|
||||
@@ -517,7 +520,7 @@ void RenderAdditiveLightQueue::lightCulling(const scene::Model *model) {
|
||||
isCulled = cullSpotLight(static_cast<const scene::SpotLight *>(light), model);
|
||||
break;
|
||||
case scene::LightType::POINT:
|
||||
isCulled = cullSphereLight(static_cast<const scene::SphereLight *>(light), model);
|
||||
isCulled = cullPointLight(static_cast<const scene::PointLight *>(light), model);
|
||||
break;
|
||||
case scene::LightType::RANGED_DIRECTIONAL:
|
||||
isCulled = cullRangedDirLight(static_cast<const scene::RangedDirectionalLight *>(light), model);
|
||||
|
||||
@@ -753,7 +753,7 @@ uint32_t LightResource::addLight(
|
||||
// already added
|
||||
auto iter = lightIndex.find(light);
|
||||
if (iter != lightIndex.end()) {
|
||||
return iter->second;
|
||||
return iter->second * elementSize;
|
||||
}
|
||||
|
||||
// resize buffer
|
||||
|
||||
@@ -1,20 +1,22 @@
|
||||
{
|
||||
"name": "cocos-creator",
|
||||
"version": "4.0.0-alpha.17",
|
||||
"version": "4.0.0-alpha.28",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "cocos-creator",
|
||||
"version": "4.0.0-alpha.17",
|
||||
"version": "4.0.0-alpha.28",
|
||||
"hasInstallScript": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@babel/plugin-proposal-class-properties": "^7.18.6",
|
||||
"@cocos/box2d": "1.0.2",
|
||||
"@cocos/cannon": "1.2.8",
|
||||
"@cocos/ccbuild": "^2.3.16",
|
||||
"@cocos/ccbuild": "^2.3.21",
|
||||
"@cocos/dragonbones-js": "^1.0.1",
|
||||
"@cocos/engine-pal": "^1.0.4",
|
||||
"@cocos/engine-platforms": "^1.0.5",
|
||||
"@types/jest": "^24.9.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
@@ -2064,9 +2066,13 @@
|
||||
}
|
||||
},
|
||||
"node_modules/@cocos/ccbuild": {
|
||||
"version": "2.3.16",
|
||||
"resolved": "https://registry.npmjs.org/@cocos/ccbuild/-/ccbuild-2.3.16.tgz",
|
||||
"integrity": "sha512-ZfY2ZJXs0uIMyjQLuZH9CZ4CGhM4YTdn9eGBPV62tXaSLkIHU/o0Vp99CqVxP1vYM1Ud1Z6a/fArLP/Qe3/IIQ==",
|
||||
"version": "2.3.21",
|
||||
"resolved": "https://registry.npmmirror.com/@cocos/ccbuild/-/ccbuild-2.3.21.tgz",
|
||||
"integrity": "sha512-hwsuAQKW55jGCZvVmH/09Mvz3i6hv8XN5udHGJJrQc2IYOLCctymoHfuyobcQ+LI+qHRSRKKgc+cKN2jWZ0sTA==",
|
||||
"license": "MIT",
|
||||
"workspaces": [
|
||||
"./modules/*"
|
||||
],
|
||||
"dependencies": {
|
||||
"@babel/core": "^7.20.12",
|
||||
"@babel/generator": "^7.22.10",
|
||||
@@ -2561,6 +2567,16 @@
|
||||
"resolved": "https://registry.npmjs.org/@cocos/dragonbones-js/-/dragonbones-js-1.0.1.tgz",
|
||||
"integrity": "sha512-1bWAW8td2WTGicyCqxtpIjc1YuK5EzMWfMEWaJlskPAQwLS0JfxEGka71sZ1BzudiWiKS7hPMg+FvXr5NRnVwQ=="
|
||||
},
|
||||
"node_modules/@cocos/engine-pal": {
|
||||
"version": "1.0.4",
|
||||
"resolved": "https://registry.npmmirror.com/@cocos/engine-pal/-/engine-pal-1.0.4.tgz",
|
||||
"integrity": "sha512-VtvO0xE2ZviVtwqWn4P6cysn2BX7QABm9sNc1rfy4gy+FHtiwSMkLLL3l3Faw+yZiTE6EwxjPvnc96Igb7mNuQ=="
|
||||
},
|
||||
"node_modules/@cocos/engine-platforms": {
|
||||
"version": "1.0.5",
|
||||
"resolved": "https://registry.npmmirror.com/@cocos/engine-platforms/-/engine-platforms-1.0.5.tgz",
|
||||
"integrity": "sha512-ES004q8DkuexPk8B4teXzCmAZNW8G4iqnLYJn/VZhYEOMDpySF8HhNfBl2M91BMnsCXlvJILABLKEVUCqUL+aA=="
|
||||
},
|
||||
"node_modules/@cocos/rollup-plugin-terser": {
|
||||
"version": "0.4.5",
|
||||
"resolved": "https://registry.npmjs.org/@cocos/rollup-plugin-terser/-/rollup-plugin-terser-0.4.5.tgz",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "cocos-creator",
|
||||
"version": "4.0.0-alpha.17",
|
||||
"version": "4.0.0-alpha.28",
|
||||
"description": "Cocos Creator is a cross-platform 2D & 3D game engine written in C++ and TypeScript, licensed under MIT.",
|
||||
"homepage": "http://www.cocos.com",
|
||||
"license": "MIT",
|
||||
@@ -12,7 +12,7 @@
|
||||
"node": ">=18.0.0"
|
||||
},
|
||||
"scripts": {
|
||||
"postinstall": "npm run build:debug-infos && npm run build:declaration && npm run build:adapter && npm run build:native-pack-tool",
|
||||
"postinstall": "node scripts/spread-pal.cjs && npm run build:debug-infos && npm run build:declaration && node scripts/spread-adapter.cjs && npm run build:native-pack-tool",
|
||||
"build": "npm run build:min && npm run build:declaration",
|
||||
"build:cli": "npm run build:cli-min && npm run build:declaration",
|
||||
"build:adapter": "node ./scripts/build-adapter.js",
|
||||
@@ -81,8 +81,14 @@
|
||||
"@babel/plugin-proposal-class-properties": "^7.18.6",
|
||||
"@cocos/box2d": "1.0.2",
|
||||
"@cocos/cannon": "1.2.8",
|
||||
"@cocos/ccbuild": "^2.3.16",
|
||||
"@cocos/ccbuild": "^2.3.21",
|
||||
"@cocos/dragonbones-js": "^1.0.1",
|
||||
"@cocos/engine-pal": "^1.0.4",
|
||||
"@cocos/engine-platforms": "^1.0.5",
|
||||
"@types/jest": "^24.9.1"
|
||||
}
|
||||
},
|
||||
"creator": {
|
||||
"version": "3.8.8"
|
||||
},
|
||||
"uuid": "8be51d4a-79ad-481c-adbf-265693b5adbf"
|
||||
}
|
||||
|
||||
@@ -1,82 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { warnID } from '../../cocos/core/platform/debug';
|
||||
|
||||
/** @mangle */
|
||||
interface CachedAudioBufferData {
|
||||
usedCount: number,
|
||||
audioBuffer: AudioBuffer,
|
||||
}
|
||||
|
||||
interface AudioBufferDataMap {
|
||||
[url: string]: CachedAudioBufferData;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a manager to manage the cache of audio buffer for web audio.
|
||||
* @mangle
|
||||
*/
|
||||
class AudioBufferManager {
|
||||
private _audioBufferDataMap: AudioBufferDataMap = {};
|
||||
|
||||
public addCache (url: string, audioBuffer: AudioBuffer): void {
|
||||
const audioBufferData = this._audioBufferDataMap[url];
|
||||
if (audioBufferData) {
|
||||
warnID(5204, url);
|
||||
return;
|
||||
}
|
||||
this._audioBufferDataMap[url] = {
|
||||
usedCount: 1,
|
||||
audioBuffer,
|
||||
};
|
||||
}
|
||||
|
||||
public retainCache (url: string): void {
|
||||
const audioBufferData = this._audioBufferDataMap[url];
|
||||
if (!audioBufferData) {
|
||||
warnID(5203, url);
|
||||
return;
|
||||
}
|
||||
audioBufferData.usedCount++;
|
||||
}
|
||||
|
||||
public getCache (url: string): AudioBuffer | null | undefined {
|
||||
const audioBufferData = this._audioBufferDataMap[url];
|
||||
return audioBufferData?.audioBuffer;
|
||||
}
|
||||
|
||||
public tryReleasingCache (url: string): void {
|
||||
const audioBufferData = this._audioBufferDataMap[url];
|
||||
if (!audioBufferData) {
|
||||
warnID(5203, url);
|
||||
return;
|
||||
}
|
||||
if (--audioBufferData.usedCount <= 0) {
|
||||
delete this._audioBufferDataMap[url];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const audioBufferManager = new AudioBufferManager();
|
||||
@@ -1,119 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { clamp } from '../../cocos/core/math/utils';
|
||||
|
||||
/**
|
||||
* Tool class to calculate audio current time.
|
||||
* For some platforms where audio.currentTime doesn't work well or isn't implemented.
|
||||
*/
|
||||
|
||||
interface IDuration {
|
||||
duration: number;
|
||||
}
|
||||
|
||||
export default class AudioTimer {
|
||||
private _nativeAudio: IDuration;
|
||||
private _startTime = 0;
|
||||
private _startOffset = 0;
|
||||
private _isPaused = true;
|
||||
|
||||
constructor (nativeAudio: IDuration) {
|
||||
this._nativeAudio = nativeAudio;
|
||||
}
|
||||
|
||||
public destroy (): void {
|
||||
// NOTE: 'undefined' is not assignable to type 'IDuration'
|
||||
this._nativeAudio = undefined as any;
|
||||
}
|
||||
|
||||
get duration (): number {
|
||||
return this._nativeAudio.duration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current time of audio timer.
|
||||
*/
|
||||
get currentTime (): number {
|
||||
if (this._isPaused) {
|
||||
return this._startOffset;
|
||||
} else {
|
||||
return this._calculateCurrentTime();
|
||||
}
|
||||
}
|
||||
|
||||
private _now (): number {
|
||||
return performance.now() / 1000;
|
||||
}
|
||||
|
||||
private _calculateCurrentTime (): number {
|
||||
const timePassed = this._now() - this._startTime;
|
||||
const currentTime = this._startOffset + timePassed;
|
||||
if (currentTime >= this.duration) {
|
||||
// timer loop
|
||||
this._startTime = this._now();
|
||||
this._startOffset = 0;
|
||||
}
|
||||
return currentTime % this.duration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the audio timer.
|
||||
* Call this method when audio is played.
|
||||
*/
|
||||
start (): void {
|
||||
this._isPaused = false;
|
||||
this._startTime = this._now();
|
||||
}
|
||||
|
||||
/**
|
||||
* Pause the audio timer.
|
||||
* Call this method when audio is paused or interrupted.
|
||||
*/
|
||||
pause (): void {
|
||||
if (this._isPaused) {
|
||||
return;
|
||||
}
|
||||
this._isPaused = true;
|
||||
this._startOffset = this._calculateCurrentTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the audio timer.
|
||||
* Call this method when audio playing ended or audio is stopped.
|
||||
*/
|
||||
stop (): void {
|
||||
this._isPaused = true;
|
||||
this._startOffset = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Seek the audio timer.
|
||||
* Call this method when audio is seeked.
|
||||
*/
|
||||
seek (time: number): void {
|
||||
this._startTime = this._now();
|
||||
this._startOffset = clamp(time, 0, this.duration);
|
||||
}
|
||||
}
|
||||
@@ -1,393 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { minigame } from 'pal/minigame';
|
||||
import { systemInfo } from 'pal/system-info';
|
||||
import { TAOBAO, TAOBAO_MINIGAME, HUAWEI, VIVO, OPPO } from 'internal:constants';
|
||||
import { EventTarget } from '../../../cocos/core/event';
|
||||
import { AudioEvent, AudioPCMDataView, AudioState, AudioType } from '../type';
|
||||
import { clamp, clamp01 } from '../../../cocos/core';
|
||||
import { enqueueOperation, OperationInfo, OperationQueueable } from '../operation-queue';
|
||||
import { OS } from '../../system-info/enum-type';
|
||||
|
||||
export class OneShotAudioMinigame {
|
||||
private declare _innerAudioContext: InnerAudioContext;
|
||||
private _onPlayCb?: () => void;
|
||||
get onPlay (): (() => void) | undefined {
|
||||
return this._onPlayCb;
|
||||
}
|
||||
set onPlay (cb) {
|
||||
this._onPlayCb = cb;
|
||||
}
|
||||
|
||||
private _onEndCb?: () => void;
|
||||
get onEnd (): (() => void) | undefined {
|
||||
return this._onEndCb;
|
||||
}
|
||||
set onEnd (cb) {
|
||||
this._onEndCb = cb;
|
||||
}
|
||||
|
||||
private constructor (nativeAudio: InnerAudioContext, volume: number) {
|
||||
this._innerAudioContext = nativeAudio;
|
||||
nativeAudio.volume = volume;
|
||||
nativeAudio.onPlay(() => {
|
||||
this._onPlayCb?.();
|
||||
});
|
||||
const endCallback = (): void => {
|
||||
if (this._innerAudioContext) {
|
||||
// NOTE: Type 'null' is not assignable to type 'InnerAudioContext'.
|
||||
this._innerAudioContext = null as any;
|
||||
systemInfo.off('hide', this._onInterruptedBegin, this);
|
||||
systemInfo.off('show', this._onInterruptedEnd, this);
|
||||
this._onEndCb?.();
|
||||
/**The destroy interface, in some platform implementations, internally invokes stop,
|
||||
* triggering the onStop callback, consequently leading to an infinite loop. **/
|
||||
nativeAudio.destroy();
|
||||
}
|
||||
};
|
||||
nativeAudio.onEnded(endCallback);
|
||||
nativeAudio.onStop(endCallback);//OneShotAudio can not be reused.
|
||||
|
||||
// event
|
||||
systemInfo.on('hide', this._onInterruptedBegin, this);
|
||||
systemInfo.on('show', this._onInterruptedEnd, this);
|
||||
}
|
||||
|
||||
private _onInterruptedBegin (): void {
|
||||
this._innerAudioContext.pause();
|
||||
}
|
||||
|
||||
private _onInterruptedEnd (): void {
|
||||
this._innerAudioContext.play();
|
||||
}
|
||||
|
||||
public play (): void {
|
||||
this._innerAudioContext.play();
|
||||
}
|
||||
public stop (): void {
|
||||
this._innerAudioContext.stop();
|
||||
}
|
||||
}
|
||||
|
||||
export class AudioPlayerMinigame implements OperationQueueable {
|
||||
private declare _innerAudioContext: InnerAudioContext;
|
||||
private _state: AudioState = AudioState.INIT;
|
||||
private _cacheTime = 0;
|
||||
private _needSeek = false;
|
||||
private _seeking = false;
|
||||
|
||||
private declare _onPlay: () => void;
|
||||
private declare _onPause: () => void;
|
||||
private declare _onStop: () => void;
|
||||
private declare _onSeeked: () => void;
|
||||
private declare _onEnded: () => void;
|
||||
private _readyToHandleOnShow = false;
|
||||
|
||||
private _resetSeekCache (): void {
|
||||
this._cacheTime = 0;
|
||||
this._needSeek = false;
|
||||
this._seeking = false;
|
||||
if ((HUAWEI || VIVO || OPPO) && this._innerAudioContext) {
|
||||
this._innerAudioContext.startTime = 0;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @deprecated since v3.5.0, this is an engine private interface that will be removed in the future.
|
||||
*/
|
||||
public _eventTarget: EventTarget = new EventTarget();
|
||||
/**
|
||||
* @deprecated since v3.5.0, this is an engine private interface that will be removed in the future.
|
||||
*/
|
||||
public _operationQueue: OperationInfo[] = [];
|
||||
|
||||
constructor (innerAudioContext: InnerAudioContext) {
|
||||
const self = this;
|
||||
self._innerAudioContext = innerAudioContext;
|
||||
|
||||
// event
|
||||
systemInfo.on('hide', self._onInterruptedBegin, self);
|
||||
systemInfo.on('show', self._onInterruptedEnd, self);
|
||||
const eventTarget = self._eventTarget;
|
||||
self._onPlay = (): void => {
|
||||
self._state = AudioState.PLAYING;
|
||||
eventTarget.emit(AudioEvent.PLAYED);
|
||||
if (self._needSeek) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||
self.seek(self._cacheTime).catch((e) => {});
|
||||
}
|
||||
};
|
||||
innerAudioContext.onPlay(self._onPlay);
|
||||
self._onPause = (): void => {
|
||||
self._state = AudioState.PAUSED;
|
||||
try {
|
||||
const currentTime = self._innerAudioContext.currentTime;
|
||||
if (currentTime !== null && currentTime !== undefined) {
|
||||
self._cacheTime = currentTime;
|
||||
}
|
||||
} catch {
|
||||
// Do nothing, cacheTime is not updated.
|
||||
}
|
||||
eventTarget.emit(AudioEvent.PAUSED);
|
||||
};
|
||||
innerAudioContext.onPause(self._onPause);
|
||||
self._onStop = (): void => {
|
||||
self._state = AudioState.STOPPED;
|
||||
// Reset all properties
|
||||
self._resetSeekCache();
|
||||
eventTarget.emit(AudioEvent.STOPPED);
|
||||
if (TAOBAO || TAOBAO_MINIGAME) {
|
||||
/**Unable to seek again after stop; After stop, regardless of whether the starttime has been set,
|
||||
the playback will always start from 0 again**/
|
||||
} else {
|
||||
const currentTime = self._innerAudioContext ? self._innerAudioContext.currentTime : 0;
|
||||
if (currentTime !== 0) {
|
||||
self._innerAudioContext.seek(0);
|
||||
}
|
||||
}
|
||||
};
|
||||
innerAudioContext.onStop(self._onStop);
|
||||
self._onSeeked = (): void => {
|
||||
eventTarget.emit(AudioEvent.SEEKED);
|
||||
self._seeking = false;
|
||||
if (self._needSeek) {
|
||||
self._needSeek = false;
|
||||
if (self._cacheTime.toFixed(2) !== self._innerAudioContext.currentTime.toFixed(2)) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||
self.seek(self._cacheTime).catch((e) => {});
|
||||
}
|
||||
}
|
||||
};
|
||||
innerAudioContext.onSeeked(self._onSeeked);
|
||||
self._onEnded = (): void => {
|
||||
self._state = AudioState.INIT;
|
||||
self._resetSeekCache();
|
||||
eventTarget.emit(AudioEvent.ENDED);
|
||||
};
|
||||
innerAudioContext.onEnded(self._onEnded);
|
||||
}
|
||||
destroy (): void {
|
||||
const self = this;
|
||||
systemInfo.off('hide', self._onInterruptedBegin, self);
|
||||
systemInfo.off('show', self._onInterruptedEnd, self);
|
||||
const innerAudioContext = self._innerAudioContext;
|
||||
if (innerAudioContext) {
|
||||
['Play', 'Pause', 'Stop', 'Seeked', 'Ended'].forEach((event) => {
|
||||
self._offEvent(event);
|
||||
});
|
||||
// NOTE: innerAudioContext might not stop the audio playing, have to call it explicitly.
|
||||
innerAudioContext.stop();
|
||||
innerAudioContext.destroy();
|
||||
// NOTE: Type 'null' is not assignable to type 'InnerAudioContext'
|
||||
self._innerAudioContext = null as any;
|
||||
// Restore the state of the audio, otherwise it will cause 'destroy' to be called first and 'stop' to be called later.
|
||||
// this will cause a error.
|
||||
self._state = AudioState.INIT;
|
||||
}
|
||||
}
|
||||
private _onInterruptedBegin (): void {
|
||||
if (this._state === AudioState.PLAYING) {
|
||||
this.pause().then(() => {
|
||||
this._state = AudioState.INTERRUPTED;
|
||||
this._readyToHandleOnShow = true;
|
||||
this._eventTarget.emit(AudioEvent.INTERRUPTION_BEGIN);
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||
}).catch((e) => {});
|
||||
}
|
||||
}
|
||||
private _onInterruptedEnd (): void {
|
||||
// We don't know whether onShow or resolve callback in pause promise is called at first.
|
||||
if (!this._readyToHandleOnShow) {
|
||||
this._eventTarget.once(AudioEvent.INTERRUPTION_END, this._onInterruptedEnd, this);
|
||||
return;
|
||||
}
|
||||
if (this._state === AudioState.INTERRUPTED) {
|
||||
this.play().then(() => {
|
||||
this._eventTarget.emit(AudioEvent.INTERRUPTION_END);
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||
}).catch((e) => {});
|
||||
}
|
||||
this._readyToHandleOnShow = false;
|
||||
}
|
||||
private _offEvent (eventName: string): void {
|
||||
if (this[`_on${eventName}`]) {
|
||||
this._innerAudioContext[`off${eventName}`](this[`_on${eventName}`]);
|
||||
this[`_on${eventName}`] = null;
|
||||
}
|
||||
}
|
||||
|
||||
get src (): string {
|
||||
return this._innerAudioContext ? this._innerAudioContext.src : '';
|
||||
}
|
||||
get type (): AudioType {
|
||||
return AudioType.MINIGAME_AUDIO;
|
||||
}
|
||||
static load (url: string): Promise<AudioPlayerMinigame> {
|
||||
return new Promise((resolve, reject) => {
|
||||
AudioPlayerMinigame.loadNative(url).then((innerAudioContext) => {
|
||||
resolve(new AudioPlayerMinigame(innerAudioContext as InnerAudioContext));
|
||||
}).catch(reject);
|
||||
});
|
||||
}
|
||||
static loadNative (url: string): Promise<unknown> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const innerAudioContext = minigame.createInnerAudioContext();
|
||||
const timer = setTimeout(() => {
|
||||
clearEvent();
|
||||
resolve(innerAudioContext);
|
||||
}, 8000);
|
||||
function clearEvent (): void {
|
||||
innerAudioContext.offCanplay(success);
|
||||
innerAudioContext.offError(fail);
|
||||
}
|
||||
function success (): void {
|
||||
clearEvent();
|
||||
clearTimeout(timer);
|
||||
resolve(innerAudioContext);
|
||||
}
|
||||
function fail (err): void {
|
||||
clearEvent();
|
||||
clearTimeout(timer);
|
||||
// eslint-disable-next-line no-console
|
||||
console.error('failed to load innerAudioContext');
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||
reject(new Error(err));
|
||||
}
|
||||
innerAudioContext.onCanplay(success);
|
||||
innerAudioContext.onError(fail);
|
||||
innerAudioContext.src = url;
|
||||
});
|
||||
}
|
||||
static loadOneShotAudio (url: string, volume: number): Promise<OneShotAudioMinigame> {
|
||||
return new Promise((resolve, reject) => {
|
||||
AudioPlayerMinigame.loadNative(url).then((innerAudioContext) => {
|
||||
// HACK: AudioPlayer should be a friend class in OneShotAudio
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||
resolve(new (OneShotAudioMinigame as any)(innerAudioContext, volume));
|
||||
}).catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
get state (): AudioState {
|
||||
return this._state;
|
||||
}
|
||||
get loop (): boolean {
|
||||
return this._innerAudioContext.loop;
|
||||
}
|
||||
set loop (val: boolean) {
|
||||
this._innerAudioContext.loop = val;
|
||||
}
|
||||
get volume (): number {
|
||||
return this._innerAudioContext.volume;
|
||||
}
|
||||
set volume (val: number) {
|
||||
val = clamp01(val);
|
||||
this._innerAudioContext.volume = val;
|
||||
}
|
||||
get duration (): number {
|
||||
// KNOWN ISSUES: duration doesn't work well
|
||||
// On WeChat platform, duration is 0 at the time audio is loaded.
|
||||
return this._innerAudioContext.duration;
|
||||
}
|
||||
get currentTime (): number {
|
||||
if ((HUAWEI || VIVO || OPPO) && (this._state === AudioState.STOPPED || this._state === AudioState.INIT)) {
|
||||
return this._innerAudioContext.startTime;
|
||||
}
|
||||
if (this._state !== AudioState.PLAYING || this._needSeek || this._seeking) {
|
||||
return this._cacheTime;
|
||||
}
|
||||
return this._innerAudioContext.currentTime;
|
||||
}
|
||||
|
||||
get sampleRate (): number {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public getPCMData (channelIndex: number): AudioPCMDataView | undefined {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
@enqueueOperation
|
||||
seek (time: number): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
// KNOWN ISSUES: on Baidu: currentTime returns without numbers on decimal places
|
||||
if (this._state === AudioState.PLAYING && !this._seeking) {
|
||||
time = clamp(time, 0, this.duration);
|
||||
this._seeking = true;
|
||||
this._cacheTime = time;
|
||||
this._eventTarget.once(AudioEvent.SEEKED, resolve);
|
||||
this._innerAudioContext.seek(time);
|
||||
} else {
|
||||
//Huawei, vivo, Oppo platform, after stop, regardless of whether the seek has been called, the playback will always start from 0 again
|
||||
if ((HUAWEI || VIVO || OPPO) && (this._state === AudioState.STOPPED || this._state === AudioState.INIT)) {
|
||||
this._innerAudioContext.startTime = time;
|
||||
} else if (this._cacheTime !== time) { // Skip the invalid seek
|
||||
this._cacheTime = time;
|
||||
this._needSeek = true;
|
||||
}
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@enqueueOperation
|
||||
play (): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
this._eventTarget.once(AudioEvent.PLAYED, resolve);
|
||||
this._innerAudioContext.play();
|
||||
});
|
||||
}
|
||||
|
||||
@enqueueOperation
|
||||
pause (): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
if (this.state !== AudioState.PLAYING) {
|
||||
resolve();
|
||||
} else {
|
||||
this._eventTarget.once(AudioEvent.PAUSED, resolve);
|
||||
this._innerAudioContext.pause();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@enqueueOperation
|
||||
stop (): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
if (AudioState.INIT === this._state) {
|
||||
this._resetSeekCache();
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
this._eventTarget.once(AudioEvent.STOPPED, resolve);
|
||||
this._innerAudioContext.stop();
|
||||
});
|
||||
}
|
||||
|
||||
onInterruptionBegin (cb: () => void): void { this._eventTarget.on(AudioEvent.INTERRUPTION_BEGIN, cb); }
|
||||
offInterruptionBegin (cb?: () => void): void { this._eventTarget.off(AudioEvent.INTERRUPTION_BEGIN, cb); }
|
||||
onInterruptionEnd (cb: () => void): void { this._eventTarget.on(AudioEvent.INTERRUPTION_END, cb); }
|
||||
offInterruptionEnd (cb?: () => void): void { this._eventTarget.off(AudioEvent.INTERRUPTION_END, cb); }
|
||||
onEnded (cb: () => void): void { this._eventTarget.on(AudioEvent.ENDED, cb); }
|
||||
offEnded (cb?: () => void): void { this._eventTarget.off(AudioEvent.ENDED, cb); }
|
||||
}
|
||||
@@ -1,323 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { minigame } from 'pal/minigame';
|
||||
import { systemInfo } from 'pal/system-info';
|
||||
import { clamp01 } from '../../../cocos/core';
|
||||
import * as debug from '../../../cocos/core/platform/debug';
|
||||
import { EventTarget } from '../../../cocos/core/event';
|
||||
import { audioBufferManager } from '../audio-buffer-manager';
|
||||
import AudioTimer from '../audio-timer';
|
||||
import { enqueueOperation, OperationInfo, OperationQueueable } from '../operation-queue';
|
||||
import { AudioEvent, AudioPCMDataView, AudioState, AudioType } from '../type';
|
||||
import { Game, game } from '../../../cocos/game';
|
||||
|
||||
declare const fsUtils: any;
|
||||
const audioContext = minigame.tt?.getAudioContext?.();
|
||||
|
||||
export class OneShotAudioWeb {
|
||||
private _bufferSourceNode: AudioBufferSourceNode;
|
||||
private _onPlayCb?: () => void;
|
||||
private _url: string;
|
||||
|
||||
get onPlay (): (() => void) | undefined {
|
||||
return this._onPlayCb;
|
||||
}
|
||||
set onPlay (cb) {
|
||||
this._onPlayCb = cb;
|
||||
}
|
||||
|
||||
private _onEndCb?: () => void;
|
||||
get onEnd (): (() => void) | undefined {
|
||||
return this._onEndCb;
|
||||
}
|
||||
set onEnd (cb) {
|
||||
this._onEndCb = cb;
|
||||
}
|
||||
|
||||
private constructor (audioBuffer: AudioBuffer, volume: number, url: string) {
|
||||
this._bufferSourceNode = audioContext!.createBufferSource();
|
||||
this._bufferSourceNode.buffer = audioBuffer;
|
||||
this._bufferSourceNode.loop = false;
|
||||
this._url = url;
|
||||
|
||||
const gainNode = audioContext!.createGain();
|
||||
gainNode.gain.value = volume;
|
||||
|
||||
this._bufferSourceNode.connect(gainNode);
|
||||
gainNode.connect(audioContext!.destination);
|
||||
}
|
||||
|
||||
public play (): void {
|
||||
this._bufferSourceNode.start();
|
||||
this.onPlay?.();
|
||||
this._bufferSourceNode.onended = (): void => {
|
||||
audioBufferManager.tryReleasingCache(this._url);
|
||||
this._onEndCb?.();
|
||||
};
|
||||
}
|
||||
|
||||
public stop (): void {
|
||||
this._bufferSourceNode.onended = null; // stop will call ended callback
|
||||
audioBufferManager.tryReleasingCache(this._url);
|
||||
this._bufferSourceNode.stop();
|
||||
this._bufferSourceNode.disconnect();
|
||||
this._bufferSourceNode.buffer = null;
|
||||
}
|
||||
}
|
||||
|
||||
export class AudioPlayerWeb implements OperationQueueable {
|
||||
private _src: string;
|
||||
private _audioBuffer: AudioBuffer;
|
||||
private _sourceNode?: AudioBufferSourceNode;
|
||||
private _gainNode: GainNode;
|
||||
private _volume = 1;
|
||||
private _loop = false;
|
||||
private _state: AudioState = AudioState.INIT;
|
||||
private _audioTimer: AudioTimer;
|
||||
private _readyToHandleOnShow = false;
|
||||
|
||||
/**
|
||||
* @deprecated since v3.5.0, this is an engine private interface that will be removed in the future.
|
||||
*/
|
||||
public _eventTarget: EventTarget = new EventTarget();
|
||||
/**
|
||||
* @deprecated since v3.5.0, this is an engine private interface that will be removed in the future.
|
||||
*/
|
||||
public _operationQueue: OperationInfo[] = [];
|
||||
|
||||
constructor (audioBuffer: AudioBuffer, url: string) {
|
||||
this._audioBuffer = audioBuffer;
|
||||
this._audioTimer = new AudioTimer(audioBuffer);
|
||||
this._gainNode = audioContext!.createGain();
|
||||
this._gainNode.connect(audioContext!.destination);
|
||||
|
||||
this._src = url;
|
||||
// event
|
||||
game.on(Game.EVENT_PAUSE, this._onInterruptedBegin, this);
|
||||
game.on(Game.EVENT_RESUME, this._onInterruptedEnd, this);
|
||||
}
|
||||
destroy (): void {
|
||||
this._audioTimer.destroy();
|
||||
if (this._audioBuffer) {
|
||||
// NOTE: need to release AudioBuffer instance
|
||||
this._audioBuffer = null as any;
|
||||
}
|
||||
audioBufferManager.tryReleasingCache(this._src);
|
||||
game.off(Game.EVENT_PAUSE, this._onInterruptedBegin, this);
|
||||
game.off(Game.EVENT_RESUME, this._onInterruptedEnd, this);
|
||||
}
|
||||
private _onInterruptedBegin (): void {
|
||||
if (this._state === AudioState.PLAYING) {
|
||||
this.pause().then(() => {
|
||||
this._state = AudioState.INTERRUPTED;
|
||||
this._readyToHandleOnShow = true;
|
||||
this._eventTarget.emit(AudioEvent.INTERRUPTION_BEGIN);
|
||||
}).catch((e) => { debug.warn('_onInterruptedBegin error', e); });
|
||||
}
|
||||
}
|
||||
private _onInterruptedEnd (): void {
|
||||
// We don't know whether onShow or resolve callback in pause promise is called at first.
|
||||
if (!this._readyToHandleOnShow) {
|
||||
this._eventTarget.once(AudioEvent.INTERRUPTION_BEGIN, this._onInterruptedEnd, this);
|
||||
return;
|
||||
}
|
||||
if (this._state === AudioState.INTERRUPTED) {
|
||||
this.play().then(() => {
|
||||
this._eventTarget.emit(AudioEvent.INTERRUPTION_END);
|
||||
}).catch((e) => { debug.warn('_onInterruptedEnd error', e); });
|
||||
}
|
||||
this._readyToHandleOnShow = false;
|
||||
}
|
||||
static load (url: string): Promise<AudioPlayerWeb> {
|
||||
return new Promise((resolve, reject) => {
|
||||
AudioPlayerWeb.loadNative(url).then((audioBuffer) => {
|
||||
resolve(new AudioPlayerWeb(audioBuffer, url));
|
||||
}).catch(reject);
|
||||
});
|
||||
}
|
||||
static loadNative (url: string): Promise<AudioBuffer> {
|
||||
return new Promise((resolve, reject) => {
|
||||
// NOTE: maybe url is a temp path, which is not reliable.
|
||||
// need to cache the decoded audio buffer.
|
||||
const cachedAudioBuffer = audioBufferManager.getCache(url);
|
||||
if (cachedAudioBuffer) {
|
||||
audioBufferManager.retainCache(url);
|
||||
resolve(cachedAudioBuffer);
|
||||
return;
|
||||
}
|
||||
// TODO: use pal/fs
|
||||
globalThis.fsUtils.readArrayBuffer(url, (err: Error, arrayBuffer: ArrayBuffer) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
audioContext!.decodeAudioData(arrayBuffer).then((decodedAudioBuffer) => {
|
||||
audioBufferManager.addCache(url, decodedAudioBuffer);
|
||||
resolve(decodedAudioBuffer);
|
||||
}).catch(reject);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
static loadOneShotAudio (url: string, volume: number): Promise<OneShotAudioWeb> {
|
||||
return new Promise((resolve, reject) => {
|
||||
AudioPlayerWeb.loadNative(url).then((audioBuffer) => {
|
||||
// HACK: AudioPlayer should be a friend class in OneShotAudio
|
||||
const oneShotAudio = new (OneShotAudioWeb as any)(audioBuffer, volume, url);
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||
resolve(oneShotAudio);
|
||||
}).catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
get src (): string {
|
||||
return this._src;
|
||||
}
|
||||
get type (): AudioType {
|
||||
return AudioType.WEB_AUDIO;
|
||||
}
|
||||
get state (): AudioState {
|
||||
return this._state;
|
||||
}
|
||||
get loop (): boolean {
|
||||
return this._loop;
|
||||
}
|
||||
set loop (val: boolean) {
|
||||
this._loop = val;
|
||||
if (this._sourceNode) {
|
||||
this._sourceNode.loop = val;
|
||||
}
|
||||
}
|
||||
get volume (): number {
|
||||
return this._volume;
|
||||
}
|
||||
set volume (val: number) {
|
||||
val = clamp01(val);
|
||||
this._volume = val;
|
||||
this._gainNode.gain.value = val;
|
||||
}
|
||||
get duration (): number {
|
||||
return this._audioBuffer.duration;
|
||||
}
|
||||
get currentTime (): number {
|
||||
return this._audioTimer.currentTime;
|
||||
}
|
||||
|
||||
get sampleRate (): number {
|
||||
return this._audioBuffer.sampleRate;
|
||||
}
|
||||
|
||||
public getPCMData (channelIndex: number): AudioPCMDataView | undefined {
|
||||
return new AudioPCMDataView(this._audioBuffer.getChannelData(channelIndex), 1);
|
||||
}
|
||||
|
||||
@enqueueOperation
|
||||
seek (time: number): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
this._audioTimer.seek(time);
|
||||
if (this._state === AudioState.PLAYING) {
|
||||
// one AudioBufferSourceNode can't start twice
|
||||
// need to create a new one to start from the offset
|
||||
this._doPlay().then(resolve).catch((e) => { debug.warn('seek error', e); });
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@enqueueOperation
|
||||
play (): Promise<void> {
|
||||
return this._doPlay();
|
||||
}
|
||||
|
||||
// The decorated play() method can't be call in seek()
|
||||
// so we define this method to ensure that the audio seeking works.
|
||||
private _doPlay (): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
// one AudioBufferSourceNode can't start twice
|
||||
this._stopSourceNode();
|
||||
this._sourceNode = audioContext!.createBufferSource();
|
||||
this._sourceNode.buffer = this._audioBuffer;
|
||||
this._sourceNode.loop = this._loop;
|
||||
this._sourceNode.connect(this._gainNode);
|
||||
|
||||
this._sourceNode.start(0, this._audioTimer.currentTime);
|
||||
this._state = AudioState.PLAYING;
|
||||
this._audioTimer.start();
|
||||
|
||||
this._sourceNode.onended = (): void => {
|
||||
this._audioTimer.stop();
|
||||
this._eventTarget.emit(AudioEvent.ENDED);
|
||||
this._state = AudioState.INIT;
|
||||
};
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
|
||||
private _stopSourceNode (): void {
|
||||
try {
|
||||
if (this._sourceNode) {
|
||||
this._sourceNode.onended = null; // stop will call ended callback
|
||||
this._sourceNode.stop();
|
||||
this._sourceNode.disconnect();
|
||||
this._sourceNode.buffer = null;
|
||||
this._sourceNode = undefined;
|
||||
}
|
||||
} catch (e) {
|
||||
// sourceNode can't be stopped twice, especially on Safari.
|
||||
}
|
||||
}
|
||||
|
||||
@enqueueOperation
|
||||
pause (): Promise<void> {
|
||||
if (this._state !== AudioState.PLAYING || !this._sourceNode) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
this._audioTimer.pause();
|
||||
this._state = AudioState.PAUSED;
|
||||
this._stopSourceNode();
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
@enqueueOperation
|
||||
stop (): Promise<void> {
|
||||
if (!this._sourceNode) {
|
||||
this._audioTimer.stop();
|
||||
this._state = AudioState.STOPPED;
|
||||
return Promise.resolve();
|
||||
}
|
||||
this._audioTimer.stop();
|
||||
this._state = AudioState.STOPPED;
|
||||
this._stopSourceNode();
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
onInterruptionBegin (cb: () => void): void { this._eventTarget.on(AudioEvent.INTERRUPTION_BEGIN, cb); }
|
||||
offInterruptionBegin (cb?: () => void): void { this._eventTarget.off(AudioEvent.INTERRUPTION_BEGIN, cb); }
|
||||
onInterruptionEnd (cb: () => void): void { this._eventTarget.on(AudioEvent.INTERRUPTION_END, cb); }
|
||||
offInterruptionEnd (cb?: () => void): void { this._eventTarget.off(AudioEvent.INTERRUPTION_END, cb); }
|
||||
onEnded (cb: () => void): void { this._eventTarget.on(AudioEvent.ENDED, cb); }
|
||||
offEnded (cb?: () => void): void { this._eventTarget.off(AudioEvent.ENDED, cb); }
|
||||
}
|
||||
@@ -1,132 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { minigame } from 'pal/minigame';
|
||||
import { legacyCC } from '../../../cocos/core/global-exports';
|
||||
import { AudioLoadOptions, AudioType, AudioState, AudioPCMDataView } from '../type';
|
||||
import { AudioPlayerMinigame, OneShotAudioMinigame } from './player-minigame';
|
||||
import { AudioPlayerWeb, OneShotAudioWeb } from './player-web';
|
||||
|
||||
type AbstractOneShotAudio = OneShotAudioMinigame | OneShotAudioWeb;
|
||||
type AbstractAudioPlayer = AudioPlayerMinigame | AudioPlayerWeb;
|
||||
|
||||
export class OneShotAudio {
|
||||
private _audio: AbstractOneShotAudio;
|
||||
get onPlay (): (() => void) | undefined {
|
||||
return this._audio.onPlay;
|
||||
}
|
||||
set onPlay (v) {
|
||||
this._audio.onPlay = v;
|
||||
}
|
||||
|
||||
get onEnd (): (() => void) | undefined {
|
||||
return this._audio.onEnd;
|
||||
}
|
||||
set onEnd (v) {
|
||||
this._audio.onEnd = v;
|
||||
}
|
||||
|
||||
private constructor (audio: AbstractOneShotAudio) {
|
||||
this._audio = audio;
|
||||
}
|
||||
public play (): void {
|
||||
this._audio.play();
|
||||
}
|
||||
public stop (): void {
|
||||
this._audio.stop();
|
||||
}
|
||||
}
|
||||
|
||||
export class AudioPlayer {
|
||||
private _player: AbstractAudioPlayer;
|
||||
constructor (player: AbstractAudioPlayer) {
|
||||
this._player = player;
|
||||
}
|
||||
|
||||
static load (url: string, opts?: AudioLoadOptions): Promise<AudioPlayer> {
|
||||
return new Promise((resolve, reject) => {
|
||||
/*if (typeof minigame.tt === 'object' && typeof minigame.tt.getAudioContext !== 'undefined') {
|
||||
AudioPlayerWeb.load(url).then((webPlayer) => {
|
||||
resolve(new AudioPlayer(webPlayer));
|
||||
}).catch(reject);
|
||||
} else {*/
|
||||
AudioPlayerMinigame.load(url).then((minigamePlayer) => {
|
||||
resolve(new AudioPlayer(minigamePlayer));
|
||||
}).catch(reject);
|
||||
// }
|
||||
});
|
||||
}
|
||||
destroy (): void {
|
||||
this._player.destroy();
|
||||
}
|
||||
static loadNative (url: string, opts?: AudioLoadOptions): Promise<unknown> {
|
||||
/*if (typeof minigame.tt === 'object' && typeof minigame.tt.getAudioContext !== 'undefined') {
|
||||
return AudioPlayerWeb.loadNative(url);
|
||||
}*/
|
||||
return AudioPlayerMinigame.loadNative(url);
|
||||
}
|
||||
static loadOneShotAudio (url: string, volume: number, opts?: AudioLoadOptions): Promise<OneShotAudio> {
|
||||
return new Promise((resolve, reject) => {
|
||||
/*if (typeof minigame.tt === 'object' && typeof minigame.tt.getAudioContext !== 'undefined') {
|
||||
AudioPlayerWeb.loadOneShotAudio(url, volume).then((oneShotAudioWeb) => {
|
||||
// HACK: AudioPlayer should be a friend class in OneShotAudio
|
||||
resolve(new (OneShotAudio as any)(oneShotAudioWeb));
|
||||
}).catch(reject);
|
||||
} else {*/
|
||||
AudioPlayerMinigame.loadOneShotAudio(url, volume).then((oneShotAudioMinigame) => {
|
||||
// HACK: AudioPlayer should be a friend class in OneShotAudio
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||
resolve(new (OneShotAudio as any)(oneShotAudioMinigame));
|
||||
}).catch(reject);
|
||||
//}
|
||||
});
|
||||
}
|
||||
static readonly maxAudioChannel = 10;
|
||||
|
||||
get src (): string { return this._player.src; }
|
||||
get type (): AudioType { return this._player.type; }
|
||||
get state (): AudioState { return this._player.state; }
|
||||
get loop (): boolean { return this._player.loop; }
|
||||
set loop (val: boolean) { this._player.loop = val; }
|
||||
get volume (): number { return this._player.volume; }
|
||||
set volume (val: number) { this._player.volume = val; }
|
||||
get duration (): number { return this._player.duration; }
|
||||
get currentTime (): number { return this._player.currentTime; }
|
||||
get sampleRate (): number { return this._player.sampleRate; }
|
||||
getPCMData (channelIndex: number): AudioPCMDataView | undefined { return this._player.getPCMData(channelIndex); }
|
||||
seek (time: number): Promise<void> { return this._player.seek(time); }
|
||||
|
||||
play (): Promise<void> { return this._player.play(); }
|
||||
pause (): Promise<void> { return this._player.pause(); }
|
||||
stop (): Promise<void> { return this._player.stop(); }
|
||||
onInterruptionBegin (cb: () => void): void { this._player.onInterruptionBegin(cb); }
|
||||
offInterruptionBegin (cb?: () => void): void { this._player.offInterruptionBegin(cb); }
|
||||
onInterruptionEnd (cb: () => void): void { this._player.onInterruptionEnd(cb); }
|
||||
offInterruptionEnd (cb?: () => void): void { this._player.offInterruptionEnd(cb); }
|
||||
onEnded (cb: () => void): void { this._player.onEnded(cb); }
|
||||
offEnded (cb?: () => void): void { this._player.offEnded(cb); }
|
||||
}
|
||||
|
||||
// REMOVE_ME
|
||||
legacyCC.AudioPlayer = AudioPlayer;
|
||||
@@ -1,343 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { systemInfo } from 'pal/system-info';
|
||||
import { AudioType, AudioState, AudioEvent, AudioPCMDataView, AudioBufferView, AudioLoadOptions } from '../type';
|
||||
import { EventTarget } from '../../../cocos/core/event';
|
||||
import { legacyCC } from '../../../cocos/core/global-exports';
|
||||
import { clamp, clamp01 } from '../../../cocos/core';
|
||||
import { enqueueOperation, OperationInfo, OperationQueueable } from '../operation-queue';
|
||||
import { Platform } from '../../system-info/enum-type';
|
||||
import { Game, game } from '../../../cocos/game';
|
||||
|
||||
const urlCount: Record<string, number> = {};
|
||||
const audioEngine = jsb.AudioEngine;
|
||||
const INVALID_AUDIO_ID = -1;
|
||||
|
||||
enum AudioBufferFormat {
|
||||
UNKNOWN = 0,
|
||||
SIGNED_8,
|
||||
UNSIGNED_8,
|
||||
SIGNED_16,
|
||||
UNSIGNED_16,
|
||||
SIGNED_32,
|
||||
UNSIGNED_32,
|
||||
FLOAT_32,
|
||||
FLOAT_64
|
||||
}
|
||||
|
||||
interface AudioBufferInfo {
|
||||
ctor: Constructor<AudioBufferView>,
|
||||
maxValue: number;
|
||||
}
|
||||
|
||||
const bufferConstructorMap: Record<number, AudioBufferInfo | undefined> = {
|
||||
[AudioBufferFormat.UNKNOWN]: undefined,
|
||||
[AudioBufferFormat.SIGNED_8]: { ctor: Int8Array, maxValue: 127 },
|
||||
[AudioBufferFormat.UNSIGNED_8]: { ctor: Uint8Array, maxValue: 255 },
|
||||
[AudioBufferFormat.SIGNED_16]: { ctor: Int16Array, maxValue: 32767 },
|
||||
[AudioBufferFormat.UNSIGNED_16]: { ctor: Uint16Array, maxValue: 65535 },
|
||||
[AudioBufferFormat.SIGNED_32]: { ctor: Int32Array, maxValue: 2147483647 },
|
||||
[AudioBufferFormat.UNSIGNED_32]: { ctor: Uint32Array, maxValue: 4294967295 },
|
||||
// decoded float data is normalized data, so we specify the maxValue as 1.
|
||||
[AudioBufferFormat.FLOAT_32]: { ctor: Float32Array, maxValue: 1 },
|
||||
[AudioBufferFormat.FLOAT_64]: { ctor: Float64Array, maxValue: 1 },
|
||||
};
|
||||
|
||||
export class OneShotAudio {
|
||||
private _id: number = INVALID_AUDIO_ID;
|
||||
private _url: string;
|
||||
private _volume: number;
|
||||
private _onPlayCb?: () => void;
|
||||
get onPlay (): (() => void) | undefined {
|
||||
return this._onPlayCb;
|
||||
}
|
||||
set onPlay (cb) {
|
||||
this._onPlayCb = cb;
|
||||
}
|
||||
|
||||
private _onEndCb?: () => void;
|
||||
get onEnd (): (() => void) | undefined {
|
||||
return this._onEndCb;
|
||||
}
|
||||
set onEnd (cb) {
|
||||
this._onEndCb = cb;
|
||||
}
|
||||
|
||||
private constructor (url: string, volume: number) {
|
||||
this._url = url;
|
||||
this._volume = volume;
|
||||
}
|
||||
public play (): void {
|
||||
this._id = jsb.AudioEngine.play2d(this._url, false, this._volume);
|
||||
jsb.AudioEngine.setFinishCallback(this._id, () => {
|
||||
this.onEnd?.();
|
||||
});
|
||||
this.onPlay?.();
|
||||
}
|
||||
public stop (): void {
|
||||
if (this._id === INVALID_AUDIO_ID) {
|
||||
return;
|
||||
}
|
||||
jsb.AudioEngine.stop(this._id);
|
||||
}
|
||||
}
|
||||
|
||||
export class AudioPlayer implements OperationQueueable {
|
||||
private _url: string;
|
||||
private _id: number = INVALID_AUDIO_ID;
|
||||
private _state: AudioState = AudioState.INIT;
|
||||
private _pcmHeader: jsb.PCMHeader | null;
|
||||
|
||||
/**
|
||||
* @deprecated since v3.5.0, this is an engine private interface that will be removed in the future.
|
||||
*/
|
||||
public _eventTarget: EventTarget = new EventTarget();
|
||||
/**
|
||||
* @deprecated since v3.5.0, this is an engine private interface that will be removed in the future.
|
||||
*/
|
||||
public _operationQueue: OperationInfo[] = [];
|
||||
|
||||
// NOTE: we need to cache the state in case the audio id is invalid.
|
||||
private _cachedState = {
|
||||
duration: 1, // wrong value before playing
|
||||
loop: false,
|
||||
currentTime: 0,
|
||||
volume: 1,
|
||||
}
|
||||
|
||||
constructor (url: string) {
|
||||
this._url = url;
|
||||
// this._pcmHeader = audioEngine.getPCMHeader(url);
|
||||
this._pcmHeader = null;
|
||||
// event
|
||||
game.on(Game.EVENT_PAUSE, this._onInterruptedBegin, this);
|
||||
game.on(Game.EVENT_RESUME, this._onInterruptedEnd, this);
|
||||
}
|
||||
destroy (): void {
|
||||
game.off(Game.EVENT_PAUSE, this._onInterruptedBegin, this);
|
||||
game.off(Game.EVENT_RESUME, this._onInterruptedEnd, this);
|
||||
if (--urlCount[this._url] <= 0) {
|
||||
audioEngine.uncache(this._url);
|
||||
}
|
||||
}
|
||||
private _onInterruptedBegin (): void {
|
||||
if (this._state === AudioState.PLAYING) {
|
||||
this.pause().then(() => {
|
||||
this._state = AudioState.INTERRUPTED;
|
||||
this._eventTarget.emit(AudioEvent.INTERRUPTION_BEGIN);
|
||||
}).catch((e) => {});
|
||||
}
|
||||
}
|
||||
private _onInterruptedEnd (): void {
|
||||
if (this._state === AudioState.INTERRUPTED) {
|
||||
this.play().then(() => {
|
||||
this._eventTarget.emit(AudioEvent.INTERRUPTION_END);
|
||||
}).catch((e) => {});
|
||||
}
|
||||
}
|
||||
static load (url: string, opts?: AudioLoadOptions): Promise<AudioPlayer> {
|
||||
return new Promise((resolve, reject) => {
|
||||
AudioPlayer.loadNative(url, opts).then((url) => {
|
||||
resolve(new AudioPlayer(url as string));
|
||||
}).catch((err) => reject(err));
|
||||
});
|
||||
}
|
||||
static loadNative (url: string, opts?: AudioLoadOptions): Promise<unknown> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (systemInfo.platform === Platform.WIN32) {
|
||||
// NOTE: audioEngine.preload() not works well on Win32 platform.
|
||||
// Especially when there is not audio output device. But still need to preload
|
||||
audioEngine.preload(url, (isSuccess) => {
|
||||
console.debug('somehow preload success on windows');
|
||||
});
|
||||
resolve(url);
|
||||
} else {
|
||||
audioEngine.preload(url, (isSuccess) => {
|
||||
if (isSuccess) {
|
||||
resolve(url);
|
||||
} else {
|
||||
reject(new Error('load audio failed'));
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
static loadOneShotAudio (url: string, volume: number, opts?: AudioLoadOptions): Promise<OneShotAudio> {
|
||||
return new Promise((resolve, reject) => {
|
||||
AudioPlayer.loadNative(url, opts).then((url) => {
|
||||
// HACK: AudioPlayer should be a friend class in OneShotAudio
|
||||
resolve(new (OneShotAudio as any)(url, volume));
|
||||
}).catch(reject);
|
||||
});
|
||||
}
|
||||
static readonly maxAudioChannel: number = audioEngine.getMaxAudioInstance();
|
||||
|
||||
private get _isValid (): boolean {
|
||||
return this._id !== INVALID_AUDIO_ID;
|
||||
}
|
||||
|
||||
get src (): string {
|
||||
return this._url;
|
||||
}
|
||||
get type (): AudioType {
|
||||
return AudioType.NATIVE_AUDIO;
|
||||
}
|
||||
get state (): AudioState {
|
||||
return this._state;
|
||||
}
|
||||
get loop (): boolean {
|
||||
if (!this._isValid) {
|
||||
return this._cachedState.loop;
|
||||
}
|
||||
return audioEngine.isLoop(this._id);
|
||||
}
|
||||
set loop (val: boolean) {
|
||||
if (this._isValid) {
|
||||
audioEngine.setLoop(this._id, val);
|
||||
}
|
||||
this._cachedState.loop = val;
|
||||
}
|
||||
get volume (): number {
|
||||
if (!this._isValid) {
|
||||
return this._cachedState.volume;
|
||||
}
|
||||
return audioEngine.getVolume(this._id);
|
||||
}
|
||||
set volume (val: number) {
|
||||
val = clamp01(val);
|
||||
if (this._isValid) {
|
||||
audioEngine.setVolume(this._id, val);
|
||||
}
|
||||
this._cachedState.volume = val;
|
||||
}
|
||||
get duration (): number {
|
||||
if (!this._isValid) {
|
||||
return this._cachedState.duration;
|
||||
}
|
||||
return audioEngine.getDuration(this._id);
|
||||
}
|
||||
get currentTime (): number {
|
||||
if (!this._isValid) {
|
||||
return this._cachedState.currentTime;
|
||||
}
|
||||
return audioEngine.getCurrentTime(this._id);
|
||||
}
|
||||
|
||||
get sampleRate (): number {
|
||||
if (this._pcmHeader === null) {
|
||||
this._pcmHeader = jsb.AudioEngine.getPCMHeader(this._url);
|
||||
}
|
||||
return this._pcmHeader.sampleRate;
|
||||
}
|
||||
|
||||
public getPCMData (channelIndex: number): AudioPCMDataView | undefined {
|
||||
const arrayBuffer = audioEngine.getOriginalPCMBuffer(this._url, channelIndex);
|
||||
if (this._pcmHeader === null) {
|
||||
this._pcmHeader = jsb.AudioEngine.getPCMHeader(this._url);
|
||||
}
|
||||
const audioBufferInfo = bufferConstructorMap[this._pcmHeader.audioFormat];
|
||||
if (!arrayBuffer || !audioBufferInfo) {
|
||||
return undefined;
|
||||
}
|
||||
return new AudioPCMDataView(arrayBuffer, audioBufferInfo.ctor, 1 / audioBufferInfo.maxValue);
|
||||
}
|
||||
|
||||
@enqueueOperation
|
||||
seek (time: number): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
// Duration is invalid before player
|
||||
// time = clamp(time, 0, this.duration);
|
||||
if (this._isValid) {
|
||||
audioEngine.setCurrentTime(this._id, time);
|
||||
}
|
||||
this._cachedState.currentTime = time;
|
||||
return resolve();
|
||||
});
|
||||
}
|
||||
|
||||
@enqueueOperation
|
||||
play (): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
if (this._isValid) {
|
||||
if (this._state === AudioState.PAUSED || this._state === AudioState.INTERRUPTED) {
|
||||
audioEngine.resume(this._id);
|
||||
} else if (this._state === AudioState.PLAYING) {
|
||||
audioEngine.pause(this._id);
|
||||
audioEngine.setCurrentTime(this._id, 0);
|
||||
audioEngine.resume(this._id);
|
||||
}
|
||||
} else {
|
||||
this._id = audioEngine.play2d(this._url, this._cachedState.loop, this._cachedState.volume);
|
||||
if (this._isValid) {
|
||||
if (this._cachedState.currentTime !== 0) {
|
||||
audioEngine.setCurrentTime(this._id, this._cachedState.currentTime);
|
||||
this._cachedState.currentTime = 0;
|
||||
}
|
||||
audioEngine.setFinishCallback(this._id, () => {
|
||||
this._cachedState.currentTime = 0;
|
||||
this._id = INVALID_AUDIO_ID;
|
||||
this._state = AudioState.INIT;
|
||||
this._eventTarget.emit(AudioEvent.ENDED);
|
||||
});
|
||||
}
|
||||
}
|
||||
this._state = AudioState.PLAYING;
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
|
||||
@enqueueOperation
|
||||
pause (): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
if (this._isValid) {
|
||||
audioEngine.pause(this._id);
|
||||
}
|
||||
this._state = AudioState.PAUSED;
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
|
||||
@enqueueOperation
|
||||
stop (): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
if (this._isValid) {
|
||||
audioEngine.stop(this._id);
|
||||
}
|
||||
this._state = AudioState.STOPPED;
|
||||
this._id = INVALID_AUDIO_ID;
|
||||
this._cachedState.currentTime = 0;
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
onInterruptionBegin (cb: () => void): void { this._eventTarget.on(AudioEvent.INTERRUPTION_BEGIN, cb); }
|
||||
offInterruptionBegin (cb?: () => void): void { this._eventTarget.off(AudioEvent.INTERRUPTION_BEGIN, cb); }
|
||||
onInterruptionEnd (cb: () => void): void { this._eventTarget.on(AudioEvent.INTERRUPTION_END, cb); }
|
||||
offInterruptionEnd (cb?: () => void): void { this._eventTarget.off(AudioEvent.INTERRUPTION_END, cb); }
|
||||
onEnded (cb: () => void): void { this._eventTarget.on(AudioEvent.ENDED, cb); }
|
||||
offEnded (cb?: () => void): void { this._eventTarget.off(AudioEvent.ENDED, cb); }
|
||||
}
|
||||
|
||||
// REMOVE_ME
|
||||
legacyCC.AudioPlayer = AudioPlayer;
|
||||
@@ -1,246 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2025 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { systemInfo } from 'pal/system-info';
|
||||
import { AudioType, AudioState, AudioEvent, AudioPCMDataView, AudioBufferView, AudioLoadOptions } from '../type';
|
||||
import { EventTarget } from '../../../cocos/core/event';
|
||||
import { legacyCC } from '../../../cocos/core/global-exports';
|
||||
import { clamp, clamp01 } from '../../../cocos/core';
|
||||
import { enqueueOperation, OperationInfo, OperationQueueable } from '../operation-queue';
|
||||
import { Platform } from '../../system-info/enum-type';
|
||||
|
||||
const urlCount: Record<string, number> = {};
|
||||
const INVALID_AUDIO_ID = -1;
|
||||
|
||||
enum AudioBufferFormat {
|
||||
UNKNOWN = 0,
|
||||
SIGNED_8,
|
||||
UNSIGNED_8,
|
||||
SIGNED_16,
|
||||
UNSIGNED_16,
|
||||
SIGNED_32,
|
||||
UNSIGNED_32,
|
||||
FLOAT_32,
|
||||
FLOAT_64
|
||||
}
|
||||
|
||||
interface AudioBufferInfo {
|
||||
ctor: Constructor<AudioBufferView>,
|
||||
maxValue: number;
|
||||
}
|
||||
|
||||
const bufferConstructorMap: Record<number, AudioBufferInfo | undefined> = {
|
||||
[AudioBufferFormat.UNKNOWN]: undefined,
|
||||
[AudioBufferFormat.SIGNED_8]: { ctor: Int8Array, maxValue: 127 },
|
||||
[AudioBufferFormat.UNSIGNED_8]: { ctor: Uint8Array, maxValue: 255 },
|
||||
[AudioBufferFormat.SIGNED_16]: { ctor: Int16Array, maxValue: 32767 },
|
||||
[AudioBufferFormat.UNSIGNED_16]: { ctor: Uint16Array, maxValue: 65535 },
|
||||
[AudioBufferFormat.SIGNED_32]: { ctor: Int32Array, maxValue: 2147483647 },
|
||||
[AudioBufferFormat.UNSIGNED_32]: { ctor: Uint32Array, maxValue: 4294967295 },
|
||||
// decoded float data is normalized data, so we specify the maxValue as 1.
|
||||
[AudioBufferFormat.FLOAT_32]: { ctor: Float32Array, maxValue: 1 },
|
||||
[AudioBufferFormat.FLOAT_64]: { ctor: Float64Array, maxValue: 1 },
|
||||
};
|
||||
|
||||
export class OneShotAudio {
|
||||
private _id: number = INVALID_AUDIO_ID;
|
||||
private _onPlayCb?: () => void;
|
||||
get onPlay (): (() => void) | undefined {
|
||||
return this._onPlayCb;
|
||||
}
|
||||
set onPlay (cb) {
|
||||
this._onPlayCb = cb;
|
||||
}
|
||||
|
||||
private _onEndCb?: () => void;
|
||||
get onEnd (): (() => void) | undefined {
|
||||
return this._onEndCb;
|
||||
}
|
||||
set onEnd (cb) {
|
||||
this._onEndCb = cb;
|
||||
}
|
||||
|
||||
private constructor (url: string, volume: number) {
|
||||
}
|
||||
public play (): void {
|
||||
|
||||
}
|
||||
public stop (): void {
|
||||
if (this._id === INVALID_AUDIO_ID) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class AudioPlayer implements OperationQueueable {
|
||||
private _id: number = INVALID_AUDIO_ID;
|
||||
private _state: AudioState = AudioState.INIT;
|
||||
|
||||
|
||||
/**
|
||||
* @deprecated since v3.5.0, this is an engine private interface that will be removed in the future.
|
||||
*/
|
||||
public _eventTarget: EventTarget = new EventTarget();
|
||||
/**
|
||||
* @deprecated since v3.5.0, this is an engine private interface that will be removed in the future.
|
||||
*/
|
||||
public _operationQueue: OperationInfo[] = [];
|
||||
|
||||
// NOTE: we need to cache the state in case the audio id is invalid.
|
||||
private _cachedState = {
|
||||
duration: 1, // wrong value before playing
|
||||
loop: false,
|
||||
currentTime: 0,
|
||||
volume: 1,
|
||||
}
|
||||
|
||||
constructor (url: string) {
|
||||
|
||||
}
|
||||
destroy (): void {
|
||||
|
||||
}
|
||||
|
||||
static load (url: string, opts?: AudioLoadOptions): Promise<AudioPlayer> {
|
||||
return new Promise((resolve, reject) => {
|
||||
AudioPlayer.loadNative(url, opts).then((url) => {
|
||||
resolve(new AudioPlayer(url as string));
|
||||
}).catch((err) => reject(err));
|
||||
});
|
||||
}
|
||||
static loadNative (url: string, opts?: AudioLoadOptions): Promise<unknown> {
|
||||
return new Promise((resolve, reject) => {
|
||||
// Todo(qgh): We need to parse the audio file, but this feature is not yet implemented.
|
||||
console.warn("Audio file parsing is not supported.");
|
||||
resolve(url);
|
||||
});
|
||||
}
|
||||
static loadOneShotAudio (url: string, volume: number, opts?: AudioLoadOptions): Promise<OneShotAudio> {
|
||||
return new Promise((resolve, reject) => {
|
||||
AudioPlayer.loadNative(url, opts).then((url) => {
|
||||
// HACK: AudioPlayer should be a friend class in OneShotAudio
|
||||
resolve(new (OneShotAudio as any)(url, volume));
|
||||
}).catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
private get _isValid (): boolean {
|
||||
return this._id !== INVALID_AUDIO_ID;
|
||||
}
|
||||
|
||||
get src (): string {
|
||||
return "";
|
||||
}
|
||||
get type (): AudioType {
|
||||
return AudioType.NATIVE_AUDIO;
|
||||
}
|
||||
get state (): AudioState {
|
||||
return this._state;
|
||||
}
|
||||
get loop (): boolean {
|
||||
if (!this._isValid) {
|
||||
return this._cachedState.loop;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
set loop (val: boolean) {
|
||||
if (this._isValid) {
|
||||
}
|
||||
this._cachedState.loop = val;
|
||||
}
|
||||
get volume (): number {
|
||||
if (!this._isValid) {
|
||||
return this._cachedState.volume;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
set volume (val: number) {
|
||||
val = clamp01(val);
|
||||
if (this._isValid) {
|
||||
}
|
||||
this._cachedState.volume = val;
|
||||
}
|
||||
get duration (): number {
|
||||
if (!this._isValid) {
|
||||
return this._cachedState.duration;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
get currentTime (): number {
|
||||
if (!this._isValid) {
|
||||
return this._cachedState.currentTime;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
get sampleRate (): number {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public getPCMData (channelIndex: number): AudioPCMDataView | undefined {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
@enqueueOperation
|
||||
seek (time: number): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
// Duration is invalid before player
|
||||
// time = clamp(time, 0, this.duration);
|
||||
if (this._isValid) {
|
||||
}
|
||||
this._cachedState.currentTime = time;
|
||||
return resolve();
|
||||
});
|
||||
}
|
||||
|
||||
@enqueueOperation
|
||||
play (): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
|
||||
@enqueueOperation
|
||||
pause (): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
|
||||
@enqueueOperation
|
||||
stop (): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
onInterruptionBegin (cb: () => void): void { this._eventTarget.on(AudioEvent.INTERRUPTION_BEGIN, cb); }
|
||||
offInterruptionBegin (cb?: () => void): void { this._eventTarget.off(AudioEvent.INTERRUPTION_BEGIN, cb); }
|
||||
onInterruptionEnd (cb: () => void): void { this._eventTarget.on(AudioEvent.INTERRUPTION_END, cb); }
|
||||
offInterruptionEnd (cb?: () => void): void { this._eventTarget.off(AudioEvent.INTERRUPTION_END, cb); }
|
||||
onEnded (cb: () => void): void { this._eventTarget.on(AudioEvent.ENDED, cb); }
|
||||
offEnded (cb?: () => void): void { this._eventTarget.off(AudioEvent.ENDED, cb); }
|
||||
}
|
||||
|
||||
// REMOVE_ME
|
||||
legacyCC.AudioPlayer = AudioPlayer;
|
||||
@@ -1,122 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { EventTarget } from '../../cocos/core';
|
||||
|
||||
type OperationMethod = (...args: any[]) => Promise<void>;
|
||||
export interface OperationInfo {
|
||||
op: string;
|
||||
id: number;
|
||||
func: OperationMethod;
|
||||
args: any[],
|
||||
invoking: boolean,
|
||||
}
|
||||
|
||||
/** @mangle */
|
||||
export interface OperationQueueable {
|
||||
_operationQueue: OperationInfo[];
|
||||
_eventTarget: EventTarget;
|
||||
}
|
||||
|
||||
function removeUnneededCalls (instance: OperationQueueable): void {
|
||||
const size = instance._operationQueue.length;
|
||||
const tmpQueue = instance._operationQueue.slice();
|
||||
const reserveOps: OperationInfo[] = [];
|
||||
let seekSearched = false;
|
||||
for (let i = size - 1; i >= 0; i--) {
|
||||
const opInfo = tmpQueue[i];
|
||||
if (opInfo.op === 'stop') {
|
||||
reserveOps.push(opInfo);
|
||||
break;
|
||||
} else if (opInfo.op === 'seek') {
|
||||
if (!seekSearched) {
|
||||
reserveOps.push(opInfo);
|
||||
seekSearched = true;
|
||||
}
|
||||
} else if (seekSearched) {
|
||||
reserveOps.push(opInfo);
|
||||
break;
|
||||
} else if (reserveOps.length === 0) {
|
||||
reserveOps.push(opInfo);
|
||||
}
|
||||
}
|
||||
instance._operationQueue = reserveOps.reverse();
|
||||
}
|
||||
|
||||
let operationId = 0;
|
||||
function _tryCallingRecursively<T extends OperationQueueable> (target: T, opInfo: OperationInfo): void {
|
||||
if (opInfo.invoking) {
|
||||
return;
|
||||
}
|
||||
opInfo.invoking = true;
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||
opInfo.func.call(target, ...opInfo.args).then(() => {
|
||||
opInfo.invoking = false;
|
||||
target._operationQueue.shift();
|
||||
target._eventTarget.emit(opInfo.id.toString());
|
||||
target._eventTarget.off(opInfo.id.toString()); // remove the key from _eventTarget._callbackTable
|
||||
removeUnneededCalls(target);
|
||||
const nextOpInfo: OperationInfo = target._operationQueue[0];
|
||||
if (nextOpInfo) {
|
||||
_tryCallingRecursively(target, nextOpInfo);
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||
}).catch((e) => {});
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a method decorator for media player class such as Audio or Video.
|
||||
* Most of the operations in media player are asynchronous.
|
||||
* When all these asynchronous operations are called concurrently, they need to be queued.
|
||||
*
|
||||
* Note: the decorated class need to implement the interface `OperationQueueable`
|
||||
* and the decorated method should be declared as `(...args: any[]): Promise<void>`.
|
||||
*
|
||||
* When you apply `enqueueOperation` on a method, remember to provide a pure operation implementation.
|
||||
* It means that, for example, you can't call stop in the implementation of play operation,
|
||||
* because that would cause the operation deadlock.
|
||||
*/
|
||||
// eslint-disable-next-line max-len
|
||||
export function enqueueOperation<T extends OperationQueueable> (target: T, propertyKey: string, descriptor: TypedPropertyDescriptor<OperationMethod>): void {
|
||||
const originalOperation = descriptor.value!;
|
||||
// eslint-disable-next-line func-names
|
||||
descriptor.value = function (...args: any[]): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
const id = operationId++;
|
||||
const instance = this as OperationQueueable;
|
||||
// enqueue operation
|
||||
instance._operationQueue.push({
|
||||
op: propertyKey,
|
||||
id,
|
||||
func: originalOperation,
|
||||
args,
|
||||
invoking: false,
|
||||
});
|
||||
// call resolve when this operation id is finishied
|
||||
instance._eventTarget.once(id.toString(), resolve);
|
||||
const opInfo: OperationInfo = instance._operationQueue[0];
|
||||
_tryCallingRecursively(instance, opInfo);
|
||||
});
|
||||
};
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
export enum AudioEvent {
|
||||
PLAYED = 'play',
|
||||
PAUSED = 'pause',
|
||||
STOPPED = 'stop',
|
||||
SEEKED = 'seeked',
|
||||
ENDED = 'ended',
|
||||
INTERRUPTION_BEGIN = 'interruptionBegin',
|
||||
INTERRUPTION_END = 'interruptionEnd',
|
||||
USER_GESTURE = 'on_gesture', // only web needed
|
||||
}
|
||||
|
||||
export enum AudioType {
|
||||
DOM_AUDIO,
|
||||
WEB_AUDIO,
|
||||
MINIGAME_AUDIO,
|
||||
NATIVE_AUDIO,
|
||||
UNKNOWN_AUDIO,
|
||||
}
|
||||
|
||||
export interface AudioLoadOptions {
|
||||
audioLoadMode?: AudioType,
|
||||
}
|
||||
|
||||
export enum AudioState {
|
||||
INIT,
|
||||
PLAYING,
|
||||
PAUSED,
|
||||
STOPPED,
|
||||
INTERRUPTED,
|
||||
}
|
||||
|
||||
export type AudioBufferView = Int8Array | Uint8Array | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array;
|
||||
|
||||
export class AudioPCMDataView {
|
||||
private _bufferView: AudioBufferView;
|
||||
private _normalizeFactor = 1;
|
||||
|
||||
constructor (arrayBufferView: AudioBufferView, normalizeFactor: number);
|
||||
constructor (arrayBuffer: ArrayBuffer, Ctor: Constructor<AudioBufferView>, normalizeFactor: number);
|
||||
constructor (...args: any[]) {
|
||||
if (args.length === 2) {
|
||||
this._bufferView = args[0] as AudioBufferView;
|
||||
this._normalizeFactor = args[1] as number;
|
||||
} else {
|
||||
const arrayBuffer = args[0] as ArrayBuffer;
|
||||
const Ctor = args[1] as Constructor<AudioBufferView>;
|
||||
const normalizeFactor = args[2] as number;
|
||||
this._bufferView = new Ctor(arrayBuffer);
|
||||
this._normalizeFactor = normalizeFactor;
|
||||
}
|
||||
}
|
||||
|
||||
get length (): number {
|
||||
return this._bufferView.length;
|
||||
}
|
||||
|
||||
public getData (offset: number): number {
|
||||
return this._bufferView[offset] * this._normalizeFactor;
|
||||
}
|
||||
}
|
||||
@@ -1,277 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { systemInfo } from 'pal/system-info';
|
||||
import { AudioEvent, AudioState, AudioPCMDataView, AudioType } from '../type';
|
||||
import { EventTarget } from '../../../cocos/core/event';
|
||||
import { clamp, clamp01 } from '../../../cocos/core';
|
||||
import { enqueueOperation, OperationInfo, OperationQueueable } from '../operation-queue';
|
||||
import { BrowserType, OS } from '../../system-info/enum-type';
|
||||
import { Game, game } from '../../../cocos/game';
|
||||
|
||||
function ensurePlaying (domAudio: HTMLAudioElement): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
const promise = domAudio.play();
|
||||
if (promise === undefined) { // Chrome50/Firefox53 below
|
||||
return resolve();
|
||||
}
|
||||
promise.then(resolve).catch((): void => {
|
||||
const onGesture = (): void => {
|
||||
domAudio.play().then(() => {
|
||||
// HACK NOTE: if the user slide after touch start, the context cannot be resumed correctly.
|
||||
canvas?.removeEventListener('touchend', onGesture, { capture: true });
|
||||
canvas?.removeEventListener('mouseup', onGesture, { capture: true });
|
||||
}).catch((e) => {});
|
||||
resolve();
|
||||
};
|
||||
const canvas = document.getElementById('GameCanvas') as HTMLCanvasElement;
|
||||
canvas?.addEventListener('touchend', onGesture, { capture: true });
|
||||
canvas?.addEventListener('mouseup', onGesture, { capture: true });
|
||||
});
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
export class OneShotAudioDOM {
|
||||
private _domAudio: HTMLAudioElement;
|
||||
private _onPlayCb?: () => void;
|
||||
get onPlay (): (() => void) | undefined {
|
||||
return this._onPlayCb;
|
||||
}
|
||||
set onPlay (cb) {
|
||||
this._onPlayCb = cb;
|
||||
}
|
||||
|
||||
private _onEndCb?: () => void;
|
||||
get onEnd (): (() => void) | undefined {
|
||||
return this._onEndCb;
|
||||
}
|
||||
set onEnd (cb) {
|
||||
if (this._onEndCb) {
|
||||
this._domAudio.removeEventListener('ended', this._onEndCb);
|
||||
}
|
||||
this._onEndCb = cb;
|
||||
if (cb) {
|
||||
this._domAudio.addEventListener('ended', cb);
|
||||
}
|
||||
}
|
||||
|
||||
private constructor (nativeAudio: HTMLAudioElement, volume: number) {
|
||||
this._domAudio = nativeAudio;
|
||||
nativeAudio.volume = volume;
|
||||
}
|
||||
public play (): void {
|
||||
ensurePlaying(this._domAudio).then(() => {
|
||||
this.onPlay?.();
|
||||
}).catch((e) => {});
|
||||
}
|
||||
public stop (): void {
|
||||
this._domAudio.pause();
|
||||
}
|
||||
}
|
||||
|
||||
export class AudioPlayerDOM implements OperationQueueable {
|
||||
private _domAudio: HTMLAudioElement;
|
||||
private _state: AudioState = AudioState.INIT;
|
||||
private _onEnded: () => void;
|
||||
|
||||
/**
|
||||
* @deprecated since v3.5.0, this is an engine private interface that will be removed in the future.
|
||||
*/
|
||||
public _eventTarget: EventTarget = new EventTarget();
|
||||
/**
|
||||
* @deprecated since v3.5.0, this is an engine private interface that will be removed in the future.
|
||||
*/
|
||||
public _operationQueue: OperationInfo[] = [];
|
||||
|
||||
constructor (nativeAudio: HTMLAudioElement) {
|
||||
this._domAudio = nativeAudio;
|
||||
|
||||
// event
|
||||
game.on(Game.EVENT_PAUSE, this._onInterruptedBegin, this);
|
||||
game.on(Game.EVENT_RESUME, this._onInterruptedEnd, this);
|
||||
this._onEnded = (): void => {
|
||||
this.seek(0).catch((e) => {});
|
||||
this._state = AudioState.INIT;
|
||||
this._eventTarget.emit(AudioEvent.ENDED);
|
||||
};
|
||||
this._domAudio.addEventListener('ended', this._onEnded);
|
||||
}
|
||||
|
||||
destroy (): void {
|
||||
game.off(Game.EVENT_PAUSE, this._onInterruptedBegin, this);
|
||||
game.off(Game.EVENT_RESUME, this._onInterruptedEnd, this);
|
||||
this._domAudio.removeEventListener('ended', this._onEnded);
|
||||
// NOTE: need to release DOM Audio instance
|
||||
this._domAudio = null as any;
|
||||
}
|
||||
static load (url: string): Promise<AudioPlayerDOM> {
|
||||
return new Promise((resolve, reject) => {
|
||||
AudioPlayerDOM.loadNative(url).then((domAudio) => {
|
||||
resolve(new AudioPlayerDOM(domAudio));
|
||||
}).catch(reject);
|
||||
});
|
||||
}
|
||||
static loadNative (url: string): Promise<HTMLAudioElement> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const domAudio = document.createElement('audio');
|
||||
let loadedEvent = 'canplaythrough';
|
||||
if (systemInfo.os === OS.IOS) {
|
||||
// iOS no event that used to parse completed callback
|
||||
// this time is not complete, can not play
|
||||
loadedEvent = 'loadedmetadata';
|
||||
} else if (systemInfo.browserType === BrowserType.FIREFOX) {
|
||||
loadedEvent = 'canplay';
|
||||
}
|
||||
|
||||
const timer = setTimeout(() => {
|
||||
if (domAudio.readyState === 0) {
|
||||
failure();
|
||||
} else {
|
||||
success();
|
||||
}
|
||||
}, 8000);
|
||||
const clearEvent = (): void => {
|
||||
clearTimeout(timer);
|
||||
domAudio.removeEventListener(loadedEvent, success, false);
|
||||
domAudio.removeEventListener('error', failure, false);
|
||||
};
|
||||
const success = (): void => {
|
||||
clearEvent();
|
||||
resolve(domAudio);
|
||||
};
|
||||
const failure = (): void => {
|
||||
clearEvent();
|
||||
const message = `load audio failure - ${url}`;
|
||||
reject(new Error(message));
|
||||
};
|
||||
domAudio.addEventListener(loadedEvent, success, false);
|
||||
domAudio.addEventListener('error', failure, false);
|
||||
domAudio.src = url;
|
||||
});
|
||||
}
|
||||
static loadOneShotAudio (url: string, volume: number): Promise<OneShotAudioDOM> {
|
||||
return new Promise((resolve, reject) => {
|
||||
AudioPlayerDOM.loadNative(url).then((domAudio) => {
|
||||
// HACK: AudioPlayer should be a friend class in OneShotAudio
|
||||
const oneShotAudio = new (OneShotAudioDOM as any)(domAudio, volume);
|
||||
resolve(oneShotAudio);
|
||||
}).catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
private _onInterruptedBegin (): void {
|
||||
if (this._state === AudioState.PLAYING) {
|
||||
this.pause().then(() => {
|
||||
this._state = AudioState.INTERRUPTED;
|
||||
this._eventTarget.emit(AudioEvent.INTERRUPTION_BEGIN);
|
||||
}).catch((e) => {});
|
||||
}
|
||||
}
|
||||
private _onInterruptedEnd (): void {
|
||||
if (this._state === AudioState.INTERRUPTED) {
|
||||
this.play().then(() => {
|
||||
this._eventTarget.emit(AudioEvent.INTERRUPTION_END);
|
||||
}).catch((e) => {});
|
||||
}
|
||||
}
|
||||
|
||||
get src (): string {
|
||||
return this._domAudio ? this._domAudio.src : '';
|
||||
}
|
||||
get type (): AudioType {
|
||||
return AudioType.DOM_AUDIO;
|
||||
}
|
||||
get state (): AudioState {
|
||||
return this._state;
|
||||
}
|
||||
get loop (): boolean {
|
||||
return this._domAudio.loop;
|
||||
}
|
||||
set loop (val: boolean) {
|
||||
this._domAudio.loop = val;
|
||||
}
|
||||
get volume (): number {
|
||||
return this._domAudio.volume;
|
||||
}
|
||||
set volume (val: number) {
|
||||
val = clamp01(val);
|
||||
this._domAudio.volume = val;
|
||||
}
|
||||
get duration (): number {
|
||||
return this._domAudio.duration;
|
||||
}
|
||||
get currentTime (): number {
|
||||
return this._domAudio.currentTime;
|
||||
}
|
||||
|
||||
get sampleRate (): number {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public getPCMData (channelIndex: number): AudioPCMDataView | undefined {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
@enqueueOperation
|
||||
seek (time: number): Promise<void> {
|
||||
time = clamp(time, 0, this.duration);
|
||||
this._domAudio.currentTime = time;
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
@enqueueOperation
|
||||
play (): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
ensurePlaying(this._domAudio).then(() => {
|
||||
this._state = AudioState.PLAYING;
|
||||
resolve();
|
||||
}).catch((e) => {});
|
||||
});
|
||||
}
|
||||
|
||||
@enqueueOperation
|
||||
pause (): Promise<void> {
|
||||
this._domAudio.pause();
|
||||
this._state = AudioState.PAUSED;
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
@enqueueOperation
|
||||
stop (): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
this._domAudio.pause();
|
||||
this._domAudio.currentTime = 0;
|
||||
this._state = AudioState.STOPPED;
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
|
||||
onInterruptionBegin (cb: () => void): void { this._eventTarget.on(AudioEvent.INTERRUPTION_BEGIN, cb); }
|
||||
offInterruptionBegin (cb?: () => void): void { this._eventTarget.off(AudioEvent.INTERRUPTION_BEGIN, cb); }
|
||||
onInterruptionEnd (cb: () => void): void { this._eventTarget.on(AudioEvent.INTERRUPTION_END, cb); }
|
||||
offInterruptionEnd (cb?: () => void): void { this._eventTarget.off(AudioEvent.INTERRUPTION_END, cb); }
|
||||
onEnded (cb: () => void): void { this._eventTarget.on(AudioEvent.ENDED, cb); }
|
||||
offEnded (cb?: () => void): void { this._eventTarget.off(AudioEvent.ENDED, cb); }
|
||||
}
|
||||
@@ -1,493 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { EDITOR_NOT_IN_PREVIEW } from 'internal:constants';
|
||||
import { AudioPCMDataView, AudioEvent, AudioState, AudioType } from '../type';
|
||||
import { EventTarget } from '../../../cocos/core/event';
|
||||
import { clamp01 } from '../../../cocos/core';
|
||||
import * as debug from '../../../cocos/core/platform/debug';
|
||||
import { enqueueOperation, OperationInfo, OperationQueueable } from '../operation-queue';
|
||||
import AudioTimer from '../audio-timer';
|
||||
import { audioBufferManager } from '../audio-buffer-manager';
|
||||
import { Game, game } from '../../../cocos/game';
|
||||
|
||||
// NOTE: fix CI
|
||||
const AudioContextClass = (window.AudioContext || window.webkitAudioContext || window.mozAudioContext);
|
||||
const _contextRunningEvent = 'on-context-running';
|
||||
|
||||
export class AudioContextAgent {
|
||||
public static support = !!AudioContextClass;
|
||||
|
||||
private _eventTarget: EventTarget;
|
||||
private _context: AudioContext;
|
||||
private _isRunning = false;
|
||||
constructor () {
|
||||
this._context = new (window.AudioContext || window.webkitAudioContext || window.mozAudioContext)();
|
||||
this._eventTarget = new EventTarget();
|
||||
this._context.onstatechange = (): void => {
|
||||
if (this._context.state === 'running') {
|
||||
this._isRunning = true;
|
||||
this._eventTarget.emit(_contextRunningEvent);
|
||||
} else {
|
||||
this._isRunning = false;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
get isRunning (): boolean {
|
||||
return this._isRunning;
|
||||
}
|
||||
|
||||
get currentTime (): number {
|
||||
return this._context.currentTime;
|
||||
}
|
||||
|
||||
public onceRunning (cb: (...args: any[]) => void, target?: any): void {
|
||||
this._eventTarget.once(_contextRunningEvent, cb, target);
|
||||
}
|
||||
|
||||
public offRunning (cb?: (...args: any[]) => void, target?: any): void {
|
||||
this._eventTarget.off(_contextRunningEvent, cb, target);
|
||||
}
|
||||
|
||||
public decodeAudioData (audioData: ArrayBuffer): Promise<AudioBuffer> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const promise = this._context.decodeAudioData(audioData, (audioBuffer) => {
|
||||
resolve(audioBuffer);
|
||||
}, (err) => {
|
||||
// TODO: need to reject the error.
|
||||
// eslint-disable-next-line no-console
|
||||
console.error('failed to load Web Audio', err);
|
||||
});
|
||||
promise?.catch(reject); // Safari doesn't support the promise based decodeAudioData
|
||||
});
|
||||
}
|
||||
|
||||
public runContext (): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
if (this.isRunning) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
const context = this._context;
|
||||
if (!context.resume) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
// only state is suspended can involve resume function
|
||||
if (context.state === 'suspended') {
|
||||
context.resume().catch((e) => { debug.warn('runContext error', e); });
|
||||
} else if (context.state === 'running') {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
// Force running audio context if state is not 'running', may be 'suspended' or 'interrupted'.
|
||||
const canvas = document.getElementById('GameCanvas') as HTMLCanvasElement;
|
||||
// HACK NOTE: if the user slide after touch start, the context cannot be resumed correctly.
|
||||
const onGesture = (): void => {
|
||||
context.resume().then(() => {
|
||||
canvas?.removeEventListener('touchend', onGesture, { capture: true });
|
||||
canvas?.removeEventListener('mouseup', onGesture, { capture: true });
|
||||
resolve();
|
||||
}).catch((e) => { debug.warn('onGesture resume error', e); });
|
||||
};
|
||||
canvas?.addEventListener('touchend', onGesture, { capture: true });
|
||||
canvas?.addEventListener('mouseup', onGesture, { capture: true });
|
||||
});
|
||||
}
|
||||
|
||||
public createBufferSource (audioBuffer?: AudioBuffer, loop?: boolean): AudioBufferSourceNode {
|
||||
const sourceBufferNode = this._context.createBufferSource();
|
||||
if (audioBuffer !== undefined) {
|
||||
sourceBufferNode.buffer = audioBuffer;
|
||||
}
|
||||
if (loop !== undefined) {
|
||||
sourceBufferNode.loop = loop;
|
||||
}
|
||||
return sourceBufferNode;
|
||||
}
|
||||
|
||||
public createGain (volume = 1): GainNode {
|
||||
const gainNode = this._context.createGain();
|
||||
this.setGainValue(gainNode, volume);
|
||||
return gainNode;
|
||||
}
|
||||
|
||||
public setGainValue (gain: GainNode, volume: number): void {
|
||||
if (gain.gain.setTargetAtTime) {
|
||||
try {
|
||||
gain.gain.setTargetAtTime(volume, this._context.currentTime, 0);
|
||||
} catch (e) {
|
||||
// Some unknown browsers may crash if timeConstant is 0
|
||||
gain.gain.setTargetAtTime(volume, this._context.currentTime, 0.01);
|
||||
}
|
||||
} else {
|
||||
gain.gain.value = volume;
|
||||
}
|
||||
}
|
||||
|
||||
public connectContext (audioNode: GainNode): void {
|
||||
if (!this._context) {
|
||||
return;
|
||||
}
|
||||
audioNode.connect(this._context.destination);
|
||||
}
|
||||
}
|
||||
|
||||
let audioContextAgent: AudioContextAgent | undefined;
|
||||
if (AudioContextAgent.support) {
|
||||
audioContextAgent = new AudioContextAgent();
|
||||
}
|
||||
|
||||
export class OneShotAudioWeb {
|
||||
private _duration: number;
|
||||
private _bufferSourceNode: AudioBufferSourceNode;
|
||||
private _onPlayCb?: () => void;
|
||||
private _currentTimer = 0;
|
||||
private _url: string;
|
||||
|
||||
get onPlay (): (() => void) | undefined {
|
||||
return this._onPlayCb;
|
||||
}
|
||||
set onPlay (cb) {
|
||||
this._onPlayCb = cb;
|
||||
}
|
||||
|
||||
private _onEndCb?: () => void;
|
||||
get onEnd (): (() => void) | undefined {
|
||||
return this._onEndCb;
|
||||
}
|
||||
set onEnd (cb) {
|
||||
this._onEndCb = cb;
|
||||
}
|
||||
|
||||
private constructor (audioBuffer: AudioBuffer, volume: number, url: string) {
|
||||
this._duration = audioBuffer.duration;
|
||||
this._url = url;
|
||||
this._bufferSourceNode = audioContextAgent!.createBufferSource(audioBuffer, false);
|
||||
const gainNode = audioContextAgent!.createGain(volume);
|
||||
this._bufferSourceNode.connect(gainNode);
|
||||
audioContextAgent!.connectContext(gainNode);
|
||||
}
|
||||
|
||||
public play (): void {
|
||||
if (EDITOR_NOT_IN_PREVIEW) {
|
||||
return;
|
||||
}
|
||||
this._bufferSourceNode.start();
|
||||
// audioContextAgent does exist
|
||||
audioContextAgent!.runContext().then(() => {
|
||||
this.onPlay?.();
|
||||
this._currentTimer = window.setTimeout(() => {
|
||||
audioBufferManager.tryReleasingCache(this._url);
|
||||
this.onEnd?.();
|
||||
}, this._duration * 1000);
|
||||
}).catch((e) => { debug.warn('play error', e); });
|
||||
}
|
||||
|
||||
public stop (): void {
|
||||
clearTimeout(this._currentTimer);
|
||||
audioBufferManager.tryReleasingCache(this._url);
|
||||
this._bufferSourceNode.stop();
|
||||
this._bufferSourceNode.disconnect();
|
||||
this._bufferSourceNode.buffer = null;
|
||||
}
|
||||
}
|
||||
|
||||
export class AudioPlayerWeb implements OperationQueueable {
|
||||
private _src: string;
|
||||
private _audioBuffer: AudioBuffer;
|
||||
private _sourceNode?: AudioBufferSourceNode;
|
||||
private _gainNode: GainNode;
|
||||
private _currentTimer = 0;
|
||||
private _volume = 1;
|
||||
private _loop = false;
|
||||
private _state: AudioState = AudioState.INIT;
|
||||
private _audioTimer: AudioTimer;
|
||||
private _runningCallback?: () => void;
|
||||
|
||||
/**
|
||||
* @deprecated since v3.5.0, this is an engine private interface that will be removed in the future.
|
||||
*/
|
||||
public _eventTarget: EventTarget = new EventTarget();
|
||||
/**
|
||||
* @deprecated since v3.5.0, this is an engine private interface that will be removed in the future.
|
||||
*/
|
||||
public _operationQueue: OperationInfo[] = [];
|
||||
|
||||
constructor (audioBuffer: AudioBuffer, url: string) {
|
||||
this._audioBuffer = audioBuffer;
|
||||
this._audioTimer = new AudioTimer(audioBuffer);
|
||||
this._gainNode = audioContextAgent!.createGain();
|
||||
audioContextAgent!.connectContext(this._gainNode);
|
||||
this._src = url;
|
||||
// event
|
||||
game.on(Game.EVENT_PAUSE, this._onInterruptedBegin, this);
|
||||
game.on(Game.EVENT_RESUME, this._onInterruptedEnd, this);
|
||||
}
|
||||
destroy (): void {
|
||||
window.clearTimeout(this._currentTimer);
|
||||
this._audioTimer.destroy();
|
||||
if (this._audioBuffer) {
|
||||
// NOTE: need to release AudioBuffer instance
|
||||
this._audioBuffer = null as any;
|
||||
}
|
||||
audioBufferManager.tryReleasingCache(this._src);
|
||||
game.off(Game.EVENT_PAUSE, this._onInterruptedBegin, this);
|
||||
game.off(Game.EVENT_RESUME, this._onInterruptedEnd, this);
|
||||
this.offRunning();
|
||||
}
|
||||
static load (url: string): Promise<AudioPlayerWeb> {
|
||||
return new Promise((resolve, reject) => {
|
||||
AudioPlayerWeb.loadNative(url).then((audioBuffer) => {
|
||||
resolve(new AudioPlayerWeb(audioBuffer, url));
|
||||
}).catch(reject);
|
||||
});
|
||||
}
|
||||
static loadNative (url: string): Promise<AudioBuffer> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const cachedAudioBuffer = audioBufferManager.getCache(url);
|
||||
if (cachedAudioBuffer) {
|
||||
audioBufferManager.retainCache(url);
|
||||
resolve(cachedAudioBuffer);
|
||||
return;
|
||||
}
|
||||
const xhr = new XMLHttpRequest();
|
||||
const errInfo = `load audio failed: ${url}, status: `;
|
||||
xhr.open('GET', url, true);
|
||||
xhr.responseType = 'arraybuffer';
|
||||
|
||||
xhr.onload = (): void => {
|
||||
if (xhr.status === 200 || xhr.status === 0) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||
audioContextAgent!.decodeAudioData(xhr.response).then((decodedAudioBuffer) => {
|
||||
audioBufferManager.addCache(url, decodedAudioBuffer);
|
||||
resolve(decodedAudioBuffer);
|
||||
}).catch((e) => { debug.warn('loadNative error', url, e); });
|
||||
} else {
|
||||
reject(new Error(`${errInfo}${xhr.status}(no response)`));
|
||||
}
|
||||
};
|
||||
xhr.onerror = (): void => { reject(new Error(`${errInfo}${xhr.status}(error)`)); };
|
||||
xhr.ontimeout = (): void => { reject(new Error(`${errInfo}${xhr.status}(time out)`)); };
|
||||
xhr.onabort = (): void => { reject(new Error(`${errInfo}${xhr.status}(abort)`)); };
|
||||
|
||||
xhr.send(null);
|
||||
});
|
||||
}
|
||||
static loadOneShotAudio (url: string, volume: number): Promise<OneShotAudioWeb> {
|
||||
return new Promise((resolve, reject) => {
|
||||
AudioPlayerWeb.loadNative(url).then((audioBuffer) => {
|
||||
// HACK: AudioPlayer should be a friend class in OneShotAudio
|
||||
const oneShotAudio = new (OneShotAudioWeb as any)(audioBuffer, volume, url);
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||
resolve(oneShotAudio);
|
||||
}).catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
get sampleRate (): number {
|
||||
return this._audioBuffer.sampleRate;
|
||||
}
|
||||
|
||||
public getPCMData (channelIndex: number): AudioPCMDataView | undefined {
|
||||
return new AudioPCMDataView(this._audioBuffer.getChannelData(channelIndex), 1);
|
||||
}
|
||||
|
||||
private _onInterruptedBegin (): void {
|
||||
if (this._state === AudioState.PLAYING) {
|
||||
this.pause().then(() => {
|
||||
this._state = AudioState.INTERRUPTED;
|
||||
this._eventTarget.emit(AudioEvent.INTERRUPTION_BEGIN);
|
||||
}).catch((e) => { debug.warn('_onInterruptedBegin error', e); });
|
||||
}
|
||||
}
|
||||
private _onInterruptedEnd (): void {
|
||||
if (this._state === AudioState.INTERRUPTED) {
|
||||
this.play().then(() => {
|
||||
this._eventTarget.emit(AudioEvent.INTERRUPTION_END);
|
||||
}).catch((e) => { debug.warn('_onInterruptedEnd error', e); });
|
||||
}
|
||||
}
|
||||
|
||||
get src (): string {
|
||||
return this._src;
|
||||
}
|
||||
get type (): AudioType {
|
||||
return AudioType.WEB_AUDIO;
|
||||
}
|
||||
get state (): AudioState {
|
||||
return this._state;
|
||||
}
|
||||
get loop (): boolean {
|
||||
return this._loop;
|
||||
}
|
||||
set loop (val: boolean) {
|
||||
this._loop = val;
|
||||
if (this._sourceNode) {
|
||||
this._sourceNode.loop = val;
|
||||
}
|
||||
}
|
||||
get volume (): number {
|
||||
return this._volume;
|
||||
}
|
||||
set volume (val: number) {
|
||||
val = clamp01(val);
|
||||
this._volume = val;
|
||||
audioContextAgent!.setGainValue(this._gainNode, val);
|
||||
}
|
||||
get duration (): number {
|
||||
return this._audioBuffer.duration;
|
||||
}
|
||||
get currentTime (): number {
|
||||
return this._audioTimer.currentTime;
|
||||
}
|
||||
private offRunning (): void {
|
||||
if (this._runningCallback) {
|
||||
audioContextAgent!.offRunning(this._runningCallback);
|
||||
this._runningCallback = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
@enqueueOperation
|
||||
seek (time: number): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
this.offRunning();
|
||||
this._audioTimer.seek(time);
|
||||
if (this._state === AudioState.PLAYING) {
|
||||
// one AudioBufferSourceNode can't start twice
|
||||
// need to create a new one to start from the offset
|
||||
this._doPlay().then(resolve).catch((e) => { debug.warn('seek error', e); });
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@enqueueOperation
|
||||
play (): Promise<void> {
|
||||
this.offRunning();
|
||||
if (EDITOR_NOT_IN_PREVIEW) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
return this._doPlay();
|
||||
}
|
||||
|
||||
// The decorated play() method can't be call in seek()
|
||||
// so we define this method to ensure that the audio seeking works.
|
||||
private _doPlay (): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
if (audioContextAgent!.isRunning) {
|
||||
this._startSourceNode();
|
||||
resolve();
|
||||
} else {
|
||||
this.offRunning();
|
||||
this._runningCallback = (): void => {
|
||||
this._startSourceNode();
|
||||
resolve();
|
||||
};
|
||||
// Running event may be emit when:
|
||||
// - manually resume audio context.
|
||||
// - system automatically resume audio context when enter foreground from background.
|
||||
audioContextAgent!.onceRunning(this._runningCallback);
|
||||
// Ensure resume context.
|
||||
audioContextAgent!.runContext().catch((e) => { debug.warn('doPlay error', e); });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private _startSourceNode (): void {
|
||||
// one AudioBufferSourceNode can't start twice
|
||||
this._stopSourceNode();
|
||||
this._sourceNode = audioContextAgent!.createBufferSource(this._audioBuffer, this.loop);
|
||||
this._sourceNode.connect(this._gainNode);
|
||||
this._sourceNode.loop = this._loop;
|
||||
this._sourceNode.start(0, this._audioTimer.currentTime);
|
||||
this._state = AudioState.PLAYING;
|
||||
this._audioTimer.start();
|
||||
|
||||
/* still not supported by all platforms *
|
||||
this._sourceNode.onended = this._onEnded;
|
||||
/* doing it manually for now */
|
||||
const checkEnded = (): void => {
|
||||
if (this.loop) {
|
||||
this._currentTimer = window.setTimeout(checkEnded, this._audioBuffer.duration * 1000);
|
||||
} else { // do ended
|
||||
this._audioTimer.stop();
|
||||
this._eventTarget.emit(AudioEvent.ENDED);
|
||||
this._state = AudioState.INIT;
|
||||
}
|
||||
};
|
||||
window.clearTimeout(this._currentTimer);
|
||||
this._currentTimer = window.setTimeout(checkEnded, (this._audioBuffer.duration - this._audioTimer.currentTime) * 1000);
|
||||
}
|
||||
|
||||
private _stopSourceNode (): void {
|
||||
try {
|
||||
if (this._sourceNode) {
|
||||
this._sourceNode.stop();
|
||||
this._sourceNode.disconnect();
|
||||
this._sourceNode.buffer = null;
|
||||
this._sourceNode = undefined;
|
||||
}
|
||||
} catch (e) {
|
||||
// sourceNode can't be stopped twice, especially on Safari.
|
||||
}
|
||||
}
|
||||
|
||||
@enqueueOperation
|
||||
pause (): Promise<void> {
|
||||
this.offRunning();
|
||||
if (this._state !== AudioState.PLAYING || !this._sourceNode) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
this._audioTimer.pause();
|
||||
this._state = AudioState.PAUSED;
|
||||
window.clearTimeout(this._currentTimer);
|
||||
this._stopSourceNode();
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
@enqueueOperation
|
||||
stop (): Promise<void> {
|
||||
this.offRunning();
|
||||
if (!this._sourceNode) {
|
||||
this._audioTimer.stop();
|
||||
this._state = AudioState.STOPPED;
|
||||
return Promise.resolve();
|
||||
}
|
||||
this._audioTimer.stop();
|
||||
this._state = AudioState.STOPPED;
|
||||
window.clearTimeout(this._currentTimer);
|
||||
this._stopSourceNode();
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
onInterruptionBegin (cb: () => void): void { this._eventTarget.on(AudioEvent.INTERRUPTION_BEGIN, cb); }
|
||||
offInterruptionBegin (cb?: () => void): void { this._eventTarget.off(AudioEvent.INTERRUPTION_BEGIN, cb); }
|
||||
onInterruptionEnd (cb: () => void): void { this._eventTarget.on(AudioEvent.INTERRUPTION_END, cb); }
|
||||
offInterruptionEnd (cb?: () => void): void { this._eventTarget.off(AudioEvent.INTERRUPTION_END, cb); }
|
||||
onEnded (cb: () => void): void { this._eventTarget.on(AudioEvent.ENDED, cb); }
|
||||
offEnded (cb?: () => void): void { this._eventTarget.off(AudioEvent.ENDED, cb); }
|
||||
}
|
||||
@@ -1,130 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { warnID } from '../../../cocos/core';
|
||||
import { AudioLoadOptions, AudioType, AudioState, AudioPCMDataView } from '../type';
|
||||
import { AudioPlayerDOM, OneShotAudioDOM } from './player-dom';
|
||||
import { AudioContextAgent, AudioPlayerWeb, OneShotAudioWeb } from './player-web';
|
||||
|
||||
type AbstractOneShotAudio = OneShotAudioDOM | OneShotAudioWeb;
|
||||
type AbstractAudioPlayer = AudioPlayerDOM | AudioPlayerWeb;
|
||||
|
||||
export class OneShotAudio {
|
||||
private _audio: AbstractOneShotAudio;
|
||||
get onPlay (): (() => void) | undefined {
|
||||
return this._audio.onPlay;
|
||||
}
|
||||
set onPlay (v) {
|
||||
this._audio.onPlay = v;
|
||||
}
|
||||
|
||||
get onEnd (): (() => void) | undefined {
|
||||
return this._audio.onEnd;
|
||||
}
|
||||
set onEnd (v) {
|
||||
this._audio.onEnd = v;
|
||||
}
|
||||
|
||||
private constructor (audio: AbstractOneShotAudio) {
|
||||
this._audio = audio;
|
||||
}
|
||||
public play (): void {
|
||||
this._audio.play();
|
||||
}
|
||||
public stop (): void {
|
||||
this._audio.stop();
|
||||
}
|
||||
}
|
||||
|
||||
export class AudioPlayer {
|
||||
private _player: AbstractAudioPlayer;
|
||||
constructor (player: AbstractAudioPlayer) {
|
||||
this._player = player;
|
||||
}
|
||||
|
||||
static load (url: string, opts?: AudioLoadOptions): Promise<AudioPlayer> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (opts?.audioLoadMode === AudioType.DOM_AUDIO || !AudioContextAgent.support) {
|
||||
if (!AudioContextAgent.support) { warnID(5201); }
|
||||
AudioPlayerDOM.load(url).then((domPlayer) => {
|
||||
resolve(new AudioPlayer(domPlayer));
|
||||
}).catch(reject);
|
||||
} else {
|
||||
AudioPlayerWeb.load(url).then((webPlayer) => {
|
||||
resolve(new AudioPlayer(webPlayer));
|
||||
}).catch(reject);
|
||||
}
|
||||
});
|
||||
}
|
||||
destroy (): void {
|
||||
this._player.destroy();
|
||||
}
|
||||
static loadNative (url: string, opts?: AudioLoadOptions): Promise<unknown> {
|
||||
if (opts?.audioLoadMode === AudioType.DOM_AUDIO || !AudioContextAgent.support) {
|
||||
if (!AudioContextAgent.support) { warnID(5201); }
|
||||
return AudioPlayerDOM.loadNative(url);
|
||||
}
|
||||
return AudioPlayerWeb.loadNative(url);
|
||||
}
|
||||
static loadOneShotAudio (url: string, volume: number, opts?: AudioLoadOptions): Promise<OneShotAudio> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (opts?.audioLoadMode === AudioType.DOM_AUDIO || !AudioContextAgent.support) {
|
||||
if (!AudioContextAgent.support) { warnID(5201); }
|
||||
AudioPlayerDOM.loadOneShotAudio(url, volume).then((oneShotAudioDOM) => {
|
||||
// HACK: AudioPlayer should be a friend class in OneShotAudio
|
||||
resolve(new (OneShotAudio as any)(oneShotAudioDOM));
|
||||
}).catch(reject);
|
||||
} else {
|
||||
AudioPlayerWeb.loadOneShotAudio(url, volume).then((oneShotAudioWeb) => {
|
||||
// HACK: AudioPlayer should be a friend class in OneShotAudio
|
||||
resolve(new (OneShotAudio as any)(oneShotAudioWeb));
|
||||
}).catch(reject);
|
||||
}
|
||||
});
|
||||
}
|
||||
static readonly maxAudioChannel = 24;
|
||||
|
||||
get src (): string { return this._player.src; }
|
||||
get type (): AudioType { return this._player.type; }
|
||||
get state (): AudioState { return this._player.state; }
|
||||
get loop (): boolean { return this._player.loop; }
|
||||
set loop (val: boolean) { this._player.loop = val; }
|
||||
get volume (): number { return this._player.volume; }
|
||||
set volume (val: number) { this._player.volume = val; }
|
||||
get duration (): number { return this._player.duration; }
|
||||
get currentTime (): number { return this._player.currentTime; }
|
||||
get sampleRate (): number { return this._player.sampleRate; }
|
||||
getPCMData (channelIndex: number): AudioPCMDataView | undefined { return this._player.getPCMData(channelIndex); }
|
||||
seek (time: number): Promise<void> { return this._player.seek(time); }
|
||||
|
||||
play (): Promise<void> { return this._player.play(); }
|
||||
pause (): Promise<void> { return this._player.pause(); }
|
||||
stop (): Promise<void> { return this._player.stop(); }
|
||||
onInterruptionBegin (cb: () => void): void { this._player.onInterruptionBegin(cb); }
|
||||
offInterruptionBegin (cb?: () => void): void { this._player.offInterruptionBegin(cb); }
|
||||
onInterruptionEnd (cb: () => void): void { this._player.onInterruptionEnd(cb); }
|
||||
offInterruptionEnd (cb?: () => void): void { this._player.offInterruptionEnd(cb); }
|
||||
onEnded (cb: () => void): void { this._player.onEnded(cb); }
|
||||
offEnded (cb?: () => void): void { this._player.offEnded(cb); }
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/* eslint-disable import/no-dynamic-require */
|
||||
import { TAOBAO, TAOBAO_MINIGAME, WECHAT, WECHAT_MINI_PROGRAM, XIAOMI } from 'internal:constants';
|
||||
import { checkPalIntegrity, withImpl } from '../../integrity-check';
|
||||
|
||||
declare const require: (path: string) => any;
|
||||
declare const __baiduRequire: (path: string) => any;
|
||||
declare const __wxRequire: (path: string) => any;
|
||||
declare const __taobaoRequire: (path: string) => any;
|
||||
|
||||
export function findCanvas (): { frame: HTMLDivElement, container: HTMLDivElement, canvas: HTMLCanvasElement } {
|
||||
const container = document.createElement('div');
|
||||
return { frame: container, canvas: window.canvas, container };
|
||||
}
|
||||
|
||||
export function loadJsFile (path: string): any {
|
||||
if (XIAOMI) {
|
||||
return require(`../../${path}`);
|
||||
}
|
||||
if (WECHAT || WECHAT_MINI_PROGRAM) {
|
||||
return __wxRequire(path);
|
||||
}
|
||||
if (TAOBAO_MINIGAME) {
|
||||
return globalThis.__taobaoRequire(path);
|
||||
}
|
||||
if (TAOBAO) {
|
||||
// NOTE: Taobao doesn't support dynamic require
|
||||
return undefined;
|
||||
}
|
||||
return require(`../${path}`);
|
||||
}
|
||||
|
||||
checkPalIntegrity<typeof import('pal/env')>(withImpl<typeof import('./env')>());
|
||||
@@ -1,74 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { NATIVE, PREVIEW } from 'internal:constants';
|
||||
import { checkPalIntegrity, withImpl } from '../../integrity-check';
|
||||
|
||||
declare const require: (path: string) => Promise<void>;
|
||||
|
||||
const ccwindow = typeof globalThis.jsb !== 'undefined' ? (typeof jsb.window !== 'undefined' ? jsb.window : window) : window;
|
||||
const ccdocument = ccwindow.document;
|
||||
|
||||
export function findCanvas (): { frame: HTMLDivElement, container: HTMLDivElement, canvas: HTMLCanvasElement } {
|
||||
const container = ccdocument.createElement('div');
|
||||
const frame = ccdocument.documentElement as HTMLDivElement;
|
||||
const canvas = ccwindow.__canvas;
|
||||
return { frame, canvas, container };
|
||||
}
|
||||
|
||||
export function loadJsFile (path: string): Promise<void> {
|
||||
if (NATIVE && window.oh && window.scriptEngineType === 'napi') {
|
||||
// TODO(qgh):OpenHarmony does not currently support dynamic require expressions
|
||||
window.oh.loadModule(path);
|
||||
return Promise.resolve();
|
||||
} else {
|
||||
if (PREVIEW) {
|
||||
// NOTE: in native preview (simulator), we need to request script with url http://x.x.x.x:xxxx/plugins/xxx.js
|
||||
// so that the editor preview server would resolve the plugin script and return the code.
|
||||
// here we use window.eval() function to execute the code of plugin script.
|
||||
return new Promise((resolve, reject) => {
|
||||
const sourceURL = window.location.href + path;
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.onload = (): void => {
|
||||
if (xhr.status !== 200) {
|
||||
reject(new Error(`load js file failed: ${sourceURL}, error status: ${xhr.status}`));
|
||||
return;
|
||||
}
|
||||
// eslint-disable-next-line no-eval
|
||||
window.eval(`${xhr.response as string}\n//# sourceURL=${sourceURL}`);
|
||||
resolve();
|
||||
};
|
||||
xhr.onerror = (): void => {
|
||||
reject(new Error(`load js file failed: ${sourceURL}`));
|
||||
};
|
||||
xhr.open('GET', sourceURL, true);
|
||||
xhr.send(null);
|
||||
});
|
||||
}
|
||||
// eslint-disable-next-line import/no-dynamic-require
|
||||
return require(`${path}`);
|
||||
}
|
||||
}
|
||||
|
||||
checkPalIntegrity<typeof import('pal/env')>(withImpl<typeof import('./env')>());
|
||||
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2025 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { checkPalIntegrity, withImpl } from '../../integrity-check';
|
||||
|
||||
export function findCanvas (): { frame: HTMLDivElement, container: HTMLDivElement, canvas: HTMLCanvasElement } {
|
||||
const frame = document.querySelector('#GameDiv') as HTMLDivElement;
|
||||
const container = document.querySelector('#Cocos3dGameContainer') as HTMLDivElement;
|
||||
const canvas = document.querySelector('#GameCanvas') as HTMLCanvasElement;
|
||||
|
||||
return { frame, container, canvas };
|
||||
}
|
||||
|
||||
export function loadJsFile (path: string): Promise<void> {
|
||||
// eslint-disable-next-line import/no-dynamic-require
|
||||
return globalThis.nodeEnv.require(`${path}`);
|
||||
}
|
||||
|
||||
checkPalIntegrity<typeof import('pal/env')>(withImpl<typeof import('./env')>());
|
||||
@@ -1,50 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { HUAWEI, OPPO, VIVO } from 'internal:constants';
|
||||
import { checkPalIntegrity, withImpl } from '../../integrity-check';
|
||||
|
||||
declare const require: (path: string) => Promise<void>;
|
||||
declare const ral: any;
|
||||
|
||||
export function findCanvas (): { frame: HTMLDivElement, container: HTMLDivElement, canvas: HTMLCanvasElement } {
|
||||
const container = document.createElement('div');
|
||||
const frame = container.parentNode === document.body ? document.documentElement : container.parentNode as any;
|
||||
|
||||
let canvas;
|
||||
if (VIVO) {
|
||||
canvas = window.mainCanvas;
|
||||
window.mainCanvas = undefined;
|
||||
} else {
|
||||
canvas = ral.createCanvas();
|
||||
}
|
||||
return { frame, canvas, container };
|
||||
}
|
||||
|
||||
export function loadJsFile (path: string): Promise<void> {
|
||||
// eslint-disable-next-line import/no-dynamic-require
|
||||
return require(`${path}`);
|
||||
}
|
||||
|
||||
checkPalIntegrity<typeof import('pal/env')>(withImpl<typeof import('./env')>());
|
||||
@@ -1,68 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { checkPalIntegrity, withImpl } from '../../integrity-check';
|
||||
|
||||
export function findCanvas (): { frame: HTMLDivElement, container: HTMLDivElement, canvas: HTMLCanvasElement } {
|
||||
const frame = document.querySelector('#GameDiv') as HTMLDivElement;
|
||||
const container = document.querySelector('#Cocos3dGameContainer') as HTMLDivElement;
|
||||
const canvas = document.querySelector('#GameCanvas') as HTMLCanvasElement;
|
||||
|
||||
return { frame, container, canvas };
|
||||
}
|
||||
|
||||
export function loadJsFile (path: string): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let err;
|
||||
function windowErrorListener (evt): void {
|
||||
if (evt.filename === path) {
|
||||
err = evt.error;
|
||||
}
|
||||
}
|
||||
window.addEventListener('error', windowErrorListener);
|
||||
const script = document.createElement('script');
|
||||
script.charset = 'utf-8';
|
||||
script.async = true;
|
||||
script.crossOrigin = 'anonymous';
|
||||
script.addEventListener('error', () => {
|
||||
window.removeEventListener('error', windowErrorListener);
|
||||
reject(Error(`Error loading ${path}`));
|
||||
});
|
||||
script.addEventListener('load', () => {
|
||||
window.removeEventListener('error', windowErrorListener);
|
||||
document.head.removeChild(script);
|
||||
// Note that if an error occurs that isn't caught by this if statement,
|
||||
// that getRegister will return null and a "did not instantiate" error will be thrown.
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
script.src = path.replace('#', '%23');
|
||||
document.head.appendChild(script);
|
||||
});
|
||||
}
|
||||
|
||||
checkPalIntegrity<typeof import('pal/env')>(withImpl<typeof import('./env')>());
|
||||
@@ -1,327 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2022 Chukong Technologies Inc.
|
||||
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { Vec2, Vec3, Quat } from '../../cocos/core/math';
|
||||
|
||||
/**
|
||||
* @en The abstract class for input source, which is used to control the input signal of a mono input source
|
||||
* @zh InputSource 抽象类,该类用于控制单一输入源的输入信号
|
||||
*/
|
||||
export abstract class InputSource<T> {
|
||||
/**
|
||||
* @en Get the signal value of the input source
|
||||
* @zh 获取输入源的信号值
|
||||
*/
|
||||
abstract getValue (): T;
|
||||
}
|
||||
|
||||
/**
|
||||
* @en The class for input source of 1D Axis, which is used to control the input signal of a mono input source
|
||||
* @zh 1D 轴的 InputSource 类,该类用于控制单一输入源的输入信号
|
||||
*/
|
||||
export class InputSourceAxis1D extends InputSource<number> {
|
||||
/**
|
||||
* @en Get the signal value of the input source, ranged from -1 to 1
|
||||
* @zh 获取输入源的信号值,取值范围从 -1 到 1
|
||||
*/
|
||||
getValue (): number {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @en The class for input source of 2D Axis, which is used to control the input signal of a mono input source
|
||||
* @zh 2D 轴的 InputSource 类,该类用于控制单一输入源的输入信号
|
||||
*/
|
||||
export class InputSourceAxis2D extends InputSource<Vec2> {
|
||||
/**
|
||||
* @en Get the signal value of the input source, which returns a Vec2 object.
|
||||
* @zh 获取输入源的信号值,该方法返回一个 Vec2 对象
|
||||
*/
|
||||
getValue (): Vec2 {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @en The class for input source of 3D Axis, which is used to control the input signal of a mono input source
|
||||
* @zh 3D 轴的 InputSource 类,该类用于控制单一输入源的输入信号
|
||||
*/
|
||||
export class InputSourceAxis3D extends InputSource<Vec3> {
|
||||
/**
|
||||
* @en Get the signal value of the input source, which returns a Vec3 object.
|
||||
* @zh 获取输入源的信号值,该方法返回一个 Vec3 对象
|
||||
*/
|
||||
getValue (): Vec3 {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @en The class for input source of Quaternion, which is used to control the input signal of a mono input source
|
||||
* @zh 四元数的 InputSource 类,该类用于控制单一输入源的输入信号
|
||||
*/
|
||||
export class InputSourceQuat extends InputSource<Quat> {
|
||||
/**
|
||||
* @en Get the signal value of the input source, which returns a Quat object.
|
||||
* @zh 获取输入源的信号值,该方法返回一个 Quat 对象
|
||||
*/
|
||||
getValue (): Quat {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @en The class for input source of 1D Axis, which is used to control the input signal of a composite of multi input source
|
||||
* @zh 1D 轴的 InputSource 类,该类用于控制多个输入源组合的输入信号
|
||||
*/
|
||||
export class CompositeInputSourceAxis1D extends InputSourceAxis1D {
|
||||
/**
|
||||
* @en The input source object of positive 1D axis
|
||||
* @zh 1D 轴的正向的输入源对象
|
||||
*/
|
||||
public declare positive: InputSourceButton;
|
||||
/**
|
||||
* @en The input source object of negative 1D axis
|
||||
* @zh 1D 轴的负向的输入源对象
|
||||
*/
|
||||
public declare negative: InputSourceButton;
|
||||
constructor (options: {positive: InputSourceButton, negative: InputSourceButton}) {
|
||||
super();
|
||||
this.positive = options.positive;
|
||||
this.negative = options.negative;
|
||||
}
|
||||
/**
|
||||
* @en Get the signal value of the input source, ranged from -1 to 1.
|
||||
* @zh 获取输入源的信号值,取值范围从 -1 到 1
|
||||
*/
|
||||
public getValue (): number {
|
||||
const positiveValue = this.positive.getValue();
|
||||
const negativeValue = this.negative.getValue();
|
||||
if (Math.abs(positiveValue) > Math.abs(negativeValue)) {
|
||||
return positiveValue;
|
||||
}
|
||||
return -negativeValue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @en The class for input source of 2D Axis, which is used to control the input signal of a composite of multi input source
|
||||
* @zh 2D 轴的 InputSource 类,该类用于控制多个输入源组合的输入信号
|
||||
*/
|
||||
export class CompositeInputSourceAxis2D extends InputSourceAxis2D {
|
||||
/**
|
||||
* @en The input source object of up direction of 2D axis
|
||||
* @zh 2D 轴的向上的输入源对象
|
||||
*/
|
||||
public declare up: InputSourceButton;
|
||||
/**
|
||||
* @en The input source object of down direction of 2D axis
|
||||
* @zh 2D 轴的向下的输入源对象
|
||||
*/
|
||||
public declare down: InputSourceButton;
|
||||
/**
|
||||
* @en The input source object of left direction of 2D axis
|
||||
* @zh 2D 轴的向左的输入源对象
|
||||
*/
|
||||
public declare left: InputSourceButton;
|
||||
/**
|
||||
* @en The input source object of right direction of 2D axis
|
||||
* @zh 2D 轴的向右的输入源对象
|
||||
*/
|
||||
public declare right: InputSourceButton;
|
||||
|
||||
/**
|
||||
* @en The composite input source object on the x axis of 2D axis
|
||||
* @zh 2D 轴在 x 轴上的组合输入源对象
|
||||
*/
|
||||
public declare xAxis: CompositeInputSourceAxis1D;
|
||||
/**
|
||||
* @en The composite input source object on the y axis of 2D axis
|
||||
* @zh 2D 轴在 y 轴上的组合输入源对象
|
||||
*/
|
||||
public declare yAxis: CompositeInputSourceAxis1D;
|
||||
constructor (options: {up: InputSourceButton, down: InputSourceButton, left: InputSourceButton, right: InputSourceButton}) {
|
||||
super();
|
||||
this.up = options.up;
|
||||
this.down = options.down;
|
||||
this.left = options.left;
|
||||
this.right = options.right;
|
||||
this.xAxis = new CompositeInputSourceAxis1D({ positive: this.right, negative: this.left });
|
||||
this.yAxis = new CompositeInputSourceAxis1D({ positive: this.up, negative: this.down });
|
||||
}
|
||||
/**
|
||||
* @en Get the signal value of the input source, which returns a Vec2 object.
|
||||
* @zh 获取输入源的信号值,该方法返回一个 Vec2 对象
|
||||
*/
|
||||
getValue (): Vec2 {
|
||||
return new Vec2(this.xAxis.getValue(), this.yAxis.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @en The class for input source of 3D Axis, which is used to control the input signal of a composite of multi input source
|
||||
* @zh 3D 轴的 InputSource 类,该类用于控制多个输入源组合的输入信号
|
||||
*/
|
||||
export class CompositeInputSourceAxis3D extends InputSourceAxis3D {
|
||||
/**
|
||||
* @en The input source object of up direction of 3D axis
|
||||
* @zh 3D 轴的向上的输入源对象
|
||||
*/
|
||||
public declare up: InputSourceButton;
|
||||
/**
|
||||
* @en The input source object of down direction of 3D axis
|
||||
* @zh 3D 轴的向下的输入源对象
|
||||
*/
|
||||
public declare down: InputSourceButton;
|
||||
/**
|
||||
* @en The input source object of left direction of 3D axis
|
||||
* @zh 3D 轴的向左的输入源对象
|
||||
*/
|
||||
public declare left: InputSourceButton;
|
||||
/**
|
||||
* @en The input source object of right direction of 3D axis
|
||||
* @zh 3D 轴的向右的输入源对象
|
||||
*/
|
||||
public declare right: InputSourceButton;
|
||||
/**
|
||||
* @en The input source object of forward direction of 3D axis
|
||||
* @zh 3D 轴的向前的输入源对象
|
||||
*/
|
||||
public declare forward: InputSourceButton;
|
||||
/**
|
||||
* @en The input source object of backward direction of 3D axis
|
||||
* @zh 3D 轴的向后的输入源对象
|
||||
*/
|
||||
public declare backward: InputSourceButton;
|
||||
|
||||
/**
|
||||
* @en The composite input source object on the x axis of 3D axis
|
||||
* @zh 3D 轴在 x 轴上的组合输入源对象
|
||||
*/
|
||||
public declare xAxis: CompositeInputSourceAxis1D;
|
||||
/**
|
||||
* @en The composite input source object on the y axis of 3D axis
|
||||
* @zh 3D 轴在 y 轴上的组合输入源对象
|
||||
*/
|
||||
public declare yAxis: CompositeInputSourceAxis1D;
|
||||
/**
|
||||
* @en The composite input source object on the z axis of 3D axis
|
||||
* @zh 3D 轴在 z 轴上的组合输入源对象
|
||||
*/
|
||||
public declare zAxis: CompositeInputSourceAxis1D;
|
||||
constructor (options: {up: InputSourceButton, down: InputSourceButton, left: InputSourceButton,
|
||||
right: InputSourceButton, forward: InputSourceButton, backward: InputSourceButton}) {
|
||||
super();
|
||||
this.up = options.up;
|
||||
this.down = options.down;
|
||||
this.left = options.left;
|
||||
this.right = options.right;
|
||||
this.forward = options.forward;
|
||||
this.backward = options.backward;
|
||||
|
||||
this.xAxis = new CompositeInputSourceAxis1D({ positive: this.right, negative: this.left });
|
||||
this.yAxis = new CompositeInputSourceAxis1D({ positive: this.up, negative: this.down });
|
||||
this.zAxis = new CompositeInputSourceAxis1D({ positive: this.forward, negative: this.backward });
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Get the signal value of the input source, which returns a Vec3 object.
|
||||
* @zh 获取输入源的信号值,该方法返回一个 Vec3 对象
|
||||
*/
|
||||
getValue (): Vec3 {
|
||||
return new Vec3(this.xAxis.getValue(), this.yAxis.getValue(), this.zAxis.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @en The class for input source of mono button, whose input signal value is ranged from 0 to 1
|
||||
* @zh 单一按键输入源类, 输入信号源的取值范围是 0 到 1
|
||||
*/
|
||||
export class InputSourceButton extends InputSourceAxis1D {
|
||||
/**
|
||||
* @en Get the signal value of the input source, ranged from 0 to 1
|
||||
* @zh 获取输入源的信号值,取值范围从 0 到 1
|
||||
*/
|
||||
getValue (): number {
|
||||
return super.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @en The class for input source of dpad, whose input signal value a Vec2 object
|
||||
* @zh 手柄方向键输入源类, 输入信号源的取值是一个 Vec2 对象
|
||||
*/
|
||||
export class InputSourceDpad extends CompositeInputSourceAxis2D {
|
||||
}
|
||||
|
||||
/**
|
||||
* @en The class for input source of stick, whose input signal value a Vec2 object
|
||||
* @zh 摇杆输入源类, 输入信号源的取值是一个 Vec2 对象
|
||||
*/
|
||||
export class InputSourceStick extends CompositeInputSourceAxis2D {
|
||||
}
|
||||
|
||||
/**
|
||||
* @en The class for input source of orientation, whose input signal value a Quat object
|
||||
* @zh 方向输入源类, 输入信号源的取值是一个 Quat 对象
|
||||
*/
|
||||
export class InputSourceOrientation extends InputSourceQuat {
|
||||
/**
|
||||
* @en Get the signal value of the input source, which returns a Quat object.
|
||||
* @zh 获取输入源的信号值,该方法返回一个 Quat 对象
|
||||
*/
|
||||
getValue (): Quat {
|
||||
return super.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @en The class for input source of position, whose input signal value a Vec3 object
|
||||
* @zh 坐标输入源类, 输入信号源的取值是一个 Vec3 对象
|
||||
*/
|
||||
export class InputSourcePosition extends InputSourceAxis3D {
|
||||
/**
|
||||
* @en Get the signal value of the input source, which returns a Vec3 object.
|
||||
* @zh 获取输入源的信号值,该方法返回一个 Vec3 对象
|
||||
*/
|
||||
getValue (): Vec3 {
|
||||
return super.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @en The class for input source of mono button touch, whose input signal value is ranged from 0 or 1
|
||||
* @zh 单一按键触摸输入源类, 输入信号源的取值范围是 0 或 1
|
||||
*/
|
||||
export class InputSourceTouch extends InputSourceAxis1D {
|
||||
/**
|
||||
* @en Get the signal value of the input source, ranged from 0 or 1
|
||||
* @zh 获取输入源的信号值,取值范围从 0 或 1
|
||||
*/
|
||||
getValue (): number {
|
||||
return super.getValue();
|
||||
}
|
||||
}
|
||||
@@ -1,104 +0,0 @@
|
||||
import { KeyCode } from '../../cocos/input/types';
|
||||
|
||||
export const code2KeyCode: Record<string, KeyCode> = {
|
||||
Backspace: KeyCode.BACKSPACE,
|
||||
Tab: KeyCode.TAB,
|
||||
Enter: KeyCode.ENTER,
|
||||
ShiftLeft: KeyCode.SHIFT_LEFT,
|
||||
ControlLeft: KeyCode.CTRL_LEFT,
|
||||
AltLeft: KeyCode.ALT_LEFT,
|
||||
ShiftRight: KeyCode.SHIFT_RIGHT,
|
||||
ControlRight: KeyCode.CTRL_RIGHT,
|
||||
AltRight: KeyCode.ALT_RIGHT,
|
||||
Pause: KeyCode.PAUSE,
|
||||
CapsLock: KeyCode.CAPS_LOCK,
|
||||
Escape: KeyCode.ESCAPE,
|
||||
Space: KeyCode.SPACE,
|
||||
PageUp: KeyCode.PAGE_UP,
|
||||
PageDown: KeyCode.PAGE_DOWN,
|
||||
End: KeyCode.END,
|
||||
Home: KeyCode.HOME,
|
||||
ArrowLeft: KeyCode.ARROW_LEFT,
|
||||
ArrowUp: KeyCode.ARROW_UP,
|
||||
ArrowRight: KeyCode.ARROW_RIGHT,
|
||||
ArrowDown: KeyCode.ARROW_DOWN,
|
||||
Insert: KeyCode.INSERT,
|
||||
Delete: KeyCode.DELETE,
|
||||
Digit0: KeyCode.DIGIT_0,
|
||||
Digit1: KeyCode.DIGIT_1,
|
||||
Digit2: KeyCode.DIGIT_2,
|
||||
Digit3: KeyCode.DIGIT_3,
|
||||
Digit4: KeyCode.DIGIT_4,
|
||||
Digit5: KeyCode.DIGIT_5,
|
||||
Digit6: KeyCode.DIGIT_6,
|
||||
Digit7: KeyCode.DIGIT_7,
|
||||
Digit8: KeyCode.DIGIT_8,
|
||||
Digit9: KeyCode.DIGIT_9,
|
||||
KeyA: KeyCode.KEY_A,
|
||||
KeyB: KeyCode.KEY_B,
|
||||
KeyC: KeyCode.KEY_C,
|
||||
KeyD: KeyCode.KEY_D,
|
||||
KeyE: KeyCode.KEY_E,
|
||||
KeyF: KeyCode.KEY_F,
|
||||
KeyG: KeyCode.KEY_G,
|
||||
KeyH: KeyCode.KEY_H,
|
||||
KeyI: KeyCode.KEY_I,
|
||||
KeyJ: KeyCode.KEY_J,
|
||||
KeyK: KeyCode.KEY_K,
|
||||
KeyL: KeyCode.KEY_L,
|
||||
KeyM: KeyCode.KEY_M,
|
||||
KeyN: KeyCode.KEY_N,
|
||||
KeyO: KeyCode.KEY_O,
|
||||
KeyP: KeyCode.KEY_P,
|
||||
KeyQ: KeyCode.KEY_Q,
|
||||
KeyR: KeyCode.KEY_R,
|
||||
KeyS: KeyCode.KEY_S,
|
||||
KeyT: KeyCode.KEY_T,
|
||||
KeyU: KeyCode.KEY_U,
|
||||
KeyV: KeyCode.KEY_V,
|
||||
KeyW: KeyCode.KEY_W,
|
||||
KeyX: KeyCode.KEY_X,
|
||||
KeyY: KeyCode.KEY_Y,
|
||||
KeyZ: KeyCode.KEY_Z,
|
||||
Numpad0: KeyCode.NUM_0,
|
||||
Numpad1: KeyCode.NUM_1,
|
||||
Numpad2: KeyCode.NUM_2,
|
||||
Numpad3: KeyCode.NUM_3,
|
||||
Numpad4: KeyCode.NUM_4,
|
||||
Numpad5: KeyCode.NUM_5,
|
||||
Numpad6: KeyCode.NUM_6,
|
||||
Numpad7: KeyCode.NUM_7,
|
||||
Numpad8: KeyCode.NUM_8,
|
||||
Numpad9: KeyCode.NUM_9,
|
||||
NumpadMultiply: KeyCode.NUM_MULTIPLY,
|
||||
NumpadAdd: KeyCode.NUM_PLUS,
|
||||
NumpadSubtract: KeyCode.NUM_SUBTRACT,
|
||||
NumpadDecimal: KeyCode.NUM_DECIMAL,
|
||||
NumpadDivide: KeyCode.NUM_DIVIDE,
|
||||
NumpadEnter: KeyCode.NUM_ENTER,
|
||||
F1: KeyCode.F1,
|
||||
F2: KeyCode.F2,
|
||||
F3: KeyCode.F3,
|
||||
F4: KeyCode.F4,
|
||||
F5: KeyCode.F5,
|
||||
F6: KeyCode.F6,
|
||||
F7: KeyCode.F7,
|
||||
F8: KeyCode.F8,
|
||||
F9: KeyCode.F9,
|
||||
F10: KeyCode.F10,
|
||||
F11: KeyCode.F11,
|
||||
F12: KeyCode.F12,
|
||||
NumLock: KeyCode.NUM_LOCK,
|
||||
ScrollLock: KeyCode.SCROLL_LOCK,
|
||||
Semicolon: KeyCode.SEMICOLON,
|
||||
Equal: KeyCode.EQUAL,
|
||||
Comma: KeyCode.COMMA,
|
||||
Minus: KeyCode.DASH,
|
||||
Period: KeyCode.PERIOD,
|
||||
Slash: KeyCode.SLASH,
|
||||
Backquote: KeyCode.BACK_QUOTE,
|
||||
BracketLeft: KeyCode.BRACKET_LEFT,
|
||||
Backslash: KeyCode.BACKSLASH,
|
||||
BracketRight: KeyCode.BRACKET_RIGHT,
|
||||
Quote: KeyCode.QUOTE,
|
||||
};
|
||||
@@ -1,95 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { AccelerometerCallback } from 'pal/input';
|
||||
import { minigame, AccelerometerIntervalMode } from 'pal/minigame';
|
||||
import { Acceleration, EventAcceleration } from '../../../cocos/input/types';
|
||||
import { EventTarget } from '../../../cocos/core/event';
|
||||
import { InputEventType } from '../../../cocos/input/types/event-enum';
|
||||
import { errorID } from '../../../cocos/core/platform/debug';
|
||||
|
||||
export class AccelerometerInputSource {
|
||||
private _isStarted = false;
|
||||
private _accelMode: AccelerometerIntervalMode = 'normal';
|
||||
private _eventTarget: EventTarget = new EventTarget();
|
||||
private declare _didAccelerateFunc: (event: AccelerometerData) => void;
|
||||
|
||||
constructor () {
|
||||
this._didAccelerateFunc = this._didAccelerate.bind(this);
|
||||
}
|
||||
|
||||
private _registerEvent (): void {
|
||||
minigame.onAccelerometerChange(this._didAccelerateFunc);
|
||||
}
|
||||
|
||||
private _unregisterEvent (): void {
|
||||
minigame.offAccelerometerChange(this._didAccelerateFunc);
|
||||
}
|
||||
|
||||
private _didAccelerate (event: AccelerometerData): void {
|
||||
const timestamp = performance.now();
|
||||
const acceleration = new Acceleration(event.x, event.y, event.z, timestamp);
|
||||
const eventAcceleration = new EventAcceleration(acceleration);
|
||||
this._eventTarget.emit(InputEventType.DEVICEMOTION, eventAcceleration);
|
||||
}
|
||||
|
||||
public start (): void {
|
||||
this._registerEvent();
|
||||
minigame.startAccelerometer({
|
||||
interval: this._accelMode,
|
||||
success: () => {
|
||||
this._isStarted = true;
|
||||
},
|
||||
});
|
||||
}
|
||||
public stop (): void {
|
||||
minigame.stopAccelerometer({
|
||||
success: () => {
|
||||
this._isStarted = false;
|
||||
},
|
||||
fail () {
|
||||
errorID(16305);
|
||||
},
|
||||
});
|
||||
this._unregisterEvent();
|
||||
}
|
||||
public setInterval (intervalInMileseconds: number): void {
|
||||
// reference: https://developers.weixin.qq.com/minigame/dev/api/device/accelerometer/wx.startAccelerometer.html
|
||||
if (intervalInMileseconds >= 200) {
|
||||
this._accelMode = 'normal';
|
||||
} else if (intervalInMileseconds >= 60) {
|
||||
this._accelMode = 'ui';
|
||||
} else {
|
||||
this._accelMode = 'game';
|
||||
}
|
||||
if (this._isStarted) {
|
||||
// restart accelerometer
|
||||
this.stop();
|
||||
this.start();
|
||||
}
|
||||
}
|
||||
public on (eventType: InputEventType, callback: AccelerometerCallback, target?: any): void {
|
||||
this._eventTarget.on(eventType, callback, target);
|
||||
}
|
||||
}
|
||||
@@ -1,216 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { GamepadCallback } from 'pal/input';
|
||||
import { InputEventType } from '../../../cocos/input/types/event-enum';
|
||||
import { EventTarget } from '../../../cocos/core/event/event-target';
|
||||
import { InputSourceButton, InputSourceDpad, InputSourceOrientation, InputSourcePosition, InputSourceStick } from '../input-source';
|
||||
import { Quat, Vec3 } from '../../../cocos/core';
|
||||
|
||||
export class GamepadInputDevice {
|
||||
public static all: GamepadInputDevice[] = [];
|
||||
public static xr: (GamepadInputDevice | null) = null;
|
||||
|
||||
public get buttonNorth (): InputSourceButton { return this._buttonNorth; }
|
||||
public get buttonEast (): InputSourceButton { return this._buttonEast; }
|
||||
public get buttonWest (): InputSourceButton { return this._buttonWest; }
|
||||
public get buttonSouth (): InputSourceButton { return this._buttonSouth; }
|
||||
public get buttonL1 (): InputSourceButton { return this._buttonL1; }
|
||||
public get buttonL2 (): InputSourceButton { return this._buttonL2; }
|
||||
public get buttonL3 (): InputSourceButton { return this._buttonL3; }
|
||||
public get buttonR1 (): InputSourceButton { return this._buttonR1; }
|
||||
public get buttonR2 (): InputSourceButton { return this._buttonR2; }
|
||||
public get buttonR3 (): InputSourceButton { return this._buttonR3; }
|
||||
// public get buttonTouchPad () { return this._buttonTouchPad; }
|
||||
// public get buttonHome () { return this._buttonHome; }
|
||||
public get buttonShare (): InputSourceButton { return this._buttonShare; }
|
||||
public get buttonOptions (): InputSourceButton { return this._buttonOptions; }
|
||||
public get dpad (): InputSourceDpad { return this._dpad; }
|
||||
public get leftStick (): InputSourceStick { return this._leftStick; }
|
||||
public get rightStick (): InputSourceStick { return this._rightStick; }
|
||||
public get buttonStart (): InputSourceButton { return this._buttonStart; }
|
||||
public get gripLeft (): InputSourceButton { return this._gripLeft; }
|
||||
public get gripRight (): InputSourceButton { return this._gripRight; }
|
||||
public get handLeftPosition (): InputSourcePosition { return this._handLeftPosition; }
|
||||
public get handLeftOrientation (): InputSourceOrientation { return this._handLeftOrientation; }
|
||||
public get handRightPosition (): InputSourcePosition { return this._handRightPosition; }
|
||||
public get handRightOrientation (): InputSourceOrientation { return this._handRightOrientation; }
|
||||
public get aimLeftPosition (): InputSourcePosition { return this._aimLeftPosition; }
|
||||
public get aimLeftOrientation (): InputSourceOrientation { return this._aimLeftOrientation; }
|
||||
public get aimRightPosition (): InputSourcePosition { return this._aimRightPosition; }
|
||||
public get aimRightOrientation (): InputSourceOrientation { return this._aimRightOrientation; }
|
||||
|
||||
public get deviceId (): number {
|
||||
return this._deviceId;
|
||||
}
|
||||
public get connected (): boolean {
|
||||
return this._connected;
|
||||
}
|
||||
|
||||
private static _eventTarget: EventTarget = new EventTarget();
|
||||
|
||||
private declare _buttonNorth: InputSourceButton;
|
||||
private declare _buttonEast: InputSourceButton;
|
||||
private declare _buttonWest: InputSourceButton;
|
||||
private declare _buttonSouth: InputSourceButton;
|
||||
private declare _buttonL1: InputSourceButton;
|
||||
private declare _buttonL2: InputSourceButton;
|
||||
private declare _buttonL3: InputSourceButton;
|
||||
private declare _buttonR1: InputSourceButton;
|
||||
private declare _buttonR2: InputSourceButton;
|
||||
private declare _buttonR3: InputSourceButton;
|
||||
// private declare buttonTouchPad: InputSourceButton;
|
||||
// private declare buttonHome: InputSourceButton;
|
||||
private declare _buttonShare: InputSourceButton;
|
||||
private declare _buttonOptions: InputSourceButton;
|
||||
private declare _dpad: InputSourceDpad;
|
||||
private declare _leftStick: InputSourceStick;
|
||||
private declare _rightStick: InputSourceStick;
|
||||
private declare _buttonStart: InputSourceButton;
|
||||
private declare _gripLeft: InputSourceButton;
|
||||
private declare _gripRight: InputSourceButton;
|
||||
private declare _handLeftPosition: InputSourcePosition;
|
||||
private declare _handLeftOrientation: InputSourceOrientation;
|
||||
private declare _handRightPosition: InputSourcePosition;
|
||||
private declare _handRightOrientation: InputSourceOrientation;
|
||||
private declare _aimLeftPosition: InputSourcePosition;
|
||||
private declare _aimLeftOrientation: InputSourceOrientation;
|
||||
private declare _aimRightPosition: InputSourcePosition;
|
||||
private declare _aimRightOrientation: InputSourceOrientation;
|
||||
|
||||
private _deviceId = -1;
|
||||
private _connected = false;
|
||||
|
||||
constructor (deviceId: number) {
|
||||
this._deviceId = deviceId;
|
||||
this._initInputSource();
|
||||
}
|
||||
|
||||
/**
|
||||
* @engineInternal
|
||||
*/
|
||||
public static _init (): void {
|
||||
// not supported
|
||||
}
|
||||
|
||||
/**
|
||||
* @engineInternal
|
||||
*/
|
||||
public static _on (eventType: InputEventType, cb: GamepadCallback, target?: any): void {
|
||||
GamepadInputDevice._eventTarget.on(eventType, cb, target);
|
||||
}
|
||||
|
||||
private _initInputSource (): void {
|
||||
const self = this;
|
||||
self._buttonNorth = new InputSourceButton();
|
||||
self._buttonNorth.getValue = (): number => 0;
|
||||
self._buttonEast = new InputSourceButton();
|
||||
self._buttonEast.getValue = (): number => 0;
|
||||
self._buttonWest = new InputSourceButton();
|
||||
self._buttonWest.getValue = (): number => 0;
|
||||
self._buttonSouth = new InputSourceButton();
|
||||
self._buttonSouth.getValue = (): number => 0;
|
||||
|
||||
self._buttonL1 = new InputSourceButton();
|
||||
self._buttonL1.getValue = (): number => 0;
|
||||
self._buttonL2 = new InputSourceButton();
|
||||
self._buttonL2.getValue = (): number => 0;
|
||||
self._buttonL3 = new InputSourceButton();
|
||||
self._buttonL3.getValue = (): number => 0;
|
||||
self._buttonR1 = new InputSourceButton();
|
||||
self._buttonR1.getValue = (): number => 0;
|
||||
self._buttonR2 = new InputSourceButton();
|
||||
self._buttonR2.getValue = (): number => 0;
|
||||
self._buttonR3 = new InputSourceButton();
|
||||
self._buttonR3.getValue = (): number => 0;
|
||||
|
||||
// self._buttonTouchPad = new InputSourceButton();
|
||||
// self._buttonTouchPad.getValue = () => 0;
|
||||
// self._buttonHome = new InputSourceButton();
|
||||
// self._buttonHome.getValue = () => 0;
|
||||
|
||||
self._buttonShare = new InputSourceButton();
|
||||
self._buttonShare.getValue = (): number => 0;
|
||||
self._buttonOptions = new InputSourceButton();
|
||||
self._buttonOptions.getValue = (): number => 0;
|
||||
|
||||
const dpadUp = new InputSourceButton();
|
||||
dpadUp.getValue = (): number => 0;
|
||||
const dpadDown = new InputSourceButton();
|
||||
dpadDown.getValue = (): number => 0;
|
||||
const dpadLeft = new InputSourceButton();
|
||||
dpadLeft.getValue = (): number => 0;
|
||||
const dpadRight = new InputSourceButton();
|
||||
dpadRight.getValue = (): number => 0;
|
||||
self._dpad = new InputSourceDpad({ up: dpadUp, down: dpadDown, left: dpadLeft, right: dpadRight });
|
||||
|
||||
const leftStickUp = new InputSourceButton();
|
||||
leftStickUp.getValue = (): number => 0;
|
||||
const leftStickDown = new InputSourceButton();
|
||||
leftStickDown.getValue = (): number => 0;
|
||||
const leftStickLeft = new InputSourceButton();
|
||||
leftStickLeft.getValue = (): number => 0;
|
||||
const leftStickRight = new InputSourceButton();
|
||||
leftStickRight.getValue = (): number => 0;
|
||||
self._leftStick = new InputSourceStick({ up: leftStickUp, down: leftStickDown, left: leftStickLeft, right: leftStickRight });
|
||||
|
||||
const rightStickUp = new InputSourceButton();
|
||||
rightStickUp.getValue = (): number => 0;
|
||||
const rightStickDown = new InputSourceButton();
|
||||
rightStickDown.getValue = (): number => 0;
|
||||
const rightStickLeft = new InputSourceButton();
|
||||
rightStickLeft.getValue = (): number => 0;
|
||||
const rightStickRight = new InputSourceButton();
|
||||
rightStickRight.getValue = (): number => 0;
|
||||
self._rightStick = new InputSourceStick({ up: rightStickUp, down: rightStickDown, left: rightStickLeft, right: rightStickRight });
|
||||
|
||||
self._buttonStart = new InputSourceButton();
|
||||
self._buttonStart.getValue = (): number => 0;
|
||||
|
||||
self._gripLeft = new InputSourceButton();
|
||||
self._gripLeft.getValue = (): number => 0;
|
||||
self._gripRight = new InputSourceButton();
|
||||
self._gripRight.getValue = (): number => 0;
|
||||
|
||||
self._handLeftPosition = new InputSourcePosition();
|
||||
self._handLeftPosition.getValue = (): Readonly<Vec3> => Vec3.ZERO;
|
||||
self._handLeftOrientation = new InputSourceOrientation();
|
||||
self._handLeftOrientation.getValue = (): Readonly<Quat> => Quat.IDENTITY;
|
||||
|
||||
self._handRightPosition = new InputSourcePosition();
|
||||
self._handRightPosition.getValue = (): Readonly<Vec3> => Vec3.ZERO;
|
||||
self._handRightOrientation = new InputSourceOrientation();
|
||||
self._handRightOrientation.getValue = (): Readonly<Quat> => Quat.IDENTITY;
|
||||
|
||||
self._aimLeftPosition = new InputSourcePosition();
|
||||
self._aimLeftPosition.getValue = (): Readonly<Vec3> => Vec3.ZERO;
|
||||
self._aimLeftOrientation = new InputSourceOrientation();
|
||||
self._aimLeftOrientation.getValue = (): Readonly<Quat> => Quat.IDENTITY;
|
||||
|
||||
self._aimRightPosition = new InputSourcePosition();
|
||||
self._aimRightPosition.getValue = (): Readonly<Vec3> => Vec3.ZERO;
|
||||
self._aimRightOrientation = new InputSourceOrientation();
|
||||
self._aimRightOrientation.getValue = (): Readonly<Quat> => Quat.IDENTITY;
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { HandheldCallback } from 'pal/input';
|
||||
import { InputEventType } from '../../../cocos/input/types/event-enum';
|
||||
import { EventTarget } from '../../../cocos/core/event/event-target';
|
||||
import { InputSourcePosition, InputSourceOrientation } from '../input-source';
|
||||
import { Quat, Vec3 } from '../../../cocos/core/math';
|
||||
|
||||
export class HandheldInputDevice {
|
||||
public get handheldPosition (): InputSourcePosition { return this._handheldPosition; }
|
||||
public get handheldOrientation (): InputSourceOrientation { return this._handheldOrientation; }
|
||||
|
||||
private _eventTarget: EventTarget = new EventTarget();
|
||||
|
||||
private declare _handheldPosition: InputSourcePosition;
|
||||
private declare _handheldOrientation: InputSourceOrientation;
|
||||
|
||||
constructor () {
|
||||
this._initInputSource();
|
||||
}
|
||||
|
||||
/**
|
||||
* @engineInternal
|
||||
*/
|
||||
public _on (eventType: InputEventType, callback: HandheldCallback, target?: any): void {
|
||||
this._eventTarget.on(eventType, callback, target);
|
||||
}
|
||||
|
||||
private _initInputSource (): void {
|
||||
this._handheldPosition = new InputSourcePosition();
|
||||
this._handheldPosition.getValue = (): Readonly<Vec3> => Vec3.ZERO;
|
||||
this._handheldOrientation = new InputSourceOrientation();
|
||||
this._handheldOrientation.getValue = (): Readonly<Quat> => Quat.IDENTITY;
|
||||
}
|
||||
}
|
||||
@@ -1,201 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { HandleCallback } from 'pal/input';
|
||||
import { InputEventType } from '../../../cocos/input/types/event-enum';
|
||||
import { EventTarget } from '../../../cocos/core/event';
|
||||
import { InputSourceButton, InputSourceStick, InputSourcePosition, InputSourceOrientation, InputSourceTouch } from '../input-source';
|
||||
import { Vec3, Quat } from '../../../cocos/core/math';
|
||||
|
||||
export class HandleInputDevice {
|
||||
public get buttonNorth (): InputSourceButton { return this._buttonNorth; }
|
||||
public get buttonEast (): InputSourceButton { return this._buttonEast; }
|
||||
public get buttonWest (): InputSourceButton { return this._buttonWest; }
|
||||
public get buttonSouth (): InputSourceButton { return this._buttonSouth; }
|
||||
public get buttonTriggerLeft (): InputSourceButton { return this._buttonTriggerLeft; }
|
||||
public get buttonTriggerRight (): InputSourceButton { return this._buttonTriggerRight; }
|
||||
public get triggerLeft (): InputSourceButton { return this._triggerLeft; }
|
||||
public get triggerRight (): InputSourceButton { return this._triggerRight; }
|
||||
public get gripLeft (): InputSourceButton { return this._gripLeft; }
|
||||
public get gripRight (): InputSourceButton { return this._gripRight; }
|
||||
public get leftStick (): InputSourceStick { return this._leftStick; }
|
||||
public get rightStick (): InputSourceStick { return this._rightStick; }
|
||||
public get buttonLeftStick (): InputSourceButton { return this._buttonLeftStick; }
|
||||
public get buttonRightStick (): InputSourceButton { return this._buttonRightStick; }
|
||||
public get buttonOptions (): InputSourceButton { return this._buttonOptions; }
|
||||
public get buttonStart (): InputSourceButton { return this._buttonStart; }
|
||||
public get handLeftPosition (): InputSourcePosition { return this._handLeftPosition; }
|
||||
public get handLeftOrientation (): InputSourceOrientation { return this._handLeftOrientation; }
|
||||
public get handRightPosition (): InputSourcePosition { return this._handRightPosition; }
|
||||
public get handRightOrientation (): InputSourceOrientation { return this._handRightOrientation; }
|
||||
public get aimLeftPosition (): InputSourcePosition { return this._aimLeftPosition; }
|
||||
public get aimLeftOrientation (): InputSourceOrientation { return this._aimLeftOrientation; }
|
||||
public get aimRightPosition (): InputSourcePosition { return this._aimRightPosition; }
|
||||
public get aimRightOrientation (): InputSourceOrientation { return this._aimRightOrientation; }
|
||||
public get touchButtonA (): InputSourceTouch { return this._touchButtonA; }
|
||||
public get touchButtonB (): InputSourceTouch { return this._touchButtonB; }
|
||||
public get touchButtonX (): InputSourceTouch { return this._touchButtonX; }
|
||||
public get touchButtonY (): InputSourceTouch { return this._touchButtonY; }
|
||||
public get touchButtonTriggerLeft (): InputSourceTouch { return this._touchButtonTriggerLeft; }
|
||||
public get touchButtonTriggerRight (): InputSourceTouch { return this._touchButtonTriggerRight; }
|
||||
public get touchButtonThumbStickLeft (): InputSourceTouch { return this._touchButtonThumbStickLeft; }
|
||||
public get touchButtonThumbStickRight (): InputSourceTouch { return this._touchButtonThumbStickRight; }
|
||||
|
||||
private _eventTarget: EventTarget = new EventTarget();
|
||||
|
||||
private declare _buttonNorth: InputSourceButton;
|
||||
private declare _buttonEast: InputSourceButton;
|
||||
private declare _buttonWest: InputSourceButton;
|
||||
private declare _buttonSouth: InputSourceButton;
|
||||
private declare _buttonTriggerLeft: InputSourceButton;
|
||||
private declare _buttonTriggerRight: InputSourceButton;
|
||||
private declare _triggerLeft: InputSourceButton;
|
||||
private declare _triggerRight: InputSourceButton;
|
||||
private declare _gripLeft: InputSourceButton;
|
||||
private declare _gripRight: InputSourceButton;
|
||||
private declare _leftStick: InputSourceStick;
|
||||
private declare _rightStick: InputSourceStick;
|
||||
private declare _buttonLeftStick: InputSourceButton;
|
||||
private declare _buttonRightStick: InputSourceButton;
|
||||
private declare _buttonOptions: InputSourceButton;
|
||||
private declare _buttonStart: InputSourceButton;
|
||||
private declare _handLeftPosition: InputSourcePosition;
|
||||
private declare _handLeftOrientation: InputSourceOrientation;
|
||||
private declare _handRightPosition: InputSourcePosition;
|
||||
private declare _handRightOrientation: InputSourceOrientation;
|
||||
private declare _aimLeftPosition: InputSourcePosition;
|
||||
private declare _aimLeftOrientation: InputSourceOrientation;
|
||||
private declare _aimRightPosition: InputSourcePosition;
|
||||
private declare _aimRightOrientation: InputSourceOrientation;
|
||||
private declare _touchButtonA: InputSourceTouch;
|
||||
private declare _touchButtonB: InputSourceTouch;
|
||||
private declare _touchButtonX: InputSourceTouch;
|
||||
private declare _touchButtonY: InputSourceTouch;
|
||||
private declare _touchButtonTriggerLeft: InputSourceTouch;
|
||||
private declare _touchButtonTriggerRight: InputSourceTouch;
|
||||
private declare _touchButtonThumbStickLeft: InputSourceTouch;
|
||||
private declare _touchButtonThumbStickRight: InputSourceTouch;
|
||||
|
||||
constructor () {
|
||||
this._initInputSource();
|
||||
}
|
||||
|
||||
/**
|
||||
* @engineInternal
|
||||
*/
|
||||
public _on (eventType: InputEventType, callback: HandleCallback, target?: any): void {
|
||||
this._eventTarget.on(eventType, callback, target);
|
||||
}
|
||||
|
||||
private _initInputSource (): void {
|
||||
const self = this;
|
||||
self._buttonNorth = new InputSourceButton();
|
||||
self._buttonNorth.getValue = (): number => 0;
|
||||
self._buttonEast = new InputSourceButton();
|
||||
self._buttonEast.getValue = (): number => 0;
|
||||
self._buttonWest = new InputSourceButton();
|
||||
self._buttonWest.getValue = (): number => 0;
|
||||
self._buttonSouth = new InputSourceButton();
|
||||
self._buttonSouth.getValue = (): number => 0;
|
||||
|
||||
self._buttonTriggerLeft = new InputSourceButton();
|
||||
self._buttonTriggerLeft.getValue = (): number => 0;
|
||||
self._buttonTriggerRight = new InputSourceButton();
|
||||
self._buttonTriggerRight.getValue = (): number => 0;
|
||||
self._triggerLeft = new InputSourceButton();
|
||||
self._triggerLeft.getValue = (): number => 0;
|
||||
self._triggerRight = new InputSourceButton();
|
||||
self._triggerRight.getValue = (): number => 0;
|
||||
self._gripLeft = new InputSourceButton();
|
||||
self._gripLeft.getValue = (): number => 0;
|
||||
self._gripRight = new InputSourceButton();
|
||||
self._gripRight.getValue = (): number => 0;
|
||||
|
||||
self._buttonLeftStick = new InputSourceButton();
|
||||
self._buttonLeftStick.getValue = (): number => 0;
|
||||
const leftStickUp = new InputSourceButton();
|
||||
leftStickUp.getValue = (): number => 0;
|
||||
const leftStickDown = new InputSourceButton();
|
||||
leftStickDown.getValue = (): number => 0;
|
||||
const leftStickLeft = new InputSourceButton();
|
||||
leftStickLeft.getValue = (): number => 0;
|
||||
const leftStickRight = new InputSourceButton();
|
||||
leftStickRight.getValue = (): number => 0;
|
||||
self._leftStick = new InputSourceStick({ up: leftStickUp, down: leftStickDown, left: leftStickLeft, right: leftStickRight });
|
||||
|
||||
self._buttonRightStick = new InputSourceButton();
|
||||
self._buttonRightStick.getValue = (): number => 0;
|
||||
const rightStickUp = new InputSourceButton();
|
||||
rightStickUp.getValue = (): number => 0;
|
||||
const rightStickDown = new InputSourceButton();
|
||||
rightStickDown.getValue = (): number => 0;
|
||||
const rightStickLeft = new InputSourceButton();
|
||||
rightStickLeft.getValue = (): number => 0;
|
||||
const rightStickRight = new InputSourceButton();
|
||||
rightStickRight.getValue = (): number => 0;
|
||||
self._rightStick = new InputSourceStick({ up: rightStickUp, down: rightStickDown, left: rightStickLeft, right: rightStickRight });
|
||||
|
||||
self._buttonOptions = new InputSourceButton();
|
||||
self._buttonOptions.getValue = (): number => 0;
|
||||
self._buttonStart = new InputSourceButton();
|
||||
self._buttonStart.getValue = (): number => 0;
|
||||
|
||||
self._handLeftPosition = new InputSourcePosition();
|
||||
self._handLeftPosition.getValue = (): Readonly<Vec3> => Vec3.ZERO;
|
||||
self._handLeftOrientation = new InputSourceOrientation();
|
||||
self._handLeftOrientation.getValue = (): Readonly<Quat> => Quat.IDENTITY;
|
||||
|
||||
self._handRightPosition = new InputSourcePosition();
|
||||
self._handRightPosition.getValue = (): Readonly<Vec3> => Vec3.ZERO;
|
||||
self._handRightOrientation = new InputSourceOrientation();
|
||||
self._handRightOrientation.getValue = (): Readonly<Quat> => Quat.IDENTITY;
|
||||
|
||||
self._aimLeftPosition = new InputSourcePosition();
|
||||
self._aimLeftPosition.getValue = (): Readonly<Vec3> => Vec3.ZERO;
|
||||
self._aimLeftOrientation = new InputSourceOrientation();
|
||||
self._aimLeftOrientation.getValue = (): Readonly<Quat> => Quat.IDENTITY;
|
||||
|
||||
self._aimRightPosition = new InputSourcePosition();
|
||||
self._aimRightPosition.getValue = (): Readonly<Vec3> => Vec3.ZERO;
|
||||
self._aimRightOrientation = new InputSourceOrientation();
|
||||
self._aimRightOrientation.getValue = (): Readonly<Quat> => Quat.IDENTITY;
|
||||
|
||||
self._touchButtonA = new InputSourceTouch();
|
||||
self._touchButtonA.getValue = (): number => 0;
|
||||
self._touchButtonB = new InputSourceTouch();
|
||||
self._touchButtonB.getValue = (): number => 0;
|
||||
self._touchButtonX = new InputSourceTouch();
|
||||
self._touchButtonX.getValue = (): number => 0;
|
||||
self._touchButtonY = new InputSourceTouch();
|
||||
self._touchButtonY.getValue = (): number => 0;
|
||||
self._touchButtonTriggerLeft = new InputSourceTouch();
|
||||
self._touchButtonTriggerLeft.getValue = (): number => 0;
|
||||
self._touchButtonTriggerRight = new InputSourceTouch();
|
||||
self._touchButtonTriggerRight.getValue = (): number => 0;
|
||||
self._touchButtonThumbStickLeft = new InputSourceTouch();
|
||||
self._touchButtonThumbStickLeft.getValue = (): number => 0;
|
||||
self._touchButtonThumbStickRight = new InputSourceTouch();
|
||||
self._touchButtonThumbStickRight.getValue = (): number => 0;
|
||||
}
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { HMDCallback } from 'pal/input';
|
||||
import { InputEventType } from '../../../cocos/input/types/event-enum';
|
||||
import { EventTarget } from '../../../cocos/core/event/event-target';
|
||||
import { InputSourcePosition, InputSourceOrientation } from '../input-source';
|
||||
import { Vec3, Quat } from '../../../cocos/core/math';
|
||||
|
||||
export class HMDInputDevice {
|
||||
public get viewLeftPosition (): InputSourcePosition { return this._viewLeftPosition; }
|
||||
public get viewLeftOrientation (): InputSourceOrientation { return this._viewLeftOrientation; }
|
||||
public get viewRightPosition (): InputSourcePosition { return this._viewRightPosition; }
|
||||
public get viewRightOrientation (): InputSourceOrientation { return this._viewRightOrientation; }
|
||||
public get headMiddlePosition (): InputSourcePosition { return this._headMiddlePosition; }
|
||||
public get headMiddleOrientation (): InputSourceOrientation { return this._headMiddleOrientation; }
|
||||
|
||||
private _eventTarget: EventTarget = new EventTarget();
|
||||
|
||||
private declare _viewLeftPosition: InputSourcePosition;
|
||||
private declare _viewLeftOrientation: InputSourceOrientation;
|
||||
private declare _viewRightPosition: InputSourcePosition;
|
||||
private declare _viewRightOrientation: InputSourceOrientation;
|
||||
private declare _headMiddlePosition: InputSourcePosition;
|
||||
private declare _headMiddleOrientation: InputSourceOrientation;
|
||||
|
||||
constructor () {
|
||||
this._initInputSource();
|
||||
}
|
||||
|
||||
/**
|
||||
* @engineInternal
|
||||
*/
|
||||
public _on (eventType: InputEventType, callback: HMDCallback, target?: any): void {
|
||||
this._eventTarget.on(eventType, callback, target);
|
||||
}
|
||||
|
||||
private _initInputSource (): void {
|
||||
const self = this;
|
||||
self._viewLeftPosition = new InputSourcePosition();
|
||||
self._viewLeftPosition.getValue = (): Readonly<Vec3> => Vec3.ZERO;
|
||||
self._viewLeftOrientation = new InputSourceOrientation();
|
||||
self._viewLeftOrientation.getValue = (): Readonly<Quat> => Quat.IDENTITY;
|
||||
|
||||
self._viewRightPosition = new InputSourcePosition();
|
||||
self._viewRightPosition.getValue = (): Readonly<Vec3> => Vec3.ZERO;
|
||||
self._viewRightOrientation = new InputSourceOrientation();
|
||||
self._viewRightOrientation.getValue = (): Readonly<Quat> => Quat.IDENTITY;
|
||||
|
||||
self._headMiddlePosition = new InputSourcePosition();
|
||||
self._headMiddlePosition.getValue = (): Readonly<Vec3> => Vec3.ZERO;
|
||||
self._headMiddleOrientation = new InputSourceOrientation();
|
||||
self._headMiddleOrientation.getValue = (): Readonly<Quat> => Quat.IDENTITY;
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { checkPalIntegrity, withImpl } from '../../integrity-check';
|
||||
|
||||
export * from './accelerometer-input';
|
||||
export * from './gamepad-input';
|
||||
export * from './handle-input';
|
||||
export * from './hmd-input';
|
||||
export * from './handheld-input';
|
||||
export * from './keyboard-input';
|
||||
export * from './mouse-input';
|
||||
export * from './touch-input';
|
||||
|
||||
checkPalIntegrity<typeof import('pal/input')>(withImpl<typeof import('./index')>());
|
||||
@@ -1,181 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { KeyboardCallback } from 'pal/input';
|
||||
import { KeyboardEventData, minigame } from 'pal/minigame';
|
||||
import { systemInfo } from 'pal/system-info';
|
||||
import { KeyCode, EventKeyboard } from '../../../cocos/input/types';
|
||||
import { EventTarget } from '../../../cocos/core/event';
|
||||
import { InputEventType } from '../../../cocos/input/types/event-enum';
|
||||
import { Feature } from '../../system-info/enum-type';
|
||||
|
||||
const code2KeyCode: Record<string, KeyCode> = {
|
||||
Backspace: KeyCode.BACKSPACE,
|
||||
Tab: KeyCode.TAB,
|
||||
Enter: KeyCode.ENTER,
|
||||
ShiftLeft: KeyCode.SHIFT_LEFT,
|
||||
ControlLeft: KeyCode.CTRL_LEFT,
|
||||
AltLeft: KeyCode.ALT_LEFT,
|
||||
ShiftRight: KeyCode.SHIFT_RIGHT,
|
||||
ControlRight: KeyCode.CTRL_RIGHT,
|
||||
AltRight: KeyCode.ALT_RIGHT,
|
||||
Pause: KeyCode.PAUSE,
|
||||
CapsLock: KeyCode.CAPS_LOCK,
|
||||
Escape: KeyCode.ESCAPE,
|
||||
Space: KeyCode.SPACE,
|
||||
PageUp: KeyCode.PAGE_UP,
|
||||
PageDown: KeyCode.PAGE_DOWN,
|
||||
End: KeyCode.END,
|
||||
Home: KeyCode.HOME,
|
||||
ArrowLeft: KeyCode.ARROW_LEFT,
|
||||
ArrowUp: KeyCode.ARROW_UP,
|
||||
ArrowRight: KeyCode.ARROW_RIGHT,
|
||||
ArrowDown: KeyCode.ARROW_DOWN,
|
||||
Insert: KeyCode.INSERT,
|
||||
Delete: KeyCode.DELETE,
|
||||
Digit0: KeyCode.DIGIT_0,
|
||||
Digit1: KeyCode.DIGIT_1,
|
||||
Digit2: KeyCode.DIGIT_2,
|
||||
Digit3: KeyCode.DIGIT_3,
|
||||
Digit4: KeyCode.DIGIT_4,
|
||||
Digit5: KeyCode.DIGIT_5,
|
||||
Digit6: KeyCode.DIGIT_6,
|
||||
Digit7: KeyCode.DIGIT_7,
|
||||
Digit8: KeyCode.DIGIT_8,
|
||||
Digit9: KeyCode.DIGIT_9,
|
||||
KeyA: KeyCode.KEY_A,
|
||||
KeyB: KeyCode.KEY_B,
|
||||
KeyC: KeyCode.KEY_C,
|
||||
KeyD: KeyCode.KEY_D,
|
||||
KeyE: KeyCode.KEY_E,
|
||||
KeyF: KeyCode.KEY_F,
|
||||
KeyG: KeyCode.KEY_G,
|
||||
KeyH: KeyCode.KEY_H,
|
||||
KeyI: KeyCode.KEY_I,
|
||||
KeyJ: KeyCode.KEY_J,
|
||||
KeyK: KeyCode.KEY_K,
|
||||
KeyL: KeyCode.KEY_L,
|
||||
KeyM: KeyCode.KEY_M,
|
||||
KeyN: KeyCode.KEY_N,
|
||||
KeyO: KeyCode.KEY_O,
|
||||
KeyP: KeyCode.KEY_P,
|
||||
KeyQ: KeyCode.KEY_Q,
|
||||
KeyR: KeyCode.KEY_R,
|
||||
KeyS: KeyCode.KEY_S,
|
||||
KeyT: KeyCode.KEY_T,
|
||||
KeyU: KeyCode.KEY_U,
|
||||
KeyV: KeyCode.KEY_V,
|
||||
KeyW: KeyCode.KEY_W,
|
||||
KeyX: KeyCode.KEY_X,
|
||||
KeyY: KeyCode.KEY_Y,
|
||||
KeyZ: KeyCode.KEY_Z,
|
||||
Numpad0: KeyCode.NUM_0,
|
||||
Numpad1: KeyCode.NUM_1,
|
||||
Numpad2: KeyCode.NUM_2,
|
||||
Numpad3: KeyCode.NUM_3,
|
||||
Numpad4: KeyCode.NUM_4,
|
||||
Numpad5: KeyCode.NUM_5,
|
||||
Numpad6: KeyCode.NUM_6,
|
||||
Numpad7: KeyCode.NUM_7,
|
||||
Numpad8: KeyCode.NUM_8,
|
||||
Numpad9: KeyCode.NUM_9,
|
||||
NumpadMultiply: KeyCode.NUM_MULTIPLY,
|
||||
NumpadAdd: KeyCode.NUM_PLUS,
|
||||
NumpadSubtract: KeyCode.NUM_SUBTRACT,
|
||||
NumpadDecimal: KeyCode.NUM_DECIMAL,
|
||||
NumpadDivide: KeyCode.NUM_DIVIDE,
|
||||
NumpadEnter: KeyCode.NUM_ENTER,
|
||||
F1: KeyCode.F1,
|
||||
F2: KeyCode.F2,
|
||||
F3: KeyCode.F3,
|
||||
F4: KeyCode.F4,
|
||||
F5: KeyCode.F5,
|
||||
F6: KeyCode.F6,
|
||||
F7: KeyCode.F7,
|
||||
F8: KeyCode.F8,
|
||||
F9: KeyCode.F9,
|
||||
F10: KeyCode.F10,
|
||||
F11: KeyCode.F11,
|
||||
F12: KeyCode.F12,
|
||||
NumLock: KeyCode.NUM_LOCK,
|
||||
ScrollLock: KeyCode.SCROLL_LOCK,
|
||||
Semicolon: KeyCode.SEMICOLON,
|
||||
Equal: KeyCode.EQUAL,
|
||||
Comma: KeyCode.COMMA,
|
||||
Minus: KeyCode.DASH,
|
||||
Period: KeyCode.PERIOD,
|
||||
Slash: KeyCode.SLASH,
|
||||
Backquote: KeyCode.BACK_QUOTE,
|
||||
BracketLeft: KeyCode.BRACKET_LEFT,
|
||||
Backslash: KeyCode.BACKSLASH,
|
||||
BracketRight: KeyCode.BRACKET_RIGHT,
|
||||
Quote: KeyCode.QUOTE,
|
||||
};
|
||||
|
||||
function getKeyCode (code: string): KeyCode {
|
||||
return code2KeyCode[code] || KeyCode.NONE;
|
||||
}
|
||||
|
||||
export class KeyboardInputSource {
|
||||
private _eventTarget: EventTarget = new EventTarget();
|
||||
|
||||
// KeyboardEvent.repeat is not supported on Wechat PC platform.
|
||||
private _keyStateMap: Record<number, boolean> = {};
|
||||
|
||||
constructor () {
|
||||
if (systemInfo.hasFeature(Feature.EVENT_KEYBOARD)) {
|
||||
this._registerEvent();
|
||||
}
|
||||
}
|
||||
|
||||
private _registerEvent (): void {
|
||||
minigame.wx?.onKeyDown?.((res) => {
|
||||
const keyCode = getKeyCode(res.code);
|
||||
if (!this._keyStateMap[keyCode]) {
|
||||
const eventKeyDown = this._getInputEvent(res, InputEventType.KEY_DOWN);
|
||||
this._eventTarget.emit(InputEventType.KEY_DOWN, eventKeyDown);
|
||||
} else {
|
||||
const eventKeyPressing = this._getInputEvent(res, InputEventType.KEY_PRESSING);
|
||||
this._eventTarget.emit(InputEventType.KEY_PRESSING, eventKeyPressing);
|
||||
}
|
||||
this._keyStateMap[keyCode] = true;
|
||||
});
|
||||
minigame.wx?.onKeyUp?.((res) => {
|
||||
const keyCode = getKeyCode(res.code);
|
||||
const eventKeyUp = this._getInputEvent(res, InputEventType.KEY_UP);
|
||||
this._keyStateMap[keyCode] = false;
|
||||
this._eventTarget.emit(InputEventType.KEY_UP, eventKeyUp);
|
||||
});
|
||||
}
|
||||
|
||||
private _getInputEvent (event: KeyboardEventData, eventType: InputEventType): EventKeyboard {
|
||||
const keyCode = getKeyCode(event.code);
|
||||
const eventKeyboard = new EventKeyboard(keyCode, eventType);
|
||||
return eventKeyboard;
|
||||
}
|
||||
|
||||
public on (eventType: InputEventType, callback: KeyboardCallback, target?: any): void {
|
||||
this._eventTarget.on(eventType, callback, target);
|
||||
}
|
||||
}
|
||||
@@ -1,117 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { MouseCallback } from 'pal/input';
|
||||
import { MouseEventData, MouseWheelEventData, minigame } from 'pal/minigame';
|
||||
import { screenAdapter } from 'pal/screen-adapter';
|
||||
import { systemInfo } from 'pal/system-info';
|
||||
import { Vec2 } from '../../../cocos/core/math';
|
||||
import { EventTarget } from '../../../cocos/core/event';
|
||||
import { EventMouse } from '../../../cocos/input/types';
|
||||
import { InputEventType } from '../../../cocos/input/types/event-enum';
|
||||
import { Feature } from '../../system-info/enum-type';
|
||||
|
||||
export class MouseInputSource {
|
||||
private _eventTarget: EventTarget = new EventTarget();
|
||||
private _isPressed = false;
|
||||
private _preMousePos: Vec2 = new Vec2();
|
||||
|
||||
constructor () {
|
||||
if (systemInfo.hasFeature(Feature.EVENT_MOUSE)) {
|
||||
this._registerEvent();
|
||||
}
|
||||
}
|
||||
|
||||
private _getLocation (event: MouseEventData): Vec2 {
|
||||
const windowSize = screenAdapter.windowSize;
|
||||
const dpr = screenAdapter.devicePixelRatio;
|
||||
const x = event.x * dpr;
|
||||
const y = windowSize.height - event.y * dpr;
|
||||
return new Vec2(x, y);
|
||||
}
|
||||
|
||||
private _registerEvent (): void {
|
||||
minigame.wx?.onMouseDown?.(this._createCallback(InputEventType.MOUSE_DOWN));
|
||||
minigame.wx?.onMouseMove?.(this._createCallback(InputEventType.MOUSE_MOVE));
|
||||
minigame.wx?.onMouseUp?.(this._createCallback(InputEventType.MOUSE_UP));
|
||||
minigame.wx?.onWheel?.(this._handleMouseWheel.bind(this));
|
||||
}
|
||||
|
||||
private _createCallback (eventType: InputEventType) {
|
||||
return (event: MouseEventData): void => {
|
||||
const location = this._getLocation(event);
|
||||
let button = event.button;
|
||||
switch (eventType) {
|
||||
case InputEventType.MOUSE_DOWN:
|
||||
this._isPressed = true;
|
||||
break;
|
||||
case InputEventType.MOUSE_UP:
|
||||
this._isPressed = false;
|
||||
break;
|
||||
case InputEventType.MOUSE_MOVE:
|
||||
if (!this._isPressed) {
|
||||
button = EventMouse.BUTTON_MISSING;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
const eventMouse = new EventMouse(eventType, false, this._preMousePos);
|
||||
eventMouse.setLocation(location.x, location.y);
|
||||
eventMouse.setButton(button);
|
||||
eventMouse.movementX = location.x - this._preMousePos.x;
|
||||
eventMouse.movementY = this._preMousePos.y - location.y;
|
||||
|
||||
// update previous mouse position.
|
||||
this._preMousePos.set(location.x, location.y);
|
||||
this._eventTarget.emit(eventType, eventMouse);
|
||||
};
|
||||
}
|
||||
|
||||
private _handleMouseWheel (event: MouseWheelEventData): void {
|
||||
const eventType = InputEventType.MOUSE_WHEEL;
|
||||
const location = this._getLocation(event);
|
||||
const button = event.button;
|
||||
|
||||
const eventMouse = new EventMouse(eventType, false, this._preMousePos);
|
||||
eventMouse.setLocation(location.x, location.y);
|
||||
eventMouse.setButton(button);
|
||||
eventMouse.movementX = location.x - this._preMousePos.x;
|
||||
eventMouse.movementY = this._preMousePos.y - location.y;
|
||||
|
||||
eventMouse.setScrollData(event.deltaX, -event.deltaY);
|
||||
// update previous mouse position.
|
||||
this._preMousePos.set(location.x, location.y);
|
||||
this._eventTarget.emit(InputEventType.MOUSE_WHEEL, eventMouse);
|
||||
}
|
||||
|
||||
public on (eventType: InputEventType, callback: MouseCallback, target?: any): void {
|
||||
this._eventTarget.on(eventType, callback, target);
|
||||
}
|
||||
|
||||
public dispatchEventsInCache (): void {
|
||||
// Do nothing
|
||||
}
|
||||
}
|
||||