Skip to content

Instantly share code, notes, and snippets.

View nobnak's full-sized avatar

Nakata Nobuyuki (仲田将之) nobnak

  • teamLab
  • Tokyo, Japan
View GitHub Profile
@nobnak
nobnak / file0.cs
Last active December 3, 2018 09:19
[Unity] AsyncGPUReadback を利用して、非 Color 形式の Texture からデータを取り出す ref: https://qiita.com/nobnak/items/1a98dcb27e4e58e31979
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
public class AsyncCPUTexture<T> where T:struct {
protected bool active = false;
protected AsyncGPUReadbackRequest req;
protected Vector2Int size;
@nobnak
nobnak / file0.cs
Created November 30, 2018 07:18
Unity - "NativeArray<T> where T:struct" の要素を "T[] where T:struct" へ高速にコピーする ref: https://qiita.com/nobnak/items/a55385e22758731f4532
public static class Kern32 {
[DllImport("kernel32.dll", EntryPoint = "CopyMemory", SetLastError = false)]
public static extern void CopyMemory(System.IntPtr dest, System.IntPtr src, uint count);
}
#ifndef NOISE_SIMPLEX_FUNC
#define NOISE_SIMPLEX_FUNC
/*
Description:
Array- and textureless CgFx/HLSL 2D, 3D and 4D simplex noise functions.
a.k.a. simplified and optimized Perlin noise.
The functions have very good performance
and no dependencies on external data.
@nobnak
nobnak / BoxMuller.cs
Last active March 8, 2016 08:28
Gaussian Sampling for Unity
using UnityEngine;
using System.Collections;
namespace Gist {
public static class BoxMuller {
public const float TWO_PI = 2f * Mathf.PI;
public static Vector2 Gaussian() {
var u1 = Random.value;
@nobnak
nobnak / ParallelFor.cs
Last active April 14, 2018 15:03
Parallel.For for Unity
using System.Collections.Generic;
using System.Threading;
namespace Gist {
public static class Parallel {
static AutoResetEvent[] _resets;
public static void For(int fromInclusive, int toExclusive, System.Action<int> body) {
var numThreads = 2 * System.Environment.ProcessorCount;
using UnityEngine;
using System.Collections;
namespace Gist {
/*
* A speed-improved simplex noise algorithm for 2D, 3D and 4D in Java.
*
* Based on example code by Stefan Gustavson ([email protected]).
* Optimisations by Peter Eastman ([email protected]).
* Better rank ordering method by Stefan Gustavson in 2012.
@nobnak
nobnak / Depth.cginc
Created February 27, 2016 05:58
Depth Buffer -> Linear Eye Depth for Unity
#ifndef __DEPTH_CGINC__
#define __DEPTH_CGINC__
float Z2EyeDepth(float z) {
return unity_OrthoParams.w < 0.5
? LinearEyeDepth(z)
: ((_ProjectionParams.z - _ProjectionParams.y) * z + _ProjectionParams.y);
}
#endif
@nobnak
nobnak / AutoHideCursor.cs
Created February 15, 2016 10:22
Auto Hide Cursor for Unity
using UnityEngine;
using System.Collections;
namespace nobnak.Blending {
public class AutoHideCursor : MonoBehaviour {
public float hideAfterSeconds = 3f;
public float thresholdInPixels = 3f;
float _lastTime;
@nobnak
nobnak / UVWorld.cs
Last active August 17, 2023 08:40
UV -> World Position (For Unity)
public class UVWorld : MonoBehaviour {
Mesh _mesh;
Vector3[] _vertices;
int[] _triangles;
Vector2[] _uvs;
Vector3[] _normals;
Triangle2D[] _uvTris;
void Awake() {
_mesh = GetComponent<MeshFilter>().sharedMesh;
@nobnak
nobnak / LensShift.cs
Last active July 26, 2022 07:06
Camera Lens Shift func. for Unity
using UnityEngine;
using System.Collections;
namespace Gist {
public static class LensShift {
public static void Frustum(this Camera cam, out float left, out float right, out float bottom, out float top, out float near, out float far) {
near = cam.nearClipPlane;
far = cam.farClipPlane;