Skip to content

Instantly share code, notes, and snippets.

@Chaosed0
Chaosed0 / SortMeshTrianglesAssetPostprocessor.cs
Created May 4, 2024 23:42
A Unity asset postprocessor for sorting mesh triangles back-to-front
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
// This sorts the triangles within a mesh back-to-front based on Wizgun's fixed camera
// angle, solving mesh self-sorting issues when transparent shaders are used.
// Normally, these objects are opaque, but when TintedUnlitShaderReveal is swapped in
// to reveal what is behind the object, depth sorting is turned off. We need Unity to
// draw the mesh in the correct order so that the triangles' depths are respected.
@Chaosed0
Chaosed0 / UprightSprite.cginc
Created May 3, 2024 19:02
Unity Shader helper for sorting 2D billboarded sprites against 3D objects
// The functions in this file are used to transform camera-facing billboards into vertical billboards
// such that the way the sprite on the billboard is rendered remains the same.
// This is done to avoid sorting issues with 3D objects.
// This code taken from here: https://forum.unity.com/threads/problem-solving-2d-billboard-sprites-clipping-into-3d-environment.680374/
// Calculates intersection point of ray and plane.
float RayPlaneIntersection(float3 rayDir, float3 rayPos, float3 planeNormal, float3 planePos)
{
float denom = dot(planeNormal, rayDir);
denom = max(denom, 0.000001); // avoid divide by zero
@Chaosed0
Chaosed0 / DelegateEventUnit.cs
Created September 8, 2020 19:03
Bolt unit for running a flow when a delegate is called. Only supports no-argument delegates.
using UnityEngine;
using System.Collections.Generic;
using Bolt;
using Ludiq;
[UnitTitle("Delegate Event")]
[SpecialUnit]
public class DelegateEventUnit : Unit, IEventUnit, IGraphEventListener, IGraphElementWithData
{
public class Data : IGraphElementData
@Chaosed0
Chaosed0 / ModifyColor.cs
Last active April 17, 2019 06:17
Component to pass color to shader through property
using UnityEngine;
using System.Collections.Generic;
[ExecuteInEditMode]
public class ModifyColor : MonoBehaviour
{
private static MaterialPropertyBlock materialPropertyBlock = null;
[SerializeField]
private string propertyName = "_Color";
@Chaosed0
Chaosed0 / Rimlight.shader
Created April 17, 2019 01:54
Simple rim light shader
Shader "Custom/Rimlight" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_RimValue ("Rim value", Range(0, 1)) = 0.5
}
SubShader {
Tags { "RenderType"="Transparent" "Queue"="Transparent" }
CGPROGRAM
#pragma surface surf Lambert alpha
Shader "Projector/Solid Projector" {
Properties{
_Color("Tint Color", Color) = (1,1,1,1)
_ShadowTex("Cookie", 2D) = "gray" {}
}
Subshader
{
Tags {"Queue" = "Transparent" "RenderType" = "Transparent"}
Pass
{
@Chaosed0
Chaosed0 / LowFovCameraUtilityEditor.cs
Created April 8, 2019 01:05
A Unity Editor utility for cameras with extremely low FOV.
using UnityEngine;
using UnityEditor;
using Cinemachine;
using System.Collections.Generic;
// To make this work, create an empty MonoBehaviour called "LowFovCameraUtility".
// Then, attach that script to your Cinemachine virtual cameras.
// If you don't want to use Cinemachine virtual cameras this script should work too,
// you'll just need to modify the components and variable names.
[CustomEditor(typeof(LowFovCameraUtility))]
#include "Callbacks.h"
void CallbackMap::setCallback(const std::string& callbackName, Callback callback)
{
map[callbackName] = callback;
}
CallbackMap::Error CallbackMap::call(const std::string& callbackName, const std::string& args)
{
auto iter = map.find(callbackName);
@Chaosed0
Chaosed0 / UE4WallSconce.cpp
Last active May 16, 2020 04:11
Example AActor-derived class for Unreal Engine 4
/* An example UE4 class derived from AActor. It plays a
* sound when an actor enters the bounding box, and lets
* the player turn a PointLight on and off using the "Use"
* key. It originally existed as a blueprint. */
/* WallSconceCpp.h */
#pragma once
#include "GameFramework/Actor.h"