Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
RedFrame floorplan screenshot capture script
using UnityEngine;
using UnityEditor;
using System.Collections;
public class FloorplanScreenshot : Editor
{
const int imageSize = 6000;
const bool oblique = true;
[MenuItem("RedFrame/Take Floorplan Screenshots")]
public static void CreateFloorplanShots ()
{
var go = new GameObject ("cam");
go.transform.rotation = Quaternion.Euler (new Vector3 (90, 0, 0));
var cam = go.AddComponent<Camera> ();
cam.isOrthoGraphic = true;
cam.orthographicSize = 20;
cam.aspect = 1;
if (oblique) {
var obliqueCam = go.AddComponent<ObliqueCameraProjection> ();
}
// Set up render texture
RenderTexture renderTex = new RenderTexture (imageSize, imageSize, 24);
renderTex.hideFlags = HideFlags.HideAndDontSave;
RenderTexture.active = renderTex;
cam.targetTexture = renderTex;
go.transform.position = (Vector3.up * 1.61f) + (Vector3.forward * 6);
CaptureShot (cam, "BottomFloor");
go.transform.position = Vector3.up * 2.75f + (Vector3.forward * 6);
cam.ResetProjectionMatrix ();
cam.farClipPlane = 1.45f;
CaptureShot (cam, "TopFloor");
RenderTexture.active = null;
Object.DestroyImmediate (renderTex);
GameObject.DestroyImmediate (go);
Debug.Log ("done");
}
static void CaptureShot (Camera cam, string filename)
{
// Set up cabinet perspective
if (oblique) {
var obliqueCam = cam.GetComponent<ObliqueCameraProjection> ();
obliqueCam.slideViewport = new Vector2 (-0.012f, 0.012f);
obliqueCam.slideFrustum = new Vector2 (-0.04f, 0.04f);
obliqueCam.CalculateProjectionInEditor ();
}
cam.Render ();
var tex2d = new Texture2D (imageSize, imageSize);
tex2d.hideFlags = HideFlags.HideAndDontSave;
tex2d.ReadPixels (new Rect (0, 0, imageSize, imageSize), 0, 0);
tex2d.Apply ();
byte[] png = tex2d.EncodeToPNG ();
System.IO.File.WriteAllBytes (Application.dataPath + "/../" + filename + ".png", png);
Object.DestroyImmediate (tex2d);
}
}
using UnityEngine;
using System.Collections;
public class ObliqueCameraProjection : MonoBehaviour
{
private Matrix4x4 originalProjection;
public Vector2 slideViewport;
public Vector2 slideFrustum;
public Vector2 slideFarClip; // compound slideViewport and slideFrustum
public Vector2 skew;
public Vector2 scale;
void Awake ()
{
originalProjection = camera.projectionMatrix;
}
void LateUpdate ()
{
CalculateProjection (originalProjection);
}
void OnPostRender ()
{
camera.projectionMatrix = originalProjection;
}
[ContextMenu ("Calculate Projection")]
public void CalculateProjectionInEditor ()
{
originalProjection = camera.projectionMatrix;
CalculateProjection (originalProjection);
}
void CalculateProjection (Matrix4x4 p)
{
// Matrix4x4 p = originalProjection;
p.m00 += scale.x;
p.m11 += scale.y;
p.m01 += skew.x;
p.m10 += skew.y;
p.m02 += slideViewport.x;
p.m12 += slideViewport.y;
p.m03 += slideFrustum.x;
p.m13 += slideFrustum.y;
p.m02 += slideFarClip.x;
p.m12 += slideFarClip.y;
p.m03 += slideFarClip.x / 2;
p.m13 += slideFarClip.y / 2;
camera.projectionMatrix = p;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment