We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi!
I might ask something obvious, but I'm a bit new. I've been searching and I couldn't find the answer.
Can a gradient be applied to a sphere in processing?
I want to use a two colour gradient (from blue to yellow) to colour a sphere. Then I want to make the gradient dynamically change, depending on data input.
I've seen linear and radial gradients, but nothing applied to a sphere.
Thanks in advance!
Comments
Sure, how are you drawing the sphere?
Let me explain a little better what I want to do. Basically I am visualizing emotions off twitter. I get a happy versus sad tweets proportion, and based on that, I was drawing a kind of graph do visualize it. You can see it here: https://youtube.com/watch?v=BuapPHUzAo4&feature=youtu.be
So my idea was to visualize this same concept in a sphere. It needs to have a gradient, going from blue (sad) to yellow (happy).
I found this code to make a sphere.
So I tried applying a regular gradient, but it doesn't work on the sphere. Or at least I don't know how to...
With that single default sphere call indeed you can't do a gradient, because you can only define a single fill color for the shape as a whole.
To have a sphere with gradient, you will need access to individual vertices, so you can set the fill however you like per vertex. To have access to individual vertices (and their fill color) you need to create the 3D shape accordingly. Either do it yourself manually or use a 3D library (Hemesh, Toxiclibs and more) to do it for you.
When you want to do it yourself, you can use beginShape(), endShape(), fill() and vertex() to create custom 2D and 3D shapes. There are different shape kinds, for example triangles, trianglestrips, quads or quadstrips. See: https://www.processing.org/reference/beginShape_.html
Thank you! This is a bit more advanced than what I'd been doing. I will try to look at it in more depth :-)
Actually perhaps you could do a gradient with a GLSL shader. That may be an even easier solution, because you don't have to do 'complex geometry' (the current sphere call may suffice). The only downside is that you would have to know how to write / work with GLSL shaders, which in themselves may be complex for a beginner.
On a sidenote, your youtube video doesn't show a gradient. It actually just shows two colors without any interpolation. It seems there is just a 'percentage' which serves as the border between colorA and colorB.
Here is a quick example of such as shader (without the advanced lighting calculations!).
Main sketch
coloredSphereFrag.glsl
coloredSphereVert.glsl