diff --git a/cocos/render-scene/scene/camera.ts b/cocos/render-scene/scene/camera.ts index eff16e74171..6f01cc5a728 100644 --- a/cocos/render-scene/scene/camera.ts +++ b/cocos/render-scene/scene/camera.ts @@ -399,6 +399,8 @@ export const SKYBOX_FLAG = SkyBoxFlagValue.VALUE; const correctionMatrices: Mat4[] = []; +let _cameraCount = 0; + /** * @en The render camera representation in the render scene, it's managed by [[Camera]] * @zh 渲染场景中的相机对象,由项目层的 [[Camera]] 管理。 @@ -799,6 +801,11 @@ export class Camera { return this._matViewProjInv$; } + /** @engineInternal */ + get cameraId (): number { + return this._cameraId$; + } + /** * @en Whether the camera is fixed size or matching the window size. * @zh 相机是固定尺寸还是跟随屏幕尺寸 @@ -862,6 +869,7 @@ export class Camera { private _cameraType$: CameraType = CameraType.DEFAULT; private _trackingType$: TrackingType = TrackingType.NO_TRACKING; private _usage$: CameraUsage = CameraUsage.GAME; + private _cameraId$ = _cameraCount++; constructor (device: Device) { this._device$ = device; diff --git a/cocos/rendering/custom/define.ts b/cocos/rendering/custom/define.ts index bddd3c9b299..0beee557c47 100644 --- a/cocos/rendering/custom/define.ts +++ b/cocos/rendering/custom/define.ts @@ -120,13 +120,8 @@ export function validPunctualLightsCulling (pipeline: BasicPipeline, camera: Cam sceneData.validPunctualLights = validPunctualLights; } -const _cameras: Camera[] = []; - export function getCameraUniqueID (camera: Camera): number { - if (!_cameras.includes(camera)) { - _cameras.push(camera); - } - return _cameras.indexOf(camera); + return camera.cameraId; } export function getLoadOpOfClearFlag (clearFlag: ClearFlagBit, attachment: AttachmentType): LoadOp { diff --git a/native/cocos/scene/Camera.cpp b/native/cocos/scene/Camera.cpp index 272cde121ae..f925d77ef01 100644 --- a/native/cocos/scene/Camera.cpp +++ b/native/cocos/scene/Camera.cpp @@ -57,6 +57,8 @@ constexpr ccstd::array, 4> PRE_TRANSFORMS = {{ {{0, -1, 1, 0}} // SurfaceTransform.ROTATE_270 }}; +std::atomic sCameraId{0}; + } // namespace const ccstd::vector Camera::FSTOPS{1.8F, 2.0F, 2.2F, 2.5F, 2.8F, 3.2F, 3.5F, 4.0F, 4.5F, 5.0F, 5.6F, 6.3F, 7.1F, 8.0F, 9.0F, 10.0F, 11.0F, 13.0F, 14.0F, 16.0F, 18.0F, 20.0F, 22.0F}; @@ -65,7 +67,8 @@ const ccstd::vector Camera::SHUTTERS{1.0F, 1.0F / 2.0F, 1.0F / 4.0F, 1.0F const ccstd::vector Camera::ISOS{100.0F, 200.0F, 400.0F, 800.0F}; Camera::Camera(gfx::Device *device) -: _device(device) { +: _device(device) +, _cameraId(sCameraId++) { _apertureValue = Camera::FSTOPS.at(static_cast(_aperture)); _shutterValue = Camera::SHUTTERS.at(static_cast(_shutter)); _isoValue = Camera::ISOS[static_cast(_iso)]; diff --git a/native/cocos/scene/Camera.h b/native/cocos/scene/Camera.h index 2e2ebfbe627..0514dc50771 100644 --- a/native/cocos/scene/Camera.h +++ b/native/cocos/scene/Camera.h @@ -181,6 +181,7 @@ class Camera : public RefCounted { void syncCameraEditor(const Camera *camera); void update(bool forceUpdate = false); // for lazy eval situations like the in-editor preview void changeTargetWindow(RenderWindow *window); + uint32_t getCameraId() const noexcept { return _cameraId; } /** * transform a screen position (in oriented space) to a world space ray @@ -419,6 +420,7 @@ class Camera : public RefCounted { CameraType _cameraType{CameraType::DEFAULT}; TrackingType _trackingType{TrackingType::NO_TRACKING}; CameraUsage _usage{CameraUsage::GAME}; + uint32_t _cameraId{0}; #if CC_USE_GEOMETRY_RENDERER IntrusivePtr _geometryRenderer; diff --git a/native/tools/swig-config/scene.i b/native/tools/swig-config/scene.i index a6acf1cc998..f002e941d67 100644 --- a/native/tools/swig-config/scene.i +++ b/native/tools/swig-config/scene.i @@ -452,6 +452,7 @@ using namespace cc; %attribute(cc::scene::Camera, cc::scene::CameraUsage, cameraUsage, getCameraUsage, setCameraUsage); %attribute(cc::scene::Camera, cc::scene::TrackingType, trackingType, getTrackingType, setTrackingType); %attribute(cc::scene::Camera, cc::scene::CameraType, cameraType, getCameraType, setCameraType); +%attribute(cc::scene::Camera, uint32_t, cameraId, getCameraId); %attribute(cc::scene::RenderScene, ccstd::string&, name, getName); %attribute(cc::scene::RenderScene, ccstd::vector>&, cameras, getCameras);