Skip to content

Instantly share code, notes, and snippets.

@companje
Created March 3, 2020 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save companje/b9b462e15c61d09c4af617a2e7f82296 to your computer and use it in GitHub Desktop.
Save companje/b9b462e15c61d09c4af617a2e7f82296 to your computer and use it in GitHub Desktop.
imageOnSphere (prev known as 'sphereCap' - warps image onto a sphere)
PShape imageOnSphere(PImage img, float sphereRadius) {
//textureMode(NORMAL);
PShape sh = createShape(GROUP);
sh.setStroke(false);
float cx = img.width/2;
float cy = img.height/2;
float radius = img.width/2;
int rres = 30;
float rStep = radius/rres;
float aStep = TWO_PI/40;
//fan
PShape fan = createShape();
fan.setStroke(false);
fan.beginShape(TRIANGLE_FAN);
fan.texture(img);
fan.vertex(0, 0, sphereRadius, cx, cy); //center
for (float a=-aStep; a<=TWO_PI; a+=aStep) {
float x = cos(a)*rStep;
float y = sin(a)*rStep;
float z = sqrt(sphereRadius*sphereRadius-(x*x)-(y*y));
fan.vertex(x, y, z, x+cx, y+cy);
}
fan.endShape();
sh.addChild(fan);
//strips
for (int i=0; i<rres; i++) {
float r=i*rStep;
PShape strip = createShape();
strip.setStroke(false);
strip.beginShape(TRIANGLE_STRIP);
strip.texture(img);
//strip.stroke(255, 255, 0);
//sh.noStroke();
for (float a=-aStep * (i%2==0 ? 1 : .5); a<=TWO_PI; a+=aStep) {
float x = cos(a)*(r+rStep);
float y = sin(a)*(r+rStep);
float z = sqrt(sphereRadius*sphereRadius-(x*x)-(y*y));
strip.vertex(x, y, z, x+cx, y+cy);
x = cos(a+aStep/2)*r;
y = sin(a+aStep/2)*r;
z = sqrt(sphereRadius*sphereRadius-(x*x)-(y*y));
strip.vertex(x, y, z, x+cx, y+cy);
}
strip.endShape();
sh.addChild(strip);
}
sh.translate(-sh.width/2,-sh.height/2,-sphereRadius); //center and put in dome so move image back into depth
return sh;
}
@companje
Copy link
Author

companje commented Mar 3, 2020

imageOnSphere-spherecap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment