Skip to content

Commit 2fea6e0

Browse files
committed
Fix for perspective camera inconsistency.
1 parent ce815f7 commit 2fea6e0

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

PixelCamera.cs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,17 @@ public CamSettings(Vector2 aspect, float zoomLevel, float fieldOfView, bool isOr
3434

3535
public bool Equals(CamSettings other)
3636
{
37-
bool isEqual = other.screenSize[0] == screenSize[0] &&
38-
other.screenSize[1] == screenSize[1] &&
39-
other.aspect != aspect &&
40-
Math.Abs(other.fieldOfView - fieldOfView) < float.Epsilon &&
41-
Math.Abs(other.zoomLevel - zoomLevel) < float.Epsilon;
37+
bool equalScreen = other.screenSize[0] == screenSize[0] &&
38+
other.screenSize[1] == screenSize[1];
39+
bool equalAspect = other.aspect == aspect;
40+
bool equalFoV = Math.Abs(other.fieldOfView - fieldOfView) <= float.Epsilon;
41+
bool equalZoom = Math.Abs(other.zoomLevel - zoomLevel) <= float.Epsilon;
42+
bool isEqual = equalScreen && equalAspect && equalFoV && equalZoom;
43+
//if (!isEqual)
44+
//{
45+
// Debug.LogFormat("scr {0}, asp {1}, fov {2}, zoom {3}", equalScreen, equalAspect, equalFoV, equalZoom);
46+
// Debug.LogFormat("Aspect: {0}, Other: {1}", aspect, other.aspect);
47+
//}
4248
return isEqual;
4349
}
4450
}
@@ -204,11 +210,9 @@ protected Vector2 GetScreenRenderSize()
204210
// in terms of fustrum distance, converted to pixels
205211
if (cam.orthographic == false)
206212
{
207-
// Reasonable fakery to use half of far clip as distance
208-
// if advanced settings doesn't exist yet
209-
float zDistance = cam.farClipPlane * 0.5f;
210-
if (advancedSettings != null)
211-
zDistance = advancedSettings.perspectiveZ;
213+
cam.aspect = (float) Screen.width / Screen.height;
214+
215+
float zDistance = PerspectiveZ;
212216

213217
var frustumHeight = 2.0f * zDistance * Mathf.Tan(cam.fieldOfView * 0.5f * Mathf.Deg2Rad);
214218
var frustumWidth = frustumHeight* cam.aspect;
@@ -226,13 +230,14 @@ protected void SetupCamera()
226230
var aspect = AspectStretch;
227231

228232
zoomLevel = Mathf.Max(0.05f, Mathf.Abs(zoomLevel))*Math.Sign(zoomLevel);
229-
// "Physical" render size
233+
// "Physical" pixel render size
230234
Vector2 screenRenderSize = GetScreenRenderSize();
231235
// Pixel render size
232236
int[] pixelRenderSize = GetRenderTextureSize(screenRenderSize, aspect);
233237

234-
// Find the settings to be used for drawing the GL quad
235-
Vector2 pixelSize = new Vector2(pixelRenderSize[0], pixelRenderSize[1]) * zoomLevel;
238+
float targetAspect = (float)pixelRenderSize[0] / (float)pixelRenderSize[1];
239+
cam.aspect = targetAspect;
240+
236241
if (cam.orthographic)
237242
{
238243
// Orthographic camera needs to use screen size when calculating quad offset
@@ -247,15 +252,13 @@ protected void SetupCamera()
247252
cam.orthographicSize = targetHeight;
248253
}
249254

255+
// Find the settings to be used for drawing the GL quad
256+
Vector2 pixelSize = new Vector2(pixelRenderSize[0], pixelRenderSize[1]) * zoomLevel;
250257
quadOffset = pixelSize - screenRenderSize;
251258
quadOffset /= 2;
252259
quadOffset.x /= Screen.width;
253260
quadOffset.y /= Screen.height;
254261

255-
// Set camera aspect ratio, since pixel aspect ratio will be different from current aspect
256-
float targetAspect = (float)pixelRenderSize[0] / (float)pixelRenderSize[1];
257-
cam.aspect = targetAspect;
258-
259262
// Important to release current render texture
260263
cam.targetTexture = null;
261264
fallbackMaterial.SetTexture("_MainTex", null);

0 commit comments

Comments
 (0)