Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix camera id #17857

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cocos/render-scene/scene/camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]] 管理。
Expand Down Expand Up @@ -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 相机是固定尺寸还是跟随屏幕尺寸
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 1 addition & 6 deletions cocos/rendering/custom/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion native/cocos/scene/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ constexpr ccstd::array<ccstd::array<float, 4>, 4> PRE_TRANSFORMS = {{
{{0, -1, 1, 0}} // SurfaceTransform.ROTATE_270
}};

std::atomic<uint32_t> sCameraId{0};
Copy link
Contributor

@dumganhar dumganhar Nov 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it need to be defined as a atomic variable?
I think Camera could not be created in difference thead.


} // namespace

const ccstd::vector<float> 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};
Expand All @@ -65,7 +67,8 @@ const ccstd::vector<float> Camera::SHUTTERS{1.0F, 1.0F / 2.0F, 1.0F / 4.0F, 1.0F
const ccstd::vector<float> 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<int>(_aperture));
_shutterValue = Camera::SHUTTERS.at(static_cast<int>(_shutter));
_isoValue = Camera::ISOS[static_cast<int>(_iso)];
Expand Down
2 changes: 2 additions & 0 deletions native/cocos/scene/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<pipeline::GeometryRenderer> _geometryRenderer;
Expand Down
1 change: 1 addition & 0 deletions native/tools/swig-config/scene.i
Original file line number Diff line number Diff line change
Expand Up @@ -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<cc::IntrusivePtr<cc::scene::Camera>>&, cameras, getCameras);
Expand Down
Loading