public static void BuildBetterVoxels(ComputeShader voxelizer, GPUVoxelData data) { var kernel = new Kernel(voxelizer, kBetterKernelKey); voxelizer.SetBuffer(kernel.Index, kVoxelBufferKey, data.Buffer); voxelizer.Dispatch(kernel.Index, data.Width / (int)kernel.ThreadX + 1, data.Height / (int)kernel.ThreadY + 1, data.Depth / (int)kernel.ThreadZ + 1); }
static RenderTexture CreateTexture3D(GPUVoxelData data, RenderTextureFormat format, FilterMode filterMode) { var texture = new RenderTexture(data.Width, data.Height, data.Depth, format, RenderTextureReadWrite.Default); texture.dimension = TextureDimension.Tex3D; texture.volumeDepth = data.Depth; texture.enableRandomWrite = true; texture.filterMode = filterMode; texture.wrapMode = TextureWrapMode.Clamp; texture.Create(); return(texture); }
public static RenderTexture BuildTexture3D(ComputeShader voxelizer, GPUVoxelData data, Texture2D texture, RenderTextureFormat format, FilterMode filterMode) { var volume = CreateTexture3D(data, format, filterMode); var kernel = new Kernel(voxelizer, kTextureKernelKey); voxelizer.SetBuffer(kernel.Index, kVoxelBufferKey, data.Buffer); voxelizer.SetTexture(kernel.Index, kVoxelTextureKey, volume); voxelizer.SetTexture(kernel.Index, kColorTextureKey, texture); voxelizer.Dispatch(kernel.Index, data.Width / (int)kernel.ThreadX + 1, data.Height / (int)kernel.ThreadY + 1, data.Depth / (int)kernel.ThreadZ + 1); return(volume); }
//[SerializeField] //Texture2D texture; private void Start() { _Mesh = _GameObject.GetComponent <MeshFilter>().sharedMesh; //texture = new Texture2D(256, 256); //Color[] colors = new Color[256 * 256]; //texture.SetPixels(colors); int maxLengthPow2 = Mathf.NextPowerOfTwo((int)Mathf.Max(_Mesh.bounds.size.x, Mathf.Max(_Mesh.bounds.size.y, _Mesh.bounds.size.z))); Vector3 boundsSize = new Vector3(maxLengthPow2, maxLengthPow2, maxLengthPow2); Bounds bounds = new Bounds(Vector3.zero, boundsSize); //make sure the unit isn't float if (resolution > maxLengthPow2) { resolution = (int)maxLengthPow2; } //create voxelData VoxelSystem.GPUVoxelData data = VoxelSystem.GPUVoxelizer.Voxelize( voxelizer, // ComputeShader (Voxelizer.compute) _Mesh, // a target mesh bounds, // custom bounds resolution, // # of voxels for largest AABB bounds false, // flag to fill in volume or not; if set flag to false, sample a surface only true ); VoxelSystem.Voxel_t[] voxels = data.GetData(); //Make the voxeldata start from 0 instead of -extents for (int i = 0; i < voxels.Length; ++i) { voxels[i].position += bounds.extents; } //TODO: when constructing drawing the svo on a certain level, the positions are not corrent //This is because the position(morton) is the first child that has data right now, while it should be the leftbottomfromt position of the entire voxel //So change it so that you don't just have an ignore morton or 0's, but actually calculate the position of each voxel List <ulong> mortons; EncodeVoxels(voxels, (int)bounds.extents.x, out mortons); // need to release a voxel buffer data.Dispose(); //put the data in a svo SVOConstructor svoc = GetComponent <SVOConstructor>(); svoc.Construct(_GameObject.name, voxels.Length, (uint)bounds.extents.x, mortons.ToArray()); }
public static RenderTexture BuildTexture3D(ComputeShader voxelizer, GPUVoxelData data, RenderTextureFormat format, FilterMode filterMode) { return(BuildTexture3D(voxelizer, data, Texture2D.whiteTexture, format, filterMode)); }
public static Mesh Build(GPUVoxelData data, bool useUV = false) { var vertices = new List <Vector3>(); var uvs = new List <Vector2>(); var triangles = new List <int>(); var normals = new List <Vector3>(); var centers = new List <Vector4>(); var unit = data.UnitLength; var up = Vector3.up * unit; var hup = up * 0.5f; var hbottom = -hup; var right = Vector3.right * unit; var hright = right * 0.5f; var left = -right; var hleft = left * 0.5f; var forward = Vector3.forward * unit; var hforward = forward * 0.5f; var back = -forward; var hback = back * 0.5f; var voxels = data.GetData(); for (int i = 0, n = voxels.Length; i < n; i++) { var v = voxels[i]; if (v.flag) { // back CalculatePlane( vertices, normals, centers, uvs, triangles, v, useUV, hback, right, up, Vector3.back ); // right CalculatePlane( vertices, normals, centers, uvs, triangles, v, useUV, hright, forward, up, Vector3.right ); // forward CalculatePlane( vertices, normals, centers, uvs, triangles, v, useUV, hforward, left, up, Vector3.forward ); // left CalculatePlane( vertices, normals, centers, uvs, triangles, v, useUV, hleft, back, up, Vector3.left ); // up CalculatePlane( vertices, normals, centers, uvs, triangles, v, useUV, hup, right, forward, Vector3.up ); // down CalculatePlane( vertices, normals, centers, uvs, triangles, v, useUV, hbottom, right, back, Vector3.down ); } } var mesh = new Mesh(); mesh.indexFormat = IndexFormat.UInt32; mesh.vertices = vertices.ToArray(); mesh.uv = uvs.ToArray(); mesh.normals = normals.ToArray(); mesh.tangents = centers.ToArray(); mesh.SetTriangles(triangles.ToArray(), 0); mesh.RecalculateBounds(); return(mesh); }
public static Mesh Build(GPUVoxelData data, bool useUV = false) { return(VoxelMesh.Build(data.GetData(), data.UnitLength, useUV)); }