It is currently Sat Aug 22, 2020 4:58 am


All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Creating coloredCubes terrain from code in unity3d
PostPosted: Thu Mar 10, 2016 12:39 pm 
User avatar

Joined: Thu Mar 10, 2016 12:19 pm
Posts: 1
Hi,

Firstly thanks :) Having a ton of fun so far, great software.

My question is pretty much the subject....

How do I go about making a colored Cubes terrain in code?
I tried messing with the smooth terrain procedural script, but I had some issue, got to the api docs and tried another set of things but I have this error now where it complains that it's getting a bad object instance.

Point is I don't know much about voxels, and I really want to learn, I just don't get the setup process that well, if someone could help me with that it would be amazing.

I'll post here if I come right, following the api docs and working between the maze example and the terrain example, maybe I'll figure something out.... anyways happy days. :D

Thanks
MJ

_________________
I don't think of myself as a rebel; I just say what I think.


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Creating coloredCubes terrain from code in unity3d
PostPosted: Thu Mar 10, 2016 3:31 pm 

Joined: Tue Apr 08, 2014 5:10 pm
Posts: 124
I have already done this before, however, I weren't using Cubiquity, but PolyVox directly in a C++ Native DLL - Unity plugin

Polyvox have the concept of Material Identifier, which can be a color. Then, after using CubicSurfaceExtractor, the resulting PolyVox::SurfaceMesh will have the color properly propagated to the faces. You will have to encode and return this to Unity using P/Invoke. You will build a Unity Mesh object where you will assign the Vertices and Indices array, as well as the Colors or Colors32 array.

Then, any shader that supports vertex colors will suffice for rendering.

As of Cubiquity, I don't know, but I believe it is possible.

You will have to generate your voxel volume data using algorithm you invent yourself.


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Creating coloredCubes terrain from code in unity3d
PostPosted: Thu Mar 10, 2016 11:28 pm 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
You should probably focus only on the Maze example, as anything with smooth terrain volumes is different and more complicated.

There are really two aspects. Firstly, how to set the color of a specfic voxel. The example contains code like this:

Code:
QuantizedColor red = new QuantizedColor(255, 0, 0, 255);
.
.
.
data.SetVoxel(x, y, z, red);


The actual process of setting the color of a voxel is always the same (using setVoxel()).

Secondly, for a given voxel position(x,y,z) you need to decide what color you want it to be. This is highly dependant on you application and involves you coding some logic based on what you want your map to look like. For example, the checkerboard pattern on the floor is created by:

Code:
if((tileXPos + tileZPos) % 2 == 1)
{
   tileColor = blue;
}
else
{
   tileColor = white;
}


Then later we actually use 'tileColor' to set the color with 'setVoxel()':

Code:
data.SetVoxel(x, y, z, tileColor);


Start of by just making small changes to the existing Maze example. Change some colors, add one new voxel, etc. Don't try to create a new scene, etc, because that is more complicated and requires more setup.


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Creating coloredCubes terrain from code in unity3d
PostPosted: Wed Sep 21, 2016 4:40 am 
User avatar

Joined: Tue Sep 20, 2016 8:51 pm
Posts: 6
Location: Utah
Hi , just hoping someone will pick this up....

I've used simplex before i'm not a big fan of perlin simplex provides a better seed... everything is alittle more random .....


I made my own engine and dumped it because I wanted phsyics and water and did everything before I did that...... had ore everything anyway just like minecraft crafting system sad shame I cannot dig back into it.....

Anyway I now understand the color chart but how do I use the simplex generator with voxels?!

It cast me back a float.... I could round these off..... i guess..?


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Creating coloredCubes terrain from code in unity3d
PostPosted: Wed Sep 21, 2016 9:27 pm 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
12aithe wrote:
It cast me back a float.... I could round these off..... i guess..?


You probably just want to use a threshold. I assume Simplex noise returns a value between -1.0 and +1.0 (or maybe 0.0 to 1.0?), so just choose a threshold value (e.g. 0.5) and set everything above this to solid and everything below to empty. The choice of threshold will affect how many empty vs. solid voxels you have.


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Creating coloredCubes terrain from code in unity3d
PostPosted: Sat Sep 24, 2016 12:10 am 
User avatar

Joined: Tue Sep 20, 2016 8:51 pm
Posts: 6
Location: Utah
RandomProceduralCubeScript

Ok.. so i've seen post on here and I had to fix it... if you're going to want more things on you're style try modifying the altitude... I even tryed using 3.142 pi* and it gave a really trippy height effect.....

But I got what I want it seems about right ..... mmm I would like it to flatten at the top alittle more but thats ok ......... also if you want lower ground just look at where the green and the brown < are and change those to larger values... also think randomizer for colors to create something pretty unique ;)

Here's the image - Deviant Art
http://fav.me/dairuby

Here's the script...
Ps. This script opens public properties attached to the terrain volume .... I will make it data importable .... inless it's already done ;) ... I'm sure You guys are going to be adding ao and lighting ....

If not good adventure just not sure how i'm going to due a few things.... like water ...... ?
I would have to get into the sdk....

But right now i'm heading towards the brushes.... that will be complicated 0.0....

Not sure what i'm going to use to sweep threw with... *randomize brushes*

Blah blah , but here you go m8's I kinda dread leaving this like this ... though .... you do need to look into what is there... I still have the slightest clue there is alot of goodies though.....

Code:
using UnityEngine;
using System.Collections;

using Cubiquity;

public class ProceduralColoredCubes : MonoBehaviour {

   public int width = 128;
   public int height = 32;
   public int depth = 128;


   // Use this for initialization
   void Start () {

      // FIXME - Where should we delete this?
      /// [DoxygenSnippet-CreateEmptyTerrainVolumeData]
      //Create an empty TerrainVolumeData with dimensions width * height * depth
      ColoredCubesVolumeData data = VolumeData.CreateEmptyVolumeData<ColoredCubesVolumeData>(new Region(0, 0, 0, width-1, height-1, depth-1));
      /// [DoxygenSnippet-CreateEmptyTerrainVolumeData]

      ColoredCubesVolume volume = GetComponent<ColoredCubesVolume>();
      ColoredCubesVolumeRenderer volumeRenderer = GetComponent<ColoredCubesVolumeRenderer>();


      volume.data = data;

      // Defines colors for each cubic square.
      QuantizedColor green = new QuantizedColor(18, 115, 38, 255);
      QuantizedColor brown = new QuantizedColor(115, 87, 18, 255);
      QuantizedColor gray = new QuantizedColor(127, 127, 127, 255);
      QuantizedColor white = new QuantizedColor(255, 255, 255, 255);

      float rockScale = 0.01F;      
   

      // Iterate over every voxel of our volume
      for (int z = 0; z < depth; z++)
      {
         for (int y = height - 1; y > 0; y--)
         {
            for (int x = 0; x < width; x++)
            {
               float sampleX = (float)x * rockScale;
               float sampleY = (float)y * rockScale;
               float sampleZ = (float)z * rockScale;


               float simplexNoiseValue = SimplexNoise.Noise.Generate(sampleX, sampleY, sampleZ);


               float altitude = (float)(y + 0.5f) / (float)height;

               // Map the altitude to the range -1.0 to +1.0..


               altitude = (altitude * simplexNoiseValue) - 0.002F;

               simplexNoiseValue -= altitude;

               //simplexNoiseValue *= 0.5f;
               simplexNoiseValue = Mathf.Clamp(simplexNoiseValue, -0.005f, 0.05f);

               simplexNoiseValue *= 15;

               int simplexCubicValue = Mathf.RoundToInt (simplexNoiseValue);

               if(y < 12)
               {
                  // Add to bedrock material channel.
                  data.SetVoxel(x , y , z , green);
               }
               if(y < 10)
               {
                  // Add to bedrock material channel.
                  data.SetVoxel(x , y , z , brown);
               }

               data.SetVoxel(x* simplexCubicValue , y * simplexCubicValue , z* simplexCubicValue , gray);
            }

         }

      }//End the loop of voxeling
   
   }
   

}







I'm going to be frank it's just complicated....

The simplex formula has different return functions...

either by xyz

or individual ....

Since SetVoxel is only got args x,y,z and not float....

I'm just confused.

Is there a way you could cook up a quick script?

If I find a method i will edit... but i've been scratching at this one..... since I need a rounded float .....
it's alittle more complicated.... using set voxel with quant color.... not levels of the "biome"

I know its no easy process i've done it before but I used a whole different approach .....

Btw I appreciate any feedback I know using unity's built in tripoligy was complicated enough...

But now i cannot use it again after figureing out how much quicker this renders.


EDIT :

woooh kay atleast I got them moving but nope.... it looks like a cube dance party going on ...

Image of what happened deviant art
http://fav.me/dair93j

Here's the code
Code:
using UnityEngine;
using System.Collections;

using Cubiquity;

public class ProceduralColoredCubes : MonoBehaviour {

   public int width = 128;
   public int height = 32;
   public int depth = 128;


   // Use this for initialization
   void Start () {

      // FIXME - Where should we delete this?
      /// [DoxygenSnippet-CreateEmptyTerrainVolumeData]
      //Create an empty TerrainVolumeData with dimensions width * height * depth
      ColoredCubesVolumeData data = VolumeData.CreateEmptyVolumeData<ColoredCubesVolumeData>(new Region(0, 0, 0, width-1, height-1, depth-1));
      /// [DoxygenSnippet-CreateEmptyTerrainVolumeData]

      ColoredCubesVolume volume = GetComponent<ColoredCubesVolume>();
      ColoredCubesVolumeRenderer volumeRenderer = GetComponent<ColoredCubesVolumeRenderer>();


      volume.data = data;

      // Defines colors for each cubic square.
      QuantizedColor green = new QuantizedColor(18, 115, 38, 255);
      QuantizedColor brown = new QuantizedColor(115, 87, 18, 255);
      QuantizedColor gray = new QuantizedColor(127, 127, 127, 255);
      QuantizedColor white = new QuantizedColor(255, 255, 255, 255);

      float rockScale = 0.5F;      
   

      // Iterate over every voxel of our volume
      for (int z = 0; z < depth; z++)
      {
         for (int y = height - 1; y > 0; y--)
         {
            for (int x = 0; x < width; x++)
            {
               float sampleX = (float)x * rockScale;
               float sampleY = (float)y * rockScale;
               float sampleZ = (float)z * rockScale;


               float simplexNoiseValue = SimplexNoise.Noise.Generate(sampleX, sampleY, sampleZ);


               float altitude = (float)(y + 1) / (float)height;

               // Map the altitude to the range -1.0 to +1.0...
               altitude = (altitude * 0.5f) - 0.5f;

               simplexNoiseValue -= altitude;


               simplexNoiseValue *= 255;

               int simplexCubicValue = Mathf.RoundToInt (simplexNoiseValue);

               data.SetVoxel(x + simplexCubicValue, y+ simplexCubicValue, z+ simplexCubicValue, green);
            }

         }

      }//End the loop of voxeling
   
   }
   

}


EDIT :

So the wierd thing is It starts to not fly around but instead it seems to implode a random noise

The box gets created but inside of the box you can see the simplex and it kinda works.
Code:
using UnityEngine;
using System.Collections;

using Cubiquity;

public class ProceduralColoredCubes : MonoBehaviour {

   public int width = 128;
   public int height = 32;
   public int depth = 128;


   // Use this for initialization
   void Start () {

      // FIXME - Where should we delete this?
      /// [DoxygenSnippet-CreateEmptyTerrainVolumeData]
      //Create an empty TerrainVolumeData with dimensions width * height * depth
      ColoredCubesVolumeData data = VolumeData.CreateEmptyVolumeData<ColoredCubesVolumeData>(new Region(0, 0, 0, width-1, height-1, depth-1));
      /// [DoxygenSnippet-CreateEmptyTerrainVolumeData]

      ColoredCubesVolume volume = GetComponent<ColoredCubesVolume>();
      ColoredCubesVolumeRenderer volumeRenderer = GetComponent<ColoredCubesVolumeRenderer>();


      volume.data = data;

      // Defines colors for each cubic square.
      QuantizedColor green = new QuantizedColor(18, 115, 38, 255);
      QuantizedColor brown = new QuantizedColor(115, 87, 18, 255);
      QuantizedColor gray = new QuantizedColor(127, 127, 127, 255);
      QuantizedColor white = new QuantizedColor(255, 255, 255, 255);

      float rockScale = 0.005F;      
   

      // Iterate over every voxel of our volume
      for (int z = 0; z < depth; z++)
      {
         for (int y = height - 1; y > 0; y--)
         {
            for (int x = 0; x < width; x++)
            {
               float sampleX = (float)x * rockScale;
               float sampleY = (float)y * rockScale;
               float sampleZ = (float)z * rockScale;


               float simplexNoiseValue = SimplexNoise.Noise.Generate(sampleX, sampleY, sampleZ);


               float altitude = (float)(y - 0.05f) / (float)height;

               // Map the altitude to the range -1.0 to +1.0...
               altitude = (altitude * 0.05f) + 0.05f;

               simplexNoiseValue -= altitude;

               simplexNoiseValue *= 1.0f;
               simplexNoiseValue = Mathf.Clamp(simplexNoiseValue, -0.05f, 0.05f);

               simplexNoiseValue *= 123;

               int simplexCubicValue = Mathf.RoundToInt (simplexNoiseValue);

               data.SetVoxel(x + simplexCubicValue, y+ simplexCubicValue, z+ simplexCubicValue, green);
            }

         }

      }//End the loop of voxeling
   
   }
   

}



EDIT :
Sorry about all the edits getting it almost figured out the last code was alittle different ...

but I like the outcome of this.....

Theres a snap
http://fav.me/daireld

Code:
using UnityEngine;
using System.Collections;

using Cubiquity;

public class ProceduralColoredCubes : MonoBehaviour {

   public int width = 128;
   public int height = 32;
   public int depth = 128;


   // Use this for initialization
   void Start () {

      // FIXME - Where should we delete this?
      /// [DoxygenSnippet-CreateEmptyTerrainVolumeData]
      //Create an empty TerrainVolumeData with dimensions width * height * depth
      ColoredCubesVolumeData data = VolumeData.CreateEmptyVolumeData<ColoredCubesVolumeData>(new Region(0, 0, 0, width-1, height-1, depth-1));
      /// [DoxygenSnippet-CreateEmptyTerrainVolumeData]

      ColoredCubesVolume volume = GetComponent<ColoredCubesVolume>();
      ColoredCubesVolumeRenderer volumeRenderer = GetComponent<ColoredCubesVolumeRenderer>();


      volume.data = data;

      // Defines colors for each cubic square.
      QuantizedColor green = new QuantizedColor(18, 115, 38, 255);
      QuantizedColor brown = new QuantizedColor(115, 87, 18, 255);
      QuantizedColor gray = new QuantizedColor(127, 127, 127, 255);
      QuantizedColor white = new QuantizedColor(255, 255, 255, 255);

      float rockScale = 0.02F;      
   

      // Iterate over every voxel of our volume
      for (int z = 0; z < depth; z++)
      {
         for (int y = height - 1; y > 0; y--)
         {
            for (int x = 0; x < width; x++)
            {
               float sampleX = (float)x * rockScale;
               float sampleY = (float)y * rockScale;
               float sampleZ = (float)z * rockScale;


               float simplexNoiseValue = SimplexNoise.Noise.Generate(sampleX, sampleY, sampleZ);


               float altitude = (float)(y - 0.05f) / (float)height;

               // Map the altitude to the range -1.0 to +1.0...
               altitude = (altitude * 0.05f) - 0.05f;

               simplexNoiseValue -= altitude;

               simplexNoiseValue *= 0.05f;
               simplexNoiseValue = Mathf.Clamp(simplexNoiseValue, -0.05f, 0.05f);

               simplexNoiseValue *= 15;

               int simplexCubicValue = Mathf.RoundToInt (simplexNoiseValue);


               if(y < 8)
               {
                  // Add to bedrock material channel.
                  data.SetVoxel(x , y , z , green);
               }

               data.SetVoxel(x , y * simplexCubicValue, z , gray);
            }

         }

      }//End the loop of voxeling
   
   }
   

}


Top
Offline Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Theme created StylerBB.net