public void SyncDataBegin() { if (m_texture3D != null) { UnityEngine.Object.Destroy(m_texture3D); m_texture3D = null; } if (m_mesh != null) { UnityEngine.Object.Destroy(m_mesh); m_mesh = null; } // create 3d texture var width = m_summary.width; var height = m_summary.height; var depth = m_summary.depth; var format = (TextureFormat)m_summary.format; m_texture3D = new Texture3D(width, height, depth, format, false); var list = new PinnedList <Color>(m_texture3D.GetPixels()); // instantiate volumeData var volumeData = default(oiVolumeData); volumeData.voxels = list; // kick async copy m_volume.FillData(ref volumeData); // update volume data summary m_volume.GetSummary(ref m_summary); // copy buffer CPU to GPU m_texture3D.SetPixels(list.Array); m_texture3D.Apply(); // create mesh unit scaled cube var position = Vector3.zero; m_mesh = Voxelizer.VoxelMesh.Build(new[] { position }, 1f); }
/// <summary> /// /// </summary> void LoadNoise3dTexture() { // dds loader hard coded for 128x128x128 3d texture // doesn't work with TextureFormat.Alpha8 for soem reason _noiseTexture = new Texture3D(128, 128, 128, TextureFormat.RGBA32, false); _noiseTexture.name = "3D Noise"; TextAsset data = Resources.Load("NoiseVolume") as TextAsset; byte[] bytes = data.bytes; uint height = BitConverter.ToUInt32(data.bytes, 12); uint width = BitConverter.ToUInt32(data.bytes, 16); uint pitch = BitConverter.ToUInt32(data.bytes, 20); uint depth = BitConverter.ToUInt32(data.bytes, 24); uint bitdepth = BitConverter.ToUInt32(data.bytes, 22 * 4); Color[] c = new Color[128 * 128 * 128]; uint index = 128; pitch = (width * bitdepth + 7) / 8; Color m = new Color(0, 0, 0, 0); for (int d = 0; d < 128; ++d) { //index = 128; for (int i = 0; i < 128; ++i) { for (int j = 0; j < 128; ++j) { float v = (bytes[index + j] / 255.0f); c[i + j * 128 + d * 128 * 128] = new Color(v, v, v, v); } index += pitch; } } _noiseTexture.SetPixels(c); _noiseTexture.Apply(); }
public override void SaveAsset() { //for readability int dim = squareResolution; //Slice 3D Render Texture to individual layers RenderTexture[] layers = new RenderTexture[squareResolution]; for (int i = 0; i < squareResolution; i++) { layers[i] = Copy3DSliceToRenderTexture(i); } //Write RenderTexture slices to static textures Texture2D[] finalSlices = new Texture2D[squareResolution]; for (int i = 0; i < squareResolution; i++) { finalSlices[i] = ConvertFromRenderTexture(layers[i], assetTextureFormat); } //Build 3D Texture from 2D slices Texture3D output = new Texture3D(dim, dim, dim, assetTextureFormat, true); output.filterMode = FilterMode.Trilinear; Color[] outputPixels = output.GetPixels(); for (int k = 0; k < dim; k++) { Color[] layerPixels = finalSlices[k].GetPixels(); for (int i = 0; i < dim; i++) { for (int j = 0; j < dim; j++) { outputPixels[i + j * dim + k * dim * dim] = layerPixels[i + j * dim]; } } } output.SetPixels(outputPixels); output.Apply(); string path = "Assets/" + assetName + ".asset"; AssetDatabase.CreateAsset(output, path); Debug.Log("Wrote 3D Texture " + output + " to " + path); }
///<summary> This function returns a Texture3D of Perlin Noise values, generated in GPU ///<returns> ///Returns a Texture3D where each pixel is a ARGB made of noise Value in all components ///</returns> ///<param name = "TextureWidth"> ///Width of texture ///</param> ///<param name = "TextureHeight"> ///Height of texture ///</param> ///<param name = "TextureDepth"> ///Depth of texture ///</param> ///</summary> public static Texture3D generateTexture3D_GPU(int textureWidth, int textureHeight, int textureDepth) { ComputeShader cs = (ComputeShader)Resources.Load("PerlinNoise"); int kernel = cs.FindKernel("NoiseTexture3D"); ComputeBuffer buffer = new ComputeBuffer(textureWidth * textureHeight * textureDepth, sizeof(float)); cs.SetBuffer(kernel, "noiseValues", buffer); cs.SetFloat("noiseScale", NoiseTexture.NoiseScale); cs.SetInts("size", new int[3] { textureWidth, textureHeight, textureDepth }); cs.SetFloats("offset", new float[3] { Offset.x, Offset.y, Offset.z }); cs.Dispatch(kernel, textureWidth / 8, textureHeight / 8, textureDepth / 8); Texture3D noiseTexture = new Texture3D(textureWidth, textureHeight, textureDepth, TextureFormat.ARGB32, false); float[] colors = new float[textureWidth * textureHeight * textureDepth]; buffer.GetData(colors); for (int i = 0; i < textureWidth; i++) { for (int j = 0; j < textureWidth; j++) { for (int k = 0; k < textureWidth; k++) { //See this float sample = colors[i * textureWidth * textureHeight + j * textureHeight + k]; Color color = new Color(sample, sample, sample, 1.0f); noiseTexture.SetPixel(i, j, k, color); } } } noiseTexture.Apply(); buffer.Release(); return(noiseTexture); }
void Save(RenderTexture selectedRenderTexture) { int voxelSize = textureSize.x; RenderTexture[] layers = new RenderTexture[voxelSize]; for (int i = 0; i < voxelSize; i++) { layers[i] = Copy3DSliceToRenderTexture(selectedRenderTexture, i); } Texture2D[] finalSlices = new Texture2D[voxelSize]; for (int i = 0; i < voxelSize; i++) { finalSlices[i] = ConvertFromRenderTexture(layers[i]); } Texture3D output = new Texture3D(voxelSize, voxelSize, voxelSize, TextureFormat.RHalf, true); output.filterMode = FilterMode.Trilinear; Color[] outputPixels = output.GetPixels(); int i_flat = 0; for (int k = 0; k < voxelSize; k++) { Color[] layerPixels = finalSlices[k].GetPixels(); for (int i = 0; i < voxelSize; i++) { for (int j = 0; j < voxelSize; j++) { var col = layerPixels[i + j * voxelSize]; //if (i_flat % 100 == 0) Debug.Log(col); outputPixels[i + j * voxelSize + k * voxelSize * voxelSize] = col; i_flat++; } } } output.SetPixels(outputPixels); output.Apply(); AssetDatabase.CreateAsset(output, resultAssetName); }
public static Texture3D Build(Texture2D myTexture, int row, int column) { d3_d2_volumn_info v_info = new d3_d2_volumn_info(myTexture.width, myTexture.height, row, column); var tex = new Texture3D(v_info.d3_w, v_info.d3_h, v_info.d3_d, TextureFormat.RGBA32, false); tex.wrapMode = TextureWrapMode.Clamp; tex.filterMode = FilterMode.Bilinear; tex.anisoLevel = 0; //int i = 0; Color[] colors = new Color[v_info.d3_w * v_info.d3_h * v_info.d3_d]; float inv = 1f / 255.0f; for (int z = 0; z < v_info.d3_d; z++) { for (int y = 0; y < v_info.d3_h; y++) { for (int x = 0; x < v_info.d3_w; x++) { //convert 3d to 3d coord d3_d2_volumn_info.d2_coord d2c = new d3_d2_volumn_info.d2_coord(-1, -1); d3_d2_volumn_info.d3_coord d3c = new d3_d2_volumn_info.d3_coord(x, y, z); v_info.d3_to_d2_pix_cood(d3c, ref d2c); // if (z == 5) // { // Debug.Log("d3c : "+"x=" + d3c.x+ ", y=" + d3c.y + ", z=" + d3c.z); // Debug.Log("d2c : "+"x=" + d2c.x+ ", y=" + d2c.y); // Debug.Log("2d color : " + myTexture.GetPixel(d2c.x,d2c.y)); // Debug.Log("3d color : "+colors[z* v_info.d3_w *v_info.d3_h + y * v_info.d3_w +x]); // } colors[z * v_info.d3_w * v_info.d3_h + y * v_info.d3_w + x] = myTexture.GetPixel(d2c.x, d2c.y); } } } tex.SetPixels(colors); tex.Apply(); return(tex); }
public static Texture3D Convert(Texture2D temp2DTex) { if (!temp2DTex) { throw new ArgumentNullException("temp2DTex"); } // 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 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."); return(null); } var c = temp2DTex.GetPixels(); var 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]; } } } var result = new Texture3D(dim, dim, dim, TextureFormat.ARGB32, false); result.SetPixels(newC); result.Apply(); return(result); }
void Render() { // float maxValue = 0; // float minValue = float.MaxValue; for (int k = 1; k < N - 1; k++) { for (int j = 1; j < N - 1; j++) { for (int i = 1; i < N - 1; i++) { float v = density[IX(i, j, k)]; // maxValue = v > maxValue ? v : maxValue; // minValue = v < minValue ? v : minValue; colorArray[IX(i, j, k)] = Color.white * v; } } } texture.SetPixels(colorArray); texture.Apply(); }
Texture3D CreateTexture3D(int size) { Color[] colorArray = new Color[size * size * size]; Texture3D texture = new Texture3D(size, size, size, TextureFormat.RGBA32, true); float r = 1.0f / (size - 1.0f); for (int x = 0; x < size; x++) { for (int y = 0; y < size; y++) { for (int z = 0; z < size; z++) { colorArray[x + (y * size) + (z * size * size)] = new Color(0f, 0f, 0f, 0f); } } } texture.SetPixels(colorArray); texture.Apply(); return(texture); }
void resetColor(ref Texture3D tex3D) { int _size = tex3D.width; Color[] colorArray = new Color[_size * _size * _size]; float r = 1.0f / (_size - 1.0f); for (int x = 0; x < _size; x++) { for (int y = 0; y < _size; y++) { for (int z = 0; z < _size; z++) { colorArray[x + (y * _size) + (z * _size * _size)] = new Color(0f, 0f, 0f, 0f); } } } tex3D.SetPixels(colorArray); tex3D.Apply(); }
public static Texture3D GenerateSeedPositionTexture3DFromArray(Color[,,] array) { var tex = new Texture3D(array.GetLength(0), array.GetLength(1), array.GetLength(2), TextureFormat.ARGB32, false) { wrapMode = TextureWrapMode.Clamp, filterMode = FilterMode.Point }; for (int x = 0; x < array.GetLength(0); x++) { for (int y = 0; y < array.GetLength(1); y++) { for (int z = 0; z < array.GetLength(2); z++) { tex.SetPixel(x, y, z, array[x, y, z]); } } } tex.Apply(); return(tex); }
//CREATE TEXTURE3D FROM COLOR ARRAY public static Texture3D CreateTexture3D(Color[] colors, int textureWidth, int textureHeight, int textureDepth) { Texture3D texture = new Texture3D(textureWidth, textureHeight, textureDepth, TextureFormat.RGBA32, true); texture.wrapMode = TextureWrapMode.Clamp; texture.filterMode = FilterMode.Bilinear; texture.anisoLevel = 6; //Debug.Log($"Creating 3D Texture"); // Copy the color values to the texture texture.SetPixels(colors); // Apply the changes to the texture and upload the updated texture to the GPU texture.Apply(); //Debug.Log($"3D Texture created"); return(texture); }
public static void Texture3DFromByteArray(Texture3D texture, byte[] bytes) { for (int x = 0; x < texture.width; x++) { for (int y = 0; y < texture.height; y++) { for (int z = 0; z < texture.depth; z++) { int index = (x + z * texture.width + y * texture.width * texture.depth) * 4; Color pixel = Color.clear; pixel.r = (float)(bytes[index] / 255.0f); pixel.g = (float)(bytes[index + 1] / 255.0f); pixel.b = (float)(bytes[index + 2] / 255.0f); pixel.a = (float)(bytes[index + 3] / 255.0f); texture.SetPixel(x, y, z, pixel); } } } texture.Apply(); }
void Generate() { lut = new Texture3D(lutSize.x, lutSize.y, lutSize.z, TextureFormat.RHalf, true); lut.wrapMode = TextureWrapMode.Clamp; lut.filterMode = FilterMode.Trilinear; int mipmaps = Mathf.RoundToInt(Mathf.Log(lut.width, 2f)); //note: the last mipmap level is skiped (1x1) for (int i = 0; i < mipmaps; i++) { float roughness = i / (float)(mipmaps); int size = Mathf.RoundToInt(Mathf.Pow(2f, (mipmaps - i))); lut.SetPixels(GenerateBlock(Vector3Int.one * size, roughness), i); } lut.Apply(false); Shader.SetGlobalTexture("_ggxLUT", lut); Shader.SetGlobalFloat("_ggxLUT_coordPow", 1f / coordPow); Shader.SetGlobalFloat("_ggxLUT_roughnessPow", 1f / roughnessPow); }
public RenderTexture LoadJson3D_Float4(string filename, out Texture3D text) { var txt = File.ReadAllText(filename); var input = Newtonsoft.Json.JsonConvert.DeserializeObject <Float4Type>(txt); var depth = input.data.f1.GetLength(0); var height = input.data.f1.GetLength(1); var width = input.data.f1.GetLength(2); var textureIn = CreateRenderTexture3D(width, height, depth, RenderTextureFormat.ARGBFloat); Texture3D texture = new Texture3D(width, height, depth, TextureFormat.RGBAFloat, false); RenderTexture.active = textureIn; var pxdata = texture.GetPixelData <float>(0); int index = 0; for (int k = 0; k < depth; ++k) { for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { pxdata[index] = input.data.f1[k, i, j]; ++index; pxdata[index] = input.data.f2[k, i, j]; ++index; pxdata[index] = input.data.f3[k, i, j]; ++index; pxdata[index] = input.data.f4[k, i, j]; ++index; } } ; } texture.Apply(updateMipmaps: false); RenderTexture.active = null; //SetN(new Vector3Int(textureIn.width, textureIn.height, textureIn.volumeDepth)); Blit3D(texture, textureIn); text = texture; return(textureIn); }
private void Convert(Texture2D temp2DTex, string path, ref Texture3D target) { if ((bool)temp2DTex) { int num = temp2DTex.width * temp2DTex.height; num = temp2DTex.height; if (!ValidDimensions(temp2DTex)) { Debug.LogWarning("The given 2D texture " + temp2DTex.name + " cannot be used as a 3D LUT."); basedOnTempTex = string.Empty; } else { 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 ((bool)target) { Object.DestroyImmediate(target); } target = new Texture3D(num, num, num, TextureFormat.ARGB32, false); target.SetPixels(array); target.Apply(); basedOnTempTex = path; } } else { Debug.LogError("Couldn't color correct with 3D LUT texture. Image Effect will be disabled."); } }
/// <summary> /// Creates a 3D texture. Internal use. /// </summary> protected 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.Bilinear; texture3D.wrapMode = TextureWrapMode.Clamp; texture3D.anisoLevel = 1; } else { Debug.LogWarningFormat("[Ibuprogames.Vintage] Texture '{0}' not found in 'Ibuprogames/Vintage/Resources/Textures' folder. Please contact with '*****@*****.**' and send the log file.", texturePathFromResources); this.enabled = false; } return(texture3D); }
// [MenuItem("Textures/ForceFieldTexture3D")] static void CreateTexture3D() { // Configure the texture int size = 32; TextureFormat format = TextureFormat.RGBA32; TextureWrapMode wrapMode = TextureWrapMode.Clamp; // Create the texture and apply the configuration Texture3D texture = new Texture3D(size, size, size, format, false); texture.wrapMode = wrapMode; // Create a 3-dimensional array to store color data Color[] colors = new Color[size * size * size]; // Populate the array so that the x, y, and z values of the texture will map to red, blue, and green colors float inverseResolution = 1.0f / (size - 1.0f); for (int z = 0; z < size; z++) { int zOffset = z * size * size; for (int y = 0; y < size; y++) { int yOffset = y * size; for (int x = 0; x < size; x++) { colors[x + yOffset + zOffset] = new Color(x * inverseResolution, y * inverseResolution, z * inverseResolution, 1.0f); } } } // Copy the color values to the texture texture.SetPixels(colors); // Apply the changes to the texture and upload the updated texture to the GPU texture.Apply(); // Save the texture to your Unity Project // AssetDatabase.CreateAsset(texture, "Assets/ForceFieldTexture3D.asset"); }
void CreateTex(int dim, Texture3D tex3D) { Color[] newC = new Color[dim * dim * dim]; //float oneOverDim = 1.0f / (1.0f * dim - 1.0f); for (int x = 0; x < dim; x++) { for (int y = 0; y < dim; y++) { for (int z = 0; z < dim; z++) { //newC [x + (y * dim) + (z * dim * dim)] = new Color ((x * 1.0f) * oneOverDim, (y * 1.0f) * oneOverDim, (z * 1.0f) * oneOverDim, 1.0f); newC [x + (y * dim) + (z * dim * dim)] = texList [y].GetPixel(x, z); } } } tex3D.SetPixels(newC); tex3D.Apply(); tex3D.wrapMode = TextureWrapMode.Repeat; AssetDatabase.CreateAsset(tex3D, "Assets/Resources/Textures/3D/3dTex_compressed.asset"); MR.material.SetTexture("_texture", tex3D); }
private void Start() { pool = new EffectPool( prefab, transform, effect => { effect.gameObject.SetActive(true); effect.SetFloat(IntensityProperty, currentIntensity); }, effect => effect.gameObject.SetActive(false), 9); colliders = FindObjectsOfType <RainCollider>(); defaultSDF = new Texture3D(1, 1, 1, TextureFormat.ARGB32, false); defaultSDF.name = name; defaultSDF.wrapMode = TextureWrapMode.Clamp; defaultSDF.SetPixel(0, 0, 0, Color.white, 0); defaultSDF.Apply(false); }
// load the texture according to isLog public void IsLoG(bool b) { switch (b) { case true: intensityRegulator = 1f; volumeLoG.SetPixels(_shape3); volumeLoG.Apply(); material.SetTexture("_Volume", volumeLoG); textureColors = _shape3; break; default: intensityRegulator = 0.0125f; volumeGaus.SetPixels(_shapeGauss); volumeGaus.Apply(); material.SetTexture("_Volume", volumeGaus); textureColors = _shapeGauss; break; } }
/// <summary> /// Paste a Texture2D to a tilemap slice. /// </summary> private void SetTexture3DSlice(Texture2D sourceTex2D, Texture3D targetTex3D, int depth) { // Get the color of the source Texture2D Color32[] newColor = sourceTex2D.GetPixels32(); // Used to access each index of the color array int index = 0; // Paste the Texture2D color into the tilemap slice pixel by pixel for (int i = 0; i < targetTex3D.width; i++) { for (int j = 0; j < targetTex3D.height; j++) { targetTex3D.SetPixel(j, i, depth, newColor[index]); index++; } } // Apply the changes to the tilemap texture targetTex3D.Apply(); }
public static Texture3D GetTex(ITextureGenerator texGen, int textureResolution, TextureFormat format = TextureFormat.RGBA32) { Texture3D result = new Texture3D(textureResolution, textureResolution, textureResolution, format, true); Color[] colors = new Color[textureResolution * textureResolution * textureResolution]; for (int x = 0; x < textureResolution; x++) { for (int y = 0; y < textureResolution; y++) { for (int z = 0; z < textureResolution; z++) { colors[x * textureResolution * textureResolution + y * textureResolution + z] = texGen.Sample(new Vector3(x, y, z) / textureResolution); } } } result.SetPixels(colors); result.wrapMode = TextureWrapMode.Repeat; result.Apply(); return(result); }
public static Texture3D CreateTexture3D(uint width, uint height, uint depth, TextureFormat textureFormat, byte[] pixels) { var texture = new Texture3D((int)width, (int)height, (int)depth, /*textureFormat*/ TextureFormat.RGBA32, false); byte[] newPixelData = DirectXTexHelper.Flip3DImage(width, width, width, GrTextureUtils.GetDXGIFormat(textureFormat), pixels); var colours = new Color32[newPixelData.Length / 4]; for (var i = 0; i < newPixelData.Length; i += 4) { // Another Unity quirk. Unity says that it wants either RGBA32 or BGRA32 as a format for volume textures if the BGRA32 format is passed in, so I have to correct for it here and in GrTextureUtils.GetTextureFormat(). //var colour = new Color32(newPixelData[i + 2], newPixelData[i + 1], newPixelData[i + 0], newPixelData[i + 3]); var colour = new Color32(newPixelData[i + 2], newPixelData[i + 1], newPixelData[i + 0], newPixelData[i + 3]); colours[i / 4] = colour; } texture.SetPixels32(colours); texture.Apply(); return(texture); }
//Generate 3D texture private void GenerateVolumeTexture(string name, bool lodLevel, int lod) { _volumeBuffer = new Texture3D(volumeWidth, volumeHeight, volumeDepth, TextureFormat.ARGB32, false); volumeBufferArray.Add(_volumeBuffer); var w = _volumeBuffer.width; var h = _volumeBuffer.height; var d = _volumeBuffer.depth; //Load the textures drawTexture(name, lod.ToString(), lod); var countOffset = (slices.Length - 1) / (float)d; var volumeColors = new Color[w * h * d]; //Load the textures in the array to the volume buffer and pass it to the shader var sliceCount = 0; var sliceCountFloat = 0f; for (int z = 0; z < d; z++) { sliceCountFloat += countOffset; sliceCount = Mathf.FloorToInt(sliceCountFloat); for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { var idx = x + (y * w) + (z * (w * h)); volumeColors[idx] = slices[sliceCount].GetPixelBilinear(x / (float)w, y / (float)h); if (increaseVisiblity) { volumeColors[idx].a *= volumeColors[idx].r; } } } } _volumeBuffer.SetPixels(volumeColors); _volumeBuffer.Apply(); _rayMarchMaterial.SetTexture("_VolumeTex", _volumeBuffer); }
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; var c = LookupTexture.GetPixels(); var 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) { hideFlags = HideFlags.HideAndDontSave, wrapMode = TextureWrapMode.Clamp }; m_Lut3D.SetPixels(newC); m_Lut3D.Apply(); }
private void GenerateVolumeTexture() { // use a bunch of memory! _volumeBuffer = new Texture3D(volumeWidth, volumeHeight, volumeDepth, TextureFormat.ARGB32, false); var w = _volumeBuffer.width; var h = _volumeBuffer.height; var d = _volumeBuffer.depth; // skip some slices if we can't fit it all in var countOffset = (slices.Length - 1) / (float)d; var volumeColors = new Color[w * h * d]; var sliceCount = 0; var sliceCountFloat = 0f; for (int z = 0; z < d; z++) { sliceCountFloat += countOffset; sliceCount = Mathf.FloorToInt(sliceCountFloat); for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { var idx = x + (y * w) + (z * (w * h)); volumeColors[idx] = slices[sliceCount].GetPixelBilinear(x / (float)w, y / (float)h); if (increaseVisiblity) { volumeColors[idx].a *= volumeColors[idx].r; } } } } _volumeBuffer.SetPixels(volumeColors); _volumeBuffer.Apply(); _rayMarchMaterial.SetTexture("_VolumeTex", _volumeBuffer); }
// convert the input RenderTexture to Texture3D public void SaveRenderTex(RenderTexture source, string textureName, int resolution) { // create an array of 2D RenderTextures RenderTexture[] layers = new RenderTexture[resolution]; // slice 3D RenderTexture into this array for (int i = 0; i < resolution; i++) { layers[i] = Copy3DSliceToRenderTexture(source, i, resolution); } // transform the 2D RenderTexture into Texture2D Texture2D[] finalSlices = new Texture2D[resolution]; for (int i = 0; i < resolution; i++) { finalSlices[i] = ConvertFromRenderTexture(layers[i], resolution); } // create a new 3D Texture and fill it with the contents of the slices Texture3D output = new Texture3D(resolution, resolution, resolution, TextureFormat.ARGB32, true); output.filterMode = FilterMode.Trilinear; Color[] outputPixels = output.GetPixels(); for (int k = 0; k < resolution; k++) { Color[] layerPixels = finalSlices[k].GetPixels(); for (int i = 0; i < resolution; i++) { for (int j = 0; j < resolution; j++) { outputPixels[i + j * resolution + k * resolution * resolution] = layerPixels[i + j * resolution]; } } } // save the texture into the resources folder output.SetPixels(outputPixels); output.Apply(); AssetDatabase.CreateAsset(output, "Assets/Resources/" + textureName + ".asset"); }
public void Test() { int size = 256; Texture3D texture = new Texture3D(size, size, size, TextureFormat.ARGB32, false); Color32[] colors = new Color32[size * size * size]; float inverseResolution = 1.0f / (size - 1.0f); for (int z = 0; z < size; z++) { int zOffset = z * size * size; for (int y = 0; y < size; y++) { int yOffset = y * size; for (int x = 0; x < size; x++) { Vector3 v3 = new Vector3(x - size / 2, y - size / 2, z - size / 2); float d = v3.magnitude; if (d > size / 2) { colors[x + yOffset + zOffset] = new Color(0, 0, 0, 0); } else { colors[x + yOffset + zOffset] = new Color(d / size, 0, 0, 1.0f); } } } } // Copy the color values to the texture texture.SetPixels32(colors); // Apply the changes to the texture and upload the updated texture to the GPU texture.Apply(); // Save the texture to your Unity Project AssetDatabase.CreateAsset(texture, "Assets/Example3DTexture.asset"); }
// this function creates a texture3d from an array of texture2Ds public Texture3D Create3DTexture(Texture2D[] textures) { int width = Mathf.ClosestPowerOfTwo(textures[0].width); int height = Mathf.ClosestPowerOfTwo(textures[0].height); int depth = Mathf.ClosestPowerOfTwo(textures.Length); Color c = Color.white; Texture2D tex2D = new Texture2D(textures[0].width, textures[0].height); Texture3D tex3D = new Texture3D(width, height, depth, TextureFormat.RGB24, false); tex3D.wrapMode = TextureWrapMode.Repeat; tex3D.filterMode = FilterMode.Point; tex3D.anisoLevel = 0; Color[] cols = new Color[width * height * depth]; int ix = 0; for (int z = 0; z < depth; z++) { tex2D = textures[z]; for (int x = 0; x < width; x++) { int xindex = (int)((float)x / (float)width) * tex2D.width; for (int y = 0; y < height; y++) { int yindex = (int)((float)y / (float)height) * tex2D.height; c = tex2D.GetPixel(xindex, yindex); cols[ix++] = c; } } } tex3D.SetPixels(cols); tex3D.Apply(); return(tex3D); }