Class for handling 3D Textures, Use this to create.
/// <summary> /// Creates a 3D texture. /// </summary> public static Texture3D CreateTexture3DFromResources(string texturePathFromResources, int slices) { Texture3D texture3D = null; Texture2D texture2D = Resources.Load<Texture2D>(texturePathFromResources); if (texture2D != null) { int height = texture2D.height; int width = texture2D.width / slices; Color[] pixels2D = texture2D.GetPixels(); Color[] pixels3D = new Color[pixels2D.Length]; for (int z = 0; z < slices; ++z) for (int y = 0; y < height; ++y) for (int x = 0; x < width; ++x) pixels3D[x + (y * width) + (z * (width * height))] = pixels2D[x + (z * width) + (((width - y) - 1) * width * height)]; texture3D = new Texture3D(width, height, slices, TextureFormat.ARGB32, false); texture3D.SetPixels(pixels3D); texture3D.Apply(); texture3D.filterMode = FilterMode.Trilinear; texture3D.wrapMode = TextureWrapMode.Clamp; texture3D.anisoLevel = 1; } else Debug.LogWarning(string.Format("Texture '{0}' not found in 'Resources/Textures' folder.", texturePathFromResources)); return texture3D; }
protected void SetIdentityLut() { int dim = 16; Color[] newC = new Color[dim * dim * dim]; float oneOverDim = 1.0f / (1.0f * dim - 1.0f); for (int i = 0; i < dim; i++) { for (int j = 0; j < dim; j++) { for (int k = 0; k < dim; k++) { newC[i + (j * dim) + (k * dim * dim)] = new Color((i * 1.0f) * oneOverDim, (j * 1.0f) * oneOverDim, (k * 1.0f) * oneOverDim, 1.0f); } } } if (m_Lut3D) DestroyImmediate(m_Lut3D); m_Lut3D = new Texture3D(dim, dim, dim, TextureFormat.ARGB32, false); m_Lut3D.hideFlags = HideFlags.HideAndDontSave; m_Lut3D.SetPixels(newC); m_Lut3D.Apply(); m_BaseTextureName = ""; }
static public int SetPixels(IntPtr l) { try{ if (matchType(l, 2, typeof(UnityEngine.Color), typeof(System.Int32))) { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); UnityEngine.Color[] a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); self.SetPixels(a1, a2); return(0); } else if (matchType(l, 2, typeof(UnityEngine.Color))) { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); UnityEngine.Color[] a1; checkType(l, 2, out a1); self.SetPixels(a1); return(0); } LuaDLL.luaL_error(l, "No matched override function to call"); return(0); } catch (Exception e) { LuaDLL.luaL_error(l, e.ToString()); return(0); } }
static public int Apply(IntPtr l) { try{ if (matchType(l, 2, typeof(System.Boolean))) { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); System.Boolean a1; checkType(l, 2, out a1); self.Apply(a1); return(0); } else if (matchType(l, 2)) { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); self.Apply(); return(0); } LuaDLL.luaL_error(l, "No matched override function to call"); return(0); } catch (Exception e) { LuaDLL.luaL_error(l, e.ToString()); return(0); } }
static public int GetPixels32(IntPtr l) { try { int argc = LuaDLL.lua_gettop(l); if (argc == 1) { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); var ret = self.GetPixels32(); pushValue(l, true); pushValue(l, ret); return(2); } else if (argc == 2) { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); System.Int32 a1; checkType(l, 2, out a1); var ret = self.GetPixels32(a1); pushValue(l, true); pushValue(l, ret); return(2); } pushValue(l, false); LuaDLL.lua_pushstring(l, "No matched override function to call"); return(2); } catch (Exception e) { return(error(l, e)); } }
static public int get_isReadable(IntPtr l) { try { #if DEBUG var method = System.Reflection.MethodBase.GetCurrentMethod(); string methodName = GetMethodName(method); #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.BeginSample(methodName); #else Profiler.BeginSample(methodName); #endif #endif UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); pushValue(l, true); pushValue(l, self.isReadable); return(2); } catch (Exception e) { return(error(l, e)); } #if DEBUG finally { #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.EndSample(); #else Profiler.EndSample(); #endif } #endif }
// fields // properties static void Texture3D_depth(JSVCall vc) { UnityEngine.Texture3D _this = (UnityEngine.Texture3D)vc.csObj; var result = _this.depth; JSApi.setInt32((int)JSApi.SetType.Rval, (System.Int32)(result)); }
static public int SetPixels(IntPtr l) { try { int argc = LuaDLL.lua_gettop(l); if (argc == 2) { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); UnityEngine.Color[] a1; checkArray(l, 2, out a1); self.SetPixels(a1); pushValue(l, true); return(1); } else if (argc == 3) { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); UnityEngine.Color[] a1; checkArray(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); self.SetPixels(a1, a2); pushValue(l, true); return(1); } pushValue(l, false); LuaDLL.lua_pushstring(l, "No matched override function to call"); return(2); } catch (Exception e) { return(error(l, e)); } }
public void Generate() { tex = new Texture3D(size, size, size, TextureFormat.RGB24, true); var cols = new Color[size * size * size]; // float mul = 1.0f / (size-1); int idx = 0; Color c = Color.black; for (int z = 0; z < size; ++z) { for (int y = 0; y < size; ++y) { for (int x = 0; x < size; ++x, ++idx) { c.r = fbm(x, y, z); c.g = c.r; c.b = c.r; cols[idx] = c; } } } tex.SetPixels(cols); tex.Apply(); Shader.SetGlobalTexture("_3DTex", tex); }
void Create3DTexture() { int iTexSizeX = m_pClouds2DTex.width; int iTexSizeY = m_pClouds2DTex.height; int iTextureDepth = iTexSizeX / m_iLayersSquaredSize; int iLayerSize = m_iLayersSquaredSize * m_iLayersSquaredSize; m_pClouds3D = new Texture3D(m_iLayersSquaredSize, m_iLayersSquaredSize, iTextureDepth, TextureFormat.ARGB32, true); Color[] pTexColor = new Color[iTexSizeX * iTexSizeY]; for (int x = 0; x < iTexSizeX; x++) { for (int y = 0; y < iTexSizeY; y++) { int iLayer = x / m_iLayersSquaredSize; int iLayerIndex = (y * m_iLayersSquaredSize) + (x % m_iLayersSquaredSize); int iIndex = (iLayer * iLayerSize) + iLayerIndex; Color tColor = m_pClouds2DTex.GetPixel(x, y); pTexColor[iIndex] = tColor; } } m_pClouds3D.SetPixels(pTexColor); m_pClouds3D.Apply(true); Shader.SetGlobalTexture("_CloudTex", m_pClouds3D); UpdateProperties(); }
static public int GetPixelBilinear(IntPtr l) { try { #if DEBUG var method = System.Reflection.MethodBase.GetCurrentMethod(); string methodName = GetMethodName(method); #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.BeginSample(methodName); #else Profiler.BeginSample(methodName); #endif #endif int argc = LuaDLL.lua_gettop(l); if (argc == 4) { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); System.Single a1; checkType(l, 2, out a1); System.Single a2; checkType(l, 3, out a2); System.Single a3; checkType(l, 4, out a3); var ret = self.GetPixelBilinear(a1, a2, a3); pushValue(l, true); pushValue(l, ret); return(2); } else if (argc == 5) { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); System.Single a1; checkType(l, 2, out a1); System.Single a2; checkType(l, 3, out a2); System.Single a3; checkType(l, 4, out a3); System.Int32 a4; checkType(l, 5, out a4); var ret = self.GetPixelBilinear(a1, a2, a3, a4); pushValue(l, true); pushValue(l, ret); return(2); } pushValue(l, false); LuaDLL.lua_pushstring(l, "No matched override function GetPixelBilinear to call"); return(2); } catch (Exception e) { return(error(l, e)); } #if DEBUG finally { #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.EndSample(); #else Profiler.EndSample(); #endif } #endif }
static public int SetPixels32(IntPtr l) { try { int argc = LuaDLL.lua_gettop(l); if (argc == 2) { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); UnityEngine.Color32[] a1; checkType(l, 2, out a1); self.SetPixels32(a1); return(0); } else if (argc == 3) { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); UnityEngine.Color32[] a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); self.SetPixels32(a1, a2); return(0); } return(error(l, "No matched override function to call")); } catch (Exception e) { return(error(l, e)); } }
void CalculateNextPixel() { if (pixelProgress < totalPixels) { int temp = pixelProgress; int r = temp % colorsteps; temp /= colorsteps; int g = temp % colorsteps; temp /= colorsteps; int b = temp % colorsteps; CalculatePixel(r, g, b); ++pixelProgress; progress = (float) pixelProgress / (float) totalPixels; } else { colormap = new Texture3D(colorsteps, colorsteps, colorsteps, TextureFormat.Alpha8, false); colormap.filterMode = FilterMode.Point; colormap.wrapMode = TextureWrapMode.Clamp; colormap.SetPixels32(pixelBuffer); colormap.Apply(); doneCallback.Invoke(); } }
static void Texture3D_format(JSVCall vc) { UnityEngine.Texture3D _this = (UnityEngine.Texture3D)vc.csObj; var result = _this.format; JSApi.setEnum((int)JSApi.SetType.Rval, (int)result); }
static public int Apply(IntPtr l) { try { int argc = LuaDLL.lua_gettop(l); if (argc == 1) { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); self.Apply(); return(0); } else if (argc == 2) { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); System.Boolean a1; checkType(l, 2, out a1); self.Apply(a1); return(0); } else if (argc == 3) { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); System.Boolean a1; checkType(l, 2, out a1); System.Boolean a2; checkType(l, 3, out a2); self.Apply(a1, a2); return(0); } return(error(l, "No matched override function to call")); } catch (Exception e) { return(error(l, e)); } }
static public int GetPixels(IntPtr l) { try{ int argc = LuaDLL.lua_gettop(l); if (argc == 2) { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); System.Int32 a1; checkType(l, 2, out a1); UnityEngine.Color[] ret = self.GetPixels(a1); pushValue(l, ret); return(1); } else if (argc == 1) { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); UnityEngine.Color[] ret = self.GetPixels(); pushValue(l, ret); return(1); } LuaDLL.luaL_error(l, "No matched override function to call"); return(0); } catch (Exception e) { LuaDLL.luaL_error(l, e.ToString()); return(0); } }
public static Texture3D LoadVolumeFromFile(string fileName, TextureFormat format, int elementSize ,int width, int height, int depth) { BinaryReader br = new BinaryReader(File.Open(fileName, FileMode.Open, FileAccess.Read)); Texture3D noiseTex = new Texture3D(width, height, depth, TextureFormat.RGBA32, false); noiseTex.filterMode = FilterMode.Bilinear; noiseTex.wrapMode = TextureWrapMode.Repeat; int numElements = width * height * depth; List<Color> colors = new List<Color>(numElements); // Get pixels from 2d texture for each slize in z direction for (int z = 0; z < depth; z++) { Texture2D tex2d = LoadTexture2DRaw(br, width, height, format, elementSize); colors.AddRange(tex2d.GetPixels()); } //colors should now be filled with all pixels noiseTex.SetPixels(colors.ToArray()); noiseTex.Apply(false); br.Close(); return noiseTex; }
public void SetIdentityLut() { int dim = 16; Color[] newC = new Color[dim*dim*dim]; float oneOverDim = 1.0f/(1.0f*dim - 1.0f); for (int i = 0; i < dim; i++) { for (int j = 0; j < dim; j++) { for (int k = 0; k < dim; k++) { newC[i + (j*dim) + (k*dim*dim)] = new Color((i*1.0f)*oneOverDim, (j*1.0f)*oneOverDim, (k*1.0f)*oneOverDim, 1.0f); } } } if (converted3DLut) DestroyImmediate(converted3DLut); converted3DLut = new Texture3D(dim, dim, dim, TextureFormat.ARGB32, false); converted3DLut.SetPixels(newC); converted3DLut.Apply(); basedOnTempTex = ""; }
/// <summary> /// Creates the material and textures. /// </summary> protected override void CreateMaterial() { DestroyLut(); lutTex = VintageHelper.CreateTexture3DFromResources(@"Textures/cremaLut", 16); base.CreateMaterial(); }
private void DestroyLut() { if (lutTex != null) { DestroyImmediate(lutTex); lutTex = null; } }
public static void CreateTexture() { int n = 64; string texName = "pyroclasticNoise"; Texture3D texture3D = new Texture3D(n, n, n, TextureFormat.ARGB32, true); Generate3DTexture(ref texture3D, n); save3DTexture(texture3D, texName); }
static public int Apply(IntPtr l) { try { #if DEBUG var method = System.Reflection.MethodBase.GetCurrentMethod(); string methodName = GetMethodName(method); #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.BeginSample(methodName); #else Profiler.BeginSample(methodName); #endif #endif int argc = LuaDLL.lua_gettop(l); if (argc == 1) { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); self.Apply(); pushValue(l, true); return(1); } else if (argc == 2) { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); System.Boolean a1; checkType(l, 2, out a1); self.Apply(a1); pushValue(l, true); return(1); } else if (argc == 3) { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); System.Boolean a1; checkType(l, 2, out a1); System.Boolean a2; checkType(l, 3, out a2); self.Apply(a1, a2); pushValue(l, true); return(1); } pushValue(l, false); LuaDLL.lua_pushstring(l, "No matched override function Apply to call"); return(2); } catch (Exception e) { return(error(l, e)); } #if DEBUG finally { #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.EndSample(); #else Profiler.EndSample(); #endif } #endif }
/// <summary> /// Read the data using the reader. /// </summary> /// <param name="reader">Reader.</param> public override object Read(ISaveGameReader reader) { UnityEngine.Texture3D texture3D = new UnityEngine.Texture3D( reader.ReadProperty <System.Int32>(), reader.ReadProperty <System.Int32>(), reader.ReadProperty <System.Int32>(), TextureFormat.ARGB32, true); ReadInto(texture3D, reader); return(texture3D); }
static public int get_depth(IntPtr l) { try { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); pushValue(l, self.depth); return(1); } catch (Exception e) { return(error(l, e)); } }
static public int get_format(IntPtr l) { try { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); pushEnum(l, (int)self.format); return(1); } catch (Exception e) { return(error(l, e)); } }
static public int get_isReadable(IntPtr l) { try { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); pushValue(l, true); pushValue(l, self.isReadable); return(2); } catch (Exception e) { return(error(l, e)); } }
static public int get_depth(IntPtr l) { try { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); pushValue(l, self.depth); return(1); } catch (Exception e) { LuaDLL.luaL_error(l, e.ToString()); return(0); } }
static public int Apply(IntPtr l) { try { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); self.Apply(); pushValue(l, true); return(1); } catch (Exception e) { return(error(l, e)); } }
static public int GetPixels32(IntPtr l) { try { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); var ret = self.GetPixels32(); pushValue(l, true); pushValue(l, ret); return(2); } catch (Exception e) { return(error(l, e)); } }
private static void save3DTexture(Texture3D texture3D, string texture3DName) { string path = "Assets/Textures3D/" + texture3DName + ".asset"; Texture3D tmp = (Texture3D)AssetDatabase.LoadAssetAtPath(path, typeof(Texture3D)); if (tmp) { AssetDatabase.DeleteAsset(path); tmp = null; } AssetDatabase.CreateAsset(texture3D, path); AssetDatabase.SaveAssets(); }
static public int Apply__Boolean(IntPtr l) { try { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); System.Boolean a1; checkType(l, 2, out a1); self.Apply(a1); pushValue(l, true); return(1); } catch (Exception e) { return(error(l, e)); } }
static public int SetPixels32__A_Color32(IntPtr l) { try { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); UnityEngine.Color32[] a1; checkArray(l, 2, out a1); self.SetPixels32(a1); pushValue(l, true); return(1); } catch (Exception e) { return(error(l, e)); } }
static public int GetPixels__Int32(IntPtr l) { try { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); System.Int32 a1; checkType(l, 2, out a1); var ret = self.GetPixels(a1); pushValue(l, true); pushValue(l, ret); return(2); } catch (Exception e) { return(error(l, e)); } }
public Texture3D Generate3DTexture() { float r = 0.3f; Texture3D texture3D = new Texture3D(n, n, n, TextureFormat.ARGB32, true); int size = n * n * n; Color[] cols = new Color[size]; int idx = 0; Color c = Color.white; float frequency = 0.01f / n; float center = n / 2.0f + 0.5f; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++, ++idx) { float dx = center - i; float dy = center - j; float dz = center - k; float off = Mathf.Abs(Perlin.Turbulence(i * frequency, j * frequency, k * frequency, 6)); float d = Mathf.Sqrt(dx * dx + dy * dy + dz * dz) / (n); //c.r = c.g = c.b = c.a = ((d-off) < r)?1.0f:0.0f; float p = d - off; c.r = c.g = c.b = c.a = Mathf.Clamp01(r - p); cols[idx] = c; } } } //for(int i = 0; i < size; i++) // Debug.Log (newC[i]); texture3D.SetPixels(cols); texture3D.Apply(); renderer.material.SetTexture("g_densityTex", texture3D); texture3D.filterMode = FilterMode.Trilinear; texture3D.wrapMode = TextureWrapMode.Clamp; texture3D.anisoLevel = 1; //Color[] cs = texture3D.GetPixels(); //for(int i = 0; i < 10; i++) // Debug.Log (cs[i]); return texture3D; }
static public int SetPixels__A_Color__Int32(IntPtr l) { try { UnityEngine.Texture3D self = (UnityEngine.Texture3D)checkSelf(l); UnityEngine.Color[] a1; checkArray(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); self.SetPixels(a1, a2); pushValue(l, true); return(1); } catch (Exception e) { return(error(l, e)); } }
static public int constructor(IntPtr l) { UnityEngine.Texture3D o; System.Int32 a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); System.Int32 a3; checkType(l, 4, out a3); UnityEngine.TextureFormat a4; checkEnum(l, 5, out a4); System.Boolean a5; checkType(l, 6, out a5); o = new UnityEngine.Texture3D(a1, a2, a3, a4, a5); pushObject(l, o); return(1); }
void Start() { if (colorCorrectionLookup == null) { return; } string lutPath = modPath + "/lut.png"; if (File.Exists(lutPath)) { defaultLut = colorCorrectionLookup.converted3DLut; Texture2D lut = new Texture2D(2, 2); lut.LoadImage(File.ReadAllBytes(lutPath)); colorCorrectionLookup.Convert(lut, lutPath); } else { Debug.LogError("Can't load LUT texture from: " + lutPath); } }
static public int constructor(IntPtr l) { try { int argc = LuaDLL.lua_gettop(l); UnityEngine.Texture3D o; if (matchType(l, "ctor__Int32__Int32__Int32__GraphicsFormat__TextureCreationFlags", argc, 2, typeof(int), typeof(int), typeof(int), typeof(UnityEngine.Experimental.Rendering.GraphicsFormat), typeof(UnityEngine.Experimental.Rendering.TextureCreationFlags))) { System.Int32 a1; checkType(l, 3, out a1); System.Int32 a2; checkType(l, 4, out a2); System.Int32 a3; checkType(l, 5, out a3); UnityEngine.Experimental.Rendering.GraphicsFormat a4; checkEnum(l, 6, out a4); UnityEngine.Experimental.Rendering.TextureCreationFlags a5; checkEnum(l, 7, out a5); o = new UnityEngine.Texture3D(a1, a2, a3, a4, a5); pushValue(l, true); pushValue(l, o); return(2); } else if (matchType(l, "ctor__Int32__Int32__Int32__TextureFormat__Boolean", argc, 2, typeof(int), typeof(int), typeof(int), typeof(UnityEngine.TextureFormat), typeof(bool))) { System.Int32 a1; checkType(l, 3, out a1); System.Int32 a2; checkType(l, 4, out a2); System.Int32 a3; checkType(l, 5, out a3); UnityEngine.TextureFormat a4; checkEnum(l, 6, out a4); System.Boolean a5; checkType(l, 7, out a5); o = new UnityEngine.Texture3D(a1, a2, a3, a4, a5); pushValue(l, true); pushValue(l, o); return(2); } return(error(l, "New object failed.")); } catch (Exception e) { return(error(l, e)); } }
public void Convert(Texture2D temp2DTex, string path) { // conversion fun: the given 2D texture needs to be of the format // w * h, wheras h is the 'depth' (or 3d dimension 'dim') and w = dim * dim if (temp2DTex) { int dim = temp2DTex.width * temp2DTex.height; dim = temp2DTex.height; if (!ValidDimensions(temp2DTex)) { Debug.LogWarning("The given 2D texture " + temp2DTex.name + " cannot be used as a 3D LUT."); basedOnTempTex = ""; return; } Color[] c = temp2DTex.GetPixels(); Color[] newC = new Color[c.Length]; for (int i = 0; i < dim; i++) { for (int j = 0; j < dim; j++) { for (int k = 0; k < dim; k++) { int j_ = dim - j - 1; newC[i + (j * dim) + (k * dim * dim)] = c[k * dim + i + j_ * dim * dim]; } } } if (converted3DLut) DestroyImmediate(converted3DLut); converted3DLut = new Texture3D(dim, dim, dim, TextureFormat.ARGB32, false); converted3DLut.SetPixels(newC); converted3DLut.Apply(); basedOnTempTex = path; } else { // error, something went terribly wrong Debug.LogError("Couldn't color correct with 3D LUT texture. Image Effect will be disabled."); } }
/// <summary> /// Write the specified value using the writer. /// </summary> /// <param name="value">Value.</param> /// <param name="writer">Writer.</param> public override void Write(object value, ISaveGameWriter writer) { UnityEngine.Texture3D texture3D = (UnityEngine.Texture3D)value; writer.WriteProperty("width", texture3D.width); writer.WriteProperty("height", texture3D.height); writer.WriteProperty("depth", texture3D.depth); writer.WriteProperty("dimension", texture3D.dimension); writer.WriteProperty("filterMode", texture3D.filterMode); writer.WriteProperty("anisoLevel", texture3D.anisoLevel); writer.WriteProperty("wrapMode", texture3D.wrapMode); #if UNITY_2017_1_OR_NEWER writer.WriteProperty("wrapModeU", texture3D.wrapModeU); writer.WriteProperty("wrapModeV", texture3D.wrapModeV); writer.WriteProperty("wrapModeW", texture3D.wrapModeW); #endif writer.WriteProperty("mipMapBias", texture3D.mipMapBias); writer.WriteProperty("name", texture3D.name); writer.WriteProperty("hideFlags", texture3D.hideFlags); }
public static void Generate3DTexture(ref Texture3D texture3D, int n) { float r = 0.3f; int size = n * n * n; Color[] cols = new Color[size]; int idx = 0; Color c = Color.white; float frequency = 0.01f / n; float center = n / 2.0f + 0.5f; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++, ++idx) { float dx = center - i; float dy = center - j; float dz = center - k; float off = Mathf.Abs(Perlin.Turbulence(i * frequency, j * frequency, k * frequency, 6)); float d = Mathf.Sqrt(dx * dx + dy * dy + dz * dz) / (n); //c.r = c.g = c.b = c.a = ((d-off) < r)?1.0f:0.0f; float p = d - off; c.r = c.g = c.b = c.a = Mathf.Clamp01(r - p); cols[idx] = c; } } } //for(int i = 0; i < size; i++) // Debug.Log (newC[i]); texture3D.SetPixels(cols); texture3D.Apply(); texture3D.filterMode = FilterMode.Trilinear; texture3D.wrapMode = TextureWrapMode.Clamp; texture3D.anisoLevel = 1; }
static public int constructor(IntPtr l) { try { #if DEBUG var method = System.Reflection.MethodBase.GetCurrentMethod(); string methodName = GetMethodName(method); #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.BeginSample(methodName); #else Profiler.BeginSample(methodName); #endif #endif UnityEngine.Texture3D o; System.Int32 a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); System.Int32 a3; checkType(l, 4, out a3); UnityEngine.TextureFormat a4; a4 = (UnityEngine.TextureFormat)LuaDLL.luaL_checkinteger(l, 5); System.Boolean a5; checkType(l, 6, out a5); o = new UnityEngine.Texture3D(a1, a2, a3, a4, a5); pushValue(l, true); pushValue(l, o); return(2); } catch (Exception e) { return(error(l, e)); } #if DEBUG finally { #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.EndSample(); #else Profiler.EndSample(); #endif } #endif }
void Start() { tex = new Texture3D (size, size, size, TextureFormat.ARGB32, true); var cols = new Color[size * size * size]; float mul = 1.0f / (size - 1); int idx = 0; Color c = Color.white; for (int z = 0; z < size; ++z) { for (int y = 0; y < size; ++y) { for (int x = 0; x < size; ++x, ++idx) { c.r = 1f - (Mathf.Abs(x-size * .5f) * 2f * mul); c.g = 1f - (Mathf.Abs(y-size * .5f) * 2f * mul); c.b = 1f - (Mathf.Abs(z-size * .5f) * 2f * mul); cols [idx] = c; } } } tex.SetPixels (cols); tex.Apply (); renderer.material.SetTexture ("_Volume", tex); }
static public int constructor(IntPtr l) { try { UnityEngine.Texture3D o; System.Int32 a1; checkType(l,2,out a1); System.Int32 a2; checkType(l,3,out a2); System.Int32 a3; checkType(l,4,out a3); UnityEngine.TextureFormat a4; checkEnum(l,5,out a4); System.Boolean a5; checkType(l,6,out a5); o=new UnityEngine.Texture3D(a1,a2,a3,a4,a5); pushValue(l,true); pushValue(l,o); return 2; } catch(Exception e) { return error(l,e); } }
static public int constructor(IntPtr l) { try { UnityEngine.Texture3D o; System.Int32 a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); System.Int32 a3; checkType(l, 4, out a3); UnityEngine.TextureFormat a4; checkEnum(l, 5, out a4); System.Boolean a5; checkType(l, 6, out a5); o = new UnityEngine.Texture3D(a1, a2, a3, a4, a5); pushValue(l, o); return(1); } catch (Exception e) { return(error(l, e)); } }
public void Convert(Texture2D temp2DTex, string path) { if (temp2DTex) { int num = temp2DTex.width * temp2DTex.height; num = temp2DTex.height; if (!this.ValidDimensions(temp2DTex)) { Debug.LogWarning("The given 2D texture " + temp2DTex.name + " cannot be used as a 3D LUT."); this.basedOnTempTex = string.Empty; return; } Color[] pixels = temp2DTex.GetPixels(); Color[] array = new Color[pixels.Length]; for (int i = 0; i < num; i++) { for (int j = 0; j < num; j++) { for (int k = 0; k < num; k++) { int num2 = num - j - 1; array[i + j * num + k * num * num] = pixels[k * num + i + num2 * num * num]; } } } if (this.converted3DLut) { UnityEngine.Object.DestroyImmediate(this.converted3DLut); } this.converted3DLut = new Texture3D(num, num, num, TextureFormat.ARGB32, false); this.converted3DLut.SetPixels(array); this.converted3DLut.Apply(); this.basedOnTempTex = path; } else { Debug.LogError("Couldn't color correct with 3D LUT texture. Image Effect will be disabled."); } }
static public int constructor(IntPtr l) { LuaDLL.lua_remove(l, 1); UnityEngine.Texture3D o; if (matchType(l, 1, typeof(System.Int32), typeof(System.Int32), typeof(System.Int32), typeof(UnityEngine.TextureFormat), typeof(System.Boolean))) { System.Int32 a1; checkType(l, 1, out a1); System.Int32 a2; checkType(l, 2, out a2); System.Int32 a3; checkType(l, 3, out a3); UnityEngine.TextureFormat a4; checkEnum(l, 4, out a4); System.Boolean a5; checkType(l, 5, out a5); o = new UnityEngine.Texture3D(a1, a2, a3, a4, a5); pushObject(l, o); return(1); } LuaDLL.luaL_error(l, "New object failed."); return(0); }
static public int ctor__Int32__Int32__Int32__GraphicsFormat__TextureCreationFlags_s(IntPtr l) { try { UnityEngine.Texture3D o; System.Int32 a1; checkType(l, 1, out a1); System.Int32 a2; checkType(l, 2, out a2); System.Int32 a3; checkType(l, 3, out a3); UnityEngine.Experimental.Rendering.GraphicsFormat a4; checkEnum(l, 4, out a4); UnityEngine.Experimental.Rendering.TextureCreationFlags a5; checkEnum(l, 5, out a5); o = new UnityEngine.Texture3D(a1, a2, a3, a4, a5); pushValue(l, true); pushValue(l, o); return(2); } catch (Exception e) { return(error(l, e)); } }
public static Texture3D LoadVolumeFromFile(string fileName) { int x, y, z; x = y = z = 16; // int numElementsTotal = 16*16*16 + 8*8*8 + 4*4*4 + 2*2*2 + 1; // all mip levels BinaryReader br = new BinaryReader(File.Open(fileName, FileMode.Open, FileAccess.Read)); int headerSize = 5 * sizeof(int); br.ReadBytes(headerSize); // skip header info Texture3D noiseTex = new Texture3D(x, y, z, TextureFormat.RGBA32, true); noiseTex.filterMode = FilterMode.Bilinear; noiseTex.wrapMode = TextureWrapMode.Repeat; int maxMipLevel = 16; for (int mipLevel = 0; mipLevel < 5; mipLevel++) { ////int dimMipLevel = Mathf.Pow(2,(float)(4-mipLevel)); // dimension of miplevel 16, 8, 4, 2, 1 int dimMipLevel = maxMipLevel >> mipLevel; int numElMip = dimMipLevel * dimMipLevel * dimMipLevel; //Color[] colors = new Color[numElMip]; List<Color> colors = new List<Color>(numElMip); // Get pixels from 2d texture for each slize in z direction for (z = 0; z < dimMipLevel; z++) { Texture2D tex2d = LoadTexture2DRaw(br, dimMipLevel, dimMipLevel); colors.AddRange(tex2d.GetPixels()); } //colors should now be filled with all pixels noiseTex.SetPixels(colors.ToArray(), mipLevel); noiseTex.Apply(false); } br.Close(); return noiseTex; }
protected virtual void CreateRenderTextures() { RenderTextureFormat mapFormat = RenderTextureFormat.ARGBFloat; RenderTextureFormat format = RenderTextureFormat.ARGBFloat; //These texture hold the actual data use in the ocean renderer CreateMap(ref m_map0, mapFormat, m_ansio); CreateMap(ref m_map1, mapFormat, m_ansio); CreateMap(ref m_map2, mapFormat, m_ansio); CreateMap(ref m_map3, mapFormat, m_ansio); CreateMap(ref m_map4, mapFormat, m_ansio); //These textures are used to perform the fourier transform CreateBuffer(ref m_fourierBuffer0, format); //heights CreateBuffer(ref m_fourierBuffer1, format); // slopes X CreateBuffer(ref m_fourierBuffer2, format); // slopes Y CreateBuffer(ref m_fourierBuffer3, format); // displacement X CreateBuffer(ref m_fourierBuffer4, format); // displacement Y //These textures hold the specturm the fourier transform is performed on m_spectrum01 = new RenderTexture(m_fourierGridSize, m_fourierGridSize, 0, format); m_spectrum01.filterMode = FilterMode.Point; m_spectrum01.wrapMode = TextureWrapMode.Repeat; m_spectrum01.enableRandomWrite = true; m_spectrum01.Create(); m_spectrum23 = new RenderTexture(m_fourierGridSize, m_fourierGridSize, 0, format); m_spectrum23.filterMode = FilterMode.Point; m_spectrum23.wrapMode = TextureWrapMode.Repeat; m_spectrum23.enableRandomWrite = true; m_spectrum23.Create(); m_WTable = new RenderTexture(m_fourierGridSize, m_fourierGridSize, 0, format); m_WTable.filterMode = FilterMode.Point; m_WTable.wrapMode = TextureWrapMode.Clamp; m_WTable.enableRandomWrite = true; m_WTable.Create(); // m_variance = new RenderTexture(m_varianceSize, m_varianceSize, 0, RenderTextureFormat.RHalf); // m_variance.volumeDepth = m_varianceSize; // m_variance.wrapMode = TextureWrapMode.Clamp; // m_variance.filterMode = FilterMode.Bilinear; // m_variance.isVolume = true; // m_variance.enableRandomWrite = true; // m_variance.useMipMap = true; // m_variance.Create(); m_variance = new Texture3D(m_varianceSize, m_varianceSize, m_varianceSize, TextureFormat.ARGB32, true); m_variance.wrapMode = TextureWrapMode.Clamp; m_variance.filterMode = FilterMode.Bilinear; }
void Start() { // Setup the base args buffer particleLocations = new Vector4[particleCount]; //particleColors = new Vector4[particleCount]; particleTargets = new Vector4[particleCount]; for (int i = 0; i < particleCount; i++) { // Generate a random location Vector3 newLoc = Random.insideUnitSphere * 0.5f + new Vector3(0.5f,0.5f,0.5f); particleLocations[i] = new Vector4(newLoc.x, newLoc.y, newLoc.z, particlePower); particleTargets[i] = newLoc; //particleColors[i] = new Vector3(Random.value, Random.value, Random.value); } targetBuffer = new ComputeBuffer(particleCount, 16); targetBuffer.SetData(particleTargets); //particleColorBuffer = new ComputeBuffer(particleCount, 16); //particleColorBuffer.SetData(particleColors); particleBuffer = new ComputeBuffer(particleCount, 16); particleBuffer.SetData(particleLocations); int pixelCount = textureSize * textureSize * textureSize; tex3D = new Texture3D( textureSize, textureSize, textureSize, TextureFormat.ARGB32, false); tex3D.wrapMode = TextureWrapMode.Clamp; tex3D.anisoLevel = 0; pixels = new Color[pixelCount]; volumeBuffer = new ComputeBuffer(pixelCount, 16); volumeBuffer.SetData(pixels); /* volume = new RenderTexture(textureSize, textureSize, 0, RenderTextureFormat.ARGB32); volume.volumeDepth = textureSize; volume.isVolume = true; volume.enableRandomWrite = true; volume.Create(); */ //renderer.material.SetTexture ("_Volume", tex3D); //renderer.material.SetTexture ("_Volume", volume); //renderer.material.SetTexture("_dataFieldTex", tex3D); marchingCubesMaterial.SetTexture("_dataFieldTex", tex3D); }
void OnDestroy() { if (converted3DLut) DestroyImmediate(converted3DLut); converted3DLut = null; }
private void OnDestroy() { if (this.converted3DLut) { UnityEngine.Object.DestroyImmediate(this.converted3DLut); } this.converted3DLut = null; }
void Release3DLutTex() { if (_3dlut != null) { GameObject.DestroyImmediate (_3dlut); _3dlut = null; } }
void Create3DLutTex(int dim) { _3dlut = new Texture3D (dim, dim, dim, TextureFormat.ARGB32, false); _3dlut.wrapMode = TextureWrapMode.Clamp; _3dlut.filterMode = FilterMode.Bilinear; _3dlut.anisoLevel = 0; _3dcolors = _3dlut.GetPixels (); }
void UpdateLut() { UpdateUserLut(); UpdateCurve(); float lutA = GetLutA(); SimplePolyFunc polyToe; SimplePolyFunc polyLinear; SimplePolyFunc polyShoulder; float gammaSpace = 2.2f; float x0 = Mathf.Pow(1.0f/3.0f, gammaSpace); float shoulderBase = .7f; float x1 = Mathf.Pow(shoulderBase, gammaSpace); float gammaHighY = Mathf.Pow(shoulderBase, 1.0f + (lutShoulder) * 1.0f); float y1 = Mathf.Pow(gammaHighY, gammaSpace); float y0 = 0.0f; { float t = x0 / x1; float lin = t * y1; float low = lin * (1.0f-lutToe*.5f); y0 = low; } float dx = x1 - x0; float dy = y1 - y0; float m = 0.0f; if (dx > 0 && dy > 0) m = dy / dx; // linear section, power is 1, slope is m polyLinear.x0 = x0; polyLinear.y0 = y0; polyLinear.A = m; polyLinear.B = 1.0f; polyLinear.signX = 1.0f; polyLinear.signY = 1.0f; polyLinear.logA = Mathf.Log(m); // toe polyToe = polyLinear; polyToe.Initialize(x0, y0, m); float linearW = GetWhitePoint(); { // shoulder, first think about it "backwards" float offsetX = linearW - x1; float offsetY = 1.0f - y1; polyShoulder = polyLinear; polyShoulder.Initialize(offsetX, offsetY, m); // flip horizontal polyShoulder.signX = -1.0f; polyShoulder.x0 = -linearW; // flip vertical polyShoulder.signY = -1.0f; polyShoulder.y0 = 1.0f; } int dim = 32; Color[] newC = new Color[dim * dim * dim]; float oneOverDim = 1.0f / (1.0f * dim - 1.0f); Color normS = NormalizeColor(lutShadows); Color normM = NormalizeColor(lutMidtones); Color normH = NormalizeColor(lutHighlights); float avgS = (normS.r + normS.g + normS.b) / 3.0f; float avgM = (normM.r + normM.g + normM.b) / 3.0f; float avgH = (normH.r + normH.g + normH.b) / 3.0f; // these are magic numbers float liftScale = .1f; float gammaScale = .5f; float gainScale = .5f; float liftR = (normS.r - avgS) * liftScale; float liftG = (normS.g - avgS) * liftScale; float liftB = (normS.b - avgS) * liftScale; float gammaR = Mathf.Pow(2.0f, (normM.r - avgM) * gammaScale); float gammaG = Mathf.Pow(2.0f, (normM.g - avgM) * gammaScale); float gammaB = Mathf.Pow(2.0f, (normM.b - avgM) * gammaScale); float gainR = Mathf.Pow(2.0f, (normH.r - avgH) * gainScale); float gainG = Mathf.Pow(2.0f, (normH.g - avgH) * gainScale); float gainB = Mathf.Pow(2.0f, (normH.b - avgH) * gainScale); float minGamma = .01f; float invGammaR = 1.0f / Mathf.Max(minGamma, gammaR); float invGammaG = 1.0f / Mathf.Max(minGamma, gammaG); float invGammaB = 1.0f / Mathf.Max(minGamma, gammaB); for (int i = 0; i < dim; i++) { for (int j = 0; j < dim; j++) { for (int k = 0; k < dim; k++) { float srcR = (i * 1.0f) * oneOverDim; float srcG = (j * 1.0f) * oneOverDim; float srcB = (k * 1.0f) * oneOverDim; float dstR = EvalFilmicHelper(srcR, lutA, polyToe, polyLinear, polyShoulder, x0, x1, linearW); float dstG = EvalFilmicHelper(srcG, lutA, polyToe, polyLinear, polyShoulder, x0, x1, linearW); float dstB = EvalFilmicHelper(srcB, lutA, polyToe, polyLinear, polyShoulder, x0, x1, linearW); // enable lut if (enableUserLut) { Color c = SampleLutLinear(dstR, dstG, dstB); dstR = c.r; dstG = c.g; dstB = c.b; } dstR = EvalCurveGradingHelper(dstR, liftR, invGammaR, gainR); dstG = EvalCurveGradingHelper(dstG, liftG, invGammaG, gainG); dstB = EvalCurveGradingHelper(dstB, liftB, invGammaB, gainB); if (enableColorGrading) { // saturation float midVal = (dstR + dstG + dstB) / 3.0f; dstR = midVal + (dstR - midVal) * lutSaturation; dstG = midVal + (dstG - midVal) * lutSaturation; dstB = midVal + (dstB - midVal) * lutSaturation; } newC[i + (j * dim) + (k * dim * dim)] = new Color(dstR, dstG, dstB, 1.0f); } } } if (lutTex) DestroyImmediate(lutTex); lutTex = new Texture3D(dim, dim, dim, TextureFormat.ARGB32, false); lutTex.filterMode = FilterMode.Bilinear; lutTex.wrapMode = TextureWrapMode.Clamp; lutTex.hideFlags = HideFlags.DontSave; lutTex.SetPixels(newC); lutTex.Apply(); #if false if (false) { // Instad of doing a single 3D lut, I tried doing this as 3x 1D luts. Or rather, // a single lut with separate curves baked into RGB channels. It wasn't actually faster // do it's disabled. But there are two reasons why in the future it might be useful: // 1. If it turns out that 3x 1D luts are faster on some hardware, it might be worth it. // 2. Updating the 3D LUT is quite slow so you can't change it every frame. If the // parameters need to lerp than the 1D version might be worthwhile. CreateFilmicCurveHelper(lutA, polyToe, polyLinear, polyShoulder, x0, x1, linearW, liftR, invGammaR, gainR, liftG, invGammaG, gainG, liftB, invGammaB, gainB); } #endif }
protected void ConvertBaseTexture() { if (!ValidDimensions(LookupTexture)) { Debug.LogWarning("The given 2D texture " + LookupTexture.name + " cannot be used as a 3D LUT. Pick another texture or adjust dimension to e.g. 256x16."); return; } m_BaseTextureName = LookupTexture.name; int dim = LookupTexture.height; Color[] c = LookupTexture.GetPixels(); Color[] newC = new Color[c.Length]; for (int i = 0; i < dim; i++) { for (int j = 0; j < dim; j++) { for (int k = 0; k < dim; k++) { int j_ = dim - j - 1; newC[i + (j * dim) + (k * dim * dim)] = c[k * dim + i + j_ * dim * dim]; } } } if (m_Lut3D) DestroyImmediate(m_Lut3D); m_Lut3D = new Texture3D(dim, dim, dim, TextureFormat.ARGB32, false); m_Lut3D.hideFlags = HideFlags.HideAndDontSave; m_Lut3D.wrapMode = TextureWrapMode.Clamp; m_Lut3D.SetPixels(newC); m_Lut3D.Apply(); }
public void SetIdentityLut() { int num = 16; Color[] array = new Color[num * num * num]; float num2 = 1f / (1f * (float)num - 1f); for (int i = 0; i < num; i++) { for (int j = 0; j < num; j++) { for (int k = 0; k < num; k++) { array[i + j * num + k * num * num] = new Color((float)i * 1f * num2, (float)j * 1f * num2, (float)k * 1f * num2, 1f); } } } if (this.converted3DLut) { UnityEngine.Object.DestroyImmediate(this.converted3DLut); } this.converted3DLut = new Texture3D(num, num, num, TextureFormat.ARGB32, false); this.converted3DLut.SetPixels(array); this.converted3DLut.Apply(); this.basedOnTempTex = string.Empty; }
/** * Return a volume texture containing a representation of this distance field. */ public Texture3D GetVolumeTexture(int size) { if (Root == null) return null; // upper bound of the distance from any point inside the bounds to the surface. float maxDist = Mathf.Max(Root.bounds.size.x,Root.bounds.size.y,Root.bounds.size.z); float spacingX = Root.bounds.size.x / (float)size; float spacingY = Root.bounds.size.y / (float)size; float spacingZ = Root.bounds.size.z / (float)size; Texture3D tex = new Texture3D (size, size, size, TextureFormat.ARGB32, false); var cols = new Color[size*size*size]; int idx = 0; Color c = Color.black; Vector3 gradient; for (int z = 0; z < size; ++z) { for (int y = 0; y < size; ++y) { for (int x = 0; x < size; ++x, ++idx) { Vector3 samplePoint = Root.bounds.min + new Vector3(spacingX * x + spacingX*0.5f, spacingY * y + spacingY*0.5f, spacingZ * z + spacingZ*0.5f); float distance = DistanceAndGradientAt(samplePoint,out gradient); if (distance >= 0) c.r = distance.Remap(0,maxDist*0.1f,0.5f,1); else c.r = distance.Remap(-maxDist*0.1f,0,0,0.5f); cols[idx] = c; } } } tex.SetPixels (cols); tex.Apply (); return tex; }
void OnDisable () { if (rt) { DestroyImmediate (rt); rt = null; } if (tonemapMaterial) { DestroyImmediate (tonemapMaterial); tonemapMaterial = null; } if (lutTex) { DestroyImmediate(lutTex); lutTex = null; } if (lutCurveTex1D) { DestroyImmediate(lutCurveTex1D); lutCurveTex1D = null; } }