Created
March 3, 2020 10:10
-
-
Save companje/b9b462e15c61d09c4af617a2e7f82296 to your computer and use it in GitHub Desktop.
imageOnSphere (prev known as 'sphereCap' - warps image onto a sphere)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Author
companje
commented
Mar 3, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment