From ac4ff3870ad201aa05791364764685f1fa836ef7 Mon Sep 17 00:00:00 2001 From: Zach Lee Date: Mon, 9 Oct 2023 17:44:02 +0800 Subject: [PATCH 1/8] feat:add imutable texture flag for Webgl2 (#16332) * feat:add imutable texture flag for Webgl2 * fix lint --- cocos/gfx/base/define.ts | 1 + cocos/gfx/webgl2/webgl2-commands.ts | 5 ++++- native/cocos/renderer/gfx-base/GFXDef-common.h | 1 + .../cocos/renderer/gfx-gles3/GLES3Commands.cpp | 17 ++++++++++++++++- 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/cocos/gfx/base/define.ts b/cocos/gfx/base/define.ts index 235e0880c8e..627c192b7b9 100644 --- a/cocos/gfx/base/define.ts +++ b/cocos/gfx/base/define.ts @@ -409,6 +409,7 @@ export enum TextureFlagBit { EXTERNAL_NORMAL = 0x8, // External normal texture LAZILY_ALLOCATED = 0x10, // Try lazily allocated mode. MUTABLE_VIEW_FORMAT = 0x40, // texture view as different format + MUTABLE_STORAGE = 0x80, // mutable storage for gl } export enum FormatFeatureBit { diff --git a/cocos/gfx/webgl2/webgl2-commands.ts b/cocos/gfx/webgl2/webgl2-commands.ts index 80f4caab35c..7938358dc41 100644 --- a/cocos/gfx/webgl2/webgl2-commands.ts +++ b/cocos/gfx/webgl2/webgl2-commands.ts @@ -681,6 +681,7 @@ export class WebGL2CmdDraw extends WebGL2CmdObject { } public clear (): void { + // noop } } @@ -1087,6 +1088,8 @@ export function WebGL2CmdFuncCreateTexture (device: WebGL2Device, gpuTexture: IW w = Math.max(1, w >> 1); h = Math.max(1, h >> 1); } + } else if (gpuTexture.flags & TextureFlagBit.MUTABLE_STORAGE) { + gl.texImage2D(gl.TEXTURE_2D, 0, gpuTexture.glInternalFmt, w, h, 0, gpuTexture.glFormat, gpuTexture.glType, null); } else { gl.texStorage2D(gl.TEXTURE_2D, gpuTexture.mipLevel, gpuTexture.glInternalFmt, w, h); } @@ -2849,7 +2852,7 @@ export function WebGL2CmdFuncCopyTexImagesToTexture ( switch (gpuTexture.glTarget) { case gl.TEXTURE_2D: { - if (toUseTexImage2D(texImages, regions)) { + if ((gpuTexture.flags & TextureFlagBit.MUTABLE_STORAGE) || toUseTexImage2D(texImages, regions)) { gl.texImage2D( gl.TEXTURE_2D, regions[0].texSubres.mipLevel, diff --git a/native/cocos/renderer/gfx-base/GFXDef-common.h b/native/cocos/renderer/gfx-base/GFXDef-common.h index 32712dbba97..84926c05202 100644 --- a/native/cocos/renderer/gfx-base/GFXDef-common.h +++ b/native/cocos/renderer/gfx-base/GFXDef-common.h @@ -479,6 +479,7 @@ enum class TextureFlagBit : uint32_t { EXTERNAL_NORMAL = 0x8, // External normal texture LAZILY_ALLOCATED = 0x10, // Try lazily allocated mode. MUTABLE_VIEW_FORMAT = 0x40, // texture view as different format + MUTABLE_STORAGE = 0x80, // mutable storage for gl image }; using TextureFlags = TextureFlagBit; CC_ENUM_BITWISE_OPERATORS(TextureFlagBit); diff --git a/native/cocos/renderer/gfx-gles3/GLES3Commands.cpp b/native/cocos/renderer/gfx-gles3/GLES3Commands.cpp index 2498af22fe1..6236321fddc 100644 --- a/native/cocos/renderer/gfx-gles3/GLES3Commands.cpp +++ b/native/cocos/renderer/gfx-gles3/GLES3Commands.cpp @@ -843,7 +843,12 @@ static void textureStorage(GLES3Device *device, GLES3GPUTexture *gpuTexture) { } else { auto target = hasFlag(gpuTexture->flags, TextureFlagBit::EXTERNAL_OES) ? GL_TEXTURE_EXTERNAL_OES : GL_TEXTURE_2D; GL_CHECK(glBindTexture(target, gpuTexture->glTexture)); - GL_CHECK(glTexStorage2D(target, gpuTexture->mipLevel, gpuTexture->glInternalFmt, w, h)); + if (hasFlag(gpuTexture->flags, TextureFlagBit::MUTABLE_STORAGE)) { + GL_CHECK(glTexImage2D(GL_TEXTURE_2D, gpuTexture->mipLevel, gpuTexture->glInternalFmt, w, h, 0, + gpuTexture->glFormat, gpuTexture->glType, nullptr)); + } else { + GL_CHECK(glTexStorage2D(GL_TEXTURE_2D, gpuTexture->mipLevel, gpuTexture->glInternalFmt, w, h)); + } } break; case TextureType::TEX3D: @@ -2736,6 +2741,16 @@ void cmdFuncGLES3CopyBuffersToTexture(GLES3Device *device, const uint8_t *const gpuTexture->glFormat, memSize, (GLvoid *)buff)); + } else if (hasFlag(gpuTexture->flags, TextureFlagBit::MUTABLE_STORAGE)) { + GL_CHECK(glTexImage2D(GL_TEXTURE_2D, + gpuTexture->mipLevel, + gpuTexture->glInternalFmt, + destWidth, + destHeight, + 0, + gpuTexture->glFormat, + gpuTexture->glType, + (GLvoid *)buff)); } else { GL_CHECK(glTexSubImage2D(GL_TEXTURE_2D, mipLevel, From 093bc8724e87de1c7407ab336857b8ae33cb665d Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 10 Oct 2023 11:06:02 +0800 Subject: [PATCH 2/8] Update v8 to 11.6.189.22 (#16364) * Update se code to adapt v8 11.4 * Fix setter * VisitHandlesWithClassIds was added again. * Remove log in evalString * Revert in ScriptEngine.cpp * Update external-config.json * Remove unused ndkPath in libcocos2dx/build.gradle * Update external config to v3.8.2-11 --- native/cocos/bindings/jswrapper/v8/Class.cpp | 27 +++++++++++++++++-- .../bindings/jswrapper/v8/ScriptEngine.cpp | 17 +++++++++++- .../bindings/jswrapper/v8/ScriptEngine.h | 9 ++++++- .../jswrapper/v8/debugger/inspector_agent.cpp | 4 +++ .../platform/android/libcocos2dx/build.gradle | 2 -- native/external-config.json | 2 +- 6 files changed, 54 insertions(+), 7 deletions(-) diff --git a/native/cocos/bindings/jswrapper/v8/Class.cpp b/native/cocos/bindings/jswrapper/v8/Class.cpp index 821a0b79bd2..6c3536273e7 100644 --- a/native/cocos/bindings/jswrapper/v8/Class.cpp +++ b/native/cocos/bindings/jswrapper/v8/Class.cpp @@ -206,6 +206,7 @@ bool Class::defineFunction(const char *name, v8::FunctionCallback func, void *da if (jsName.IsEmpty()) { return false; } + _constructorTemplate.Get(__isolate)->PrototypeTemplate()->Set(jsName.ToLocalChecked(), v8::FunctionTemplate::New(__isolate, func, createExternal(__isolate, data))); return true; } @@ -218,7 +219,18 @@ bool Class::defineProperty(const char *name, v8::FunctionCallback getter, v8::Fu auto prototypeTemplate = _constructorTemplate.Get(__isolate)->PrototypeTemplate(); auto externalData = createExternal(__isolate, data); - prototypeTemplate->SetAccessorProperty(jsName.ToLocalChecked(), v8::FunctionTemplate::New(__isolate, getter, externalData), v8::FunctionTemplate::New(__isolate, setter, externalData)); + + v8::Local getterTemplate = v8::Local(); + v8::Local setterTemplate = v8::Local(); + + if (getter != nullptr) { + getterTemplate = v8::FunctionTemplate::New(__isolate, getter, externalData); + } + + if (setter != nullptr) { + setterTemplate = v8::FunctionTemplate::New(__isolate, setter, externalData); + } + prototypeTemplate->SetAccessorProperty(jsName.ToLocalChecked(), getterTemplate, setterTemplate); return true; } @@ -246,7 +258,18 @@ bool Class::defineStaticProperty(const char *name, v8::FunctionCallback getter, } auto externalData = createExternal(__isolate, data); - _constructorTemplate.Get(__isolate)->SetAccessorProperty(jsName.ToLocalChecked(), v8::FunctionTemplate::New(__isolate, getter, externalData), v8::FunctionTemplate::New(__isolate, setter, externalData)); + v8::Local getterTemplate = v8::Local(); + v8::Local setterTemplate = v8::Local(); + + if (getter != nullptr) { + getterTemplate = v8::FunctionTemplate::New(__isolate, getter, externalData); + } + + if (setter != nullptr) { + setterTemplate = v8::FunctionTemplate::New(__isolate, setter, externalData); + } + + _constructorTemplate.Get(__isolate)->SetAccessorProperty(jsName.ToLocalChecked(), getterTemplate, setterTemplate); return true; } diff --git a/native/cocos/bindings/jswrapper/v8/ScriptEngine.cpp b/native/cocos/bindings/jswrapper/v8/ScriptEngine.cpp index 67f08968d7f..bf308fb44b6 100644 --- a/native/cocos/bindings/jswrapper/v8/ScriptEngine.cpp +++ b/native/cocos/bindings/jswrapper/v8/ScriptEngine.cpp @@ -246,7 +246,11 @@ class ScriptEngineV8Context { ~ScriptEngineV8Context() { v8::V8::Dispose(); +#if V8_MAJOR_VERSION > 9 || ( V8_MAJOR_VERSION == 9 && V8_MINOR_VERSION > 7) + v8::V8::DisposePlatform(); +#else v8::V8::ShutdownPlatform(); +#endif delete platform; } v8::Platform *platform = nullptr; @@ -279,12 +283,22 @@ void ScriptEngine::onFatalErrorCallback(const char *location, const char *messag getInstance()->callExceptionCallback(location, message, "(no stack information)"); } -void ScriptEngine::onOOMErrorCallback(const char *location, bool isHeapOom) { +void ScriptEngine::onOOMErrorCallback(const char *location, +#if V8_MAJOR_VERSION > 10 || (V8_MAJOR_VERSION == 10 && V8_MINOR_VERSION > 4) + const v8::OOMDetails& details +#else + bool isHeapOom +#endif + ) { ccstd::string errorStr = "[OOM ERROR] location: "; errorStr += location; ccstd::string message; message = "is heap out of memory: "; +#if V8_MAJOR_VERSION > 10 || (V8_MAJOR_VERSION == 10 && V8_MINOR_VERSION > 4) + if (details.is_heap_oom) { +#else if (isHeapOom) { +#endif message += "true"; } else { message += "false"; @@ -899,6 +913,7 @@ bool ScriptEngine::evalString(const char *script, uint32_t length /* = 0 */, Val if (!success) { SE_LOGE("ScriptEngine::evalString script %s, failed!\n", fileName); } + return success; } diff --git a/native/cocos/bindings/jswrapper/v8/ScriptEngine.h b/native/cocos/bindings/jswrapper/v8/ScriptEngine.h index 59940b65226..c1ada9b3777 100644 --- a/native/cocos/bindings/jswrapper/v8/ScriptEngine.h +++ b/native/cocos/bindings/jswrapper/v8/ScriptEngine.h @@ -361,7 +361,14 @@ class ScriptEngine final { private: static void privateDataFinalize(PrivateObjectBase *privateObj); static void onFatalErrorCallback(const char *location, const char *message); - static void onOOMErrorCallback(const char *location, bool isHeapOom); + + static void onOOMErrorCallback(const char *location, +#if V8_MAJOR_VERSION > 10 || (V8_MAJOR_VERSION == 10 && V8_MINOR_VERSION > 4) + const v8::OOMDetails& details +#else + bool isHeapOom +#endif + ); static void onMessageCallback(v8::Local message, v8::Local data); static void onPromiseRejectCallback(v8::PromiseRejectMessage msg); diff --git a/native/cocos/bindings/jswrapper/v8/debugger/inspector_agent.cpp b/native/cocos/bindings/jswrapper/v8/debugger/inspector_agent.cpp index 942b212f9db..6f06a12853d 100644 --- a/native/cocos/bindings/jswrapper/v8/debugger/inspector_agent.cpp +++ b/native/cocos/bindings/jswrapper/v8/debugger/inspector_agent.cpp @@ -391,7 +391,11 @@ class ChannelImpl final : public v8_inspector::V8Inspector::Channel { explicit ChannelImpl(V8Inspector *inspector, InspectorSessionDelegate *delegate) : delegate_(delegate) { +#if V8_MAJOR_VERSION > 10 || (V8_MAJOR_VERSION == 10 && V8_MINOR_VERSION > 3) + session_ = inspector->connect(1, this, StringView(), v8_inspector::V8Inspector::ClientTrustLevel::kFullyTrusted); +#else session_ = inspector->connect(1, this, StringView()); +#endif } virtual ~ChannelImpl() {} diff --git a/native/cocos/platform/android/libcocos2dx/build.gradle b/native/cocos/platform/android/libcocos2dx/build.gradle index 6309734c1b2..5c47318e45b 100644 --- a/native/cocos/platform/android/libcocos2dx/build.gradle +++ b/native/cocos/platform/android/libcocos2dx/build.gradle @@ -1,9 +1,7 @@ apply plugin: 'com.android.library' android { - ndkPath PROP_NDK_PATH compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger() - ndkPath PROP_NDK_PATH namespace 'com.cocos.lib' defaultConfig { minSdkVersion PROP_MIN_SDK_VERSION diff --git a/native/external-config.json b/native/external-config.json index 88416cdce72..432a1a05969 100644 --- a/native/external-config.json +++ b/native/external-config.json @@ -3,6 +3,6 @@ "type": "github", "owner": "cocos-creator", "name": "engine-native-external", - "checkout": "v3.8.2-9" + "checkout": "v3.8.2-11" } } \ No newline at end of file From fcb95b5a3741d6e115a8727931f6c4c64ba6272d Mon Sep 17 00:00:00 2001 From: oahc09 Date: Tue, 10 Oct 2023 13:53:38 +0800 Subject: [PATCH 3/8] feat:[xr] support dispatch xr event to ts (#16382) * feat:[xr] support dispatch xr event to ts * improve:add se::AutoHandleScope scope; --- native/cocos/platform/interfaces/modules/XRCommon.h | 1 + native/cocos/platform/java/modules/XRInterface.cpp | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/native/cocos/platform/interfaces/modules/XRCommon.h b/native/cocos/platform/interfaces/modules/XRCommon.h index 682c448c005..d33f2115081 100644 --- a/native/cocos/platform/interfaces/modules/XRCommon.h +++ b/native/cocos/platform/interfaces/modules/XRCommon.h @@ -120,6 +120,7 @@ enum class XRConfigKey { ASYNC_LOAD_ASSETS_IMAGE_RESULTS = 56, LEFT_CONTROLLER_ACTIVE = 57, RIGHT_CONTROLLER_ACTIVE= 58, + TS_EVENT_CALLBACK = 59, MAX_COUNT }; diff --git a/native/cocos/platform/java/modules/XRInterface.cpp b/native/cocos/platform/java/modules/XRInterface.cpp index b6224a9f137..b59f0447cfb 100644 --- a/native/cocos/platform/java/modules/XRInterface.cpp +++ b/native/cocos/platform/java/modules/XRInterface.cpp @@ -499,6 +499,11 @@ void XRInterface::initialize(void *javaVM, void *activity) { }); } else if (key == xr::XRConfigKey::ASYNC_LOAD_ASSETS_IMAGE && value.isInt()) { _isFlipPixelY = value.getInt() == static_cast(gfx::API::GLES3); + } else if (key == xr::XRConfigKey::TS_EVENT_CALLBACK) { + se::AutoHandleScope scope; + se::ValueArray args; + args.emplace_back(se::Value(value.getString())); + EventDispatcher::doDispatchJsEvent("onXREvent", args); } }); #if XR_OEM_PICO @@ -871,6 +876,7 @@ bool XRInterface::beginRenderEyeFrame(uint32_t eye) { #if CC_USE_XR if (IS_ENABLE_XR_LOG) CC_LOG_INFO("[XR] beginRenderEyeFrame %d", eye); if (_isEnabledEyeRenderJsCallback) { + se::AutoHandleScope scope; se::ValueArray args; args.emplace_back(se::Value(eye)); EventDispatcher::doDispatchJsEvent("onXREyeRenderBegin", args); @@ -895,6 +901,7 @@ bool XRInterface::endRenderEyeFrame(uint32_t eye) { #if CC_USE_XR if (IS_ENABLE_XR_LOG) CC_LOG_INFO("[XR] endRenderEyeFrame %d", eye); if (_isEnabledEyeRenderJsCallback) { + se::AutoHandleScope scope; se::ValueArray args; args.emplace_back(se::Value(eye)); EventDispatcher::doDispatchJsEvent("onXREyeRenderEnd", args); From 6eb501d6ab488a004453d9cb5f72e65c3404215c Mon Sep 17 00:00:00 2001 From: Zeqiang Li Date: Tue, 10 Oct 2023 16:46:54 +0800 Subject: [PATCH 4/8] geometry renderer macro in physx (#16396) --- native/cocos/physics/physx/PhysXWorld.cpp | 4 +++ native/cocos/physics/physx/PhysXWorld.h | 37 ++++++++++++++--------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/native/cocos/physics/physx/PhysXWorld.cpp b/native/cocos/physics/physx/PhysXWorld.cpp index bc19ec27eb2..a962ab35629 100644 --- a/native/cocos/physics/physx/PhysXWorld.cpp +++ b/native/cocos/physics/physx/PhysXWorld.cpp @@ -125,9 +125,12 @@ void PhysXWorld::step(float fixedTimeStep) { _mScene->simulate(fixedTimeStep); _mScene->fetchResults(true); syncPhysicsToScene(); +#if CC_USE_GEOMETRY_RENDERER debugDraw(); +#endif } +#if CC_USE_GEOMETRY_RENDERER pipeline::GeometryRenderer* PhysXWorld::getDebugRenderer () { auto cameras = Root::getInstance()->getMainWindow()->getCameras(); scene::Camera* camera = nullptr; @@ -222,6 +225,7 @@ void PhysXWorld::setDebugDrawConstraintSize(float size) { float PhysXWorld::getDebugDrawConstraintSize() { return _debugConstraintSize; } +#endif void PhysXWorld::setGravity(float x, float y, float z) { _mScene->setGravity(physx::PxVec3(x, y, z)); diff --git a/native/cocos/physics/physx/PhysXWorld.h b/native/cocos/physics/physx/PhysXWorld.h index 561fde2efaf..3315a4550fd 100644 --- a/native/cocos/physics/physx/PhysXWorld.h +++ b/native/cocos/physics/physx/PhysXWorld.h @@ -28,7 +28,6 @@ #include "base/Macros.h" #include "base/std/container/vector.h" #include "core/scene-graph/Node.h" -#include "renderer/pipeline/GeometryRenderer.h" #include "physics/physx/PhysXEventManager.h" #include "physics/physx/PhysXFilterShader.h" #include "physics/physx/PhysXInc.h" @@ -36,6 +35,7 @@ #include "physics/physx/PhysXSharedBody.h" #include "physics/physx/character-controllers/PhysXCharacterController.h" #include "physics/spec/IWorld.h" +#include "renderer/pipeline/GeometryRenderer.h" namespace cc { namespace physics { @@ -61,17 +61,17 @@ class PhysXWorld final : virtual public IPhysicsWorld { RaycastResult &raycastClosestResult() override; bool sweep(RaycastOptions &opt, const physx::PxGeometry &geometry, const physx::PxQuat &orientation); - bool sweepClosest(RaycastOptions& opt, const physx::PxGeometry& geometry, const physx::PxQuat& orientation); + bool sweepClosest(RaycastOptions &opt, const physx::PxGeometry &geometry, const physx::PxQuat &orientation); bool sweepBox(RaycastOptions &opt, float halfExtentX, float halfExtentY, float halfExtentZ, - float orientationW, float orientationX, float orientationY, float orientationZ) override; + float orientationW, float orientationX, float orientationY, float orientationZ) override; bool sweepBoxClosest(RaycastOptions &opt, float halfExtentX, float halfExtentY, float halfExtentZ, - float orientationW, float orientationX, float orientationY, float orientationZ) override; + float orientationW, float orientationX, float orientationY, float orientationZ) override; bool sweepSphere(RaycastOptions &opt, float radius) override; bool sweepSphereClosest(RaycastOptions &opt, float radius) override; bool sweepCapsule(RaycastOptions &opt, float radius, float height, - float orientationW, float orientationX, float orientationY, float orientationZ) override; + float orientationW, float orientationX, float orientationY, float orientationZ) override; bool sweepCapsuleClosest(RaycastOptions &opt, float radius, float height, - float orientationW, float orientationX, float orientationY, float orientationZ) override; + float orientationW, float orientationX, float orientationY, float orientationZ) override; ccstd::vector &sweepResult() override; RaycastResult &sweepClosestResult() override; @@ -86,7 +86,7 @@ class PhysXWorld final : virtual public IPhysicsWorld { inline ccstd::vector> &getContactEventPairs() override { return _mEventMgr->getConatctPairs(); } - inline ccstd::vector>& getCCTShapeEventPairs() override { + inline ccstd::vector> &getCCTShapeEventPairs() override { return _mEventMgr->getCCTShapePairs(); } inline ccstd::vector> &getCCTTriggerEventPairs() override { @@ -109,13 +109,13 @@ class PhysXWorld final : virtual public IPhysicsWorld { void removeActor(const PhysXSharedBody &sb); void addCCT(const PhysXCharacterController &cct); void removeCCT(const PhysXCharacterController &cct); - - //Mapping PhysX Object ID and Pointer + + // Mapping PhysX Object ID and Pointer uint32_t addPXObject(uintptr_t PXObjectPtr); void removePXObject(uint32_t pxObjectID); uintptr_t getPXPtrWithPXObjectID(uint32_t pxObjectID); - //Mapping Wrapper PhysX Object ID and Pointer + // Mapping Wrapper PhysX Object ID and Pointer uint32_t addWrapperObject(uintptr_t wrapperObjectPtr); void removeWrapperObject(uint32_t wrapperObjectID); uintptr_t getWrapperPtrWithObjectID(uint32_t wrapperObjectID); @@ -125,17 +125,24 @@ class PhysXWorld final : virtual public IPhysicsWorld { float getFixedTimeStep() const override { return _fixedTimeStep; } void setFixedTimeStep(float fixedTimeStep) override { _fixedTimeStep = fixedTimeStep; } - virtual void setDebugDrawFlags(EPhysicsDrawFlags flags) override; - virtual EPhysicsDrawFlags getDebugDrawFlags() override; +#if CC_USE_GEOMETRY_RENDERER + void setDebugDrawFlags(EPhysicsDrawFlags flags) override; + EPhysicsDrawFlags getDebugDrawFlags() override; - virtual void setDebugDrawConstraintSize(float size) override; - virtual float getDebugDrawConstraintSize() override; + void setDebugDrawConstraintSize(float size) override; + float getDebugDrawConstraintSize() override; private: - pipeline::GeometryRenderer* getDebugRenderer(); + pipeline::GeometryRenderer *getDebugRenderer(); void debugDraw(); void setDebugDrawMode(); +#else + void setDebugDrawFlags(EPhysicsDrawFlags flags) override{}; + EPhysicsDrawFlags getDebugDrawFlags() override { return EPhysicsDrawFlags::NONE; }; + void setDebugDrawConstraintSize(float size) override{}; + float getDebugDrawConstraintSize() override { return 0.0; }; +#endif private: static PhysXWorld *instance; physx::PxFoundation *_mFoundation; From db5779c77c72ae6a2ec7509d26b30910a74578fb Mon Sep 17 00:00:00 2001 From: Ling Zhan Date: Tue, 10 Oct 2023 16:57:01 +0800 Subject: [PATCH 5/8] V3.8.2 box2d wasm node scale performance opt (#16386) * node scale cause box2d shape recreate, low perf * box2d and polygon2d shape * WASM_OBJECT_PTR_2_TS_OBJECT WASM_OBJECT_PTR_2_WASM_OBJECT * tweak * update external-config.json to v3.8.2-12 --- cocos/physics-2d/box2d-wasm/instantiated.ts | 71 ++++++++++++++----- .../physics-2d/box2d-wasm/joints/joint-2d.ts | 10 +-- .../physics-2d/box2d-wasm/physics-contact.ts | 13 ++-- cocos/physics-2d/box2d-wasm/physics-world.ts | 34 ++++----- .../platform/physics-aabb-query-callback.ts | 16 ++--- .../platform/physics-ray-cast-callback.ts | 15 ++-- cocos/physics-2d/box2d-wasm/rigid-body.ts | 13 ++-- .../box2d-wasm/shapes/box-shape-2d.ts | 11 ++- .../box2d-wasm/shapes/circle-shape-2d.ts | 14 ++-- .../box2d-wasm/shapes/polygon-shape-2d.ts | 33 +++++---- .../physics-2d/box2d-wasm/shapes/shape-2d.ts | 58 ++++++++------- native/external-config.json | 2 +- 12 files changed, 163 insertions(+), 127 deletions(-) diff --git a/cocos/physics-2d/box2d-wasm/instantiated.ts b/cocos/physics-2d/box2d-wasm/instantiated.ts index fcb536b7263..a59969209c8 100644 --- a/cocos/physics-2d/box2d-wasm/instantiated.ts +++ b/cocos/physics-2d/box2d-wasm/instantiated.ts @@ -40,6 +40,14 @@ export function getImplPtr (wasmObject: any): number { return (wasmObject).$$.ptr as number; } +// type : Fixture, Body, Contact, Joint, ... +export const enum B2ObjectType { + Fixture = 0, + Body, + Contact, + Joint, +} + /** * mapping wasm-object-ptr to ts-object * B2.Fixture pointer -->B2Shape2D @@ -48,22 +56,35 @@ export function getImplPtr (wasmObject: any): number { * B2.Joint pointer --> B2Joint * ... */ -const WASM_OBJECT_PTR_2_TS_OBJECT = {}; -export function addImplPtrReference (TSObject: any, implPtr: number): void { - if (implPtr) { WASM_OBJECT_PTR_2_TS_OBJECT[implPtr] = TSObject; } -} -export function removeImplPtrReference (implPtr: number): void { +const WASM_OBJECT_PTR_2_TS_OBJECT = new Map>(); + +export function addImplPtrReference (Type: B2ObjectType, TSObject: any, implPtr: number): void { if (implPtr) { - delete WASM_OBJECT_PTR_2_TS_OBJECT[implPtr]; + let map = WASM_OBJECT_PTR_2_TS_OBJECT.get(Type); + if (!map) { + map = new Map(); + WASM_OBJECT_PTR_2_TS_OBJECT.set(Type, map); + } + map.set(implPtr, TSObject); } } -export function getTSObjectFromWASMObjectPtr (implPtr: number): T { - // eslint-disable-next-line @typescript-eslint/no-unsafe-return - return WASM_OBJECT_PTR_2_TS_OBJECT[implPtr]; + +export function removeImplPtrReference (Type: B2ObjectType, implPtr: number): void { + if (implPtr) { + const map = WASM_OBJECT_PTR_2_TS_OBJECT.get(Type); + if (map && map.has(implPtr)) { + map.delete(implPtr); + if (map.size === 0) { + WASM_OBJECT_PTR_2_TS_OBJECT.delete(Type); + } + } + } } -export function getTSObjectFromWASMObject (impl: any): T { + +export function getTSObjectFromWASMObjectPtr (Type: B2ObjectType, implPtr: number): T { + const map = WASM_OBJECT_PTR_2_TS_OBJECT.get(Type); // eslint-disable-next-line @typescript-eslint/no-unsafe-return - return WASM_OBJECT_PTR_2_TS_OBJECT[getImplPtr(impl)]; + return map?.get(implPtr); } /** @@ -74,20 +95,34 @@ export function getTSObjectFromWASMObject (impl: any): T { * B2.Joint pointer --> B2.Joint * ... */ -const WASM_OBJECT_PTR_2_WASM_OBJECT = {}; -export function addImplPtrReferenceWASM (WASMObject: any, implPtr: number): void { - if (implPtr) { WASM_OBJECT_PTR_2_WASM_OBJECT[implPtr] = WASMObject; } +const WASM_OBJECT_PTR_2_WASM_OBJECT = new Map>(); +export function addImplPtrReferenceWASM (Type: B2ObjectType, WASMObject: any, implPtr: number): void { + if (implPtr) { + let map = WASM_OBJECT_PTR_2_WASM_OBJECT.get(Type); + if (!map) { + map = new Map(); + WASM_OBJECT_PTR_2_WASM_OBJECT.set(Type, map); + } + map.set(implPtr, WASMObject); + } } -export function removeImplPtrReferenceWASM (implPtr: number): void { +export function removeImplPtrReferenceWASM (Type: B2ObjectType, implPtr: number): void { if (implPtr) { - delete WASM_OBJECT_PTR_2_WASM_OBJECT[implPtr]; + const map = WASM_OBJECT_PTR_2_WASM_OBJECT.get(Type); + if (map && map.has(implPtr)) { + map.delete(implPtr); + if (map.size === 0) { + WASM_OBJECT_PTR_2_WASM_OBJECT.delete(Type); + } + } } } -export function getWASMObjectFromWASMObjectPtr (implPtr: number): T { +export function getWASMObjectFromWASMObjectPtr (Type: B2ObjectType, implPtr: number): T { + const map = WASM_OBJECT_PTR_2_WASM_OBJECT.get(Type); // eslint-disable-next-line @typescript-eslint/no-unsafe-return - return WASM_OBJECT_PTR_2_WASM_OBJECT[implPtr]; + return map?.get(implPtr); } /** diff --git a/cocos/physics-2d/box2d-wasm/joints/joint-2d.ts b/cocos/physics-2d/box2d-wasm/joints/joint-2d.ts index b0ba8173bff..841c2aa3fea 100644 --- a/cocos/physics-2d/box2d-wasm/joints/joint-2d.ts +++ b/cocos/physics-2d/box2d-wasm/joints/joint-2d.ts @@ -22,7 +22,7 @@ THE SOFTWARE. */ -import { B2, addImplPtrReference, addImplPtrReferenceWASM, getImplPtr, removeImplPtrReference, removeImplPtrReferenceWASM } from '../instantiated'; +import { B2, B2ObjectType, addImplPtrReference, addImplPtrReferenceWASM, getImplPtr, removeImplPtrReference, removeImplPtrReferenceWASM } from '../instantiated'; import { IJoint2D } from '../../spec/i-physics-joint'; import { Joint2D, PhysicsSystem2D, RigidBody2D } from '../../framework'; import { B2PhysicsWorld } from '../physics-world'; @@ -105,8 +105,8 @@ export class B2Joint implements IJoint2D { def.collideConnected = comp.collideConnected; this._b2joint = (PhysicsSystem2D.instance.physicsWorld as B2PhysicsWorld).impl.CreateJoint(def); - addImplPtrReference(this, getImplPtr(this._b2joint)); - addImplPtrReferenceWASM(this._b2joint, getImplPtr(this._b2joint)); + addImplPtrReference(B2ObjectType.Joint, this, getImplPtr(this._b2joint)); + addImplPtrReferenceWASM(B2ObjectType.Joint, this._b2joint, getImplPtr(this._b2joint)); this._inited = true; } @@ -114,8 +114,8 @@ export class B2Joint implements IJoint2D { destroy (): void { if (!this._inited) return; - removeImplPtrReference(getImplPtr(this._b2joint)); - removeImplPtrReferenceWASM(getImplPtr(this._b2joint)); + removeImplPtrReference(B2ObjectType.Joint, getImplPtr(this._b2joint)); + removeImplPtrReferenceWASM(B2ObjectType.Joint, getImplPtr(this._b2joint)); (PhysicsSystem2D.instance.physicsWorld as B2PhysicsWorld).impl.DestroyJoint(this._b2joint!); this._b2joint = null; diff --git a/cocos/physics-2d/box2d-wasm/physics-contact.ts b/cocos/physics-2d/box2d-wasm/physics-contact.ts index 85f180a9a0e..9eeceb5bcb7 100644 --- a/cocos/physics-2d/box2d-wasm/physics-contact.ts +++ b/cocos/physics-2d/box2d-wasm/physics-contact.ts @@ -22,7 +22,7 @@ THE SOFTWARE. */ -import { B2, addImplPtrReference, getTSObjectFromWASMObjectPtr, removeImplPtrReference } from './instantiated'; +import { B2, B2ObjectType, addImplPtrReference, getTSObjectFromWASMObjectPtr, removeImplPtrReference } from './instantiated'; import { Vec2 } from '../../core'; import { PHYSICS_2D_PTM_RATIO } from '../framework/physics-types'; import { Collider2D, Contact2DType, PhysicsSystem2D } from '../framework'; @@ -74,7 +74,7 @@ export class PhysicsContact implements IPhysics2DContact { } static put (b2contact: number): void { - const c = getTSObjectFromWASMObjectPtr(b2contact); + const c = getTSObjectFromWASMObjectPtr(B2ObjectType.Contact, b2contact); if (!c) return; pools.push(c); @@ -97,15 +97,16 @@ export class PhysicsContact implements IPhysics2DContact { } init (b2contact: number): void { - this.colliderA = (getTSObjectFromWASMObjectPtr(B2.ContactGetFixtureA(b2contact) as number)).collider; - this.colliderB = (getTSObjectFromWASMObjectPtr(B2.ContactGetFixtureB(b2contact) as number)).collider; + const ab = B2.ContactGetFixture(b2contact) as Vec2; + this.colliderA = (getTSObjectFromWASMObjectPtr(B2ObjectType.Fixture, ab.x)).collider; + this.colliderB = (getTSObjectFromWASMObjectPtr(B2ObjectType.Fixture, ab.y)).collider; this.disabled = false; this.disabledOnce = false; this._impulsePtr = 0; this._inverted = false; this._implPtr = b2contact; - addImplPtrReference(this, this._implPtr); + addImplPtrReference(B2ObjectType.Contact, this, this._implPtr); this._b2WorldmanifoldPtr = B2.WorldManifoldNew(); } @@ -119,7 +120,7 @@ export class PhysicsContact implements IPhysics2DContact { this.disabled = false; this._impulsePtr = 0; - removeImplPtrReference(this._implPtr); + removeImplPtrReference(B2ObjectType.Contact, this._implPtr); this._implPtr = 0; B2.WorldManifoldDelete(this._b2WorldmanifoldPtr); diff --git a/cocos/physics-2d/box2d-wasm/physics-world.ts b/cocos/physics-2d/box2d-wasm/physics-world.ts index c0997b14915..e5f7b95fa07 100644 --- a/cocos/physics-2d/box2d-wasm/physics-world.ts +++ b/cocos/physics-2d/box2d-wasm/physics-world.ts @@ -23,8 +23,8 @@ */ import { EDITOR_NOT_IN_PREVIEW } from 'internal:constants'; -import { B2, getImplPtr, addImplPtrReference, addImplPtrReferenceWASM, getTSObjectFromWASMObject, - getTSObjectFromWASMObjectPtr, removeImplPtrReference, removeImplPtrReferenceWASM } from './instantiated'; +import { B2, getImplPtr, addImplPtrReference, addImplPtrReferenceWASM, + getTSObjectFromWASMObjectPtr, removeImplPtrReference, removeImplPtrReferenceWASM, B2ObjectType } from './instantiated'; import { IPhysicsWorld } from '../spec/i-physics-world'; import { IVec2Like, Vec3, Quat, toRadian, Vec2, toDegree, Rect, CCObject, js } from '../../core'; import { PHYSICS_2D_PTM_RATIO, ERaycast2DType, ERigidBody2DType } from '../framework/physics-types'; @@ -64,6 +64,7 @@ export class B2PhysicsWorld implements IPhysicsWorld { private _temoBodyDef: B2.BodyDef; private _tempB2AABB: B2.AABB; + public tempB2FixtureDefPtr: number; get impl (): B2.World { return this._world; @@ -88,6 +89,7 @@ export class B2PhysicsWorld implements IPhysicsWorld { this._temoBodyDef = new B2.BodyDef(); this._tempB2AABB = new B2.AABB(); + this.tempB2FixtureDefPtr = B2.FixtureDefNew(); } _debugGraphics: Graphics | null = null; @@ -189,7 +191,7 @@ export class B2PhysicsWorld implements IPhysicsWorld { const results: RaycastResult2D[] = []; for (let i = 0, l = fixtures.length; i < l; i++) { const fixture = fixtures[i]; - const shape = getTSObjectFromWASMObject(fixture); + const shape = getTSObjectFromWASMObjectPtr(B2ObjectType.Fixture, fixture); const collider = shape.collider; if (type === ERaycast2DType.AllClosest) { @@ -319,8 +321,8 @@ export class B2PhysicsWorld implements IPhysicsWorld { bodyDef.angularVelocity = toRadian(compPrivate._angularVelocity as number); const b2Body = this._world.CreateBody(bodyDef); - addImplPtrReference(body, getImplPtr(b2Body)); - addImplPtrReferenceWASM(b2Body, getImplPtr(b2Body)); + addImplPtrReference(B2ObjectType.Body, body, getImplPtr(b2Body)); + addImplPtrReferenceWASM(B2ObjectType.Body, b2Body, getImplPtr(b2Body)); body._imp = b2Body; this._bodies.push(body); @@ -331,8 +333,8 @@ export class B2PhysicsWorld implements IPhysicsWorld { return; } if (body.impl) { - removeImplPtrReference(getImplPtr(body.impl)); - removeImplPtrReferenceWASM(getImplPtr(body.impl)); + removeImplPtrReference(B2ObjectType.Body, getImplPtr(body.impl)); + removeImplPtrReferenceWASM(B2ObjectType.Body, getImplPtr(body.impl)); this._world.DestroyBody(body.impl); body._imp = null; } @@ -344,11 +346,11 @@ export class B2PhysicsWorld implements IPhysicsWorld { } } - registerContactFixture (fixture: B2.Fixture): void { - this._contactListener.registerContactFixture(getImplPtr(fixture)); + registerContactFixture (fixture: number): void { //B2.Fixture ptr + this._contactListener.registerContactFixture(fixture); } - unregisterContactFixture (fixture: B2.Fixture): void { - this._contactListener.unregisterContactFixture(getImplPtr(fixture)); + unregisterContactFixture (fixture: number): void { //B2.Fixture ptr + this._contactListener.unregisterContactFixture(fixture); } testPoint (point: Vec2): readonly Collider2D[] { @@ -366,7 +368,7 @@ export class B2PhysicsWorld implements IPhysicsWorld { const fixtures = PhysicsAABBQueryCallback.getFixtures(); testResults.length = 0; for (let i = 0; i < fixtures.length; i++) { - const collider = getTSObjectFromWASMObject(fixtures[i]).collider; + const collider = getTSObjectFromWASMObjectPtr(B2ObjectType.Fixture, fixtures[i]).collider; if (!testResults.includes(collider)) { testResults.push(collider); } @@ -385,7 +387,7 @@ export class B2PhysicsWorld implements IPhysicsWorld { const fixtures = PhysicsAABBQueryCallback.getFixtures(); testResults.length = 0; for (let i = 0; i < fixtures.length; i++) { - const collider = getTSObjectFromWASMObject(fixtures[i]).collider; + const collider = getTSObjectFromWASMObjectPtr(B2ObjectType.Fixture, fixtures[i]).collider; if (!testResults.includes(collider)) { testResults.push(collider); } @@ -409,7 +411,7 @@ export class B2PhysicsWorld implements IPhysicsWorld { } _onEndContact (b2contact: number): void { - const c = getTSObjectFromWASMObjectPtr(b2contact); + const c = getTSObjectFromWASMObjectPtr(B2ObjectType.Contact, b2contact); if (!c) { return; } @@ -419,7 +421,7 @@ export class B2PhysicsWorld implements IPhysicsWorld { } _onPreSolve (b2contact: number): void { - const c = getTSObjectFromWASMObjectPtr(b2contact); + const c = getTSObjectFromWASMObjectPtr(B2ObjectType.Contact, b2contact); if (!c) { return; } @@ -428,7 +430,7 @@ export class B2PhysicsWorld implements IPhysicsWorld { } _onPostSolve (b2contact: number, impulse: number): void { - const c = getTSObjectFromWASMObjectPtr(b2contact); + const c = getTSObjectFromWASMObjectPtr(B2ObjectType.Contact, b2contact); if (!c) { return; } diff --git a/cocos/physics-2d/box2d-wasm/platform/physics-aabb-query-callback.ts b/cocos/physics-2d/box2d-wasm/platform/physics-aabb-query-callback.ts index 657dcc2b407..6cc67b023e1 100644 --- a/cocos/physics-2d/box2d-wasm/platform/physics-aabb-query-callback.ts +++ b/cocos/physics-2d/box2d-wasm/platform/physics-aabb-query-callback.ts @@ -22,14 +22,13 @@ THE SOFTWARE. */ -import { B2, getTSObjectFromWASMObject, getWASMObjectFromWASMObjectPtr } from '../instantiated'; +import { B2 } from '../instantiated'; import { Vec2 } from '../../../core'; -import { B2RigidBody2D } from '../rigid-body'; export class PhysicsAABBQueryCallback { static _point = { x: 0, y: 0 }; static _isPoint = false; - static _fixtures: B2.Fixture[] = []; + static _fixtures: number[] = [];//B2.Fixture ptr static init (point?: Vec2): void { if (point) { @@ -43,9 +42,9 @@ export class PhysicsAABBQueryCallback { this._fixtures.length = 0; } - static ReportFixture (fixture: B2.Fixture): boolean { + static ReportFixture (fixture: number): boolean { if (this._isPoint) { - if (fixture.TestPoint(this._point)) { + if (B2.FixtureTestPoint(fixture, this._point)) { this._fixtures.push(fixture); } } else { @@ -56,18 +55,17 @@ export class PhysicsAABBQueryCallback { return true; } - static getFixture (): any { + static getFixture (): number { return this._fixtures[0]; } - static getFixtures (): any[] { + static getFixtures (): number[] { return this._fixtures; } static callback = { ReportFixture (fixture: number): boolean { - const f = getWASMObjectFromWASMObjectPtr(fixture); - return PhysicsAABBQueryCallback.ReportFixture(f); + return PhysicsAABBQueryCallback.ReportFixture(fixture); }, }; } diff --git a/cocos/physics-2d/box2d-wasm/platform/physics-ray-cast-callback.ts b/cocos/physics-2d/box2d-wasm/platform/physics-ray-cast-callback.ts index 3c067a73da2..801165e8419 100644 --- a/cocos/physics-2d/box2d-wasm/platform/physics-ray-cast-callback.ts +++ b/cocos/physics-2d/box2d-wasm/platform/physics-ray-cast-callback.ts @@ -22,15 +22,13 @@ THE SOFTWARE. */ -import { B2, getTSObjectFromWASMObject, getWASMObjectFromWASMObjectPtr } from '../instantiated'; +import { B2 } from '../instantiated'; import { Vec2 } from '../../../core'; import { ERaycast2DType } from '../../framework'; -import { B2Shape2D } from '../shapes/shape-2d'; -import { B2RigidBody2D } from '../rigid-body'; export class PhysicsRayCastCallback {// extends B2.RayCastCallback { static _type = ERaycast2DType.Closest; - static _fixtures: B2.Fixture[] = []; + static _fixtures: number[] = [];//B2.Fixture ptr static _points: Vec2[] = []; static _normals: Vec2[] = []; static _fractions: number[] = []; @@ -46,8 +44,8 @@ export class PhysicsRayCastCallback {// extends B2.RayCastCallback { PhysicsRayCastCallback._fractions.length = 0; } - static ReportFixture (fixture: B2.Fixture, point: B2.Vec2, normal: B2.Vec2, fraction: number): any { - if ((fixture.GetFilterData().categoryBits & PhysicsRayCastCallback._mask) === 0) { + static ReportFixture (fixture: number, point: B2.Vec2, normal: B2.Vec2, fraction: number): any { + if ((B2.FixtureGetFilterData(fixture).categoryBits & PhysicsRayCastCallback._mask) === 0) { return 0; } @@ -73,7 +71,7 @@ export class PhysicsRayCastCallback {// extends B2.RayCastCallback { return fraction; } - static getFixtures (): B2.Fixture[] { + static getFixtures (): number[] { return PhysicsRayCastCallback._fixtures; } @@ -91,8 +89,7 @@ export class PhysicsRayCastCallback {// extends B2.RayCastCallback { static callback = { ReportFixture (fixture: number, point: B2.Vec2, normal: B2.Vec2, fraction: number): any { - const f = getWASMObjectFromWASMObjectPtr(fixture); - return PhysicsRayCastCallback.ReportFixture(f, point, normal, fraction); + return PhysicsRayCastCallback.ReportFixture(fixture, point, normal, fraction); }, }; } diff --git a/cocos/physics-2d/box2d-wasm/rigid-body.ts b/cocos/physics-2d/box2d-wasm/rigid-body.ts index 569142e0944..e8de966ef32 100644 --- a/cocos/physics-2d/box2d-wasm/rigid-body.ts +++ b/cocos/physics-2d/box2d-wasm/rigid-body.ts @@ -22,7 +22,7 @@ THE SOFTWARE. */ -import { B2, getTSObjectFromWASMObject, getTSObjectFromWASMObjectPtr } from './instantiated'; +import { B2, B2ObjectType, getTSObjectFromWASMObjectPtr } from './instantiated'; import { IRigidBody2D } from '../spec/i-rigid-body'; import { RigidBody2D } from '../framework/components/rigid-body-2d'; import { PhysicsSystem2D } from '../framework/physics-system'; @@ -32,7 +32,6 @@ import { PHYSICS_2D_PTM_RATIO, ERigidBody2DType } from '../framework/physics-typ import { Node } from '../../scene-graph/node'; import { Collider2D, Joint2D } from '../framework'; -import { NodeEventType } from '../../scene-graph/node-event'; import { B2Shape2D } from './shapes/shape-2d'; import { B2Joint } from './joints/joint-2d'; @@ -119,11 +118,11 @@ export class B2RigidBody2D implements IRigidBody2D { //collect all fixtures attached to this rigid body and process them const fixtureList = this.impl?.GetFixtureList(); if (fixtureList) { - let shapeTSObj = getTSObjectFromWASMObject(fixtureList); + let shapeTSObj = getTSObjectFromWASMObjectPtr(B2ObjectType.Fixture, fixtureList); while (shapeTSObj && shapeTSObj.impl) { shapeTSObj.destroy(); - const nextFixture = fixtureList.GetNext(); - shapeTSObj = getTSObjectFromWASMObject(nextFixture); + const nextFixture = B2.FixtureGetNext(fixtureList) as number; + shapeTSObj = getTSObjectFromWASMObjectPtr(B2ObjectType.Fixture, nextFixture); } } @@ -131,11 +130,11 @@ export class B2RigidBody2D implements IRigidBody2D { const jointListPtr = this.impl?.GetJointList(); if (jointListPtr) { let jointWASMPtr = B2.JointEdgeGetJoint(jointListPtr) as number; - let jointTSObj = getTSObjectFromWASMObjectPtr(jointWASMPtr); + let jointTSObj = getTSObjectFromWASMObjectPtr(B2ObjectType.Fixture, jointWASMPtr); while (jointTSObj) { jointTSObj.destroy(); jointWASMPtr = B2.JointEdgeGetNext(jointListPtr) as number; - jointTSObj = getTSObjectFromWASMObjectPtr(jointWASMPtr); + jointTSObj = getTSObjectFromWASMObjectPtr(B2ObjectType.Fixture, jointWASMPtr); } } diff --git a/cocos/physics-2d/box2d-wasm/shapes/box-shape-2d.ts b/cocos/physics-2d/box2d-wasm/shapes/box-shape-2d.ts index 381632fb03a..b32d55b5ba6 100644 --- a/cocos/physics-2d/box2d-wasm/shapes/box-shape-2d.ts +++ b/cocos/physics-2d/box2d-wasm/shapes/box-shape-2d.ts @@ -55,7 +55,7 @@ export class B2BoxShape extends B2Shape2D implements IBoxShape { return wps; } - _createShapes (scaleX: number, scaleY: number, relativePositionX: number, relativePositionY: number): B2.PolygonShape[] { + _createShapes (scaleX: number, scaleY: number, relativePositionX: number, relativePositionY: number): number[] { //B2.PolygonShape[] scaleX = Math.abs(scaleX); scaleY = Math.abs(scaleY); @@ -66,11 +66,8 @@ export class B2BoxShape extends B2Shape2D implements IBoxShape { const offsetX = (relativePositionX + comp.offset.x * scaleX) / PHYSICS_2D_PTM_RATIO; const offsetY = (relativePositionY + comp.offset.y * scaleY) / PHYSICS_2D_PTM_RATIO; - const shape = new B2.PolygonShape(); - tempB2Vec2_1.x = offsetX; - tempB2Vec2_1.y = offsetY; - shape.SetAsBoxWithCenterAndAngle(width, height, tempB2Vec2_1, 0); - - return [shape as unknown as B2.PolygonShape]; + const shape = B2.PolygonShapeNew() as number; + B2.PolygonShapeSetAsBoxWithCenterAndAngle(shape, width, height, offsetX, offsetY, 0); + return [shape]; } } diff --git a/cocos/physics-2d/box2d-wasm/shapes/circle-shape-2d.ts b/cocos/physics-2d/box2d-wasm/shapes/circle-shape-2d.ts index 828fa57cadd..9a4a8eae614 100644 --- a/cocos/physics-2d/box2d-wasm/shapes/circle-shape-2d.ts +++ b/cocos/physics-2d/box2d-wasm/shapes/circle-shape-2d.ts @@ -31,16 +31,16 @@ import { Vec2 } from '../../../core'; export class B2CircleShape extends B2Shape2D implements ICircleShape { get worldRadius (): number { - return (this._shapes[0]).m_radius * PHYSICS_2D_PTM_RATIO; + return B2.CircleShapeGetRadius(this._shapes[0]) * PHYSICS_2D_PTM_RATIO; } _worldPosition = new Vec2(); get worldPosition (): Vec2 { - const p = (this._shapes[0] as B2.CircleShape).m_p; + 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); } - _createShapes (scaleX: number, scaleY: number, relativePositionX: number, relativePositionY: number): B2.CircleShape[] { + _createShapes (scaleX: number, scaleY: number, relativePositionX: number, relativePositionY: number): number[] { //B2.CircleShape[] scaleX = Math.abs(scaleX); scaleY = Math.abs(scaleY); @@ -49,10 +49,10 @@ export class B2CircleShape extends B2Shape2D implements ICircleShape { const offsetX = (relativePositionX + comp.offset.x * scaleX) / PHYSICS_2D_PTM_RATIO; const offsetY = (relativePositionY + comp.offset.y * scaleY) / PHYSICS_2D_PTM_RATIO; - const shape = new B2.CircleShape(); - shape.m_radius = comp.radius / PHYSICS_2D_PTM_RATIO * scaleX; - shape.m_p = { x: offsetX, y: offsetY }; + const shape = B2.CircleShapeNew() as number; + B2.ShapeSetRadius(shape, comp.radius / PHYSICS_2D_PTM_RATIO * scaleX); + B2.CircleShapeSetPosition(shape, offsetX, offsetY); - return [shape as unknown as B2.CircleShape]; + return [shape]; } } diff --git a/cocos/physics-2d/box2d-wasm/shapes/polygon-shape-2d.ts b/cocos/physics-2d/box2d-wasm/shapes/polygon-shape-2d.ts index e192901f5ad..8bd31e5be25 100644 --- a/cocos/physics-2d/box2d-wasm/shapes/polygon-shape-2d.ts +++ b/cocos/physics-2d/box2d-wasm/shapes/polygon-shape-2d.ts @@ -48,8 +48,8 @@ export class B2PolygonShape extends B2Shape2D implements IPolygonShape { return this._worldPoints; } - _createShapes (scaleX: number, scaleY: number, relativePositionX: number, relativePositionY: number): B2.PolygonShape[] { - const shapes: B2.PolygonShape[] = []; + _createShapes (scaleX: number, scaleY: number, relativePositionX: number, relativePositionY: number): number[] { + const shapes: number[] = []; const comp = this.collider as PolygonCollider2D; const points = comp.points; @@ -70,44 +70,43 @@ export class B2PolygonShape extends B2Shape2D implements IPolygonShape { for (let i = 0; i < polys.length; i++) { const poly = polys[i]; - let shape: B2.PolygonShape | null = null; - const vertices = new B2.Vec2Vector(); + let shape: number = 0;//B2.PolygonShape ptr + const vertices = B2.Vec2VectorNew(); let firstVertice: B2.Vec2 | null = null; for (let j = 0, l = poly.length; j < l; j++) { if (!shape) { - shape = new B2.PolygonShape(); + shape = B2.PolygonShapeNew() as number; } const p = poly[j]; const x = (relativePositionX + (p.x + offset.x) * scaleX) / PHYSICS_2D_PTM_RATIO; const y = (relativePositionY + (p.y + offset.y) * scaleY) / PHYSICS_2D_PTM_RATIO; const v = { x, y }; - vertices.push_back(v); + B2.Vec2VectorPush(vertices, x, y); if (!firstVertice) { firstVertice = v; } - if (vertices.size() === B2.maxPolygonVertices) { - shape!.Set(vertices, vertices.size() as number); - shapes.push(shape!); - - shape = null; + if (B2.Vec2VectorSize(vertices) === B2.maxPolygonVertices) { + B2.PolygonShapeSet(shape, B2.Vec2VectorGetPtr(vertices), B2.Vec2VectorSize(vertices)); + shapes.push(shape); + shape = 0; if (j < l - 1) { - const temp = vertices.get(vertices.size() - 1); - vertices.resize(0, { x: 0, y: 0 });//clear - vertices.push_back(firstVertice); - vertices.push_back(temp); + const temp = B2.Vec2VectorGet(vertices, B2.Vec2VectorSize(vertices) - 1); + B2.Vec2VectorResize(vertices, 0, 0, 0);//clear + B2.Vec2VectorPush(vertices, firstVertice.x, firstVertice.y); + B2.Vec2VectorPush(vertices, temp.x, temp.y); } } } if (shape) { - shape.Set(vertices, vertices.size() as number); + B2.PolygonShapeSet(shape, B2.Vec2VectorGetPtr(vertices), B2.Vec2VectorSize(vertices) as number); shapes.push(shape); } - vertices.delete(); + B2.Vec2VectorDelete(vertices); } return shapes; diff --git a/cocos/physics-2d/box2d-wasm/shapes/shape-2d.ts b/cocos/physics-2d/box2d-wasm/shapes/shape-2d.ts index 84ab4a5881b..0b4d119299b 100644 --- a/cocos/physics-2d/box2d-wasm/shapes/shape-2d.ts +++ b/cocos/physics-2d/box2d-wasm/shapes/shape-2d.ts @@ -23,7 +23,8 @@ */ import { B2, getImplPtr, addImplPtrReference, addImplPtrReferenceWASM, removeImplPtrReference, - removeImplPtrReferenceWASM } from '../instantiated'; + removeImplPtrReferenceWASM, + B2ObjectType } from '../instantiated'; import { IBaseShape } from '../../spec/i-physics-shape'; import { Collider2D, PhysicsSystem2D, RigidBody2D, PHYSICS_2D_PTM_RATIO } from '../../../../exports/physics-2d-framework'; import { Rect, Vec3 } from '../../../core'; @@ -46,8 +47,8 @@ function getFilter (shape: B2Shape2D): B2.Filter { } export class B2Shape2D implements IBaseShape { - protected _shapes: B2.Shape[] = []; - protected _fixtures: B2.Fixture[] = []; + protected _shapes: number[] = []; + protected _fixtures: number[] = [];//B2.Fixture ptr protected _collider: Collider2D | null = null; protected _body: B2.Body | null = null; @@ -56,7 +57,7 @@ export class B2Shape2D implements IBaseShape { private _rect = new Rect(); - get impl (): B2.Shape[] { + get impl (): number[] { return this._shapes; } @@ -87,7 +88,7 @@ export class B2Shape2D implements IBaseShape { onGroupChanged (): void { const filter = getFilter(this); this._fixtures.forEach((f): void => { - f.SetFilterData(filter); + B2.FixtureSetFilterData(f, filter); }); } @@ -108,15 +109,16 @@ export class B2Shape2D implements IBaseShape { for (let i = 0; i < fixtures.length; i++) { const fixture = fixtures[i]; - const count = fixture.GetShape().GetChildCount(); + const shape = B2.FixtureGetShape(fixture) as number; + const count = B2.ShapeGetChildCount(shape); for (let j = 0; j < count; j++) { - const aabb = fixture.GetAABB(j); + const aabb = B2.FixtureGetAABB(fixture, j); lowerBound.x = aabb.lowerBound.x; lowerBound.y = aabb.lowerBound.y; upperBound.x = aabb.upperBound.x; upperBound.y = aabb.upperBound.y; - if (fixture.GetShape().m_type === B2.ShapeType.e_polygon) { //b2ShapeType.e_polygonShape - const skinWidth = fixture.GetShape().m_radius; + if (B2.ShapeGetType(shape) === 2) { //b2ShapeType.e_polygonShape + const skinWidth = B2.ShapeGetRadius(shape); lowerBound.x += skinWidth; lowerBound.y += skinWidth; upperBound.x += skinWidth; @@ -143,12 +145,12 @@ export class B2Shape2D implements IBaseShape { return r; } - getFixtureIndex (fixture: B2.Fixture): number { + getFixtureIndex (fixture: number): number { //B2.Fixture ptr return this._fixtures.indexOf(fixture); } //relativePositionX/Y : relative Position from shape to rigid body - _createShapes (scaleX: number, scaleY: number, relativePositionX: number, relativePositionY: number): B2.Shape[] { + _createShapes (scaleX: number, scaleY: number, relativePositionX: number, relativePositionY: number): number[] { return []; } @@ -177,17 +179,23 @@ export class B2Shape2D implements IBaseShape { for (let i = 0; i < shapes.length; i++) { const shape = shapes[i]; - const fixDef = new B2.FixtureDef(); - fixDef.density = comp.density; - fixDef.isSensor = comp.sensor; - fixDef.friction = comp.friction; - fixDef.restitution = comp.restitution; - fixDef.SetShape(shape); - fixDef.filter = filter; - const fixture = this._body.CreateFixture(fixDef as B2.FixtureDef); - //fixture.m_userData = this; - addImplPtrReference(this, getImplPtr(fixture)); - addImplPtrReferenceWASM(fixture, getImplPtr(fixture)); + const fixDef = (PhysicsSystem2D.instance.physicsWorld as B2PhysicsWorld).tempB2FixtureDefPtr; + B2.FixtureDefSetAll( + fixDef, + shape, + 0, + comp.friction, + comp.restitution, + comp.density, + comp.sensor, + filter.categoryBits, + filter.maskBits, + filter.groupIndex, + ); + + const fixture = B2.BodyCreateFixture(getImplPtr(this._body), fixDef) as number; + addImplPtrReference(B2ObjectType.Fixture, this, fixture); + addImplPtrReferenceWASM(B2ObjectType.Fixture, fixture, fixture); if (body?.enabledContactListener) { (PhysicsSystem2D.instance.physicsWorld as B2PhysicsWorld).registerContactFixture(fixture); @@ -209,13 +217,13 @@ export class B2Shape2D implements IBaseShape { for (let i = fixtures.length - 1; i >= 0; i--) { const fixture = fixtures[i]; //fixture.m_userData = null; - removeImplPtrReference(getImplPtr(fixture)); - removeImplPtrReferenceWASM(getImplPtr(fixture)); + removeImplPtrReference(B2ObjectType.Fixture, fixture); + removeImplPtrReferenceWASM(B2ObjectType.Fixture, fixture); (PhysicsSystem2D.instance.physicsWorld as B2PhysicsWorld).unregisterContactFixture(fixture); if (body) { - body.DestroyFixture(fixture); + B2.BodyDestroyFixture(getImplPtr(body), fixture); } } diff --git a/native/external-config.json b/native/external-config.json index 432a1a05969..bfb8870c10e 100644 --- a/native/external-config.json +++ b/native/external-config.json @@ -3,6 +3,6 @@ "type": "github", "owner": "cocos-creator", "name": "engine-native-external", - "checkout": "v3.8.2-11" + "checkout": "v3.8.2-12" } } \ No newline at end of file From c76c1603937212ba9ced80ee80e778600677c806 Mon Sep 17 00:00:00 2001 From: bofeng-song Date: Tue, 10 Oct 2023 18:19:33 +0800 Subject: [PATCH 6/8] Fix the issue that after double-clicking the editbox, the editbox shifts or displays an offset on web mobile platforms (#16402) * Fix the issue that after double-clicking the editbox, the editbox shifts or displays an offset on web mobile platforms --- cocos/ui/editbox/edit-box-impl-base.ts | 5 ----- cocos/ui/editbox/edit-box-impl.ts | 20 ++++++++++++++++++-- cocos/ui/editbox/edit-box.ts | 6 ------ 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/cocos/ui/editbox/edit-box-impl-base.ts b/cocos/ui/editbox/edit-box-impl-base.ts index 9ce78026dc9..9bc3544a6b9 100644 --- a/cocos/ui/editbox/edit-box-impl-base.ts +++ b/cocos/ui/editbox/edit-box-impl-base.ts @@ -37,11 +37,6 @@ export class EditBoxImplBase { */ public _delegate: EditBox | null = null; - /** - * @engineInternal dirty flag to update the matrix - */ - public _dirtyFlag: boolean | null = false; - public init (delegate: EditBox): void { // To be overrode } diff --git a/cocos/ui/editbox/edit-box-impl.ts b/cocos/ui/editbox/edit-box-impl.ts index f6a36d25700..eeec2ad8cb8 100644 --- a/cocos/ui/editbox/edit-box-impl.ts +++ b/cocos/ui/editbox/edit-box-impl.ts @@ -137,7 +137,10 @@ export class EditBoxImpl extends EditBoxImplBase { } public update (): void { - if (!this._dirtyFlag) return; + const node = this._delegate!.node; + if (!node.hasChangedFlags) { + return; + } this._updateMatrix(); } @@ -242,9 +245,22 @@ export class EditBoxImpl extends EditBoxImplBase { this._scrollBackWindow(); } + private _isElementInViewport (): boolean { + if (this._edTxt) { + const rect = this._edTxt.getBoundingClientRect(); + + return ( + rect.top >= 0 && rect.left >= 0 + && rect.bottom <= (ccwindow.innerHeight || ccdocument.documentElement.clientHeight) + && rect.right <= (ccwindow.innerWidth || ccdocument.documentElement.clientWidth) + ); + } + return false; + } + private _adjustWindowScroll (): void { setTimeout(() => { - if (ccwindow.scrollY < SCROLLY) { + if (ccwindow.scrollY < SCROLLY && !this._isElementInViewport()) { this._edTxt!.scrollIntoView({ block: 'start', inline: 'nearest', behavior: 'smooth' }); } }, DELAY_TIME); diff --git a/cocos/ui/editbox/edit-box.ts b/cocos/ui/editbox/edit-box.ts index 63846eeb9cb..4bca693cdc7 100644 --- a/cocos/ui/editbox/edit-box.ts +++ b/cocos/ui/editbox/edit-box.ts @@ -492,9 +492,6 @@ export class EditBox extends Component { public _editBoxEditingDidBegan (): void { ComponentEventHandler.emitEvents(this.editingDidBegan, this); this.node.emit(EventType.EDITING_DID_BEGAN, this); - if (this._impl) { - this._impl._dirtyFlag = true; - } } /** @@ -506,9 +503,6 @@ export class EditBox extends Component { public _editBoxEditingDidEnded (text?: string): void { ComponentEventHandler.emitEvents(this.editingDidEnded, this); this.node.emit(EventType.EDITING_DID_ENDED, this, text); - if (this._impl) { - this._impl._dirtyFlag = false; - } } /** From 40ec21934b9e184f675bff80c447eaa8defd7b86 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 11 Oct 2023 10:00:13 +0800 Subject: [PATCH 7/8] fixed #16355: Compilation fails in CI env if using Android NDK r26 (#16401) * fixed #16355: Compilation fails in CI env if using Android NDK r26 * Update predefine.cmake --- native/cmake/predefine.cmake | 2 ++ 1 file changed, 2 insertions(+) diff --git a/native/cmake/predefine.cmake b/native/cmake/predefine.cmake index ad50d295355..c071f71aa99 100644 --- a/native/cmake/predefine.cmake +++ b/native/cmake/predefine.cmake @@ -129,6 +129,8 @@ if("$ENV{COCOS_ENGINE_DEV}" EQUAL "1") set(WERROR_FLAGS " ${WERROR_FLAGS} -Wno-deprecated-declarations") elseif(LINUX) set(WERROR_FLAGS " ${WERROR_FLAGS} -Wno-nullability-completeness -Wno-deprecated-declarations") + elseif(ANDROID) + set(WERROR_FLAGS " ${WERROR_FLAGS} -Wno-deprecated-declarations -Wno-unknown-warning-option -Wno-deprecated-builtins") endif() if(CMAKE_CXX_COMPILER_ID MATCHES "GNU") From 2831c61f0d1f5633eaf6516f984792d0091881b3 Mon Sep 17 00:00:00 2001 From: bofeng-song Date: Wed, 11 Oct 2023 10:06:51 +0800 Subject: [PATCH 8/8] Fix webaudio's bug: calling the 'checkEnded' interface after 'destroy' has been called can trigger a JavaScript error (#16393) (cherry picked from commit a8e7f18bf7e4bc5decad9b78d1dc169c6068ce86) --- pal/audio/web/player-web.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/pal/audio/web/player-web.ts b/pal/audio/web/player-web.ts index 410e305d8d9..46fefbaf3ce 100644 --- a/pal/audio/web/player-web.ts +++ b/pal/audio/web/player-web.ts @@ -245,6 +245,7 @@ export class AudioPlayerWeb implements OperationQueueable { 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