-
-
Save ArieLeo/39051c64d534ff609204 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
using System.Collections; | |
public class CreateTex3D : EditorWindow | |
{ | |
[MenuItem("sugi.cho/Window/CreatePerlin3DTexture")] | |
public static void Init () | |
{ | |
EditorWindow window = EditorWindow.GetWindow (typeof(CreateTex3D)); | |
window.title = "3DNoise"; | |
} | |
int | |
count = 0, | |
size = 256; | |
IEnumerator coroutine; | |
void OnGUI () | |
{ | |
if (count > 0) { | |
float progress = (float)count / (float)(size * size * size); | |
EditorGUI.ProgressBar (Rect.MinMaxRect (0, 0, position.width, 25f), progress, (progress * 100f).ToString ("000.0") + "%"); | |
if (GUI.Button (Rect.MinMaxRect (0, position.height - 25f, position.width, position.height), "Cancel")) { | |
count = 0; | |
EditorApplication.update -= Process; | |
coroutine = null; | |
} | |
return; | |
} | |
size = EditorGUILayout.IntField (size); | |
if (GUILayout.Button ("Create")) { | |
count = 0; | |
coroutine = ProcessCoroutine (); | |
EditorApplication.update += Process; | |
} | |
} | |
void Process () | |
{ | |
if (!coroutine.MoveNext ()) { | |
count = 0; | |
EditorApplication.update -= Process; | |
coroutine = null; | |
} | |
} | |
IEnumerator ProcessCoroutine () | |
{ | |
Color[] cs = new Color[size * size * size]; | |
int i = 0; | |
for (int z = 0; z < size; z++) { | |
for (int y = 0; y < size; y++) { | |
for (int x = 0; x < size; x++) { | |
Color c = Color.red; | |
Vector3 v = new Vector3 (5f * ((float)x / size), 5f * ((float)y / size), 5f * ((float)z / size)); | |
c.r = GetFbmLoop (v, 1); | |
c.g = GetFbmLoop (v, 2); | |
c.b = GetFbmLoop (v, 3); | |
c.a = GetFbmLoop (v, 4); | |
cs [i] = c; | |
count = i++; | |
if (i % 100 == 0) | |
yield return 0; | |
} | |
} | |
} | |
Texture3D t3d = new Texture3D (size, size, size, TextureFormat.ARGB32, false); | |
t3d.SetPixels (cs); | |
t3d.Apply (); | |
AssetDatabase.CreateAsset (t3d, "Assets/perlin3d_" + size + ".asset"); | |
AssetDatabase.SaveAssets (); | |
AssetDatabase.Refresh (); | |
Selection.activeObject = t3d; | |
count = 0; | |
} | |
float GetFbmLoop (Vector3 v, int octave) | |
{ | |
Vector3 v1 = v; | |
v1.x -= 5f; | |
float | |
f = GetLoopY (v, octave), | |
f1 = GetLoopY (v1, octave); | |
f = Mathf.Lerp (f, f1, v.x / 5f); | |
return f / 2f + 0.5f; | |
} | |
float GetLoopY (Vector3 v, int octave) | |
{ | |
Vector3 v1 = v; | |
v1.y -= 5f; | |
float | |
f = GetLoopZ (v, octave), | |
f1 = GetLoopZ (v1, octave); | |
f = Mathf.Lerp (f, f1, v.y / 5f); | |
return f; | |
} | |
float GetLoopZ (Vector3 v, int octave) | |
{ | |
Vector3 v1 = v; | |
v1.z -= 5f; | |
float | |
f = Perlin.Fbm (v, octave), | |
f1 = Perlin.Fbm (v1, octave); | |
f = Mathf.Lerp (f, f1, v.z / 5f); | |
return f; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/FullscreenNoise" { | |
Properties { | |
_MainTex ("Base (RGB)", 2D) = "white" {} | |
_N3D ("noise 3Dtexture", 3D) = "gray" {} | |
} | |
CGINCLUDE | |
#include "UnityCG.cginc" | |
sampler3D _N3D; | |
sampler2D _MainTex; | |
half4 _MainTex_TexelSize; | |
half4 frag(v2f_img i) : COLOR{ | |
return tex3D(_N3D, float3(i.uv, _Time.x)).r*2-1; | |
} | |
ENDCG | |
SubShader { | |
ZTest Always Cull Off ZWrite Off | |
Fog { Mode off } | |
pass{ | |
CGPROGRAM | |
#pragma fragmentoption ARB_precision_hint_fastest | |
#pragma vertex vert_img | |
#pragma fragment frag | |
ENDCG | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
// | |
// A Perlin noise class for Unity | |
// By Keijiro Takahashi, 2013 | |
// https://github.com/keijiro/unity-perlin | |
// | |
// Based on the original implementation by Ken Perlin | |
// http://mrl.nyu.edu/~perlin/noise/ | |
// | |
public static class Perlin | |
{ | |
#region Noise functions | |
public static float Noise (float x) | |
{ | |
var X = Mathf.FloorToInt (x) & 0xff; | |
x -= Mathf.Floor (x); | |
return Lerp (Fade (x), Grad(X, x), Grad (X + 1, x - 1)); | |
} | |
public static float Noise (float x, float y) | |
{ | |
var X = Mathf.FloorToInt (x) & 0xff; | |
var Y = Mathf.FloorToInt (y) & 0xff; | |
x -= Mathf.Floor (x); | |
y -= Mathf.Floor (y); | |
var u = Fade (x); | |
var v = Fade (y); | |
var A = (perm [X ] + Y) & 0xff; | |
var B = (perm [X + 1] + Y) & 0xff; | |
return Lerp (v, Lerp (u, Grad (A , x, y ), Grad (B, x - 1, y )), | |
Lerp (u, Grad (A + 1, x, y - 1), Grad (B + 1, x - 1, y - 1))); | |
} | |
public static float Noise (Vector2 coord) | |
{ | |
return Noise (coord.x, coord.y); | |
} | |
public static float Noise (float x, float y, float z) | |
{ | |
var X = Mathf.FloorToInt (x) & 0xff; | |
var Y = Mathf.FloorToInt (y) & 0xff; | |
var Z = Mathf.FloorToInt (z) & 0xff; | |
x -= Mathf.Floor (x); | |
y -= Mathf.Floor (y); | |
z -= Mathf.Floor (z); | |
var u = Fade (x); | |
var v = Fade (y); | |
var w = Fade (z); | |
var A = (perm [X ] + Y) & 0xff; | |
var B = (perm [X + 1] + Y) & 0xff; | |
var AA = (perm [A ] + Z) & 0xff; | |
var BA = (perm [B ] + Z) & 0xff; | |
var AB = (perm [A + 1] + Z) & 0xff; | |
var BB = (perm [B + 1] + Z) & 0xff; | |
return Lerp (w, Lerp (v, Lerp (u, Grad (AA , x , y , z ), Grad (BA , x - 1, y , z )), | |
Lerp (u, Grad (AB , x , y - 1, z ), Grad (BB , x - 1, y - 1, z ))), | |
Lerp (v, Lerp (u, Grad (AA + 1, x , y , z - 1), Grad (BA + 1, x - 1, y , z - 1)), | |
Lerp (u, Grad (AB + 1, x , y - 1, z - 1), Grad (BB + 1, x - 1, y - 1, z - 1)))); | |
} | |
public static float Noise (Vector3 coord) | |
{ | |
return Noise (coord.x, coord.y, coord.z); | |
} | |
#endregion | |
#region fBm functions | |
public static float Fbm (float x, int octave) | |
{ | |
var f = 0.0f; | |
var w = 0.5f; | |
for (var i = 0; i < octave; i++) { | |
f += w * Noise (x); | |
x *= 2.0f; | |
w *= 0.5f; | |
} | |
return f; | |
} | |
public static float Fbm (Vector2 coord, int octave) | |
{ | |
var f = 0.0f; | |
var w = 0.5f; | |
for (var i = 0; i < octave; i++) { | |
f += w * Noise (coord); | |
coord *= 2.0f; | |
w *= 0.5f; | |
} | |
return f; | |
} | |
public static float Fbm (Vector3 coord, int octave) | |
{ | |
var f = 0.0f; | |
var w = 0.5f; | |
for (var i = 0; i < octave; i++) { | |
f += w * Noise (coord); | |
coord *= 2.0f; | |
w *= 0.5f; | |
} | |
return f; | |
} | |
#endregion | |
#region Private functions | |
static float Fade (float t) | |
{ | |
return t * t * t * (t * (t * 6 - 15) + 10); | |
} | |
static float Lerp (float t, float a, float b) | |
{ | |
return a + t * (b - a); | |
} | |
static float Grad (int i, float x) | |
{ | |
return (perm [i] & 1) != 0 ? x : -x; | |
} | |
static float Grad (int i, float x, float y) | |
{ | |
var h = perm [i]; | |
return ((h & 1) != 0 ? x : -x) + ((h & 2) != 0 ? y : -y); | |
} | |
static float Grad (int i, float x, float y, float z) | |
{ | |
var h = perm [i] & 15; | |
var u = h < 8 ? x : y; | |
var v = h < 4 ? y : (h == 12 || h == 14 ? x : z); | |
return ((h & 1) != 0 ? u : -u) + ((h & 2) != 0 ? v : -v); | |
} | |
static int[] perm = { | |
151,160,137,91,90,15, | |
131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23, | |
190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33, | |
88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166, | |
77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244, | |
102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196, | |
135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123, | |
5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42, | |
223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9, | |
129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228, | |
251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107, | |
49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254, | |
138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180, | |
151 | |
}; | |
#endregion | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class ShowMat : MonoBehaviour | |
{ | |
public Material mat; | |
void OnRenderImage (RenderTexture s, RenderTexture d) | |
{ | |
Graphics.Blit (s, d, mat); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment