Skip to content

Instantly share code, notes, and snippets.

View unitycoder's full-sized avatar
✅
‏‏‎

mika unitycoder

✅
‏‏‎
View GitHub Profile
@unitycoder
unitycoder / if-branchless.shader
Last active July 10, 2026 10:02
Avoiding Branching / Conditionals in Shaders Fast No If
"The common wisdom of "don't use conditionals in shaders" is one of my biggest frustrations with how shaders are taught.
step(y, x) _is_ a conditional! It compiles to identical code as:
float val = (x >= y ? 1.0 : 0.0)
or
float val = 0.0;
if (x >= y) val = 1.0;"
https://twitter.com/bgolus/status/1235254923819802626
// Performing shader divisions without diving *rcp = approximation of 1/x
float myDividedVal = myValToDivide * rcp(myDivider);
@unitycoder
unitycoder / Sprites_Clip-NoTransparency.shader
Last active January 20, 2019 12:46
Shader Sprites/Clip-NoTransparency
// removed ColorTint, Transparency, Added clip() based on alpha
Shader "Sprites/Clip-NoTransparency"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
_Cutoff ("AlphaCutoff", Range(0,1)) = 1
}
@unitycoder
unitycoder / Sprites_Clip.shader
Last active January 20, 2019 12:46
Shader Sprites/Clip
// removed ColorTint, Added clip() based on alpha
Shader "Sprites/Clip"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
_Cutoff ("AlphaCutoff", Range(0,1)) = 1
}
@unitycoder
unitycoder / shader_values.txt
Created August 20, 2016 10:17
Shader Float Half Fixed Precision
High precision: float
Highest precision floating point value; generally 32 bits (just like float from regular programming languages).
Full float precision is generally used for world space positions, texture coordinates, or scalar computations involving complex functions such as trigonometry or power/exponentiation.
Medium precision: half
Medium precision floating point value; generally 16 bits (range of –60000 to +60000, with about 3 decimal digits of precision).
Half precision is useful for short vectors, directions, object space positions, high dynamic range colors.
@unitycoder
unitycoder / PlayerHoverController.cs
Created August 9, 2016 12:16
Player Hover Controller (use with SimpleSmoothMouseLook.cs)
using UnityEngine;
using System.Collections;
public class PlayerHoverController : MonoBehaviour
{
public float forwardSpeed = 10;
public float strafeSpeed = 10;
public float runMultiplier = 2;
public KeyCode runKey = KeyCode.LeftShift;
public LayerMask groundLayer;
@unitycoder
unitycoder / UnityShaderSnippets.shader
Created August 9, 2016 06:32
Unity Shader Snippets
var c = lerp(_Color1, _Color2, abs(fmod(_Time.w * _Speed, 2.0) - 1.0)); // ping pong
@unitycoder
unitycoder / CollectItems.cs
Last active December 1, 2021 22:56
Simple Item Collector Test + Release with Button
using UnityEngine;
using System.Collections;
// attach this script to your player gameobject
// player needs RigidBody2D and some 2D collider
// your collectible objects need: 2D Collider with [x] Trigger and gameobject tag: "Item"
public class CollectItems : MonoBehaviour
{
public float speed = 10;
@unitycoder
unitycoder / clockfix.reg
Created July 21, 2016 13:21
Enable old windows clock for win10
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\ImmersiveShell]
"UseWin32TrayClockExperience"=dword:00000001
; reference: http://www.thewindowsclub.com/enable-old-windows-clock-calendar-windows-10
@unitycoder
unitycoder / wrap.cs
Last active January 27, 2019 17:32
Modulo (Remainder) Wrap Positive/Negative value
// http://stackoverflow.com/a/23214321/5452781
// http://answers.unity.com/comments/271621/view.html
int wrap(int k, int n) { return ((k %= n) < 0) ? k + n : k; }
// example
myval = wrap(myval-1,maxval);
myval = wrap(myval+1,maxval);
@unitycoder
unitycoder / ReadBarcodeFromFile.cs
Last active August 17, 2021 07:25
Read QR Barcode From Texture File with ZXing
using UnityEngine;
using System.Collections;
using ZXing;
public class ReadBarcodeFromFile : MonoBehaviour
{
public Texture2D inputTexture; // Note: [x] Read/Write must be enabled from texture import settings
void Start()
{