Archive

Optimizing shader performance with texture packing

Learn how to use GIMP to pack multiple grey scale texture maps into a single image file.
Published Monday, 26 October 2020

When working with shader, there is a limit on how many texture samplers a shader can use. Every sampler is also quite expensive so there is a value in using a few as possible.

Example of a bad shader

Below is a simple Shader example that will use three different grey scale maps to create a (rather ugly) shield effect.

This is the ShaderLab code making the effect.

Shader "MageQuest/MagicForwardShield" {    Properties {       _PatternTex ("Pattern", 2D) = "white" {}       _NoiseTex("Noise", 2D) = "white" {}       _AlphaGuideTex("Alpha", 2D) = "white" {}       _Scroll("Scroll", Range(0, 10)) = 1       _BaseColor("Color (Base)", Color) = (1, 1, 1, 1)       _PatternColor("Color (Pattern)", Color) = (1, 1, 1, 1)    }    SubShader{       Tags

Create a spline tool: Part 2

Continuation on how the build a spline tool for Unity. Goes over on-the-fly generation of a mesh.
Published Sunday, 25 October 2020

The last post covered how a spline tool can be created and edited in Unity's scene view. This post covers how we can use that data and create a mesh around it to get a cable.

Full source for this and the previous post can be found at https://github.com/bonahona/CableSpline

Quick recap

We have a CableSpline.cs file containing the monobehaviour that represents and spline (and soon are cable as well) and Editor/CableSplineEditor.cs containing the editor.

We left of with an empty UpdateMesh() in the CableSpline class method. The goal is now to implement that method.

Build the mesh generation

Out cable will need some new Unity components to work. Add these two attribute above the declaration of the CableSpline class.

[RequireComponent(typeof(MeshFilter))] [RequireComponent(typeof(MeshRenderer))] public class ConnectionSystem : MonoBehaviour 

The next time a CableSpline is created it will automatically add these two components to the


Create a spline tool: Part 1

Learn how to use cubic Bezier curve the create a spline based on control points.
Published Saturday, 03 October 2020

This post is the first part of two on how to create a spline based cable tool, based of Cubic Beezier curves, editable in the scene. In this post we will cover how to create the base scripts and an editor for it. The next post will cover how we can generate a cable mesh to surround the spline.

Full source can be found at https://github.com/bonahona/CableSpline

Create the Cable Spline

Create a new file named CableSpline.cs in your project, and empty out anything apart from the class declaration.

using System.Collections.Generic; using System.Linq; using UnityEngine; public class CableSpline : MonoBehaviour { }

The spline will consists of a series of control points along which the cable will run. Each of these points will need a relative position and a direction they are facing. Add this class as a nested class inside the CableSpline class.

[System.Serializable] public class SplineControlPoint { public Vector3 Position; public Quaternion Direction

Better tools visualization: adding editor handles

Improve the usability of Unity Editors by adding Handles. Real examples from Bonafide Prefab Painter Unity Addon.
Published Wednesday, 30 September 2020

I'm trying something different for this post. BonaFide Prefab Painter is a Unity addon created by me, available for purchase in the Asset Store. I'm working on improving its usability by adding something Unity calls Handles and the plan is to go over how that is done at the same time.

Changes mentioned will be available in the (at time of writing) next version of the prefab painter, v2.5.4.

If you're not familiar with Bonafide prefab painter (doubt anyone is), it's a prefab painter tool I made for Unity for my current game project to quickly place hundreds of prefabs in a scene with a certain degree of control and help me build levels. The game is question is a top-top co-op action game, similiar to the Diablo series, so that's the kind of maps the Editor is created with in mind.

Quick example of what it can do:

The tool supports a few different brush types that will place prefabs in different ways, for example a circle, a square and a line.

The

Unity editor: More tricks to protect you from yourself

Examples on how to create PropertyAttributes and PropertyDrawers to make better Unity Editors.
Published Monday, 28 September 2020

In the last post I went over a few simple and ready-available C# Attributes to make better inspectors. This article will cover how you can extend the Unity Editor and make a few attributes yourself.

Create a C# attribute from scratch

Attributes is a language construct in C# that's used to add metadata to classes, methods, and variables. They all inherits from the abstract base class Attribute. When working with Unity properties, they have their own abstract base class PropertyAttribute that you should inherit from.

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/attributes/

https://docs.unity3d.com/ScriptReference/PropertyAttribute.html

In the last post I showed how [TooltipAttribute] can be used in your projects. That attribute is very simple to recreate and will serve as an example for how to create Unity property attributes with custom property drawers.

Somewhere in your Unity project, create a new


Unity editor: Tricks to protect you from yourself

Collection of tips on how to make Unity editors more useful and less error prone.
Published Tuesday, 01 September 2020

When working a lot in Unity it's easy to get crashes and errors because you forget to set the correct data in components, or having forget to add a component type altogether.

This is common and there is no silver bullet to solve it bu there are however steps you can take while writing your code to decrease the chance of making mistakes. Many of these tricks are built-in in Unity's editor from start and very simple to get started with.

Adding tool tips

Naming variables is hard. They have to convey a lot of meaning while still being short. In programming, comments are there to help you when you need more information about a variable. Unfortunately they wont be visible in the inspector.

This is where the[ToolTip("")] attribute comes in handy. Add this to any public variables in behaviors and they will be displayed when you mouse-over the field in the inspector. Write down what the variables does and any special considerations needed when setting it


Shader effect: Soft "alpha clipping"

Simple way to fix some less-than-perfect textures for visual effects using built-in shader functions for Unity3D.
Published Sunday, 03 May 2020
Problem

Short solution to a simple problem I came across. I downloaded a ready made effect from the Asset store and found a gray scale texture with no easing before the edge. I'm not sure how to describe it so a picture might be the best example.

As can be seen here, when rendered with transparency the edges of this texture are very clearly visible against the black background because the texture is drawn all the way to the edges. If used in a particle system it will look very obvious.

The best solution

If you are creating the textures yourself, the solution to this problem is to not draw all the way to the edges if the images. But that's of course not what this blog entry is about.

Fixing it in the shader

The goal is to find a way to solve the issue of softening the edges of the image in the shader instead. As mentioned above, it would be simpler and more efficient, compute power wise, to just go back and fix the source image but that is not always possible.

The


Unity: Enable camera Depth Buffer

Want to create a visual effect using the depth buffer but didn't get it to work? This is how to enable it in Unity3D.
Published Thursday, 09 April 2020
What is the depth buffer?

In modern game development, a renderer sometimes uses a depth buffer (also called a Z-buffer) to store the distance from the camera a certain pixel was rendered. This buffer can be very useful to have when creating custom shader effect for Unity but is unfortunately not always enabled. Luckily there are a few ways to remedy this.

Use the deferred rendering pipeline.

If you use Unity's deferred rendering pipeline the depth buffer is enabled by default because deferred rendering uses it.

You can set your game to use deferred rending by changing it from Edit->Project Settings -> Graphics. Every tier will have a Rendering Path setting from where you can select Deferred.

Enable it manually for a camera.

Create a new C# script for your game as follows and attach it you your camera.

Note: The editors camera will have it's Depth Buffer enabled if the game camera does.

using UnityEngine; [ExecuteInEditMode] [RequireComponent(typeof(Camera))] public

Unity Effects: Decal shader

Create a decal effect shader for Unity3D from scratch.
Published Wednesday, 08 April 2020
What is a decal?

Ever played a shooter where bullet holes appear as fire at a wall? Seen blood splattered or footprints across the floor? Then you've most likely seen decals. In game development a decal is a mesh with a texture rendered into a scene that takes no space of its own but is instead projected down onto the underlying geometry. In essence you are rendering stuff onto other stuff.

Example of a decal texture being projected down onto the underlying geometry.

Creating decal shaders in Unity

For some reason Unity has no decal shader available out-of-the-box which I find rather funny because I have based this article upon one written by Unity themselves back 2015 (Extending Unity 5 rendering pipeline: Command Buffers). There are probably plenty of implementation available in the Asset Store as well but they are not particularly hard to create from scratch so why pay for something when you can learn to create it yourself?

This shader relies


Unity Effects: Soft glow

Recreating a soft light glow effect found in World of warcraft in Unity3D using ShaderLabs/Cg.
Published Saturday, 04 April 2020
Inspiration

Full honestly from the start. I've been playing quite a bit of World of Warcraft Classic lately and it has been great opportunity for me to get inspired graphics wise. I was running around in Duskwood, a really dark and moody area of the game when I came across one of their light posts. It had a soft glow surrounding the light source making it look more present. This is an effect I want to recreate for my own game in Unity.

Creating a custom shader

This visual effect requires a custom shader. First I need to determine what results I'm going for.

Is in every sense of the way a billboard. Just a plain quad always facing the camera.Does not interact with lightning in the scene. Any actual light from the prop will be handled with Point Lights.Appear to "light up" meshes underneath it.Be non-static / change is subtle ways.

This boils down to an unlit transparent additive shader, billboarded, scaling it correctly and slowly changin it's size back and forth


Unity editors: Getting started

Get a quick quick overview of Unity's editor API, how to access it and some very early and simple pitfalls you should avoid.
Published Friday, 27 March 2020
Creating your own Unity tool from scratch

This blog post will cover the basics of how to setup your project in order to create some custom editors. What is written here applies to all custom editors wether they be Editors, Editor windows, custom property drawers, custom object previews etc.

Unity3D is can be great game engine to work with and it gets more features added all the time. The tooling for Unity is however quite lacking in many areas. Their solution to this is an extensive editor API allowing for custom tools. If you are looking for something, Unity's Assets store and github.com are both full of tools. But what do you do when you cant find what you are looking for? The answer is easy: build your own.

Accessing Unity's Editor API.

For performance and build size reason Unity has chosen to split their API up in several different parts (called Assemblies). The Editor part of Unity is only used when working in the Unity editor and is therefor not included