/// <summary> /// Use when you need the sprite to be pixel perfect on a specific tk2dCamera. /// </summary> public static tk2dSpriteCollectionSize ForTk2dCamera(tk2dCamera camera) { tk2dSpriteCollectionSize s = new tk2dSpriteCollectionSize(); tk2dCameraSettings cameraSettings = camera.SettingsRoot.CameraSettings; if (cameraSettings.projection == tk2dCameraSettings.ProjectionType.Orthographic) { switch (cameraSettings.orthographicType) { case tk2dCameraSettings.OrthographicType.PixelsPerMeter: s.type = Type.PixelsPerMeter; s.pixelsPerMeter = cameraSettings.orthographicPixelsPerMeter; break; case tk2dCameraSettings.OrthographicType.OrthographicSize: s.type = Type.Explicit; s.height = camera.nativeResolutionHeight; s.orthoSize = cameraSettings.orthographicSize; break; } } else if (cameraSettings.projection == tk2dCameraSettings.ProjectionType.Perspective) { s.type = Type.PixelsPerMeter; s.pixelsPerMeter = 20; // some random value } return(s); }
/// <summary> /// Updates the camera matrix to ensure 1:1 pixel mapping /// Or however the override is set up. /// </summary> public void UpdateCameraMatrix() { Upgrade(); if (!this.viewportClippingEnabled) { inst = this; } Camera unityCamera = UnityCamera; tk2dCamera settings = SettingsRoot; tk2dCameraSettings inheritedCameraSettings = settings.CameraSettings; if (unityCamera.rect != cameraSettings.rect) { unityCamera.rect = cameraSettings.rect; } // Projection type is inherited from base camera _targetResolution = GetScreenPixelDimensions(settings); if (inheritedCameraSettings.projection == tk2dCameraSettings.ProjectionType.Perspective) { if (unityCamera.orthographic == true) { unityCamera.orthographic = false; } float fov = Mathf.Min(179.9f, inheritedCameraSettings.fieldOfView / Mathf.Max(0.001f, ZoomFactor)); if (unityCamera.fieldOfView != fov) { unityCamera.fieldOfView = fov; } _screenExtents.Set(-unityCamera.aspect, -1, unityCamera.aspect * 2, 2); _nativeScreenExtents = _screenExtents; unityCamera.ResetProjectionMatrix(); } else { if (unityCamera.orthographic == false) { unityCamera.orthographic = true; } // Find an override if necessary Matrix4x4 m = GetProjectionMatrixForOverride(settings, settings.CurrentResolutionOverride, _targetResolution.x, _targetResolution.y, true, out _screenExtents, out _nativeScreenExtents); int mHash = m.GetHashCode(); if (mHash != unityCamera.projectionMatrix.GetHashCode()) { unityCamera.projectionMatrix = m; } } }
// Use this for initialization void Awake() { Upgrade(); if (allCameras.IndexOf(this) == -1) { allCameras.Add(this); } tk2dCamera settings = SettingsRoot; tk2dCameraSettings inheritedCameraSettings = settings.CameraSettings; if (inheritedCameraSettings.projection == tk2dCameraSettings.ProjectionType.Perspective) { UnityCamera.transparencySortMode = inheritedCameraSettings.transparencySortMode; } }
// Gives you the size of one pixel in world units at the native resolution // For perspective cameras, it is dependent on the distance to the camera. public float GetSizeAtDistance(float distance) { tk2dCameraSettings cameraSettings = SettingsRoot.CameraSettings; switch (cameraSettings.projection) { case tk2dCameraSettings.ProjectionType.Orthographic: if (cameraSettings.orthographicType == tk2dCameraSettings.OrthographicType.PixelsPerMeter) { return(1.0f / cameraSettings.orthographicPixelsPerMeter); } else { return(2.0f * cameraSettings.orthographicSize / SettingsRoot.nativeResolutionHeight); } case tk2dCameraSettings.ProjectionType.Perspective: return(Mathf.Tan(CameraSettings.fieldOfView * Mathf.Deg2Rad * 0.5f) * distance * 2.0f / SettingsRoot.nativeResolutionHeight); } return(1); }
/// <summary> /// Updates the camera matrix to ensure 1:1 pixel mapping /// Or however the override is set up. /// </summary> public void UpdateCameraMatrix() { Upgrade(); if (!this.viewportClippingEnabled) { inst = this; } Camera unityCamera = UnityCamera; tk2dCamera settings = SettingsRoot; tk2dCameraSettings inheritedCameraSettings = settings.CameraSettings; if (unityCamera.rect != cameraSettings.rect) { unityCamera.rect = cameraSettings.rect; } // Projection type is inherited from base camera _targetResolution = GetScreenPixelDimensions(settings); if (inheritedCameraSettings.projection == tk2dCameraSettings.ProjectionType.Perspective) { if (unityCamera.orthographic == true) { unityCamera.orthographic = false; } float fov = Mathf.Min(179.9f, inheritedCameraSettings.fieldOfView / Mathf.Max(0.001f, ZoomFactor)); if (unityCamera.fieldOfView != fov) { unityCamera.fieldOfView = fov; } _screenExtents.Set(-unityCamera.aspect, -1, unityCamera.aspect * 2, 2); _nativeScreenExtents = _screenExtents; unityCamera.ResetProjectionMatrix(); } else { if (unityCamera.orthographic == false) { unityCamera.orthographic = true; } // Find an override if necessary Matrix4x4 m = GetProjectionMatrixForOverride(settings, settings.CurrentResolutionOverride, _targetResolution.x, _targetResolution.y, true, out _screenExtents, out _nativeScreenExtents); #if !(UNITY_3_5 || UNITY_4_0 || UNITY_4_1) // Windows phone? if (Application.platform == RuntimePlatform.WP8Player && (Screen.orientation == ScreenOrientation.LandscapeLeft || Screen.orientation == ScreenOrientation.LandscapeRight)) { float angle = (Screen.orientation == ScreenOrientation.LandscapeRight) ? 90.0f : -90.0f; Matrix4x4 m2 = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0, 0, angle), Vector3.one); m = m2 * m; } #endif if (unityCamera.projectionMatrix != m) { unityCamera.projectionMatrix = m; } } }
void DrawCameraGUI(tk2dCamera target, bool complete) { bool allowProjectionParameters = target.SettingsRoot == target; bool oldGuiEnabled = GUI.enabled; SerializedObject so = this.serializedObject; SerializedObject cam = new SerializedObject(target.camera); SerializedProperty m_ClearFlags = cam.FindProperty("m_ClearFlags"); SerializedProperty m_BackGroundColor = cam.FindProperty("m_BackGroundColor"); SerializedProperty m_CullingMask = cam.FindProperty("m_CullingMask"); SerializedProperty m_TargetTexture = cam.FindProperty("m_TargetTexture"); SerializedProperty m_Near = cam.FindProperty("near clip plane"); SerializedProperty m_Far = cam.FindProperty("far clip plane"); SerializedProperty m_Depth = cam.FindProperty("m_Depth"); SerializedProperty m_RenderingPath = cam.FindProperty("m_RenderingPath"); SerializedProperty m_HDR = cam.FindProperty("m_HDR"); TransparencySortMode transparencySortMode = target.camera.transparencySortMode; if (complete) { EditorGUILayout.PropertyField(m_ClearFlags); EditorGUILayout.PropertyField(m_BackGroundColor); EditorGUILayout.PropertyField(m_CullingMask); EditorGUILayout.Space(); } tk2dCameraSettings cameraSettings = target.CameraSettings; tk2dCameraSettings inheritedSettings = target.SettingsRoot.CameraSettings; GUI.enabled &= allowProjectionParameters; inheritedSettings.projection = (tk2dCameraSettings.ProjectionType)EditorGUILayout.EnumPopup("Projection", inheritedSettings.projection); EditorGUI.indentLevel++; if (inheritedSettings.projection == tk2dCameraSettings.ProjectionType.Orthographic) { inheritedSettings.orthographicType = (tk2dCameraSettings.OrthographicType)EditorGUILayout.EnumPopup("Type", inheritedSettings.orthographicType); switch (inheritedSettings.orthographicType) { case tk2dCameraSettings.OrthographicType.OrthographicSize: inheritedSettings.orthographicSize = Mathf.Max(0.001f, EditorGUILayout.FloatField("Orthographic Size", inheritedSettings.orthographicSize)); break; case tk2dCameraSettings.OrthographicType.PixelsPerMeter: inheritedSettings.orthographicPixelsPerMeter = Mathf.Max(0.001f, EditorGUILayout.FloatField("Pixels per Meter", inheritedSettings.orthographicPixelsPerMeter)); break; } inheritedSettings.orthographicOrigin = (tk2dCameraSettings.OrthographicOrigin)EditorGUILayout.EnumPopup("Origin", inheritedSettings.orthographicOrigin); } else if (inheritedSettings.projection == tk2dCameraSettings.ProjectionType.Perspective) { inheritedSettings.fieldOfView = EditorGUILayout.Slider("Field of View", inheritedSettings.fieldOfView, 1, 179); transparencySortMode = (TransparencySortMode)EditorGUILayout.EnumPopup("Sort mode", transparencySortMode); } EditorGUI.indentLevel--; GUI.enabled = oldGuiEnabled; if (complete) { EditorGUILayout.Space(); GUILayout.Label("Clipping Planes"); GUILayout.BeginHorizontal(); GUILayout.Space(14); GUILayout.Label("Near"); if (m_Near != null) { EditorGUILayout.PropertyField(m_Near, GUIContent.none, GUILayout.Width(60)); } GUILayout.Label("Far"); if (m_Far != null) { EditorGUILayout.PropertyField(m_Far, GUIContent.none, GUILayout.Width(60)); } GUILayout.EndHorizontal(); cameraSettings.rect = EditorGUILayout.RectField("Normalized View Port Rect", cameraSettings.rect); EditorGUILayout.Space(); if (m_Depth != null) { EditorGUILayout.PropertyField(m_Depth); } if (m_RenderingPath != null) { EditorGUILayout.PropertyField(m_RenderingPath); } if (m_TargetTexture != null) { EditorGUILayout.PropertyField(m_TargetTexture); } if (m_HDR != null) { EditorGUILayout.PropertyField(m_HDR); } } cam.ApplyModifiedProperties(); so.ApplyModifiedProperties(); if (transparencySortMode != target.camera.transparencySortMode) { target.camera.transparencySortMode = transparencySortMode; EditorUtility.SetDirty(target.camera); } }
void DrawCameraGUI(tk2dCamera target, bool complete) { bool allowProjectionParameters = target.SettingsRoot == target; bool oldGuiEnabled = GUI.enabled; SerializedObject so = this.serializedObject; SerializedProperty m_ClearFlags = so.FindProperty("cameraSettings.clearFlags"); SerializedProperty m_BackGroundColor = so.FindProperty("cameraSettings.backgroundColor"); SerializedProperty m_CullingMask = so.FindProperty("cameraSettings.cullingMask"); SerializedProperty m_TargetTexture = so.FindProperty("cameraSettings.targetTexture"); if (complete) { EditorGUILayout.PropertyField(m_ClearFlags); EditorGUILayout.PropertyField(m_BackGroundColor); EditorGUILayout.PropertyField(m_CullingMask); EditorGUILayout.Space(); } tk2dCameraSettings cameraSettings = target.CameraSettings; tk2dCameraSettings inheritedSettings = target.SettingsRoot.CameraSettings; GUI.enabled &= allowProjectionParameters; inheritedSettings.projection = (tk2dCameraSettings.ProjectionType)EditorGUILayout.EnumPopup("Projection", inheritedSettings.projection); EditorGUI.indentLevel++; if (inheritedSettings.projection == tk2dCameraSettings.ProjectionType.Orthographic) { inheritedSettings.orthographicType = (tk2dCameraSettings.OrthographicType)EditorGUILayout.EnumPopup("Type", inheritedSettings.orthographicType); switch (inheritedSettings.orthographicType) { case tk2dCameraSettings.OrthographicType.OrthographicSize: inheritedSettings.orthographicSize = Mathf.Max(0.001f, EditorGUILayout.FloatField("Orthographic Size", inheritedSettings.orthographicSize)); break; case tk2dCameraSettings.OrthographicType.PixelsPerMeter: inheritedSettings.orthographicPixelsPerMeter = Mathf.Max(0.001f, EditorGUILayout.FloatField("Pixels per Meter", inheritedSettings.orthographicPixelsPerMeter)); break; } inheritedSettings.orthographicOrigin = (tk2dCameraSettings.OrthographicOrigin)EditorGUILayout.EnumPopup("Origin", inheritedSettings.orthographicOrigin); } else if (inheritedSettings.projection == tk2dCameraSettings.ProjectionType.Perspective) { inheritedSettings.fieldOfView = EditorGUILayout.Slider("Field of View", inheritedSettings.fieldOfView, 1, 179); inheritedSettings.transparencySortMode = (TransparencySortMode)EditorGUILayout.EnumPopup("Sort mode", inheritedSettings.transparencySortMode); } EditorGUI.indentLevel--; GUI.enabled = oldGuiEnabled; if (complete) { EditorGUILayout.Space(); GUILayout.Label("Clipping Planes"); GUILayout.BeginHorizontal(); GUILayout.Space(14); GUILayout.Label("Near"); cameraSettings.nearClipPlane = Mathf.Min(EditorGUILayout.FloatField(cameraSettings.nearClipPlane), cameraSettings.farClipPlane - 0.01f); GUILayout.Label("Far"); cameraSettings.farClipPlane = Mathf.Max(EditorGUILayout.FloatField(cameraSettings.farClipPlane), cameraSettings.nearClipPlane + 0.01f); GUILayout.EndHorizontal(); cameraSettings.rect = EditorGUILayout.RectField("Normalized View Port Rect", cameraSettings.rect); EditorGUILayout.Space(); cameraSettings.depth = EditorGUILayout.FloatField("Depth", cameraSettings.depth); cameraSettings.renderingPath = (RenderingPath)EditorGUILayout.EnumPopup("Rendering Path", cameraSettings.renderingPath); if (m_TargetTexture != null) { EditorGUILayout.PropertyField(m_TargetTexture); } cameraSettings.hdr = EditorGUILayout.Toggle("HDR", cameraSettings.hdr); } so.ApplyModifiedProperties(); }
/// <summary> /// Updates the camera matrix to ensure 1:1 pixel mapping /// Or however the override is set up. /// </summary> public void UpdateCameraMatrix() { Upgrade(); if (!this.viewportClippingEnabled) { inst = this; } Camera unityCamera = UnityCamera; tk2dCamera settings = SettingsRoot; tk2dCameraSettings inheritedCameraSettings = settings.CameraSettings; if (unityCamera.clearFlags != cameraSettings.clearFlags) { unityCamera.clearFlags = cameraSettings.clearFlags; } if (unityCamera.backgroundColor != cameraSettings.backgroundColor) { unityCamera.backgroundColor = cameraSettings.backgroundColor; } if (unityCamera.cullingMask != cameraSettings.cullingMask) { unityCamera.cullingMask = cameraSettings.cullingMask; } if (unityCamera.farClipPlane != cameraSettings.farClipPlane) { unityCamera.farClipPlane = cameraSettings.farClipPlane; } if (unityCamera.nearClipPlane != cameraSettings.nearClipPlane) { unityCamera.nearClipPlane = cameraSettings.nearClipPlane; } if (unityCamera.rect != cameraSettings.rect) { unityCamera.rect = cameraSettings.rect; } if (unityCamera.depth != cameraSettings.depth) { unityCamera.depth = cameraSettings.depth; } if (unityCamera.renderingPath != cameraSettings.renderingPath) { unityCamera.renderingPath = cameraSettings.renderingPath; } // Checking unityCamera.targetTexture allocates memory. Simply setting it all the time now. unityCamera.targetTexture = cameraSettings.targetTexture; if (unityCamera.hdr != cameraSettings.hdr) { unityCamera.hdr = cameraSettings.hdr; } // Projection type is inherited from base camera _targetResolution = GetScreenPixelDimensions(settings); if (inheritedCameraSettings.projection == tk2dCameraSettings.ProjectionType.Perspective) { unityCamera.orthographic = false; unityCamera.fieldOfView = inheritedCameraSettings.fieldOfView * ZoomFactor; unityCamera.transparencySortMode = inheritedCameraSettings.transparencySortMode; _screenExtents.Set(-unityCamera.aspect, -1, unityCamera.aspect * 2, 2); _nativeScreenExtents = _screenExtents; unityCamera.ResetProjectionMatrix(); } else { unityCamera.transparencySortMode = TransparencySortMode.Orthographic; unityCamera.orthographic = true; // Find an override if necessary Matrix4x4 m = GetProjectionMatrixForOverride(settings, settings.CurrentResolutionOverride, _targetResolution.x, _targetResolution.y, true, out _screenExtents, out _nativeScreenExtents); if (unityCamera.projectionMatrix != m) { unityCamera.projectionMatrix = m; } } }
/// <summary> /// Updates the camera matrix to ensure 1:1 pixel mapping /// Or however the override is set up. /// </summary> public void UpdateCameraMatrix() { Upgrade(); if (!this.viewportClippingEnabled) { inst = this; } Camera unityCamera = UnityCamera; tk2dCamera settings = SettingsRoot; tk2dCameraSettings inheritedCameraSettings = settings.CameraSettings; if (unityCamera.clearFlags != cameraSettings.clearFlags) { unityCamera.clearFlags = cameraSettings.clearFlags; } if (unityCamera.backgroundColor != cameraSettings.backgroundColor) { unityCamera.backgroundColor = cameraSettings.backgroundColor; } if (unityCamera.cullingMask != cameraSettings.cullingMask) { unityCamera.cullingMask = cameraSettings.cullingMask; } if (unityCamera.farClipPlane != cameraSettings.farClipPlane) { unityCamera.farClipPlane = cameraSettings.farClipPlane; } if (unityCamera.nearClipPlane != cameraSettings.nearClipPlane) { unityCamera.nearClipPlane = cameraSettings.nearClipPlane; } if (unityCamera.rect != cameraSettings.rect) { unityCamera.rect = cameraSettings.rect; } if (unityCamera.depth != cameraSettings.depth) { unityCamera.depth = cameraSettings.depth; } if (unityCamera.renderingPath != cameraSettings.renderingPath) { unityCamera.renderingPath = cameraSettings.renderingPath; } // Checking unityCamera.targetTexture allocates memory. Simply setting it all the time now. unityCamera.targetTexture = cameraSettings.targetTexture; if (unityCamera.hdr != cameraSettings.hdr) { unityCamera.hdr = cameraSettings.hdr; } // Projection type is inherited from base camera _targetResolution = GetScreenPixelDimensions(settings); if (inheritedCameraSettings.projection == tk2dCameraSettings.ProjectionType.Perspective) { unityCamera.orthographic = false; unityCamera.fieldOfView = inheritedCameraSettings.fieldOfView * ZoomFactor; unityCamera.transparencySortMode = inheritedCameraSettings.transparencySortMode; _screenExtents.Set(-unityCamera.aspect, -1, unityCamera.aspect * 2, 2); _nativeScreenExtents = _screenExtents; unityCamera.ResetProjectionMatrix(); } else { unityCamera.transparencySortMode = TransparencySortMode.Orthographic; unityCamera.orthographic = true; // Find an override if necessary Matrix4x4 m = GetProjectionMatrixForOverride(settings, settings.CurrentResolutionOverride, _targetResolution.x, _targetResolution.y, true, out _screenExtents, out _nativeScreenExtents); #if !(UNITY_3_5 || UNITY_4_0 || UNITY_4_1) // Windows phone? if (Application.platform == RuntimePlatform.WP8Player && (Screen.orientation == ScreenOrientation.LandscapeLeft || Screen.orientation == ScreenOrientation.LandscapeRight)) { float angle = (Screen.orientation == ScreenOrientation.LandscapeRight) ? 90.0f : -90.0f; Matrix4x4 m2 = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0, 0, angle), Vector3.one); m = m2 * m; } #endif if (unityCamera.projectionMatrix != m) { unityCamera.projectionMatrix = m; } } }
private void applyMask() { Matrix4x4 maskTransform = Matrix4x4.identity; maskTransform.m00 = m_Mask.currentState.a; maskTransform.m01 = m_Mask.currentState.c; maskTransform.m10 = m_Mask.currentState.b; maskTransform.m11 = m_Mask.currentState.d; #if GAF_USING_TK2D float screenHeight = 0; float screenWidth = 0; Vector2 cameraPosShift = Vector2.zero; tk2dCamera tk2d_camera = Camera.current.GetComponent <tk2dCamera>(); if (tk2d_camera != null) { tk2dCameraSettings cameraSettings = tk2d_camera.CameraSettings; if (cameraSettings.orthographicType == tk2dCameraSettings.OrthographicType.PixelsPerMeter) { screenHeight = tk2d_camera.nativeResolutionHeight / cameraSettings.orthographicPixelsPerMeter; } else { screenHeight = tk2d_camera.CameraSettings.orthographicSize * 2; } screenWidth = Camera.current.aspect * screenHeight; cameraPosShift = Camera.current.transform.position - new Vector3(screenWidth / 2f, -screenHeight / 2f); } else { screenHeight = Camera.current.orthographicSize * 2; screenWidth = Camera.current.aspect * screenHeight; cameraPosShift = Camera.current.transform.position - new Vector3(screenWidth / 2f, -screenHeight / 2f); } #else float screenHeight = Camera.current.orthographicSize * 2; float screenWidth = Camera.current.aspect * screenHeight; Vector2 cameraPosShift = Camera.current.transform.position - new Vector3(screenWidth / 2f, -screenHeight / 2f); #endif // GAF_USING_TK2D float scaleX = Mathf.Sqrt((maskTransform.m00 * maskTransform.m00) + (maskTransform.m01 * maskTransform.m01)); float scaleY = Mathf.Sqrt((maskTransform.m11 * maskTransform.m11) + (maskTransform.m10 * maskTransform.m10)); float scale = movieClip.settings.pixelsPerUnit * m_Mask.atlasElement.scale * movieClip.settings.csf; float sizeXUV = (float)screenWidth / (m_Mask.texture.width / scale * scaleX * transform.parent.localScale.x * Camera.current.aspect); float sizeYUV = (float)screenHeight / (m_Mask.texture.height / scale * scaleY * transform.parent.localScale.y); float maskWidth = (float)m_Mask.texture.width / movieClip.settings.csf; float maskHeight = (float)m_Mask.texture.height / movieClip.settings.csf; float pivotX = m_Mask.atlasElement.pivotX / maskWidth; float pivotY = (maskHeight - m_Mask.atlasElement.pivotY) / maskHeight; float moveX = (-m_Mask.transform.position.x + cameraPosShift.x) / screenWidth; float moveY = -1f - (m_Mask.transform.position.y - cameraPosShift.y) / screenHeight; Matrix4x4 _transform = Matrix4x4.identity; _transform *= Matrix4x4.TRS(new Vector3(pivotX, pivotY, 0f), Quaternion.identity, Vector3.one); _transform *= Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(sizeXUV, sizeYUV, 1f)); _transform *= Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0f, 0f, -transform.parent.localRotation.eulerAngles.z), Vector3.one); _transform *= maskTransform; _transform *= Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(1f / scaleX, 1f / scaleY, 1f)); _transform *= Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(Camera.current.aspect, 1f, 1f)); _transform *= Matrix4x4.TRS(new Vector3(moveX, moveY, 0f), Quaternion.identity, Vector3.one); renderer.sharedMaterial.SetMatrix("_TransformMatrix", _transform); renderer.sharedMaterial.SetTexture("_MaskMap", m_Mask.texture); }