diff --git a/spine-unity/Assets/Spine Examples/Scripts/Sample Components/SkeletonRenderTexture/SkeletonGraphicRenderTexture.cs b/spine-unity/Assets/Spine Examples/Scripts/Sample Components/SkeletonRenderTexture/SkeletonGraphicRenderTexture.cs index 538dda064..4ab76153b 100644 --- a/spine-unity/Assets/Spine Examples/Scripts/Sample Components/SkeletonRenderTexture/SkeletonGraphicRenderTexture.cs +++ b/spine-unity/Assets/Spine Examples/Scripts/Sample Components/SkeletonRenderTexture/SkeletonGraphicRenderTexture.cs @@ -260,24 +260,30 @@ namespace Spine.Unity.Examples { commandBuffer.SetRenderTarget(renderTexture); commandBuffer.ClearRenderTarget(true, true, Color.clear); - Rect canvasRect = skeletonGraphic.canvas.pixelRect; - - Matrix4x4 projectionMatrix = Matrix4x4.Ortho( - canvasRect.x, canvasRect.x + canvasRect.width, - canvasRect.y, canvasRect.y + canvasRect.height, - float.MinValue, float.MaxValue); + Vector2 targetViewportSize = new Vector2( + screenSpaceMax.x - screenSpaceMin.x, + screenSpaceMax.y - screenSpaceMin.y); RenderMode canvasRenderMode = skeletonGraphic.canvas.renderMode; if (canvasRenderMode == RenderMode.ScreenSpaceOverlay) { + Rect canvasRect = skeletonGraphic.canvas.pixelRect; + canvasRect.x += screenSpaceMin.x; + canvasRect.y += screenSpaceMin.y; + canvasRect.width = targetViewportSize.x; + canvasRect.height = targetViewportSize.y; + Matrix4x4 projectionMatrix = Matrix4x4.Ortho( + canvasRect.x, canvasRect.x + canvasRect.width, + canvasRect.y, canvasRect.y + canvasRect.height, + float.MinValue, float.MaxValue); commandBuffer.SetViewMatrix(Matrix4x4.identity); commandBuffer.SetProjectionMatrix(projectionMatrix); } else { commandBuffer.SetViewMatrix(targetCamera.worldToCameraMatrix); - commandBuffer.SetProjectionMatrix(targetCamera.projectionMatrix); + Matrix4x4 projectionMatrix = CalculateProjectionMatrix(targetCamera, + screenSpaceMin, screenSpaceMax, skeletonGraphic.canvas.pixelRect.size); + commandBuffer.SetProjectionMatrix(projectionMatrix); } - - Vector2 targetCameraViewportSize = targetCamera.pixelRect.size; - Rect viewportRect = new Rect(-screenSpaceMin * downScaleFactor, targetCameraViewportSize * downScaleFactor); + Rect viewportRect = new Rect(Vector2.zero, targetViewportSize * downScaleFactor); commandBuffer.SetViewport(viewportRect); } diff --git a/spine-unity/Assets/Spine Examples/Scripts/Sample Components/SkeletonRenderTexture/SkeletonRenderTexture.cs b/spine-unity/Assets/Spine Examples/Scripts/Sample Components/SkeletonRenderTexture/SkeletonRenderTexture.cs index 769253834..bafe6ee4a 100644 --- a/spine-unity/Assets/Spine Examples/Scripts/Sample Components/SkeletonRenderTexture/SkeletonRenderTexture.cs +++ b/spine-unity/Assets/Spine Examples/Scripts/Sample Components/SkeletonRenderTexture/SkeletonRenderTexture.cs @@ -179,10 +179,16 @@ namespace Spine.Unity.Examples { commandBuffer.SetRenderTarget(renderTexture); commandBuffer.ClearRenderTarget(true, true, Color.clear); - commandBuffer.SetProjectionMatrix(targetCamera.projectionMatrix); commandBuffer.SetViewMatrix(targetCamera.worldToCameraMatrix); - Vector2 targetCameraViewportSize = targetCamera.pixelRect.size; - Rect viewportRect = new Rect(-screenSpaceMin * downScaleFactor, targetCameraViewportSize * downScaleFactor); + + Matrix4x4 projectionMatrix = CalculateProjectionMatrix(targetCamera, + screenSpaceMin, screenSpaceMax, targetCamera.pixelRect.size); + commandBuffer.SetProjectionMatrix(projectionMatrix); + + Vector2 targetViewportSize = new Vector2( + screenSpaceMax.x - screenSpaceMin.x, + screenSpaceMax.y - screenSpaceMin.y); + Rect viewportRect = new Rect(Vector2.zero, targetViewportSize * downScaleFactor); commandBuffer.SetViewport(viewportRect); } diff --git a/spine-unity/Assets/Spine Examples/Scripts/Sample Components/SkeletonRenderTexture/SkeletonRenderTextureBase.cs b/spine-unity/Assets/Spine Examples/Scripts/Sample Components/SkeletonRenderTexture/SkeletonRenderTextureBase.cs index 4cbfdd603..6c4c9de86 100644 --- a/spine-unity/Assets/Spine Examples/Scripts/Sample Components/SkeletonRenderTexture/SkeletonRenderTextureBase.cs +++ b/spine-unity/Assets/Spine Examples/Scripts/Sample Components/SkeletonRenderTexture/SkeletonRenderTextureBase.cs @@ -122,6 +122,45 @@ namespace Spine.Unity.Examples { } } + protected Matrix4x4 CalculateProjectionMatrix (Camera targetCamera, + Vector3 screenSpaceMin, Vector3 screenSpaceMax, Vector2 fullSizePixels) { + if (targetCamera.orthographic) + return CalculateOrthoMatrix(targetCamera, screenSpaceMin, screenSpaceMax, fullSizePixels); + else + return CalculatePerspectiveMatrix(targetCamera, screenSpaceMin, screenSpaceMax, fullSizePixels); + } + + protected Matrix4x4 CalculateOrthoMatrix (Camera targetCamera, + Vector3 screenSpaceMin, Vector3 screenSpaceMax, Vector2 fullSizePixels) { + + Vector2 cameraSize = new Vector2( + targetCamera.orthographicSize * 2.0f * targetCamera.aspect, + targetCamera.orthographicSize * 2.0f); + Vector2 min = new Vector2(screenSpaceMin.x, screenSpaceMin.y) / fullSizePixels; + Vector2 max = new Vector2(screenSpaceMax.x, screenSpaceMax.y) / fullSizePixels; + Vector2 centerOffset = new Vector2(-0.5f, -0.5f); + min = (min + centerOffset) * cameraSize; + max = (max + centerOffset) * cameraSize; + + return Matrix4x4.Ortho(min.x, max.x, min.y, max.y, float.MinValue, float.MaxValue); + } + + protected Matrix4x4 CalculatePerspectiveMatrix (Camera targetCamera, + Vector3 screenSpaceMin, Vector3 screenSpaceMax, Vector2 fullSizePixels) { + + FrustumPlanes frustumPlanes = targetCamera.projectionMatrix.decomposeProjection; + Vector2 planesSize = new Vector2( + frustumPlanes.right - frustumPlanes.left, + frustumPlanes.top - frustumPlanes.bottom); + Vector2 min = new Vector2(screenSpaceMin.x, screenSpaceMin.y) / fullSizePixels * planesSize; + Vector2 max = new Vector2(screenSpaceMax.x, screenSpaceMax.y) / fullSizePixels * planesSize; + frustumPlanes.right = frustumPlanes.left + max.x; + frustumPlanes.top = frustumPlanes.bottom + max.y; + frustumPlanes.left += min.x; + frustumPlanes.bottom += min.y; + return Matrix4x4.Frustum(frustumPlanes); + } + protected void AssignAtQuad () { Transform quadTransform = quad.transform; quadTransform.position = this.transform.position; diff --git a/spine-unity/Assets/Spine Examples/package.json b/spine-unity/Assets/Spine Examples/package.json index 51fc6ddb2..e6d9c0d53 100644 --- a/spine-unity/Assets/Spine Examples/package.json +++ b/spine-unity/Assets/Spine Examples/package.json @@ -2,7 +2,7 @@ "name": "com.esotericsoftware.spine.spine-unity-examples", "displayName": "spine-unity Runtime Examples", "description": "This plugin provides example scenes and scripts for the spine-unity runtime.", - "version": "4.2.31", + "version": "4.2.32", "unity": "2018.3", "author": { "name": "Esoteric Software",