コード例 #1
0
        public void LogDistances()
        {
            // No debugging if the volume is > 8^3 as thats more than 512 entries!
            if (sdfData.sdfTexture.width * sdfData.sdfTexture.height * sdfData.sdfTexture.depth > 512)
            {
                return;
            }

            float[]    data = VolumeComputeMethods.ExtractVolumeFloatData(sdfData.sdfTexture, volumeComputeMethodsShader);
            Vector3Int dim  = new Vector3Int(sdfData.sdfTexture.width, sdfData.sdfTexture.height, sdfData.sdfTexture.depth);
            string     log  = $"{this.name}  Dimension: {dim}  MaxLength: {sdfData.maxDistance} Mag: {bounds.size.magnitude}\n";

            for (int z = 0; z < sdfData.sdfTexture.depth; z++)
            {
                for (int y = 0; y < sdfData.sdfTexture.height; y++)
                {
                    for (int x = 0; x < sdfData.sdfTexture.width; x++)
                    {
                        log += string.Format("{0:F6}, ", data[z * dim.y * dim.x + y * dim.x + x] * bounds.size.magnitude);
                    }
                    log += "\n";
                }
                log += "\n";
            }

            Debug.Log(log);
        }
コード例 #2
0
        public void BakeHalfSizeTest()
        {
            SDFData sdfmip = ScriptableObject.CreateInstance <SDFData>();

            sdfmip.bounds     = sdfData.bounds;
            sdfmip.voxelSize  = sdfData.voxelSize * 2;
            sdfmip.dimensions = new Vector3Int(sdfData.dimensions.x / 2, sdfData.dimensions.y / 2, sdfData.dimensions.z / 2);

            float minAxis = Mathf.Min(sdfmip.bounds.size.x, Mathf.Min(sdfmip.bounds.size.y, sdfmip.bounds.size.z));

            sdfmip.nonUniformScale = new Vector3(sdfmip.bounds.size.x / minAxis, sdfmip.bounds.size.y / minAxis, sdfmip.bounds.size.z / minAxis);


            // Save sdf
            string suggestedName = "sdfData_" + this.name + "_HalfBake";
            string path          = EditorUtility.SaveFilePanelInProject("Save As...", suggestedName, "asset", "Save the SDF Data");

            if (string.IsNullOrEmpty(path))
            {
                if (EditorUtility.DisplayDialog("Error", "Path was invalid, retry?", "ok", "cancel"))
                {
                    path = EditorUtility.SaveFilePanelInProject("Save As...", suggestedName, "asset", "Save the SDF Data");
                }

                if (string.IsNullOrEmpty(path))
                {
                    return;
                }
            }

            // GetDistances from srcVolume
            float[]    distances = VolumeComputeMethods.ExtractVolumeFloatData(sdfData.sdfTexture, volumeComputeMethodsShader);
            Vector3Int dim       = new Vector3Int(sdfData.sdfTexture.width, sdfData.sdfTexture.height, sdfData.sdfTexture.depth);

            // Get half size data
            Color[] colorBuffer = new Color[distances.Length / 2 / 2 / 2];

            int index = 0;

            for (int z = 0; z < sdfData.sdfTexture.depth; z += 2)
            {
                for (int y = 0; y < sdfData.sdfTexture.height; y += 2)
                {
                    for (int x = 0; x < sdfData.sdfTexture.width; x += 2)
                    {
                        colorBuffer[index++] = new Color(distances[z * dim.y * dim.x + y * dim.x + x], 0f, 0f, 0f);
                    }
                }
            }


            // Create Texture3D and set name to filename of sdfData
            Texture3D newTex = new Texture3D(sdfmip.dimensions.x, sdfmip.dimensions.y, sdfmip.dimensions.z, TextureFormat.RHalf, false);

            newTex.name = System.IO.Path.GetFileNameWithoutExtension(path);
            newTex.SetPixels(colorBuffer);
            newTex.Apply();

            sdfmip.sdfTexture  = newTex;
            sdfmip.maxDistance = sdfData.maxDistance; // TODO - IS this still constant?

            EditorUtility.SetDirty(sdfmip);

            //create it
            AssetDatabase.CreateAsset(sdfmip, path);
            AssetDatabase.AddObjectToAsset(newTex, sdfmip);

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }