Skip to content

Commit

Permalink
Merge pull request cocos#15725 from knoxHuang/develop
Browse files Browse the repository at this point in the history
Merge remote-tracking branch 'cocos-for-editor/v3.8.1' into develop
  • Loading branch information
minggo authored Jul 14, 2023
2 parents b61096c + 7943b1b commit edcbce0
Show file tree
Hide file tree
Showing 36 changed files with 261 additions and 35 deletions.
1 change: 1 addition & 0 deletions @types/pal/input.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ declare module 'pal/input' {
* Class designed for gamepad input
*/
export class GamepadInputDevice {
private constructor(deviceId: number);
/**
* @engineInternal
*/
Expand Down
8 changes: 2 additions & 6 deletions @types/pal/screen-adapter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,6 @@ declare module 'pal/screen-adapter' {
*/
public get resolution (): import('cocos/core/math').Size;

/**
* Update the resolution by resolutionScale.
* This method rebuilds the size of frame buffer and the size of canvas.
* This method should be called when window resized (fullscreen changing included) or the resolution scale changed.
*/
private _updateResolution ();
/**
* Get and set the resolution scale of screen, which will affect the quality of the rendering.
* Note: if this value is set too high, the rendering performance of GPU will be reduced, this value is 1 by default.
Expand Down Expand Up @@ -123,4 +117,6 @@ declare module 'pal/screen-adapter' {
}

export const screenAdapter: ScreenAdapter;

export {};
}
6 changes: 4 additions & 2 deletions @types/pal/system-info.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ declare module 'pal/system-info' {
*/
public exit(): void;

on (event: PalSystemEvent, cb: (...args: any)=>void, target?: any): void;
off (event: PalSystemEvent, cb?: (...args: any)=>void, target?: any): void;
on (event: PalSystemEvent, cb: (...args: any) => void, target?: any): void;
off (event: PalSystemEvent, cb?: (...args: any) => void, target?: any): void;
// TODO: support onError
}

export const systemInfo: SystemInfo;

export {};
}
49 changes: 27 additions & 22 deletions cocos/core/math/vec3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ export class Vec3 extends ValueType {
// If the directions are almost opposite,
// every vector that orthonormal to the directions can be the rotation axis.
const fromNormalized = Vec3.multiplyScalar(cacheV1, from, 1.0 / lenFrom);
const axis = chooseAnyPerpendicular(cacheV2, fromNormalized);
const axis = Vec3.generateOrthogonal(cacheV2, fromNormalized);
const angle = Math.PI * t;
rotateAxisAngle(cacheV3, fromNormalized, axis, angle);
Vec3.multiplyScalar(out, cacheV3, lenLerped);
Expand Down Expand Up @@ -806,6 +806,32 @@ export class Vec3 extends ValueType {
return out;
}

/**
* @zh 生成指定向量的一个正交单位向量。如果指定的向量 **精确地** 是零向量,则返回 **精确的** 零向量。
* @en Generates an unit vector orthogonal to specified vector.
* If the specified vector is **strictly** zero vector, the result is **strict** zero vector.
* @param out @zh 生成的向量。@en The generated vector.
* @param n @zh 输入向量。该向量 **不必** 是标准化的。 @en The input vector. **Need not** to be normalized.
* @returns `out`
*/
public static generateOrthogonal<Out extends IVec3Like> (out: Out, n: Readonly<IVec3Like>): Out {
const { x, y, z } = n;
// 1. Drop the component with minimal magnitude.
// 2. Negate one of the remain components.
// 3. Swap the remain components.
const absX = Math.abs(x);
const absY = Math.abs(y);
const absZ = Math.abs(z);
if (absX < absY && absX < absZ) {
Vec3.set(out, 0.0, z, -y);
} else if (absY < absZ) {
Vec3.set(out, z, 0.0, -x);
} else {
Vec3.set(out, y, -x, 0.0);
}
return Vec3.normalize(out, out);
}

/**
* @en x component.
* @zh x 分量。
Expand Down Expand Up @@ -1190,27 +1216,6 @@ export function v3 (x?: number | Vec3, y?: number, z?: number): Vec3 {
return new Vec3(x as any, y, z);
}

/**
* Chooses an arbitrary unit vector that is perpendicular to input.
*/
function chooseAnyPerpendicular (out: Vec3, v: Readonly<Vec3>): Vec3 {
const { x, y, z } = v;
// 1. Drop the component with minimal magnitude.
// 2. Negate one of the remain components.
// 3. Swap the remain components.
const absX = Math.abs(x);
const absY = Math.abs(y);
const absZ = Math.abs(z);
if (absX < absY && absX < absZ) {
Vec3.set(out, 0.0, z, -y);
} else if (absY < absZ) {
Vec3.set(out, z, 0.0, -x);
} else {
Vec3.set(out, y, -x, 0.0);
}
return Vec3.normalize(out, out);
}

/**
* Rotates `input` around `axis` for `angle` radians.
*/
Expand Down
17 changes: 14 additions & 3 deletions cocos/terrain/terrain-asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export const TERRAIN_BLOCK_TILE_COMPLEXITY = 32;
export const TERRAIN_BLOCK_VERTEX_COMPLEXITY = 33;
export const TERRAIN_BLOCK_VERTEX_SIZE = 8; // position + normal + uv
export const TERRAIN_HEIGHT_BASE = 32768;
export const TERRAIN_HEIGHT_FACTORY = 1.0 / 512.0;
export const TERRAIN_HEIGHT_FACTORY = 1.0 / 128.0;
export const TERRAIN_HEIGHT_FACTORY_V7 = 1.0 / 512.0;
export const TERRAIN_HEIGHT_FMIN = (-TERRAIN_HEIGHT_BASE) * TERRAIN_HEIGHT_FACTORY;
export const TERRAIN_HEIGHT_FMAX = (65535 - TERRAIN_HEIGHT_BASE) * TERRAIN_HEIGHT_FACTORY;
export const TERRAIN_NORTH_INDEX = 0;
Expand All @@ -47,6 +48,7 @@ export const TERRAIN_DATA_VERSION4 = 0x01010004;
export const TERRAIN_DATA_VERSION5 = 0x01010005;
export const TERRAIN_DATA_VERSION6 = 0x01010006;
export const TERRAIN_DATA_VERSION7 = 0x01010007;
export const TERRAIN_DATA_VERSION8 = 0x01010008;
export const TERRAIN_DATA_VERSION_DEFAULT = 0x01010111;

class TerrainBuffer {
Expand Down Expand Up @@ -475,7 +477,8 @@ export class TerrainAsset extends Asset {
&& this._version !== TERRAIN_DATA_VERSION4
&& this._version !== TERRAIN_DATA_VERSION5
&& this._version !== TERRAIN_DATA_VERSION6
&& this._version !== TERRAIN_DATA_VERSION7) {
&& this._version !== TERRAIN_DATA_VERSION7
&& this._version !== TERRAIN_DATA_VERSION8) {
return false;
}

Expand All @@ -498,6 +501,14 @@ export class TerrainAsset extends Asset {
this.heights[i] = stream.readInt16();
}

if (this._version < TERRAIN_DATA_VERSION8) {
for (let i = 0; i < this.heights.length; ++i) {
const h = (this._heights[i] - TERRAIN_HEIGHT_BASE) * TERRAIN_HEIGHT_FACTORY_V7;
const ch = TERRAIN_HEIGHT_BASE + h / TERRAIN_HEIGHT_FACTORY;
this.heights[i] = ch;
}
}

// normals
if (this._version >= TERRAIN_DATA_VERSION6) {
const normalBufferSize = stream.readInt();
Expand Down Expand Up @@ -560,7 +571,7 @@ export class TerrainAsset extends Asset {
const stream = new TerrainBuffer();

// version
stream.writeInt32(TERRAIN_DATA_VERSION7);
stream.writeInt32(TERRAIN_DATA_VERSION8);

// geometry info
stream.writeDouble(this.tileSize);
Expand Down
3 changes: 3 additions & 0 deletions pal/env/minigame/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

/* eslint-disable import/no-dynamic-require */
import { BAIDU, 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;
Expand Down Expand Up @@ -54,3 +55,5 @@ export function loadJsFile (path: string): any {
}
return require(`../${path}`);
}

checkPalIntegrity<typeof import('pal/env')>(withImpl<typeof import('./env')>());
5 changes: 4 additions & 1 deletion pal/env/native/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
THE SOFTWARE.
*/

import { PREVIEW } from "internal:constants";
import { PREVIEW } from 'internal:constants';
import { checkPalIntegrity, withImpl } from '../../integrity-check';

declare const require: (path: string) => Promise<void>;

Expand Down Expand Up @@ -69,3 +70,5 @@ export function loadJsFile (path: string): Promise<void> {
return require(`${path}`);
}
}

checkPalIntegrity<typeof import('pal/env')>(withImpl<typeof import('./env')>());
3 changes: 3 additions & 0 deletions pal/env/runtime/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

import { COCOSPLAY, HUAWEI, OPPO, VIVO } from 'internal:constants';
import { checkPalIntegrity, withImpl } from '../../integrity-check';

declare const require: (path: string) => Promise<void>;
declare const ral: any;
Expand Down Expand Up @@ -52,3 +53,5 @@ 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')>());
4 changes: 4 additions & 0 deletions pal/env/web/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
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;
Expand Down Expand Up @@ -62,3 +64,5 @@ export function loadJsFile (path: string): Promise<void> {
document.head.appendChild(script);
});
}

checkPalIntegrity<typeof import('pal/env')>(withImpl<typeof import('./env')>());
4 changes: 4 additions & 0 deletions pal/input/minigame/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
THE SOFTWARE.
*/

import { checkPalIntegrity, withImpl } from '../../integrity-check';

export * from './accelerometer-input';
export * from './gamepad-input';
export * from './handle-input';
Expand All @@ -30,3 +32,5 @@ 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')>());
4 changes: 4 additions & 0 deletions pal/input/native/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
THE SOFTWARE.
*/

import { checkPalIntegrity, withImpl } from '../../integrity-check';

export * from './accelerometer-input';
export * from './gamepad-input';
export * from './handle-input';
Expand All @@ -30,3 +32,5 @@ 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')>());
4 changes: 4 additions & 0 deletions pal/input/web/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
THE SOFTWARE.
*/

import { checkPalIntegrity, withImpl } from '../../integrity-check';

export * from './accelerometer-input';
export * from './gamepad-input';
export * from './handle-input';
Expand All @@ -30,3 +32,5 @@ 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')>());
54 changes: 54 additions & 0 deletions pal/integrity-check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
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.
*/

declare const guard: unique symbol;

type Guard = typeof guard;

/**
* Checks if a PAL implementation module is compatible with its target interface module.
*
* @example
* If you write the following in somewhere:
*
* ```ts
* checkPalIntegrity<typeof import('pal-interface-module')>(
* withImpl<typeof import('pal-implementation-module')>());
* ```
*
* you will receive a compilation error
* if your implementation module is not fulfil the interface module.
*
* @note This function should be easily tree-shaken.
*/
export function checkPalIntegrity<T> (impl: T & Guard) {
}

/**
* Utility function, see example of `checkPalIntegrity()`.
*
*/
export function withImpl<T> () {
return 0 as unknown as T & Guard;
}
3 changes: 3 additions & 0 deletions pal/minigame/alipay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

import { IMiniGame } from 'pal/minigame';
import { checkPalIntegrity, withImpl } from '../integrity-check';
import { Orientation } from '../screen-adapter/enum-type';
import { cloneObject, createInnerAudioContextPolyfill } from '../utils';

Expand Down Expand Up @@ -158,3 +159,5 @@ minigame.getSafeArea = function (): SafeArea {
// #endregion SafeArea

export { minigame };

checkPalIntegrity<typeof import('pal/minigame')>(withImpl<typeof import('./alipay')>());
3 changes: 3 additions & 0 deletions pal/minigame/baidu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

import { IMiniGame } from 'pal/minigame';
import { checkPalIntegrity, withImpl } from '../integrity-check';
import { Orientation } from '../screen-adapter/enum-type';
import { cloneObject, createInnerAudioContextPolyfill } from '../utils';

Expand Down Expand Up @@ -111,3 +112,5 @@ minigame.getSafeArea = function (): SafeArea {
// #endregion SafeArea

export { minigame };

checkPalIntegrity<typeof import('pal/minigame')>(withImpl<typeof import('./baidu')>());
3 changes: 3 additions & 0 deletions pal/minigame/bytedance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

import { IMiniGame, SystemInfo } from 'pal/minigame';
import { checkPalIntegrity, withImpl } from '../integrity-check';
import { Orientation } from '../screen-adapter/enum-type';
import { cloneObject, createInnerAudioContextPolyfill } from '../utils';

Expand Down Expand Up @@ -125,3 +126,5 @@ minigame.getSafeArea = function (): SafeArea {
// #endregion SafeArea

export { minigame };

checkPalIntegrity<typeof import('pal/minigame')>(withImpl<typeof import('./bytedance')>());
4 changes: 4 additions & 0 deletions pal/minigame/non-minigame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@
THE SOFTWARE.
*/

import { checkPalIntegrity, withImpl } from '../integrity-check';

const minigame: any = {};
export { minigame };

checkPalIntegrity<typeof import('pal/minigame')>(withImpl<typeof import('./non-minigame')>());
3 changes: 3 additions & 0 deletions pal/minigame/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import { COCOSPLAY, HUAWEI, LINKSURE, OPPO, QTT, VIVO } from 'internal:constants';
import { SystemInfo, IMiniGame } from 'pal/minigame';
import { checkPalIntegrity, withImpl } from '../integrity-check';

import { Orientation } from '../screen-adapter/enum-type';
import { cloneObject, createInnerAudioContextPolyfill } from '../utils';
Expand Down Expand Up @@ -152,3 +153,5 @@ minigame.getSafeArea = function (): SafeArea {
// #endregion SafeArea

export { minigame };

checkPalIntegrity<typeof import('pal/minigame')>(withImpl<typeof import('./runtime')>());
Loading

0 comments on commit edcbce0

Please sign in to comment.