public LevelDataSystem()
        {
            var levelGo = new GameObject("Level Volume", typeof(VoxelVolume));

            levelGo.name = "Level Volume";
            _levelVolume = levelGo.GetComponent <VoxelVolume>();
        }
Exemplo n.º 2
0
        public MetaModel(GameObject parent)
            : base(parent)
        {
            volume = new VoxelVolumeSphere(Scene.voxelManager, 3f);

            IsStatic = true;

            grabable = false;
        }
Exemplo n.º 3
0
    public override async void _Ready()
    {
        var   voxelSize = 0.125f;
        float r         = 0.02f;
        float g         = 0.25f;
        float b         = 0.9f;

        var manager = this.GetNode("/root/GlobalGameSystemsManager") as GlobalGameSystemsManager;
        var mat     = new ShaderMaterial();

        mat.SetShader(ResourceLoader.Load(@"res://assets/shaders/point_shader_test.shader") as Shader);

        mat.SetShaderParam("albedo", new Color(r, g, b, 1));
        mat.SetShaderParam("voxelSize", voxelSize);

        var cam      = GetViewport().GetCamera();
        var position = cam.GetGlobalTransform().origin;

        mat.SetShaderParam("camera_pos", position);

        var screenRes = GetViewport().Size;
        var screenPos = GetViewport().GetVisibleRect().Position;

        GD.Print($"Screen Res: {screenRes}");
        mat.SetShaderParam("screen_size", screenRes);
        mat.SetShaderParam("viewport_pos", screenPos);
        MeshInstance m = this.GetChild(3) as MeshInstance;



        Console.WriteLine("LAYER: {0}", Convert.ToString(m.Layers, 2));
        VoxelVolume basicVoxel = new VoxelVolume(new NoisePopulator(), 256, 1024, 1024, 64, voxelSize * 2.0f, mat);

        // VoxelTypes s = basicVoxel[1,2,3];
        // Vector3 a = new Vector3(1,0,1);
        // Console.WriteLine("Vector before {0}",a);
        // vecTest(a);
        // Console.WriteLine("Vector after {0}",a);
        this.AddChild(basicVoxel);

        material = mat;
        GetViewport().Connect("size_changed", this, nameof(ScreenResChanged));
    }
Exemplo n.º 4
0
 public VoxelGenerator(VoxelSettings settings)
 {
     this.settings = settings;
     Volume        = new VoxelVolume(settings);
 }
    public static Mesh Evaluate(IBlackBox phenome, VoxelVolume volume, out EvaluationInfo evaluationInfo)
    {
        var processedOutput = new float[volume.width, volume.height, volume.length];
        var cleanOutput = new float[volume.width, volume.height, volume.length];
        var distanceToCenter = new float[volume.width, volume.height, volume.length];
        var minOutputValue = 1f;
        var maxOutputValue = -1f;

        float r = 0, g = 0, b = 0;

        for (int x = 0; x < volume.width; x++)
        {
            for (int y = 0; y < volume.height; y++)
            {
                for (int z = 0; z < volume.length; z++)
                {
                    AssingInput(phenome, volume, x, y, z);

                    // activate phenome
                    phenome.Activate();

                    // store output values
                    ISignalArray outputArr = phenome.OutputSignalArray;
                    processedOutput[x, y, z] = phenome.OutputSignalArray.Length >= 4 ? (float)outputArr[3] : (float)outputArr[0];
                    cleanOutput[x, y, z] = processedOutput[x, y, z];

                    if (phenome.OutputSignalArray.Length >= 4)
                    {
                        r += (float)outputArr[0];
                        g += (float)outputArr[1];
                        b += (float)outputArr[2];
                    }

                    // store min and max output values
                    if (processedOutput[x, y, z] < minOutputValue)
                        minOutputValue = processedOutput[x, y, z];
                    if (processedOutput[x, y, z] > maxOutputValue)
                        maxOutputValue = processedOutput[x, y, z];

                    // Clamps the final shape within a sphere
                    //distanceToCenter[x, y, z] = DistanceFunctions.DistanceToCenter(x, y, z, volume);
                    //if (DistanceFunctions.DistanceToCenter(x, y, z, volume) > -0.5f)
                    //{
                    //    cleanOutput[x, y, z] = -1;
                    //    processedOutput[x, y, z] = -1;
                    //}

                    // border should have negative values
                    if (x == 0 || x == volume.width - 1 || y == 0 || y == volume.height - 1 || z == 0 || z == volume.length - 1)
                        processedOutput[x, y, z] = -1f;
                }
            }
        }

        r /= volume.width*volume.width*volume.width;
        g /= volume.width*volume.width*volume.width;
        b /= volume.width*volume.width*volume.width;
        artefactColor = new Color((r + 1f) / 2f, (g + 1f) / 2f, (b + 1f) / 2f);
        //Debug.Log(artefactColor);

        //Debug.Log("Output in range [" + minOutputValue + ", " + maxOutputValue + "]");

        // TODO: need to find a workaround for this. It results in cubes. In multiplayer we don't want to spawn cubes as seeds.
        // Either save network to disk, look and see what's wrong or maybe if this happens on the client it could request another regeneration.
        if (Mathf.Approximately(minOutputValue, maxOutputValue))
        {
            //Debug.LogWarning("All output values are the same! Min equals max!");
        }

        for (int index00 = 1; index00 < processedOutput.GetLength(0) - 1; index00++)
            for (int index01 = 1; index01 < processedOutput.GetLength(1) - 1; index01++)
                for (int index02 = 1; index02 < processedOutput.GetLength(2) - 1; index02++)
                {
                    // if initially, border value at 0,0,0 was maxFill, we invert all values so minFill is on the border
                    if (Mathf.Approximately(cleanOutput[0, 0, 0], maxOutputValue))
                        processedOutput[index00, index01, index02] = minOutputValue + (maxOutputValue - processedOutput[index00, index01, index02]);
                    // based on a threshold ( middle value between min and max) we change the value to either 0 or 1. This give a blocky look instead of smooth details
                    processedOutput[index00, index01, index02] = processedOutput[index00, index01, index02] < minOutputValue + (maxOutputValue - minOutputValue) / 2f ? 0f : 1f;
                }

        // fill evaluation info struct with data
        evaluationInfo = new EvaluationInfo();
        evaluationInfo.cleanOutput = cleanOutput.Clone() as float[,,];
        evaluationInfo.processedOutput = processedOutput.Clone() as float[,,];
        evaluationInfo.distanceToCenter = distanceToCenter.Clone() as float[,,];
        evaluationInfo.minOutputValue = minOutputValue;
        evaluationInfo.maxOutputValue = maxOutputValue;

        // Apply marching cubes on processed output and return Mesh
        Profiler.BeginSample("MarchingCubes");
        var mesh = MarchingCubes.CreateMesh(processedOutput);
        Profiler.EndSample();

        return mesh;
    }
    private static void AssingInput(IBlackBox phenome, VoxelVolume volume, int x, int y, int z)
    {
        ISignalArray inputArr = phenome.InputSignalArray;
        // input values for x,y,z coord in [-1, 1] range
        inputArr[0] = Mathf.Abs((float)x / (volume.width - 1) * 2 - 1);
        inputArr[1] = Mathf.Abs((float)y / (volume.height - 1) * 2 - 1);
        inputArr[2] = Mathf.Abs((float)z / (volume.length - 1) * 2 - 1);

        var sphereDistance = DistanceFunctions.SphereDistance(x, y, z, volume, volume.width / 4f, new Vector3(volume.width / 2f, volume.height / 2f, volume.length / 2f));
        var boxSize = new Vector3(6, volume.height / 6f, volume.length / 5f);
        var boxDistance = DistanceFunctions.BoxDistance(x, y, z, volume, boxSize, new Vector3(volume.width / 2f, volume.height / 2f, volume.length / 2f));

        switch (DefaultInputType)
        {
            case InputType.Sphere:
                inputArr[3] = sphereDistance;
                break;
            case InputType.Box:
                inputArr[3] = boxDistance;
                break;
            case InputType.Combined:
                inputArr[3] = Mathf.Min(sphereDistance, boxDistance);
                break;
            case InputType.DistanceToCenter:
                inputArr[3] = DistanceFunctions.DistanceToCenter(x, y, z, volume);
                break;
        }
    }
Exemplo n.º 7
0
 static void Main(string[] args)
 {
     if (args.Length < 2)
     {
         Console.WriteLine("not enough args - voxelbox [file] [out]");
     }
     else
     {
         Console.WriteLine("voxelbox.net v1");
         using (FileStream stream = new FileStream(args[0], FileMode.Open))
         {
             byte[] voxelData = Vox2Rbv.ReadFile(stream);
             if (voxelData == null)
             {
                 return;
             }
             VoxelModel voxelModel = new VoxelRaster().CreateVBFromVoxels(voxelData);
             using (FileStream outStream = new FileStream(args[1], FileMode.Create))
                 using (StreamWriter outStreamWriter = new StreamWriter(outStream))
                 {
                     List <VoxelColor>  colors  = voxelModel.colors;
                     List <VoxelVolume> volumes = voxelModel.volumes;
                     if (colors.Count > 34)
                     {
                         Console.WriteLine("format does not support > 34 materials");
                         return;
                     }
                     outStreamWriter.Write(IntToBase36(colors.Count));
                     for (int i = 0; i < colors.Count; i++)
                     {
                         int r = (int)Math.Round(colors[i].r / 32d);
                         int g = (int)Math.Round(colors[i].g / 32d);
                         int b = (int)Math.Round(colors[i].b / 32d);
                         outStreamWriter.Write("" + r + g + b);
                     }
                     bool singleVoxels = false;
                     for (int i = 0; i < volumes.Count; i++)
                     {
                         VoxelVolume vol = volumes[i];
                         char        x1  = IntToBase36(vol.x1);
                         char        y1  = IntToBase36(vol.y1);
                         char        z1  = IntToBase36(vol.z1);
                         char        x2  = IntToBase36(vol.x2);
                         char        y2  = IntToBase36(vol.y2);
                         char        z2  = IntToBase36(vol.z2);
                         char        mat = IntToBase36(vol.mat);
                         if (vol.x1 == vol.x2 && vol.y1 == vol.y2 && vol.z1 == vol.z2 && singleVoxels == false)
                         {
                             outStreamWriter.Write("Z");
                             singleVoxels = true;
                         }
                         if (!singleVoxels)
                         {
                             outStreamWriter.Write("" + x1 + y1 + z1 + x2 + y2 + z2 + mat);
                         }
                         else
                         {
                             outStreamWriter.Write("" + x1 + y1 + z1 + mat);
                         }
                     }
                     Console.WriteLine("done");
                 }
         }
     }
 }