Black Hole Demo

I made a demo with my kid, and it somehow managed to get 1st place at a (small) demoparty!

The story of this demo started around year 2006 (!). I had just started working at Unity in Copenhagen, and was renting a tiny room (it was like 12 square meters; back then that’s all I could afford). The guy I was renting from was into writing and music. One of his stories was narrated into a music track, and that’s what I wanted to make a demo from… so we agreed that I would make a demo, using his story & music for it.

His band was called “Aheadahead”, back then they (of course) had a MySpace page. Now that is gone, but turns out they do have an album on Spotify! It’s called “High Status and Continued Success in the Ongoing Battle for Power and Prestige”, go check it out.

And… I never made the demo with that soundtrack.

Until sometime in 2018, during one week of vacation, I sat down with my kid and we decided to try to make it. A bunch of sketching, planning and experimenting with fractals followed.

And then the vacation week was over, and Regular Life happened again. I only came back to fiddle a bit with the demo sometime in 2019.

And then… (hmm is there a pattern here?) that got parked again. Until now, mid-2020, I had another vacation week with nothing much to do. So here it is! Wrapped it up, submitted to Field-FX demoparty, and somehow managed to get 1st place \o/

Credits

Tech Details

It’s a Unity 2018.2 project with nothing fancy really. A timeline, and mostly raymarched fractals that render into the g-buffer. Some utilities I’ve used from the most excellent Keijiro’s github, and the fractals are based on “Kali Sets”, particularly “kalizyl and kalizul” shadertoy by Stefan Berke.


Various details about Handles

I wanted to fix one thing about Unity editor Handles, and accidentally ended up fixing some more. So here’s more random things about Handles than you can handle!

What I wanted to fix

For several years now, I wanted built-in tool (Move/Scale/Rotate) handle lines to be thicker than one pixel. One pixel might have been fine when displays were 96 DPI, but these days a pixel is a tiny little non-square.

Often with some real colored content behind the tool handles, my old eyes can’t quite see them. Look (click for full size):

That’s way too thin for my taste. So I looked at how other similar programs (Maya, Blender, 3dsmax, Modo, Unreal) do it, and all of them except 3dsmax have thicker tool handles. Maya in particular has extremely customizable handles where you can configure everything to your taste – perhaps too much even :)

Recently at work I got an “ok aras do it!” approval for making the handles thicker, and so I went:

That by itself is not remarkable at all (well, except that I spent way too much time figuring out how to do something, that is placed in world space, to be constant size in pixel space, LOL). But while playing around with handle thickness, I noticed a handful other little things that were bugging me, I just never clearly thought about them.

Here they are in no particular order, in no other reason than an excuse to post some pictures & gifs!

Hover indicator with yellow color is not great

When mouse is hovering over or near enough the handle axis, it changes color to yellow-ish, to indicate that when you click, the handle will get picked up. The problem is, “slight yellow” is too similar to say the green (Y axis) color, or to the gray “rotate facing screen” outline. In the two images below, one of them has the outer rotation circle highlighted since I’m hovering over it. It’s not easy to tell that the highlight is actually happening.

What I tried doing instead is: upon hovering, the existing handle color turns a bit brighter, more opaque and the handle gets a bit thicker.

Move/Scale tool cap hover detection is weird

In some cases, the mouse being directly over one cap still picks another (close, but further from the mouse) axis. Here, the mouse is directly over the red axis cap, yet the blue one is “picked”. That seems to be fine with axes, but the “cap” part feels wonky.

I dug into the code, and the cause turned out to be that cone & cube caps use very approximate “distance to sphere” mouse hover calculation. E.g. for the Move tool, these spheres are what the arrow caps “look like” for the mouse picking. Which does not quite match the actual cone shape :) For a Scale tool, the virtual spheres are even way larger than cubes. A similar issue was with the scene view axis widget, where axis mouse hit detection was using spheres of this size for picking :facepalm:

Now, I get that checking distance to sphere is much easier, particularly when it has to be done in screen space, but come on. A sphere is not a great approximation for a cone :) Fixed this by writing “distance to cone” and “distance to cube” (in screen space) functions. Underneath both are “distance to a 2D convex hull of these points projected into screen space”. Yay my first convex hull code, I don’t remember ever writing that!

At almost parallel viewing directions, axis is wrongly picked

Here, moving the mouse over the plane widget and try to move, poof the Z axis got picked instead (see this tweet too).

What I did: 1) when the axis is almost faded out, never pick it. The code was trying to do that, but only when the axis is almost entirely invisible. 2) for a partially faded out axis, make the hover indicator not be faded out, so you can clearly see it being highlighted.

Cap draw ordering issues (Z always on top of X etc.)

Handle axes were always rendered in X, Y, Z order. Which makes the Z always look “in front” of X even when it’s actually behind:

Fixing this one is easy, just sort the axis handles before processing them.

Free rotate circle is barely visible

The inner circle of rotation gizmo that is for “free” (arcball) rotation is barely visible:

Make it more visible. A tiny bit more visible when not hovering (left), thicker + more visible when hovering (right):

Move tool plane handles are on the opposite side in Ortho projection

With an orthographic scene view camera, the “move on plane” handles are on the opposite side of the gizmo:

Turns out that was just a sign bug.

And that’s it for this round of Handles tweaks! There’s a ton more that could be done (see replies to Will’s tweet), but that’s for some other day. Nothing from above has shipped or landed to mainline code branch yet, by the way. So no promises :) Update: should ship in Unity 2020.2 alpha 9 soon!


Fourteen years at Unity o_O

Looks like I’ve been working at Unity for 14 years. What?!?! So here’s another blog post that looks at the past without presenting any useful information, similar to the ones from two, four, ten, eleven years.

A year ago I wrote how I started mentoring several juniors at work, and then how I’ve spent two years on the build system team.

What happened next is that I somehow managed to convince others (or someone else has convinced me – it’s a blur) that I should stop being on the build system team, “steal” the juniors I was mentoring and create a whole new team. And so one thing led to another, and I ended up leading/managing a whole new 8-person team, with most of us being in Unity Kaunas office. Due to lack of imagination, this was simply called a “Core Kaunas” team.

We spent most of 2019 focusing on improving version control (mostly Perforce) integration in Unity – fixing bugs, improving integration UI/UX, fixing lots of cases of versioned files being written to disk for no good reason (which under Perforce causes a checkout), and so on. See release notes items starting with “Version Control” in 2019.3 release notes for an example. Most of that work ships in 2019.3, some in 2020.1, some was backported all the way back to 2018.4 LTS. Most of what we did was either reported bugs / feature requests by users, or things coming from our own internal production(s), that for the first time ever used Perforce (on purpose! so that we could see the issues with our own eyes).

But also we managed to do some “random other” work, here’s a summary of what we casually did on the side in 2019 Q3 and Q4 respectively:

For a team where 5 out of 8 people have only about a year of “professional programming/QA” experience, and where this “side work” is not even our main focus area, I think that’s pretty decent! Happy there.

Starting this year, my team will be transitioning towards “various quality-of-life improvements” work, mostly in the editor based on artist/production feedback. Not “large features”, but various “low hanging fruit” that is relatively easy to do, but for whatever reason no one did yet. Some of that because teams are busy doing more important stuffs, some because work lands in-between teams with unclear ownership, and so on. “Editor Quality of Life” in Q4 random work image above is basically what we’re after. Version Control integration and improvements we’ll hand over to another team. Let’s see how that goes.

On a more personal side of work, I keep on doing short summaries of every week, and then at end of year I write up a “wot is it that aras did” doc. Partially because every one of my bosses is in some other office and I rarely get to see them, and partially so that I can argue I’m worth my salary :), or whatever.

Happy to report that I managed to delete 747 thousand lines of code last year! That is a bit cheating though, since half a million of that was versioned Quicktime headers, and turns out they are huge. Most of other deletions were things like “remove the old C#<->C++ bindings system”, which is no longer used. Anyway, I like deleting code, and this year was good.

Looking forward to what “my” team will be able to pull off in 2020, and also how juniors on the team will grow. Let’s make and ship more of these improvements, optimizations and “someone should totally have done this by now” type of things. \o/


It's Raining Cubes

So a dozen years ago I wrote “hey, 4 kilobyte intros are starting to get interesting”. Fast forward to 2019, and we made an attempt to make a 4KB demo with my team at work. None of us have any previous size-limited demo experience? ✅ We have no idea what the demo would be about? ✅ Does it have a high chance of being totally “not good”? ✅ So we did the only thing that made sense in this situation – try to do it!

We did not follow the modern trend of making 4KB demos that are purely “one giant shader that does raymarching”, and instead did most of the code on the CPU in C++. Physics simulation? Sure why not. Deferred rendering? Of course. Just write it in regular programming style, without paying that much attention to size coding tricks (see in4k or sizecoding)? Naturally.

Maybe that’s why this did not fit into 4 kilobytes :) and ended up being 6.6KB in size.

Executable: ItsRainingCubes.exe (6.6KB)
Source: Zip with VS2019 projects
Pouët: Link

Tech details:

  • Verlet style physics simulation. Simulates points and springs between them; also approximates each cube with a sphere :) and pushes points outside of them.
  • Deferred rendering (world’s most pointless deferred usage?) with colors, normals and the Z-buffer. There’s one shadowmap for the light source. The whole G-buffer is blurred (including depth and normals too!) with an Masaki Kawase style iterative filter and then the lighting is computed. That’s what produces the bloom-like outlines, soft edges on cubes and weird shadow shapes. It should not have worked at all.
  • Music is made in Renoise, using 4klang for playback.
  • Executable compressed using Crinkler, and shaders minified using Shader Minifier.
  • Visual Studio 2019, C++, OpenGL (compatibility profile with some GL3/GL4 extensions) was used.

Credits: Ascentress, shana, TomasK, NeARAZ.

Youtube video capture:

While it’s not impressive by any standards, I kinda expected us to achieve even less. Again, no previous experience in this area whatsoever! Four (well ok, almost seven…) kilobytes is not much, but with tools like Crinkler (great executable size reporting there, by the way - here’s an example) it’s manageable. There’s some wrestling with MSVC if you want to ignore all the default libraries, like you have to make your own implementations of _fltused, _ftol2(), _ftol2_sse(), memset(); load functions like cos() manually from the old msvcrt.dll, and so on. Funtimes. But once the basic setup is done, then it’s “just programming” really.

That’s it! Go make some demos.


Clang Build Analyzer

So! A while ago I worked on adding -ftime-trace support for Clang. That landed and shipped in Clang 9.0 in September of 2019, so \(^O^)/ Looks like it will also be coming to Sony development tools soon (see SN Systems blog post).

All that is good, but it works on one compiled file at a time. If you know which source files are problematic in your whole build, then great. But what if you don’t, and just want to see things like “which headers are most expensive to include across whole codebase”?

I built a little tool just for that: Clang Build Analyzer. Here: github.com/aras-p/ClangBuildAnalyzer

Basically it grabs *.json files produced by -ftime-trace from your whole build, smashes them together and does some analysis across all of them. For headers being included, templates being instantiated, functions code-generated, etc. And then prints the slowest things, like this:

Analyzing build trace from 'artifacts/FullCapture.json'...
**** Time summary:
Compilation (1761 times):
  Parsing (frontend):         5167.4 s
  Codegen & opts (backend):   7576.5 s

**** Files that took longest to parse (compiler frontend):
 19524 ms: artifacts/Modules_TLS_0.o
 18046 ms: artifacts/Editor_Src_4.o
 17026 ms: artifacts/Modules_Audio_Public_1.o
 
**** Files that took longest to codegen (compiler backend):
145761 ms: artifacts/Modules_TLS_0.o
123048 ms: artifacts/Runtime_Core_Containers_1.o
 56975 ms: artifacts/Runtime_Testing_3.o

**** Templates that took longest to instantiate:
 19006 ms: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::... (2665 times, avg 7 ms)
 12821 ms: std::__1::map<core::basic_string<char, core::StringStorageDefault<ch... (250 times, avg 51 ms)
  9142 ms: std::__1::map<core::basic_string<char, core::StringStorageDefault<ch... (432 times, avg 21 ms)

**** Functions that took longest to compile:
  8710 ms: yyparse(glslang::TParseContext*) (External/ShaderCompilers/glslang/glslang/MachineIndependent/glslang_tab.cpp)
  4580 ms: LZ4HC_compress_generic_dictCtx (External/Compression/lz4/lz4hc_quarantined.c)
  4011 ms: sqlite3VdbeExec (External/sqlite/sqlite3.c)

*** Expensive headers:
136567 ms: /MacOSX10.14.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h (included 92 times, avg 1484 ms), included via:
  CocoaObjectImages.o AppKit.h  (2033 ms)
  OSXNativeWebViewWindowHelper.o OSXNativeWebViewWindowHelper.h AppKit.h  (2007 ms)
  RenderSurfaceMetal.o RenderSurfaceMetal.h MetalSupport.h Metal.h MTLTypes.h  (2003 ms)

112344 ms: Runtime/BaseClasses/BaseObject.h (included 729 times, avg 154 ms), included via:
  PairTests.cpp TestFixtures.h  (337 ms)
  Stacktrace.cpp MonoManager.h GameManager.h EditorExtension.h  (312 ms)
  PlayerPrefs.o PlayerSettings.h GameManager.h EditorExtension.h  (301 ms)

103856 ms: Runtime/Threads/ReadWriteLock.h (included 478 times, avg 217 ms), included via:
  DownloadHandlerAssetBundle.cpp AssetBundleManager.h  (486 ms)
  LocalizationDatabase.cpp LocalizationDatabase.h LocalizationAsset.h StringTable.h  (439 ms)
  Runtime_BaseClasses_1.o MonoUtility.h ScriptingProfiler.h  (418 ms)

The actual output even has colors, imagine that!

Aaanyway. Maybe that will be useful for someone. Issue reports and pull requests welcome, and here’s the github repo again: github.com/aras-p/ClangBuildAnalyzer

That’s it!