• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by idbrii · Apr 27, 2021 at 10:08 PM · camerarenderingmapbounds

Capture Bounds with an orthographic camera -- runtime world map

I have bounds for a group of objects (calculated with Bounds.Encapsulate on the group). How can I capture an image of the objects to a texture?

For example, the objects are my level and I want to take an overhead capture of them to use as an in-game map. I want to precisely capture the bounds to make it easy to map a position within the bounds to a position within the map.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by idbrii · Apr 27, 2021 at 10:09 PM

Setup an orthographic camera where the size is set to the z of the bounds and render to a render texture with the same aspect ratio as the bounds.

Here's my solution:

 public RawImage _MapImage;
 // AspectRatioFitter ensures the resulting image matches the
 // bounds dimensions and allows converting between the
 // normalized position within the bounds to the position
 // within the map (for drawing positions on the map).
 public AspectRatioFitter _AspectFitter;
 public Bounds _WorldBounds;

 void CaptureMapImage() {
     var aspect_ratio = _WorldBounds.extents.x / _WorldBounds.extents.z;
     var t = _MapImage.transform as RectTransform;
     var height = Mathf.RoundToInt(t.rect.size.y);
     var width  = Mathf.RoundToInt(height * aspect_ratio);
     _AspectFitter.aspectRatio = aspect_ratio;

     // To make the map a live view of the world, allocate and
     // store RenderTexture instead of temporary and don't
     // destroy the camera.
     var render_target = RenderTexture.GetTemporary(width, height, 16);

     var cam = new GameObject("overhead map camera").AddComponent<Camera>();
     cam.targetTexture = render_target;
     cam.renderingPath = RenderingPath.Forward;

     // Clear solid color so empty areas are transparent
     cam.clearFlags = CameraClearFlags.SolidColor;
     cam.backgroundColor = Color.clear;

     // You may need to play with offset. I wanted it close to
     // my main ground plane so ceilings are not visible. You
     // may want it at _WorldBounds.extent.y height.
     cam.orthographic = true;
     var offset = Vector3.up;
     cam.transform.SetPositionAndRotation(_WorldBounds.center + offset, Quaternion.LookRotation(Vector3.down));
     cam.orthographicSize = _WorldBounds.extents.z;

     cam.Render();
     Util.DestroyGameObject(cam.gameObject);

     var tex = new Texture2D(render_target.width, render_target.height);
     var rect = new Rect(0, 0, tex.width, tex.height);

     var old_rt = RenderTexture.active;
     RenderTexture.active = render_target;
     tex.ReadPixels(rect, 0, 0);
     tex.Apply();
     RenderTexture.active = old_rt;

     RenderTexture.ReleaseTemporary(render_target);

     _MapImage.texture = tex;
 }

 // Use this to position world objects on the map.
 void PlaceOnMap(Transform world_obj, RectTransform map_obj) {
     var pos = OverheadToMap(world_obj.position);
     map_obj.anchoredPosition = pos;

     var yaw = world_obj.rotation.eulerAngles.y;
     map_obj.rotation = Quaternion.Euler(0f, 0f, -yaw);
 }

 // 
 // Helper math functions.
 //

 Vector2 OverheadToMap(Vector3 v) {
     var pos = OverheadTo2D(v - _WorldBounds.center);
     var world_size = OverheadTo2D(_WorldBounds);
     var pos_normalized = Vector2.Scale(pos, Inverse(world_size));
     var t = _MapImage.transform as RectTransform;
     var map_size = t.rect.size;
     return Vector2.Scale(pos_normalized, map_size);
 }

 Vector2 OverheadTo2D(Vector3 v) {
     return new Vector2(v.x, v.z);
 }

 Vector2 Inverse(Vector2 v) {
     return new Vector2(1f / v.x, 1f / v.y);
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

190 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rendering the same Render Texture differently to separate cameras 0 Answers

prefab rendering on top of its parent? 0 Answers

Keeping the camera in bounds 1 Answer

[Solved] Move camera with mouse dragging (2D) 1 Answer

Fastest way to hide objects during render calls and still have them update 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges