Skip to content

Commit

Permalink
fix: fix camera for baking mesh
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Aug 19, 2020
1 parent 50e749c commit 6395a4f
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 171 deletions.
119 changes: 119 additions & 0 deletions Packages/UIParticle/Scripts/BakingCamera.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using System;
using UnityEngine;

namespace Coffee.UIExtensions
{
internal class BakingCamera : MonoBehaviour
{
static BakingCamera s_Instance;

#if UNITY_2018_3_OR_NEWER && UNITY_EDITOR
static BakingCamera s_InstanceForPrefab;

private static BakingCamera InstanceForPrefab
{
get
{
// If current scene is prefab mode, create OverlayCamera for editor.
var prefabStage = UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage();
if (prefabStage == null || !prefabStage.scene.isLoaded) return null;
if (s_InstanceForPrefab) return s_InstanceForPrefab;

s_InstanceForPrefab = Create();
s_InstanceForPrefab.name += " (For Prefab Stage)";
UnityEngine.SceneManagement.SceneManager.MoveGameObjectToScene(s_InstanceForPrefab.gameObject, prefabStage.scene);

return s_InstanceForPrefab;
}
}
#endif

private static BakingCamera Instance
{
get
{
#if UNITY_2018_3_OR_NEWER && UNITY_EDITOR
var inst = InstanceForPrefab;
if (inst) return inst;
#endif
// Find instance in scene, or create new one.
return s_Instance
? s_Instance
: (s_Instance = FindObjectOfType<BakingCamera>() ?? Create());
}
}

private Camera _camera;
private int _refCount;

private static BakingCamera Create()
{
var gameObject = new GameObject(typeof(BakingCamera).Name);

// This camera object is just for internal use
gameObject.hideFlags = HideFlags.HideAndDontSave;
gameObject.hideFlags = HideFlags.DontSave;

var inst = gameObject.AddComponent<BakingCamera>();
inst._camera = gameObject.AddComponent<Camera>();
inst._camera.orthographic = true;

// Turn camera off because particle mesh baker will use only camera matrix
gameObject.SetActive(false);

return inst;
}

private void Awake()
{
if (this == s_Instance)
DontDestroyOnLoad(gameObject);
}

public static void Register()
{
Instance._refCount++;
}

public static void Unregister()
{
if (s_Instance == null) return;

Instance._refCount--;
if (0 < Instance._refCount) return;

if (Application.isPlaying)
Destroy(Instance.gameObject);
else
DestroyImmediate(Instance.gameObject);

s_Instance = null;
}

public static Camera GetCamera(Canvas canvas)
{
if (!canvas) return Camera.main;

canvas = canvas.rootCanvas;
// Adjust camera orthographic size to canvas size
// for canvas-based coordinates of particles' size and speed.
var size = ((RectTransform) canvas.transform).rect.size;
Instance._camera.orthographicSize = Mathf.Max(size.x, size.y) * canvas.scaleFactor;

var camera = canvas.worldCamera;
var transform = Instance.transform;
if (canvas.renderMode != RenderMode.ScreenSpaceOverlay && camera)
{
var cameraTr = camera.transform;
transform.SetPositionAndRotation(cameraTr.position, cameraTr.rotation);
}
else
{
Instance._camera.orthographic = true;
transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
}

return Instance._camera;
}
}
}

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

170 changes: 0 additions & 170 deletions Packages/UIParticle/Scripts/UIParticleOverlayCamera.cs

This file was deleted.

0 comments on commit 6395a4f

Please sign in to comment.