Advertisement
Guest User

level snapshotting minimap camera

a guest
Jun 28th, 2022
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.10 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. // @kurtdekker
  6. //
  7. // This is only the driver-wrapper for the LevelSnapshotter, which does all the actual work
  8. //
  9. // Methods of making one of these:
  10. //
  11. //  1. Put it in a scene, set the public variables
  12. //  2. call Create() with position and extent
  13. //  3. call ClearVertexBounds, ConsiderVertex, and CreateAuto() to do it all from procgen!
  14. //
  15.  
  16. public class TakeLevelSnapshot : MonoBehaviour
  17. {
  18.     public float Delay = 1.0f;
  19.  
  20.     public Vector3 ArealExtent = new Vector3( 250, 500, 250);
  21.  
  22.     public static TakeLevelSnapshot Create( Vector3 Position, Vector3 ArealExtent)
  23.     {
  24.         var tls = new GameObject("TakeLevelSnapshot.Create();").AddComponent<TakeLevelSnapshot>();
  25.  
  26.         tls.transform.position = Position;
  27.  
  28.         tls.ArealExtent = ArealExtent;
  29.  
  30.         tls.Reset();
  31.  
  32.         return tls;
  33.     }
  34.  
  35.     void Reset()
  36.     {
  37.         name = GetType().ToString();
  38.     }
  39.  
  40.     IEnumerator Start ()
  41.     {
  42.         // always arbitrarily delayed to ensure all the capture-able and fuel spots
  43.         yield return new WaitForSeconds( Delay);
  44.  
  45.         LevelSnapshotter.TakeSnapshot(
  46.             Position: transform.position,
  47.             ArealExtent: ArealExtent); 
  48.     }
  49.  
  50.  
  51.     // auto-areal-attempt creation
  52.     // is reset at start of procedural generation
  53.     static int InputCounter;
  54.     static Vector3 MinPosition;
  55.     static Vector3 MaxPosition;
  56.  
  57.     public static void ClearVertexBounds()
  58.     {
  59.         InputCounter = 0;
  60.     }
  61.  
  62.     public static void ConsiderVertex( Vector3 pos)
  63.     {
  64.         if (InputCounter == 0)
  65.         {
  66.             MaxPosition = pos;
  67.             MinPosition = pos;
  68.         }
  69.  
  70.         if (pos.x < MinPosition.x) MinPosition.x = pos.x;
  71.         if (pos.y < MinPosition.y) MinPosition.y = pos.y;
  72.         if (pos.z < MinPosition.z) MinPosition.z = pos.z;
  73.  
  74.         if (pos.x > MaxPosition.x) MaxPosition.x = pos.x;
  75.         if (pos.y > MaxPosition.y) MaxPosition.y = pos.y;
  76.         if (pos.z > MaxPosition.z) MaxPosition.z = pos.z;
  77.  
  78.         InputCounter++;
  79.     }
  80.  
  81.     // for things like NewCubes and Floating Islands, where
  82.     // the "considered verts" were actually larger objects
  83.     // so we need to draw in more space...
  84.     public static void StretchAutoDimensions( Vector3 extra)
  85.     {
  86.         MinPosition -= extra;
  87.  
  88.         MaxPosition += extra;
  89.     }
  90.  
  91.     const float VerticalMargin = 50.0f;
  92.  
  93.     // call the delayed one below already
  94.     static void CreateAuto()
  95.     {
  96.         if (InputCounter > 0)
  97.         {
  98.             Vector3 Position = (MaxPosition + MinPosition) / 2;
  99.  
  100.             Position.y = MaxPosition.y + VerticalMargin;
  101.  
  102.             // makes x and z identical, at the larger span
  103.             float xSpan = MaxPosition.x - MinPosition.x;
  104.             float zSpan = MaxPosition.z - MinPosition.z;
  105.             Vector3 ArealExtent = Vector3.one * Mathf.Max( xSpan, zSpan);
  106.  
  107.             // remember only ArealExtent.x is used to render!
  108.             float size = Mathf.Max( ArealExtent.x, ArealExtent.z);
  109.  
  110.             size *= 0.50f;
  111.  
  112.             ArealExtent.x = size;
  113.             ArealExtent.z = size;
  114.  
  115.             ArealExtent.y += VerticalMargin * 2;
  116.  
  117.             Create( Position: Position, ArealExtent: ArealExtent);
  118.         }
  119.     }
  120.  
  121.     public static void CreateAutoDelayed( Transform levelParent)
  122.     {
  123.         TakeLevelSnapshot.CreateAuto();
  124.     }
  125.  
  126.     void OnDrawGizmos()
  127.     {
  128.         Gizmos.DrawWireCube( transform.position, ArealExtent * 2);
  129.     }
  130. }
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147. using System.Collections;
  148. using System.Collections.Generic;
  149. using UnityEngine;
  150.  
  151. // @kurtdekker
  152. //
  153. // Level snapshotter for Jetpack Kurt - takes a snapshot
  154. // of the level for later ground track display use.
  155.  
  156. public static class LevelSnapshotter
  157. {
  158.     const string filename = "snapshot.png";
  159.  
  160.     static string snapshotFilename
  161.     {
  162.         get
  163.         {
  164.             string result = System.IO.Path.Combine( Application.persistentDataPath, filename);
  165.  
  166.             Debug.Log( "snapshotFilename:" + result);
  167.  
  168.             return result;
  169.         }
  170.     }
  171.  
  172.     public static void ClearPreviousSnapshot()
  173.     {
  174.         try
  175.         {
  176.             System.IO.File.Delete( snapshotFilename);
  177.         }
  178.         catch( System.Exception e)      // pokemon
  179.         {
  180.             e.ToString();
  181.         }
  182.     }
  183.  
  184.     static bool DoWeNeedLight()
  185.     {
  186.         var all = GameObject.FindObjectsOfType<Light>();
  187.  
  188.         foreach( Light L in all)
  189.         {
  190.             if (L.type == LightType.Directional)
  191.             {
  192.                 if (L.enabled)
  193.                 {
  194.                     if (L.intensity >= 1.0f)
  195.                     {
  196.                         return false;
  197.                     }
  198.                 }
  199.             }
  200.         }
  201.         return true;
  202.     }
  203.  
  204.     public static void TakeSnapshot( Vector3 Position, Vector3 ArealExtent)
  205.     {
  206.         Camera cam = new GameObject("LevelSnapshotter.TakeSnapshot.Camera();").AddComponent<Camera>();
  207.  
  208.         cam.transform.position = Position;
  209.         cam.transform.localRotation = Quaternion.Euler( 90, 0, 0);
  210.         cam.orthographic = true;
  211.         cam.orthographicSize = ArealExtent.x;
  212.         cam.clearFlags = CameraClearFlags.SolidColor;
  213.         cam.backgroundColor = new Color (0.0f, 0.0f, 0.1f);
  214.  
  215.         if (DoWeNeedLight())
  216.         {
  217.             var lt = new GameObject( "LevelSnapshotter.Light").AddComponent<Light>();
  218.             lt.transform.SetParent( cam.transform);
  219.             lt.transform.localPosition = Vector3.zero;
  220.             lt.transform.localRotation = Quaternion.identity;
  221.  
  222.             lt.type = LightType.Directional;
  223.  
  224.             lt.intensity = 1.0f;
  225.         }
  226.  
  227.         int size = 1024;
  228.  
  229.         var rt = new RenderTexture( size, size, 32);
  230.  
  231.         cam.targetTexture = rt;
  232.  
  233.         cam.Render();
  234.  
  235.         RenderTexture.active = rt;
  236.         Texture2D t2d = new Texture2D( size, size, TextureFormat.RGB24, false);
  237.         t2d.ReadPixels( new Rect( 0, 0, size, size), 0, 0);
  238.         RenderTexture.active = null;
  239.  
  240.         GameObject.DestroyImmediate(cam.gameObject);
  241.         GameObject.DestroyImmediate( rt);
  242.  
  243.         var pngData = t2d.EncodeToPNG();
  244.  
  245.         try
  246.         {
  247.             System.IO.File.WriteAllBytes( snapshotFilename, pngData);
  248.  
  249.             DSM.LevelSnapshotter.Position.v3Value = Position;
  250.             DSM.LevelSnapshotter.ArealExtent.v3Value = ArealExtent;
  251.         }
  252.         catch( System.Exception e)      // pokemon
  253.         {
  254.             Debug.LogError( "LevelSnapshotter.TakeSnapshot(): Unable to write snapshot file to disk!" + e.ToString());
  255.         }
  256.  
  257.         GameObject.DestroyImmediate(t2d);
  258.     }
  259.  
  260.     public static bool SnapshotExists()
  261.     {
  262.         return LoadSnapshot() != null;
  263.     }
  264.  
  265.     public static Texture2D LoadSnapshot()
  266.     {
  267.         try
  268.         {
  269.             var pngData = System.IO.File.ReadAllBytes( snapshotFilename);
  270.  
  271.             var t2d = new Texture2D(256,256,TextureFormat.ARGB32, false);
  272.  
  273.             t2d.LoadImage( pngData);
  274.  
  275.             return t2d;
  276.         }
  277.         catch( System.Exception e)      // pokemon
  278.         {
  279.             e.ToString();
  280.         }
  281.         return null;
  282.     }
  283. }
  284.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement