Casting 3d to 2d

Hello,

I am searching for a method of casting a loaded 3d object into 2d image, similarly to 2D Topographical Map - #2 by solub

Although i would need, instead of height contours, a full 2d image, where gray-scale represents given height.

So, when casting a sphere, it should look like this:

Could somebody point me in the direction if this even is possible in Processing?

I was thinking about parsing data from OBJ file, but those are vector parameters so it wont work

PShape sphere;

float ry;
  
public void setup() {
  size(500,500, P3D);
    
  sphere = loadShape("sphere.obj");
}

public void draw() {
  background(0);
  //lights();
  
  translate(width/2-200, height/2);
  rotateZ(PI);
  rotateY(ry);
  shape(sphere);
 

 /*
   i wih i could just pixel[x,y,z]  :(
 
 
 */
 
}

I imagine what if possible, has to be done.

  • Checking each pixel and finding min and max according to given view.
  • Mapping those as 0 and 255 color.
  • Checking one again each pixel and casting it into new 2d image with proper color

Help ? :slight_smile:

Might want to use ortho. Here is an example thread.

Dont need to read all of it just the first few suggestions.

Hey,

Thanks. By the way, very interesting topic those voronoi diagrams. May be tricky to apply in 3d. Isn’t there really a simpler way to do it?

I guess, there needs to be an imaginary plane, and then, try to cast from each point on the shape.

But, can do an imaginary plane like this? And more important, how can i check if an object is inside the shape?


ArrayList<Point> finalPoints = new ArrayList<Point>();
for (int i = 0; i < width; i ++){
  for (int j = 0; j < height; j++ ){
        int z = 0
        while (!z = 0){
               
          is Point(x,y, z) in the shape ?
         yes ->  save into ArrayList and break;
         no -> z ++

           }
   
   }
}


Maybe somebody knows some pure Java library ?

Any ideas or examples that could be used ?:slight_smile:

The idea is that it just gives you a top down view. In the example I’ve provided I am in fact recommended to start with cones which are drawn in 3d and then the whole perspective is rotated so the camera is top down. So everything looks circular

Is this not what you were after.

Hi @Infi,

Could you provide an example of a 3d shape that you would like to use for this project ?

Depending on your needs you could either:

  • use the distance between the vertices of your mesh and their orthogonal projections on a chosen cross section

  • get the 2d contour of your 3d mesh at a chosen cross section and offset it inward multiple times

Hey,

Thank you.
Back in the days, I used a mix of Blender, Meshmixer and some other online software(can’t remember the name) to do the job,

E.g. this OBJ:

into this BMP

A simple sphere:

into:

etc.

although, in the above examples, the higher the darker. I’d prefer opposite but it does not matter right now.

Found some on github GitHub - ryobg/obj2hmap: Very simple convertor of Wavefront's obj files to displacement map (projected on one of the main XYZ planes) which may be an inspiration but as I am not a C++ person it’s quite hard to analyze. additionally, i cannot open the file which this program creates…

@Infi

If all you want is a bitmap image with grayscale mapping then no need for casting nor 2d projection. You could just colorize the faces of your mesh based on the depth of their vertices (either y or z value depending on the orientation of your original mesh) and the color value of your background.

With Processing:

  • load your ‘obj’ file with loadShape()

  • iterate over the vertices of your mesh to find the deepest and highest ones

  • convert your mesh to a PShape object with createShape()

    • while doing so, map the depth of each vertex against a color range starting from 0 (black) to the chosen background color (up to 255).

    important: depending on the topology of your shape you may want to opt for a non-linear interpolation (circular, exponential or else)

  • save the final output with saveFrame()

A quick example (Python mode) with exponential interpolation:


the higher the exponent (from 2 to 9), the better the blend with the background

W, H = 700, 900   #Dimensions of canvas
BGC = 200         #Background color value
P = 6             #Exponent value

def setup():
    perspective(60 * DEG_TO_RAD, W/float(H), 2, 6000)
    translate(W>>1, H>>1)
    background(BGC)
    size(W, H, P3D)
    noStroke()
    
    global miny, maxy
    
    obj = loadShape('Skull.obj')    #Load mesh object
    miny, maxy = get_YBounds(obj)   #Get min/max height values
    shp = to_PShape(obj)            #Convert mesh (obj) to PShape
    
    #Move shape in front of camera + display
    translate(0, 11, 740)
    rotate(PI)
    rotateX(-HALF_PI*.9)
    shape(shp)  
    
    #Save output
    saveFrame('output.png')
    
    
def to_PShape(s):
    
    '''Traverse the selected mesh and construct a PShape object from its faces/vertices'''
    
    shp = createShape(GROUP)
    for child in s.getChildren():
        face = createShape()
        face.beginShape(QUAD)
        for i in xrange(child.getVertexCount()):
            v = child.getVertex(i)
            c = map(v.y**P, miny**P, maxy, 0, BGC)
            face.fill(c)
            face.vertex(v.x, v.y, v.z)
        face.endShape()
        shp.addChild(face)
    
    return shp


def get_YBounds(s):
    
    '''Traverse the selected mesh and get the min/max height values'''
    
    vals = []
    
    for child in s.getChildren():
        for i in xrange(child.getVertexCount()):
            v = child.getVertex(i)
            vals.append(v.y)

    vals = sorted(vals)
    
    return vals[0], vals[-1]
1 Like

Hello,

Yes, that’s it ! :slight_smile:

I am having some problems with running your code, probably because I cannot focus the obj in the middle of screen, but will work on it :slight_smile: I will also refractor the code later into Java, just for future regeneration.

It surprises me how little is on the internet in this topic. Especially in form of code. Your method is the same that Blender uses. I additionally found TerreSculptor which does the job.

Cheers