Skip to content

Commit

Permalink
Add CesiumCameraManager.
Browse files Browse the repository at this point in the history
  • Loading branch information
kring committed Sep 9, 2024
1 parent 590e652 commit 2839f59
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 79 deletions.
29 changes: 0 additions & 29 deletions Runtime/CesiumCamera.cs

This file was deleted.

3 changes: 0 additions & 3 deletions Runtime/CesiumCamera.cs.meta

This file was deleted.

107 changes: 107 additions & 0 deletions Runtime/CesiumCameraManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using Reinterop;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using UnityEngine;

namespace CesiumForUnity
{
/// <summary>
/// Manages the set of cameras that are used for Cesium3DTileset culling and level-of-detail.
/// </summary>
[DisallowMultipleComponent]
[AddComponentMenu("Cesium/Cesium Camera Manager")]
[Icon("Packages/com.cesium.unity/Editor/Resources/Cesium-24x24.png")]
public class CesiumCameraManager : MonoBehaviour
{
/// <summary>
/// Gets the instance suitable for use with the given game object.
/// </summary>
/// <remarks>
/// This method looks for an existing instance using `GetComponentInParent`. If it fails to find one, then
/// it will create one. When one is created, it be added to the same `GameObject` that has the
/// `CesiumGeoreference` (found using `GetComponentInParent` again) if there is one. If there is no
/// `CesiumGeoreference`, the instance is added to the originally-provided `GameObject`.
/// </remarks>
/// <param name="gameObject">The game object.</param>
/// <returns></returns>
public static CesiumCameraManager GetOrCreate(GameObject gameObject)
{
if (gameObject == null) throw new ArgumentNullException("gameObject");

CesiumCameraManager result = gameObject.GetComponentInParent<CesiumCameraManager>();
if (result == null)
return CesiumCameraManager.Create(gameObject);
else
return result;
}

public static CesiumCameraManager GetOrCreate(Component component)
{
if (component == null) throw new ArgumentNullException("component");

return CesiumCameraManager.GetOrCreate(component.gameObject);
}

private static CesiumCameraManager Create(GameObject gameObject)
{
// Add it to the same game object that has the CesiumGeoreference, if any.
// Otherwise, add it to the current game object.
CesiumGeoreference georeference = gameObject.GetComponentInParent<CesiumGeoreference>();
GameObject owner = georeference == null ? gameObject : georeference.gameObject;
if (owner == null) return null;

return owner.AddComponent<CesiumCameraManager>();
}

#region User-editable properties

[SerializeField]
private bool _useMainCamera = true;

/// <summary>
/// Determines whether the camera tagged `MainCamera` should be used for Cesium3DTileset
/// culling and level-of-detail.
/// </summary>
public bool useMainCamera
{
get => this._useMainCamera;
set
{
this._useMainCamera = value;
}
}

[SerializeField]
private bool _useActiveSceneViewCameraInEditor = true;

/// <summary>
/// Determines whether the camera associated with the Editor's active scene view should be
/// used for Cesium3DTileset culling and level-of-detail. In a game, this property has
/// no effect.
/// </summary>
public bool useActiveSceneViewCameraInEditor
{
get => this._useActiveSceneViewCameraInEditor;
set
{
this._useActiveSceneViewCameraInEditor = value;
}
}

[SerializeField]
private List<Camera> _additionalCameras = new List<Camera>();

/// <summary>
/// Additional Cameras to use for Cesium3DTileset culling and level-of-detail. These cameras
/// may be disabled, which is useful for creating a virtual camera that affects Cesium3DTileset
/// but that is not actually rendered.
/// </summary>
public List<Camera> additionalCameras
{
get => this._additionalCameras;
}

#endregion
}
}
11 changes: 11 additions & 0 deletions Runtime/CesiumCameraManager.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 10 additions & 13 deletions Runtime/ConfigureReinterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,7 @@ internal partial class ConfigureReinterop

public void ExposeToCPP()
{


Camera c = Camera.main;
List<Camera> lc = new List<Camera>();

lc = CesiumCamera.cameraList;
lc.Add(c);
for (int i = 0; i < lc.Count; ++i)
{
c = lc[i];
}




Transform t = c.transform;
Vector3 u = t.up;
Expand Down Expand Up @@ -894,6 +881,16 @@ Cesium3DTilesetLoadFailureDetails tilesetDetails
length = float3x3Array.Length;
length = float4x4Array.Length;

CesiumCameraManager manager = CesiumCameraManager.GetOrCreate(go);
manager = CesiumCameraManager.GetOrCreate(tileset);
manager.useActiveSceneViewCameraInEditor = false;
manager.useMainCamera = false;
Camera camera = null;
for (int i = 0; i < manager.additionalCameras.Count; ++i)
{
camera = manager.additionalCameras[i];
}

#if UNITY_EDITOR
SceneView sv = SceneView.lastActiveSceneView;
sv.pivot = sv.pivot;
Expand Down
104 changes: 76 additions & 28 deletions native~/Runtime/src/CameraManager.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#include "CameraManager.h"

#include "Cesium3DTilesetImpl.h"
#include "CesiumGeoreferenceImpl.h"
#include "UnityTransforms.h"

#include <CesiumGeospatial/Ellipsoid.h>
#include <CesiumGeospatial/GlobeTransforms.h>
#include <CesiumUtility/Math.h>

#include <DotNet/CesiumForUnity/Cesium3DTileset.h>
#include <DotNet/CesiumForUnity/CesiumEllipsoid.h>
#include <DotNet/CesiumForUnity/CesiumGeoreference.h>
#include <DotNet/CesiumForUnity/CesiumCamera.h>
#include <DotNet/System/Collections/Generic/List1.h>
#include <DotNet/UnityEngine/Camera.h>
#include <DotNet/UnityEngine/GameObject.h>
#include <DotNet/UnityEngine/Matrix4x4.h>
#include <DotNet/UnityEngine/Transform.h>
#include <DotNet/UnityEngine/Vector3.h>
#include <DotNet/System/Collections/Generic/List1.h>

#include <glm/trigonometric.hpp>

#if UNITY_EDITOR
Expand All @@ -43,7 +43,7 @@ ViewState unityCameraToViewState(
const CesiumGeoreference& georeference,
const LocalHorizontalCoordinateSystem* pCoordinateSystem,
const glm::dmat4& unityWorldToTileset,
Camera& camera) {
const Camera& camera) {
Transform transform = camera.transform();

Vector3 cameraPositionUnity = transform.position();
Expand Down Expand Up @@ -94,14 +94,58 @@ ViewState unityCameraToViewState(

} // namespace

std::vector<ViewState> CameraManager::getAllCameras(const GameObject& context) {
namespace {

void addMainCamera(
std::vector<ViewState>& result,
const CesiumGeoreference& georeferenceComponent,
const CesiumGeospatial::LocalHorizontalCoordinateSystem* pCoordinateSystem,
const glm::dmat4& unityWorldToTileset) {
Camera camera = Camera::main();
if (camera != nullptr) {
result.emplace_back(unityCameraToViewState(
georeferenceComponent,
pCoordinateSystem,
unityWorldToTileset,
camera));
}
}

void addActiveSceneCameraInEditor(
std::vector<ViewState>& result,
const CesiumGeoreference& georeferenceComponent,
const CesiumGeospatial::LocalHorizontalCoordinateSystem* pCoordinateSystem,
const glm::dmat4& unityWorldToTileset) {
#if UNITY_EDITOR
if (!EditorApplication::isPlaying()) {
SceneView lastActiveEditorView = SceneView::lastActiveSceneView();
if (lastActiveEditorView != nullptr) {
Camera editorCamera = lastActiveEditorView.camera();
if (editorCamera != nullptr) {
result.emplace_back(unityCameraToViewState(
georeferenceComponent,
pCoordinateSystem,
unityWorldToTileset,
editorCamera));
}
}
}
#endif
}

} // namespace

/*static*/ std::vector<Cesium3DTilesSelection::ViewState>
CameraManager::getAllCameras(
const DotNet::CesiumForUnity::Cesium3DTileset& tileset,
const CesiumForUnityNative::Cesium3DTilesetImpl& impl) {
const LocalHorizontalCoordinateSystem* pCoordinateSystem = nullptr;

glm::dmat4 unityWorldToTileset =
UnityTransforms::fromUnity(context.transform().worldToLocalMatrix());
UnityTransforms::fromUnity(tileset.transform().worldToLocalMatrix());

CesiumGeoreference georeferenceComponent =
context.GetComponentInParent<CesiumGeoreference>();
tileset.gameObject().GetComponentInParent<CesiumGeoreference>();
if (georeferenceComponent != nullptr) {
CesiumGeoreferenceImpl& georeference =
georeferenceComponent.NativeImplementation();
Expand All @@ -110,37 +154,41 @@ std::vector<ViewState> CameraManager::getAllCameras(const GameObject& context) {
}

std::vector<ViewState> result;


System::Collections::Generic::List1<Camera> cameraList = CesiumCamera::cameraList();
const CesiumCameraManager& cameraManager = impl.getCameraManager();

if (cameraList.Count()==0) {
cameraList.Add(Camera::main());
if (cameraManager == nullptr || cameraManager.useMainCamera()) {
addMainCamera(
result,
georeferenceComponent,
pCoordinateSystem,
unityWorldToTileset);
}

for (int i=0; i < cameraList.Count(); i++){
result.emplace_back(unityCameraToViewState(
if (cameraManager == nullptr ||
cameraManager.useActiveSceneViewCameraInEditor()) {
addActiveSceneCameraInEditor(
result,
georeferenceComponent,
pCoordinateSystem,
unityWorldToTileset,
cameraList[i]));
unityWorldToTileset);
}

#if UNITY_EDITOR
if (!EditorApplication::isPlaying()) {
SceneView lastActiveEditorView = SceneView::lastActiveSceneView();
if (lastActiveEditorView != nullptr) {
Camera editorCamera = lastActiveEditorView.camera();
if (editorCamera != nullptr) {
result.emplace_back(unityCameraToViewState(
georeferenceComponent,
pCoordinateSystem,
unityWorldToTileset,
editorCamera));
}
if (cameraManager != nullptr) {
System::Collections::Generic::List1<Camera> cameras =
cameraManager.additionalCameras();
for (int32_t i = 0, len = cameras.Count(); i < len; ++i) {
Camera camera = cameras[i];
if (camera == nullptr)
continue;

result.emplace_back(unityCameraToViewState(
georeferenceComponent,
pCoordinateSystem,
unityWorldToTileset,
camera));
}
}
#endif

return result;
}
Expand Down
11 changes: 9 additions & 2 deletions native~/Runtime/src/CameraManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@ namespace DotNet::UnityEngine {
class GameObject;
}

namespace DotNet::CesiumForUnity {
class Cesium3DTileset;
}

namespace CesiumForUnityNative {

class Cesium3DTilesetImpl;

class CameraManager {
public:
static std::vector<Cesium3DTilesSelection::ViewState>
getAllCameras(const DotNet::UnityEngine::GameObject& context);
static std::vector<Cesium3DTilesSelection::ViewState> getAllCameras(
const DotNet::CesiumForUnity::Cesium3DTileset& tileset,
const CesiumForUnityNative::Cesium3DTilesetImpl& impl);
};

} // namespace CesiumForUnityNative
Loading

0 comments on commit 2839f59

Please sign in to comment.