Skip to content

Instantly share code, notes, and snippets.

@runevision
runevision / BurstMethodTester.cs
Last active May 3, 2024 13:31
Using Unity Burst directly on methods without jobs - unofficial example code
using Unity.Burst;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Jobs;
using UnityEngine;
// This example code demonstrates using Unity Burst directly on a static method without jobs.
// Unity NativeArrays can't be used directly in Unity Burst methods (only in Burst jobs),
// so we have to pass pointers to arrays instead.
@Anthelmed
Anthelmed / WorldSpaceUIDocument.cs
Last active April 29, 2024 09:31
Until Unity decide to make it official, this is a custom WorldSpaceUIDocument component. Code is under the MIT license: https://github.com/git/git-scm.com/blob/main/MIT-LICENSE.txt
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Rendering;
using UnityEngine.UIElements;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem.UI;
#endif
#if UNITY_EDITOR
using UnityEditor;
using System.Net;
using Mono.Debugger.Soft;
using static System.Console;
static int PidToPort(int pid) => 56000 + (pid % 1000);
var pid = 49149; // process ID of the Unity Editor or the IL2CPP Game
var ip = IPAddress.Loopback; // IP where the Unity Editor/Game is running
@marcospgp
marcospgp / SafeTask.cs
Last active January 13, 2024 22:58
Cancel async tasks in Unity upon exiting play mode
using System;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
namespace UnityUtilities
{
/// <summary>
/// A replacement for `Task.Run()` that cancels tasks when entering or
/// exiting play mode in the Unity editor (which doesn't happen by default).
Shader "Hidden/JumpFloodOutline"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "PreviewType" = "Plane" }
Cull Off ZWrite Off ZTest Always
// For the new DOTS animation. Although you need to use DrawMeshInstanced yourself, Hybrid renderer does not support instanced
// props for builtin.
Shader "Custom/VertexSkinning"
{
Properties
{
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
public static class Sample
{
static int gsN = 127; // N(N+1)/2 = 8128 < 2^{13}
static float gsB = 2 * gsN + 1;
static float gsB2 = gsB * gsB;
static float gsFactor = (gsN - 1) * Mathf.Sqrt(0.5f);
static float gsInvFactor = 1.0f / gsFactor;
public static ushort CompressNormal(Vector3 normal)
{
@crearo
crearo / fragment_shader_NV12_to_RGB.frag
Last active December 1, 2023 09:56
A fragment shader to convert NV12 to RGB.
/** A fragment shader to convert NV12 to RGB.
* Input textures Y - is a block of size w*h.
* texture UV is of size w*h/2.
* Remember, both U and V are individually of size w/2*h/2, but they are interleaved.
* The layout looks like this :
* ----------
* | |
* | Y | size = w*h
* | |
* |________|