// public bool showSubCube = false; // Start is called before the first frame update void Start() { float subSize = size / detail; subCubes = new CubeBox[detail, detail, detail]; for (int x = 0; x < detail; x++) { for (int y = 0; y < detail; y++) { for (int z = 0; z < detail; z++) { CubeBox cube = Instantiate( cubePrefab, new Vector3(x * subSize + (subSize - size) / 2f, y * subSize + (subSize - size) / 2f, z * subSize + (subSize - size) / 2f), Quaternion.identity, transform ).GetComponent <CubeBox>(); cube.gameObject.SetActive(false); cube.transform.localScale = new Vector3(subSize, subSize, subSize); subCubes[x, y, z] = cube; } } } }
public float computeCubeValueSimple(CubeBox cube) { bool inSphere = Mathf.Pow(cube.transform.position.x - transform.position.x, 2) + Mathf.Pow(cube.transform.position.y - transform.position.y, 2) + Mathf.Pow(cube.transform.position.z - transform.position.z, 2) - (radius * radius) <= 0; if (inSphere) { return(1f); } return(0f); }
// Update is called once per frame void Update() { for (int x = 0; x < detail; x++) { for (int y = 0; y < detail; y++) { for (int z = 0; z < detail; z++) { CubeBox cube = subCubes[x, y, z]; cube.value = 0f; foreach (Sphere sphere in spheres) { // cube.value += sphere.computeCubeValueSimple(cube); cube.value += sphere.computeCubeValueExp(cube); } if (cube.value > threshold) { cube.exist = true; } else { cube.exist = false; } } } } // Remove invisible cube for (int x = 0; x < detail; x++) { for (int y = 0; y < detail; y++) { for (int z = 0; z < detail; z++) { CubeBox cube = subCubes[x, y, z]; if (cube.exist) { bool isCubeSurrounded = false; if (x > 0 && x < detail - 1 && y > 0 && y < detail - 1 && z > 0 && z < detail - 1) { isCubeSurrounded = subCubes[x + 1, y, z].exist && subCubes[x - 1, y, z].exist && subCubes[x, y + 1, z].exist && subCubes[x, y - 1, z].exist && subCubes[x, y, z + 1].exist && subCubes[x, y, z - 1].exist; } if (isCubeSurrounded) { cube.gameObject.SetActive(false); } else { cube.gameObject.SetActive(true); } } else { cube.gameObject.SetActive(false); } } } } }
public float computeCubeValueExp(CubeBox cube) { float d = Vector3.Distance(cube.transform.position, transform.position); return(Mathf.Exp(-d * d / 20f)); }