Skip to content

Instantly share code, notes, and snippets.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// a different way of generating random values that may "feel" better than true randomness
// every possible value will show up once, but in a random order
// it simply draws from the "bag" every time you get NextValue
//
// construct either with a list of values, or a size for the most common use
// list values like: new {1, 1, 2, 2, 3, 4};
@jhocking
jhocking / LitTransparentWithDepth.shader
Last active March 11, 2021 18:09
Lit shader with both transparency and depth sorting
// surface shader version of https://forum.unity.com/threads/gltfutility-a-simple-gltf-plugin.654319/page-4#post-6854009
// how to enable transparency for surface shaders
// https://forum.unity.com/threads/transparency-with-standard-surface-shader.394551/
// https://forum.unity.com/threads/simply-adding-alpha-fade-makes-my-shader-only-work-in-scene-view.546852/
Shader "Custom/LitTransparentWithDepth"
{
Properties
{
@jhocking
jhocking / UnlitTransparentWithDepth.shader
Last active February 19, 2022 15:49
Unlit shader with both transparency and depth sorting
// addresses https://forum.unity.com/threads/gltfutility-a-simple-gltf-plugin.654319/page-3#post-6041123
// relevant documentation:
// https://docs.unity3d.com/Manual/SL-Blend.html
// https://docs.unity3d.com/Manual/SL-CullAndDepth.html
// https://forum.unity.com/threads/function-of-the-clip-command.93179/
// clip() is basically the same as 'discard' https://learnopengl.com/Advanced-OpenGL/Blending
// https://answers.unity.com/questions/1242982/discard-pixels-from-fragment-shader-based-on-scree.html
// if you don't want 'float4 _MainTex_ST;' and TRANSFORM_TEX(v.uv, _MainTex);
@jhocking
jhocking / AddColliderAroundChildren
Created February 10, 2021 15:57
Create a BoxCollider that surrounds an object and its children
// https://gamedev.stackexchange.com/questions/129116/how-to-create-a-boxcollider-which-surrounds-the-gameobject-and-all-of-his-child
private void AddColliderAroundChildren(GameObject assetModel)
{
var pos = assetModel.transform.localPosition;
var rot = assetModel.transform.localRotation;
var scale = assetModel.transform.localScale;
// need to clear out transforms while encapsulating bounds
assetModel.transform.localPosition = Vector3.zero;
assetModel.transform.localRotation = Quaternion.identity;
@jhocking
jhocking / TintedUIBlur.shader
Created February 9, 2021 16:17
Blur the background behind UI
// blur shader from https://stackoverflow.com/questions/29030321/unity3d-blur-the-background-of-a-ui-canvas
// added toggle https://forum.unity.com/threads/shader-properties-no-bool-support.157580/#post-3013337
Shader "Custom/TintedUIBlur" {
Properties {
_Size("Blur", Range(0, 30)) = 3
[HideInInspector] _MainTex("Masking Texture", 2D) = "white" {}
_AdditiveColor("Additive Tint color", Color) = (0, 0, 0, 0)
_MultiplyColor("Multiply Tint color", Color) = (1, 1, 1, 1)
[Toggle(MAKE_DESATURATED)] _MakeDesaturated ("Desaturate", Float) = 0