Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- // @kurtdekker
- //
- // This is only the driver-wrapper for the LevelSnapshotter, which does all the actual work
- //
- // Methods of making one of these:
- //
- // 1. Put it in a scene, set the public variables
- // 2. call Create() with position and extent
- // 3. call ClearVertexBounds, ConsiderVertex, and CreateAuto() to do it all from procgen!
- //
- public class TakeLevelSnapshot : MonoBehaviour
- {
- public float Delay = 1.0f;
- public Vector3 ArealExtent = new Vector3( 250, 500, 250);
- public static TakeLevelSnapshot Create( Vector3 Position, Vector3 ArealExtent)
- {
- var tls = new GameObject("TakeLevelSnapshot.Create();").AddComponent<TakeLevelSnapshot>();
- tls.transform.position = Position;
- tls.ArealExtent = ArealExtent;
- tls.Reset();
- return tls;
- }
- void Reset()
- {
- name = GetType().ToString();
- }
- IEnumerator Start ()
- {
- // always arbitrarily delayed to ensure all the capture-able and fuel spots
- yield return new WaitForSeconds( Delay);
- LevelSnapshotter.TakeSnapshot(
- Position: transform.position,
- ArealExtent: ArealExtent);
- }
- // auto-areal-attempt creation
- // is reset at start of procedural generation
- static int InputCounter;
- static Vector3 MinPosition;
- static Vector3 MaxPosition;
- public static void ClearVertexBounds()
- {
- InputCounter = 0;
- }
- public static void ConsiderVertex( Vector3 pos)
- {
- if (InputCounter == 0)
- {
- MaxPosition = pos;
- MinPosition = pos;
- }
- if (pos.x < MinPosition.x) MinPosition.x = pos.x;
- if (pos.y < MinPosition.y) MinPosition.y = pos.y;
- if (pos.z < MinPosition.z) MinPosition.z = pos.z;
- if (pos.x > MaxPosition.x) MaxPosition.x = pos.x;
- if (pos.y > MaxPosition.y) MaxPosition.y = pos.y;
- if (pos.z > MaxPosition.z) MaxPosition.z = pos.z;
- InputCounter++;
- }
- // for things like NewCubes and Floating Islands, where
- // the "considered verts" were actually larger objects
- // so we need to draw in more space...
- public static void StretchAutoDimensions( Vector3 extra)
- {
- MinPosition -= extra;
- MaxPosition += extra;
- }
- const float VerticalMargin = 50.0f;
- // call the delayed one below already
- static void CreateAuto()
- {
- if (InputCounter > 0)
- {
- Vector3 Position = (MaxPosition + MinPosition) / 2;
- Position.y = MaxPosition.y + VerticalMargin;
- // makes x and z identical, at the larger span
- float xSpan = MaxPosition.x - MinPosition.x;
- float zSpan = MaxPosition.z - MinPosition.z;
- Vector3 ArealExtent = Vector3.one * Mathf.Max( xSpan, zSpan);
- // remember only ArealExtent.x is used to render!
- float size = Mathf.Max( ArealExtent.x, ArealExtent.z);
- size *= 0.50f;
- ArealExtent.x = size;
- ArealExtent.z = size;
- ArealExtent.y += VerticalMargin * 2;
- Create( Position: Position, ArealExtent: ArealExtent);
- }
- }
- public static void CreateAutoDelayed( Transform levelParent)
- {
- TakeLevelSnapshot.CreateAuto();
- }
- void OnDrawGizmos()
- {
- Gizmos.DrawWireCube( transform.position, ArealExtent * 2);
- }
- }
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- // @kurtdekker
- //
- // Level snapshotter for Jetpack Kurt - takes a snapshot
- // of the level for later ground track display use.
- public static class LevelSnapshotter
- {
- const string filename = "snapshot.png";
- static string snapshotFilename
- {
- get
- {
- string result = System.IO.Path.Combine( Application.persistentDataPath, filename);
- Debug.Log( "snapshotFilename:" + result);
- return result;
- }
- }
- public static void ClearPreviousSnapshot()
- {
- try
- {
- System.IO.File.Delete( snapshotFilename);
- }
- catch( System.Exception e) // pokemon
- {
- e.ToString();
- }
- }
- static bool DoWeNeedLight()
- {
- var all = GameObject.FindObjectsOfType<Light>();
- foreach( Light L in all)
- {
- if (L.type == LightType.Directional)
- {
- if (L.enabled)
- {
- if (L.intensity >= 1.0f)
- {
- return false;
- }
- }
- }
- }
- return true;
- }
- public static void TakeSnapshot( Vector3 Position, Vector3 ArealExtent)
- {
- Camera cam = new GameObject("LevelSnapshotter.TakeSnapshot.Camera();").AddComponent<Camera>();
- cam.transform.position = Position;
- cam.transform.localRotation = Quaternion.Euler( 90, 0, 0);
- cam.orthographic = true;
- cam.orthographicSize = ArealExtent.x;
- cam.clearFlags = CameraClearFlags.SolidColor;
- cam.backgroundColor = new Color (0.0f, 0.0f, 0.1f);
- if (DoWeNeedLight())
- {
- var lt = new GameObject( "LevelSnapshotter.Light").AddComponent<Light>();
- lt.transform.SetParent( cam.transform);
- lt.transform.localPosition = Vector3.zero;
- lt.transform.localRotation = Quaternion.identity;
- lt.type = LightType.Directional;
- lt.intensity = 1.0f;
- }
- int size = 1024;
- var rt = new RenderTexture( size, size, 32);
- cam.targetTexture = rt;
- cam.Render();
- RenderTexture.active = rt;
- Texture2D t2d = new Texture2D( size, size, TextureFormat.RGB24, false);
- t2d.ReadPixels( new Rect( 0, 0, size, size), 0, 0);
- RenderTexture.active = null;
- GameObject.DestroyImmediate(cam.gameObject);
- GameObject.DestroyImmediate( rt);
- var pngData = t2d.EncodeToPNG();
- try
- {
- System.IO.File.WriteAllBytes( snapshotFilename, pngData);
- DSM.LevelSnapshotter.Position.v3Value = Position;
- DSM.LevelSnapshotter.ArealExtent.v3Value = ArealExtent;
- }
- catch( System.Exception e) // pokemon
- {
- Debug.LogError( "LevelSnapshotter.TakeSnapshot(): Unable to write snapshot file to disk!" + e.ToString());
- }
- GameObject.DestroyImmediate(t2d);
- }
- public static bool SnapshotExists()
- {
- return LoadSnapshot() != null;
- }
- public static Texture2D LoadSnapshot()
- {
- try
- {
- var pngData = System.IO.File.ReadAllBytes( snapshotFilename);
- var t2d = new Texture2D(256,256,TextureFormat.ARGB32, false);
- t2d.LoadImage( pngData);
- return t2d;
- }
- catch( System.Exception e) // pokemon
- {
- e.ToString();
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement