Friday, November 23, 2018

Perlin Noise Texture Generator

The Perlin Noise texture generator is a small processing program I wrote that could easily and quickly generate Perlin Noise textures for whatever need. While I've never needed to use Perlin Noise textures, having the tool there ready to use would be useful should I ever need it but also is available for others to use as well.

There are three windows:
  • The texture window, which displays the noise texture.
  • The settings window, which displays the current settings for the current/next texture, but also handles input for changing those settings.
  • Finally the controls window, which just displays the keyboard controls of the program.
Codebase: here
Executables: 32 bit and 64 bit windows

Thursday, November 22, 2018

Reference Sorter Program

Why Create A Reference Sorter?

The reason why I created this small program is purely out of laziness, whilst writing my thesis for my masters dissertation I had added various references over time but never put them in alphabetical order when I did. However, in the end I needed them to be in alphabetical order, I could have simply cut and pasted the references into the correct order, but instead I wrote a C++ program to do it instead. It probably would have been just as fast if not faster to cut and paste but having a program that can do it in seconds could also be reused, not that I plan on writing another thesis anytime soon. That being said it can be used by any other student who might need it and therefore still useful.

Links

Repository

Executables: 32 bit and 64 bit

Wednesday, November 14, 2018

Camera Shake

These are notes made while watching this GDC talk.

General Information

  • Use a trauma variable that has a range of (0,1)
  • Factors like damage or stress adds to the trauma variable
  • The trauma variable decreases linearly overtime
  • Set the camera shake amount equal to trauma² or trauma³
  • This means that the camera shake and trauma variable are not linearly proportional
  • This creates a spring and damper effect that feels right for the player
  • It also allows the player to perceive how its escalating

Translational VS Rotational

2D

  • Rotational is okay
  • Translational is great
  • But Translational + Rotational is awesome

3D

  • Rotational is awesome
  • Translational is lame
N.B. Translational in 3D can also cause clipping with the environment.

VR

DON'T DO IT!!!

Implementations

In both implementations a second camera is used to do the camera shake effect, this means that the original camera is preserved. Also all angles used are eular angles.


2D

angle = maxAngle * shake * Random.Range(-1, 1);
offsetX = maxOffset * shake * Random.Range(-1, 1);
offsetY = maxOffset * shake * Random.Range(-1, 1);

shakyCam.angle = cam.angle + angle;
shakyCam.center = cam.center + Vector2(offsetX, offsetY);


3D

yaw = maxYaw * shake * Random.Range(-1, 1);
pitch = maxPitch * shake * Random.Range(-1, 1);
roll = maxRoll * shake * Random.Range(-1, 1);

shakyCam.rotation = cam.rotation + Vector3(pitch, yaw, roll);

If you want translational as well in 3D then include:
offsetX = maxOffset * shake * Random.Range(-1, 1);
offsetY = maxOffset * shake * Random.Range(-1, 1);
offsetZ = maxOffset * shake * Random.Range(-1, 1);

shakyCam.center = cam.center + Vector3(offsetX, offsetY, offsetZ);


3D using Perlin Noise

yaw = maxYaw * shake * GetPerlinNoise(seed, deltaTime, ...);
pitch = maxPitch * shake * GetPerlinNoise(seed + 1, deltaTime, ...);
roll = maxRoll * shake * GetPerlinNoise(seed + 2, deltaTime, ...);

shakyCam.rotation = cam.rotation + Vector3(pitch, yaw, roll);

If you want translational as well in 3D then include:
offsetX = maxOffset * shake * GetPerlinNoise(seed + 3, deltaTime, ...);
offsetY = maxOffset * shake * GetPerlinNoise(seed + 4, deltaTime, ...);
offsetZ = maxOffset * shake * GetPerlinNoise(seed + 5, deltaTime, ...);

shakyCam.center = cam.center + Vector3(offsetX, offsetY, offsetZ);

Random VS Smoothed Noise (Such as Perlin Noise)

  • Smoothed noise feels better
  • Smoothed noise automatically works with pause and slow motion due to use of deltaTime
  • Smoothed noise has adjustable frequency
  • Smoothed noise is more easily reproducible on replays

Monday, November 12, 2018

Rotating Images

Recently I have been working on a small image processing program built using Processing 3. I wanted to add the ability to rotate the image 90° clockwise or anti-clockwise, but I needed to modify the pixels themselves and figure out where they should be after the rotation.


Original Image

Image Rotated 90° Clockwise


To do this I needed to use Vector Math and more specifically rotating a vector. Each pixel has a position in screen space, made of an x co-ordinate and a y co-ordinate. Depending on the program/engine/framework being used the screen space will start (aka the origin (0, 0)) either in the top left corner or bottom left corner (this will be important later).

To rotate a vector in any angle (ϑ) we use the equation:
New X = v.x * cos(ϑ) - v.y * sin(ϑ)
New Y = v.x * sin(ϑ) + v.y * cos(ϑ)

This will then rotate any vector assuming that you are rotating it around the origin (0, 0), however, you'll most likely want to rotate it using a different pivot point. You could move each pixel relative to the origin, rotate each pixel using the above fomula and then translate that back to the actual pivot point but thats a lot of extra calculations. The equation can be modified to do this all in one go.



New X = (v.x - rotate point.x) * cos(ϑ) - (v.y - rotate point.y) * sin(ϑ) +  rotate point.x
New Y = (v.x - rotate point.x) * sin(ϑ) + (v.y - rotate point.y) * cos(ϑ) +  rotate point.y

Now depending on which way your screen origin is located, you might have to invert the angle, so if your screen origin is in the top left, you are fine and can use 90° to rotate something clockwise. However, if your screen origin is in the bottom left, you unfortunately have to invert the angle, so for 90° clockwise, the angle actually used is -90°.

Tuesday, November 6, 2018

Unity Property Attributes

Unity allows developers to use property attributes to modify how a class instance looks within the inspector. Here are some of them and how they work.

[Header("Some Header")]
Header allows you to title a set of variables so if you had a string for the characters name and a string for their description you can place a header attribute above them to signal to anyone that those variables are specifically for a character's description, then use another header attribute for the character's movement stats.

[Tooltip("Some Tip")]
Tooltip allows you to display a tip within the inspector to help explain any variable viewed in the inspector. This is especially useful if the naming of the variable is confusing or could possibly be confused with another variable.

[ContextMenu("Some Menu Name")]
Context Menu allows you to call a function defined within the class from the inspector and can be used to check the functionality of a function without going into play mode in the editor. This attribute is placed above the function that would be called. An example of this would be a TakeDamage() function that in game would be called if the player/character gets hit. Adding a context menu attribute allows that TakeDamage function to be visible in the inspector and allows you to check that the function works as expected due to being able to see the characters health decreasing when called.

[Multiline]
The multiline attribute modifies the text field of a string to be a text area instead, providing more room to write and as the name suggests use multiple lines. This attribute is useful for things such as character/level descriptions.

[Range(min, max)]
The range attribute creates a slider for the variable attached to and requires a min value and a max value. An example use of this would be for a damage per second variable, where you can adjust that value using the slider instead of typing a value in.

[ContextMenuItem("Some Menu Item Name", "Some Function Name")]
Context Menu Item works similar to the context menu attribute but is specific to a variable aka the one below said attribute. This will then call a specified function when clicked on within the inspector. An example of this would be a characters health, which has a context menu item that calls the TakeDamage function from earlier. Another could be added that can then be used to call a GainHealth function, which gives the character more health.

[CreateAssetMenu(fileName = "Some File Name", menuName = "Some Menu Name", order = Some Number]
Create Asset Menu is particularly useful when creating scriptable object classes. This attribute allows you to create a scriptable object file within the project view. This attribute goes above the class definition.
For Example:
[CreateAssetMenu(fileName = "Gun Stats", menuName = "ScriptableObject/Guns/Stats", order = 1]
public class GunStats : ScriptableObject
{
  public float reloadRate;
}

[Space]
The space attribute added a gap between two variables being displayed within the unity inspector.