Michael Palmos

Personal memory bank, and things I wish I found on google before now.

Read this first

Finding sprite UV/texture coordinates in Unity.

This page is now maintained at http://toqoz.fyi/unity-sprite-texture-coordinates.html

Eyedrop demo.

If you want to do any kind of texture manipulation in games, you’ll need some form of texture coordinates. If you’re in 3D, you can obtain UV coordinates of a particular point on a mesh via raycasting, but there’s no easy way to achieve the same thing for sprite renders.

Luckily, we can calculate these coordinates ourselves.
The approach here is not very complicated, however, there are many edge cases, and the Unity documentation doesn’t exactly make things simple to arrive at a good solution on your own.

Code

Attach this to each sprite:

[RequireComponent(typeof(SpriteRenderer))]
public class CoordinateMap : MonoBehaviour {
    private Sprite sprite;

    private void Start() {
        sprite = GetComponent<SpriteRenderer>().sprite;
    }

    public Vector2 TextureSpaceCoord(Vector3 worldPos)
...

Continue reading →


Everything you need to know about writing mods for Duck Game.

This page is now maintained at http://toqoz.fyi/duck-game-mods.html

So you want to make some mods for Duck Game? Look no further. This guide should tell you almost everything you need to know about making a good mod that actually functions. We’ll talk about getting set up, explain some of Duck Game’s core mod structure, talk about networking, and hopefully most of the other little details you might need.

duck-game-cover.jpg

Is making mods difficult? Can I write mods even if I don’t have much experience?

Mods for Duck Game can definitely be made with only a base understanding of programming. Whether those mods are spaghetti or not might be a different story, but making basic mods in Duck Game is very easy.

Skills that may help significantly: knowledge of basic object oriented programming, basic C knowledge, a bit of intuition in reading other people’s code, and most of all, time. You might have...

Continue reading →


Portfolio

This is a portfolio of things I’ve worked on, and stuff I’m proud about.


This blog

I enjoy writing, and I especially enjoy when it’s useful. These posts may come slow but they’re hopefully noteworthy.


Golemancer – Unity

Golemancer (Google Play) was my final year Capstone project at QUT where I worked as lead programmer. At present, this is probably the biggest cornerstone of my life as a game developer. I learnt so much.

It’s difficult to cover the entire breadth of this project; here’s the salient points;

Minigames

Cutting minigame Points display

The minigames are much of the foundation of Golemancer. Throughout development we changed these games more times than you can count, which naturally required a well-reasoned code base. Considering the rate of iteration, this is actually true for most things in the game, but for the minigames especially.

See this album if you want to see some really early...

Continue reading →


An object database pattern for Unity3D.

This page is now maintained at http://toqoz.fyi/unity-object-database.html

Assigning object references through the Unity inspector is a great tool. Unfortunately though, it tends to really get in the way of doing code-based object instantiation; in particular, there’s no clean Unity-endorsed solution to making simple static classes which utilize game objects.

When I really need to solve this problem, I’ve been using a scriptable object database type solution. I’ll tie this post into my inventory post, where we needed a simple and tangle-free way to associate item names with game objects, because raw jsonified object references would change between builds.

What we want

What we really want here is pretty basic. We just want a way to do something like ItemDatabase.GetActual("ruby"); and get back an asset – be it a prefab, scriptable object, sprite, whatever. A dictionary of sorts...

Continue reading →


Making a painless inventory system with scriptable objects in Unity.

This page is now maintained at http://toqoz.fyi/unity-painless-inventory.html

Something that might surprise you is that there is actually a lack of documentation on making functional inventory systems in Unity.

When I say functional, I mean:

  • Based on Scriptable Objects.
  • Easily saved to and loaded from disk.
  • Resource efficient.
  • Integrates with the Unity UI.

And I think that these points are quite significant, particularly the first, because it ties everything else together. The majority of material I found online 1 2 3 4… really doesn’t explain much more than some tips on getting started, or misses these marks. So my goal here is to try and collate the information and experience I gathered when writing our inventory system.

Why Scriptable Objects?

Don’t be alarmed if you haven’t even heard of scriptable objects in Unity until now. These things are severely underrepresented...

Continue reading →


A thread safe ISAAC-rand.

This page is now maintained at http://toqoz.fyi/thread-safe-isaac-rand.html

A short while ago I was required to multithread a program that used the ISAAC-rand library for some random numbers. Unfortunatley, ISAAC-rand is not thread safe out of the box, but thread safety can be achieved with a few small changes to the source.

ISAAC-rand isn’t thread safe because it uses a global context to keep track of the random state, meaninnng that when the context is seeded in a threaded environment, its state is likely to be clobbered by another thread – potentially while its value is still in use.

A solution to this may be to place a lock around code using the random number generator, however this is likely to substantially slow down our parallel code due to lock contention. On the project I was working on, the function utilising RNG was the program’s bottleneck, so any lock-based solution...

Continue reading →


Circular accuracy reticle via shader in Unity3D.

In the game Deadbolt, there’s an effect where the crosshair will expand and shrink to show the accuracy of your weapon depending on distance from the player.

reticle accuracy in deadbolt

To recreate this effect in Unity, your first instinct might be to simply re-scale a UI Image depending on some distance value, but when we try this approach (particularly at its extremes) it’s obvious that we should search for a different solution:

reticle accuracy in deadbolt, scaling method

In particular, notice that the actual thickness of the circle changes as we scale it – of course it does. We’re scaling an image, not changing the circle’s radius. Consequently, the edges look aliased when the circle is too small and blurry when the circle is too big. It follows that any approach that scales in this way is deficient.

So then what seems like a quick-and-easy problem actually becomes pretty puzzling.

One could use Unity’s GL API to draw a circle yourself, however...

Continue reading →


Calculating distance per 360 sensitivity in Unity3D.

This page is now maintained at http://toqoz.fyi/unity-distance-per-360-sensitivity.html

I was recently working on a first person project where it was important that I know the player’s exact physical mouse distance in relation to the camera’s view movement. This is commonly referred to by players as the inches/cm per 360 sensitivity, and can be a useful metric in unifying look sensitivity between games. There are entire sites dedicated to making this conversion.

The scripts in this post will be Unity-specific, however the process should be able to be easily applied to any engine.

rotation degrees

The first step is obviously a script that lets the mouse affect the camera. A basic implementation might look like the following:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MouseLook : MonoBehaviour {
    public float sensitivityX = 1f;
...

Continue reading →


Creating a pure utility window with properties in Xlib/X11.

This page is now maintained at http://toqoz.fyi/xlib-x11-utility-window.html

When I first started writing yarn (a notification daemon) I knew that I wanted it to be at least relatively lightweight, particularly in comparison to the beloved dunst. Because dunst didn’t use GTK, and because it seemed more simple at the time, I decided to draw the window with Xlib and Cairo instead.

The main thing this meant was that I had to interface with X11 directly. I wanted a pure X11 utility window – no window manager positioning, no window borders, just compositor shadows.

As an example of what I mean, here:
pure utility window

This window can be placed anywhere by us, and resized independently of the window manager. While this approach might not be the most elegant depending on your sentiment, it’s the only real way to have a good level of customization and control over your window. This type of solution is...

Continue reading →