{"id":3692,"date":"2015-07-26T16:13:43","date_gmt":"2015-07-26T15:13:43","guid":{"rendered":"http:\/\/t-machine.org\/?p=3692"},"modified":"2015-08-01T15:55:11","modified_gmt":"2015-08-01T14:55:11","slug":"new-unity3d-feature-virtual-scenes","status":"publish","type":"post","link":"http:\/\/new.t-machine.org\/index.php\/2015\/07\/26\/new-unity3d-feature-virtual-scenes\/","title":{"rendered":"New #unity3d feature: Virtual Scenes"},"content":{"rendered":"<p>Unity3D has a great core architecture &#8211; it&#8217;s easy to understand and use. However, it has some significant flaws.<\/p>\n<p>One of the recurring problems I run into is that Unity requirs you to have precisely one &#8220;Scene&#8221; at any one time, but often you need to have multiple scenes at the same time. Problem? Yes, lots&#8230;<\/p>\n<p>Unity 5 has some hacks to make it &#8220;sort-of possible&#8221; to have multiple scenes at once in-Editor.<\/p>\n<p><em>Note: these were <a href=\"http:\/\/blogs.unity3d.com\/2014\/08\/04\/multi-scene-editing\/\">mentioned by Unity corp in August 2014<\/a>, but aren&#8217;t available to most users yet<\/em><\/p>\n<p>But they (appear to be) hacks: they help for a fraction of the use-cases. That&#8217;s fine: Unity has 10 years of software written on the assumption there is only one &#8220;Scene&#8221;; that&#8217;s an enormous amount of code to change! But can we do something about this?<\/p>\n<h2>TL;DR: I&#8217;ll put this on the Asset Store<\/h2>\n<p><strong>If you just want a working solution, <a href=\"http:\/\/forum.unity3d.com\/threads\/virtual-scenes-create-manage-multiple-areas-cameras-in-one-unity-scene-file.345009\/\">here&#8217;s the Unity Asset store page \/ support page, new versions will appear here first<\/a><\/strong>.<\/p>\n<p>I&#8217;m using it myself on my own projects, and will do some beta testing with other people&#8217;s games. If it works well enough, it&#8217;ll go on Asset Store. I suggest reading this full post anyway &#8211; it&#8217;s an example of how to creatively solve small issues like this in Unity.<\/p>\n<p>(if you&#8217;d like to beta-test this, tweet me @t_machine_org or email me (address at top of page). Unity Corp only let me have 12 beta-testers, so you&#8217;ll need to give me some info about what you&#8217;ll use it for, how soon, how much feedback you&#8217;ll give me, etc! Sorry :( )<\/p>\n<p><!--more--><\/p>\n<h2>Limitations, hard-coded<\/h2>\n<h3>There can be only One!<\/h3>\n<p>Unity has hardcoded there can only be one Scene object. It&#8217;s unbelievably hard to change such decisions (which is why it&#8217;s famously &#8220;terrible OOP design&#8221;, and we tell new programmers: NEVER do this).<\/p>\n<p>So we&#8217;ll have to do everything with Virtual Scenes: things that are like Scenes, but all exist simultaneously inside the same Unity Scene.<\/p>\n<h3>The universe is &#8230; not &#8230; infinite<\/h3>\n<p>Everything in Unity is required to have a position in 3D space. There are two positions: a &#8220;world position&#8221; (labelled &#8220;position&#8221; in code) and &#8220;local-area position&#8221; (labelled &#8220;localPosition&#8221;). In the GUI, Unity displays only the local-area position, and with parenting of objects this is frequently small numbers &#8211; 0.0, or 5.0, or 1.234324, etc.<\/p>\n<p>However, every object also has a global position (calculated by adding the localPosition to the parent-object&#8217;s position), and this position is affected by the maximum size you can hold in a &#8220;float&#8221; &#8211; and more: by the precision.<\/p>\n<p>So, if you place objects at (0,0, 10000000000000) &#8230; you&#8217;ll hit some very odd problems. For examples of what might happen, google MineCraft&#8217;s FarLands (where you&#8217;ve travelled so far from the 3D origin of (0,0,0) that floating-point numbers stop working properly):<\/p>\n<blockquote><p>\n&#8220;As the player journeys even deeper into the Far Lands, the effects worsen to the point where the game is unplayable. At X\/Z \u00b132,000,000,[5] block physics stop functioning correctly. Lighting doesn&#8217;t work and the blocks, although they appear to be there, aren&#8217;t solid. If the player tries to walk on these blocks, he or she will fall into the Void. At excessive X\/Z positions, world renderer no longer works, or takes incredibly long times and uses most, if not all CPU usage. It then becomes almost impossible to close Minecraft without a task manager.&#8221; &#8211; <a href=\"http:\/\/minecraft.gamepedia.com\/Far_Lands\">Minecraft Gamepedia wiki<\/a><\/p><\/blockquote>\n<h2>Unity features we can leverage<\/h2>\n<h3>Unity layers keep stuff apart<\/h3>\n<p>Unity&#8217; layers system lets you mark things as &#8220;independent, overlapping universes&#8221;. Many missing-features and problems in Unity are officially worked-around using Layers. That&#8217;s good.<\/p>\n<p>Unfortunately, someone hardcoded a tiny limit to the number of layers. As a bonus, the GUI for visualising, using, and debugging them is poor compared to most of core Unity.<\/p>\n<p>So, layers work, but &#8230; a big game may not have any layers to spare. So we need to use them carefully.<\/p>\n<h3>Things in the same scene don&#8217;t necessarily overlap<\/h3>\n<p>In a game-engine, very little you see on screen is actually infinite: there&#8217;s no point. So, in theory:<\/p>\n<ul>\n<li>a camera can see infinitely far into the distance\n<li>light travels infinitely far, unless blocked by objects\n<li>physics calculations affect everything infinitely far into distance\n<li>&#8230;etc\n<\/ul>\n<p>In practice, your camera has a &#8220;far plane&#8221; (coincidentally: for almost the same reasons to do with floating-point precision as we have with position) &#8230; light usually ignore things that are &#8220;too far away&#8221; (performance optimization) &#8230; physics pre-groups things into &#8220;near enough to worry about&#8221; and &#8220;too far away to care, for now&#8221; (massive performance optimization!).<\/p>\n<h2>Use cases for Virtual Scenes<\/h2>\n<p>Here are some I&#8217;ve run into, and felt myself wanting help with:<\/p>\n<ol>\n<li>Using the New GUI \/ uGUI from Unity 5 into a game, without have stupid giant GUI 10-miles-high messing up your Editor view of the level\n<ol>\n<li>Unity corp should have made uGUI have its own window for editing; they didn&#8217;t, so can we solve this?<\/ol>\n<li>If a scene has multiple areas &#8211; e.g. a world-map at small scale, and individual towns \/ houses \/ dungeons at big scale &#8211; you need it all in the same scene, but physically displaced\n<li>When you are embedding a preview-camera into your HUD (or GUI, or even in-scene), Unity&#8217;s implementation of RenderTexture etc requires you to construct a mini-scene somewhere inside the same Scene, but gives you no help or support to do this.\n<ul>\n<ol>\n<li>We want to be able to make a bunch of these &#8220;dioramas&#8221; and switch between them\n<li>&#8230;both while editing, and while trying-out different level-designs\n<\/ol>\n<\/ol>\n<p>Recurring themes:<\/p>\n<ul>\n<li>A lot of the cases involve embedding a camera-pointing-at-some-other-scene into your main Scene\n<li>It has to work both in-Editor and in-Game\n<li>In Unity, changing &#8220;Scene&#8221; is very very slow and wipes all data in the Editor; this breaks a huge amount of code and makes you write very long, painful, crappy code to workaround it. We want to avoid that and have multiple scenes that live in one Scene\n<\/ul>\n<h2>Implementation \/ Solution<\/h2>\n<p>I&#8217;ve built a system of &#8220;Virtual Scenes&#8221;. The basic idea is that each Virtual Scene is a parent object floating somewhere very far away in the distance. Easy!<\/p>\n<p>Most of the work so far went into adding custom GUI and window that lets you easily add, navigate, right-click move objects to\/from virtual-scenes, etc. e.g. &#8230;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/t-machine.org\/wp-content\/uploads\/Screen-Shot-2015-07-26-at-15.56.37.png\" alt=\"Screen Shot 2015-07-26 at 15.56.37\" width=\"857\" height=\"607\" class=\"aligncenter size-full wp-image-3693\" srcset=\"https:\/\/t-machine.org\/wp-content\/uploads\/Screen-Shot-2015-07-26-at-15.56.37.png 857w, https:\/\/t-machine.org\/wp-content\/uploads\/Screen-Shot-2015-07-26-at-15.56.37-150x106.png 150w, https:\/\/t-machine.org\/wp-content\/uploads\/Screen-Shot-2015-07-26-at-15.56.37-300x212.png 300w, https:\/\/t-machine.org\/wp-content\/uploads\/Screen-Shot-2015-07-26-at-15.56.37-624x442.png 624w\" sizes=\"(max-width: 857px) 100vw, 857px\" \/><\/p>\n<p>Features working at the moment:<\/p>\n<ol>\n<li>Each Virtual Scene has its own name (can rename whatever you like)\n<li>Fully integrated with Unity Editor &#8211; selecting a VirtualScene in Hierarchy selects it in the Scenes Inspector, and vice-versa\n<li>You can right-click any GameObject \/ selected GameObjects and &#8220;move them to a Virtual Scene&#8221;\n<li>When moving-to-scene, if things have a parent, they got to the same localPosition in the Virtual Scene; this makes it very easy to organize things in different Virtual Scenes and move them to\/from your main Scene\n<li>A SceneManager lets you easily find, view, and jump the Editor &#8220;Scene View&#8221; to each one\n<li>It also manages and (re-)positions them when needed (if you decide to delete some, add some, change how far &#8220;far away&#8221; is because you made your main Terrain bigger, etc)\n<li>They optionally have a &#8220;Scene Camera&#8221; (which is a local version of MainCamera); when you jump to a scene, it takes you to &#8220;looking hrough the Scene Camera&#8221; since that&#8217;s normally what the sub-scene is being used for\n<li>Setup is all automatic: if you open the Virtual Scene Manager, it automatically configures itself and adds a Virtual Scenes system to the current Scene &#8212; (this was hard to make work: I found some new bugs in Unity&#8217;s EditorWindow implementation. Sigh)\n<li>Everything is serialized correctly within the Unity Scene, using Unity Serialization &#8212; (this was hard to get right, since Unity doesn&#8217;t let you debug their crappy Serializer &#8230; at all :()\n<\/ol>\n<p>Features I&#8217;d like to add:<\/p>\n<ul>\n<li>NOT IMPLEMENTED YET: automatic managing of Unity layers, so each Virtual Scene exists in a layer of its own. NB: the Layers in Unity suck. You&#8217;ll be limited to a small number of virtual scenes, etc.\n<li>NOT IMPLEMENTED YET: exporting \/ importing Virtual Scenes (SPECIFICALLY: I&#8217;d like to be able to transfer them between Unity Scenes. Unity&#8217;s &#8220;export package&#8221; feature sucks DONKEY, and still has the same major bugs it&#8217;s had for years; I can probably do much, much better)\n<li>NOT IMPLEMENTED YET: &#8220;SceneCam&#8221; preview system: show thumbnails of all the scenes, rather than just customised names. This is useful if you have many scenes, but more just for fun and because I think it&#8217;ll look cool :).\n<\/ul>\n<h2>Want an email when this goes live in the Asset Store?<\/h2>\n<div style=\"background: #beb; border: thin gray solid; margin: 5px auto; text-align: center; width: 350px; \">\n[smlsubform emailtxt=&#8221;&#8221; showname=&#8221; mailinglist=&#8221;buy-virtualscenes&#8221;]\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Unity3D has a great core architecture &#8211; it&#8217;s easy to understand and use. However, it has some significant flaws. One of the recurring problems I run into is that Unity requirs you to have precisely one &#8220;Scene&#8221; at any one time, but often you need to have multiple scenes at the same time. Problem? Yes, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,20,67],"tags":[],"_links":{"self":[{"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/posts\/3692"}],"collection":[{"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/comments?post=3692"}],"version-history":[{"count":15,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/posts\/3692\/revisions"}],"predecessor-version":[{"id":3710,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/posts\/3692\/revisions\/3710"}],"wp:attachment":[{"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/media?parent=3692"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/categories?post=3692"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/tags?post=3692"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}