How to create a 3D geometry with faceted reflection of light (sphere)

My goal is to create a 3d geometry with many facets, where each facet reflects light differently–a disco ball may be a good visual for what I want to say. I created a primitive 3d sphere with low numbers of detailX and detailY. This sphere looks very faceted but it reflects light just like a very smooth sphere (high numbers of detailX and detailY). I cannot see the difference.

It seems 3d geometry automatically calculates smoothed reflection of nearby facets. I also tried same faceted sphere using 3d.Geometry(), but its result was same as the primitive one. Do I have to code each individual triangle as 3d plane and calculate its normal?

Interesting

Did you try to just use lights ();

2 Likes

Sounds like a good idea

1 Like

Hello @peskythisdot,

Related:

:)

I don’t know if you can override the default behaviour for Sphere, Box, Cylinder etc. but if not you may have to create your own geometry.

I was looking for a way to render a large 3D terrain and a week ago I discovered p5.Geometry and have successfully created a terrain with ~400000 triangles that renders in under 2ms.

Unfortunately the reference material is rather brief and not very helpful but I did find this article which helped enormously.

4 Likes

Thank you all,

After reading all of your responses, I now believe that I have to code each triangle as a p5.Geometry to get the result I want. When I tried a faceted sphere using p5.Geometry following paulwheeler.us’s example, I got the same result as primitive sphere.

1 Like

Just a followup: I created a strip of triangles in 3D.Geometry (top) and compared it with the one (bottom) created by following the above-mentioned example.

The top one specifies each triangle and calculates its face and normal, while the bottom one does not specify each triangle. It seems the second approach uses some kind of smoothing behind the scene. In any case, to achieve the effects I want, I have to code each triangle. That’s my conclusion.

2 Likes

start here


float radius = 200.0;
float rho = radius;
float factor = TWO_PI/20;
float x, y, z;

float zAngle;
float yAngle;

void setup() {
  size(500, 500, P3D);
  smooth();
}

void draw() {
  background(50);
  stroke(255);
  lights();

  translate(width/2, height/2);

  rotateX(radians(mouseY));
  rotateY(radians(mouseX));

  for (float phi = 0.0; phi < PI; phi += factor) {

    for (float theta = 0.0; theta < TWO_PI + factor; theta += factor) {

      x = rho * sin(phi) * cos(theta);
      z = rho * sin(phi) * sin(theta);
      y = -rho * cos(phi);

      pushMatrix();

      translate(x, y, z);

      //I think this should give me relevant angles that can then be used (with modification) for rotation of the rectangles
      zAngle = acos(z/radius);
      yAngle = atan2(y, x);

      rotateY(radians(zAngle-90)); //this is my pain point - I can't find any combination of rotations that does what I want
      rotateX(radians(yAngle-90));

      rect(0, 0, 15, 15);

      popMatrix();
    }
  }
}
//

1 Like