How do you Create Shadows in Processing?

I am trying to make my images more realistic by adding the shadows of the rendered shapes. I thought that it may be automatic when adding directional lights, but this does not appear to work. Below is a simple test code that I wrote at attempts to use directional and spot lights to draw the shadow of a sphere on the inside of a box but does not work.

/*
 * Shadow Test Code
 *
 * Move the mouse the change the direction of the light.
 *
 */

void setup() {
  size(900, 400, P3D);
  noStroke();
}

void draw() {
  noStroke();
  background(0);
  float dirY = (mouseY / float(height) - 0.5) * 2;
  float dirX = (mouseX / float(width) - 0.5) * 2;
  directionalLight(204, 204, 204, -dirX, -dirY, -1);
  spotLight(250, 250, 0, 0, 0, 1200, -dirX, -dirY, -1, PI/8, 100);
  translate(width/2, height/2, 0);
  sphere(80);
  box(900.0, 400.0, 1000.0);
}

I searched other posts and found a discussion that gave a rather complex method but being a processing novice, I could not get the posted code to work.

Is there a relatively simple method of creating object shadows?

1 Like