Skip to content

Instantly share code, notes, and snippets.

@steve-salmond
Last active June 11, 2023 12:25
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save steve-salmond/5952537 to your computer and use it in GitHub Desktop.
Save steve-salmond/5952537 to your computer and use it in GitHub Desktop.
A simple implementation of an infinite zooming technique a la Zoom Quilt (http://zoomquilt.org) for Unity 3d. Takes the player's position on the z axis and adjusts the cards to match. The cards should be rendered by an orthographic camera
using UnityEngine;
using System.Collections;
public class ZoomCardController : MonoBehaviour {
/** Collection of zooming cards to be displayed. */
public GameObject[] Cards;
/** Distance player travels between cards. */
public float DistanceBetweenCards = 10;
void LateUpdate () {
// Locate player in world space.
Vector3 p = PlayerController.Instance.transform.position;
// Determine current travel fraction along the z-axis.
// Varies from [0..1] as the player moves between successive cards.
float t = (p.z % DistanceBetweenCards) / DistanceBetweenCards;
if (t < 0)
t += 1;
// Bias the fraction to smooth out view jerkiness.
// Not exactly sure why this helps, but it does.. :)
t = Mathf.Pow(t, 1.3f);
// Set up each card for current travel fraction.
int n = Cards.Length;
for (int i = 0; i<n; i++)
UpdateCard(Cards[i], i, n, t);
// Adjust alpha of the smallest card.
// The smallest card fades in as you get closer to it.
Cards[0].renderer.material.color = new Color(1, 1, 1, t);
}
private void UpdateCard(GameObject card, int i, int n, float t)
{
float divisor = Mathf.Pow(3, n - 1);
float minZoom = Mathf.Pow(3, i) / divisor;
float maxZoom = Mathf.Pow(3, i + 1) / divisor;
float s = Mathf.Lerp(minZoom, maxZoom, t);
card.transform.localScale = new Vector3(s, s, s);
}
}
@karolisbutkevicius
Copy link

What would be the solution for changing of resolutions? How to prevent distortion of sprites with each change?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment