User avatar
Jim Manley
Posts: 1600
Joined: Thu Feb 23, 2012 8:41 pm
Location: SillyCon Valley, California, and Powell, Wyoming, USA, plus The Universe
Contact: Website

Pi3D Shape Surface Color

Mon Mar 25, 2013 5:58 am

With the shift to ES 2.0 shaders, is that and textures now the only ways to apply color to Shape surfaces? Is it possible to specify RGBA values instead, and is it also possible to specify interpolated shading between edges or vertices? I realize it's possible to create a texture file to do this, but then there are all sorts of alignment issues. I also know that there's probably a way to do this via a shader (e.g., the pi3d2.py and pi3d3.py demos), but I haven't drilled down into that yet. Thanks!
The best things in life aren't things ... but, a Pi comes pretty darned close! :D
"Education is not the filling of a pail, but the lighting of a fire." -- W.B. Yeats
In theory, theory & practice are the same - in practice, they aren't!!!

User avatar
paddyg
Posts: 2555
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Pi3D Shape Surface Color

Mon Mar 25, 2013 9:19 am

Jim, There are two alternatives built in: either send Texture files [ambient texture, normal map texture, reflection texture] using Shape.set_draw_details()
Or else set the material rgb using Shape.set_material() see http://pi3d.github.com/html/pi3d.html#module-pi3d.Shape
I can't remember now why it doesn't set the alpha value, it probably should be made to do that at some stage.
To draw an object using material rgb you need to use a shader beginning with mat_

It would be "very easy" to make a shader that blended from one shade to another but you would need to pass it the second shade as a uniform variable. Easiest would be to just use the 'standard' ones available via Shape.set_custom_data() unif[42] onwards these are used for the fancy shader effect you mention in Pi3d2.py
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
Jim Manley
Posts: 1600
Joined: Thu Feb 23, 2012 8:41 pm
Location: SillyCon Valley, California, and Powell, Wyoming, USA, plus The Universe
Contact: Website

Re: Pi3D Shape Surface Color

Mon Mar 25, 2013 5:57 pm

Thanks Paddy! I figured it was possible, I'm just pressed for time and didn't see an obvious means of specifying it. With this info I now see where it can be done. It seemed odd that such a simple, fundamental 3-D graphics task might not be possible and I wanted to be able to show how to do that in a tutorial without overwhelming newbies by them having to create shaders and textures right off the bat, if possible. Yes, they can use the demo examples, but those are pretty fancy and contain few/no comments. I want to get across the concept of edge and vertex color interpolation as directly as possible.
The best things in life aren't things ... but, a Pi comes pretty darned close! :D
"Education is not the filling of a pail, but the lighting of a fire." -- W.B. Yeats
In theory, theory & practice are the same - in practice, they aren't!!!

rosenfranz
Posts: 17
Joined: Sun Oct 26, 2014 11:19 am

Re: Pi3D Shape Surface Color

Sat Nov 22, 2014 12:06 pm

Hey there Paddy and Jim!

I stumbled upon this thread as I was looking for the simplest possible way to colorize a Pi3D shape without applying a texture to it.

Paddy, your post made me try around with set_material but so far I didn't see any color :( .

Would any of you guys be so kind and shed a little more detailed light on this topic? A piece of code with a rock bottom setup would be so nice. Just a colorized, flat-shaded cuboid would be totally enough.

User avatar
paddyg
Posts: 2555
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Pi3D Shape Surface Color

Sat Nov 22, 2014 12:29 pm

I think you can set the materials of a Shape using the set_material() which seems to be a wrapper to change the values in the Buffer using "self.unib[3:6] = mtrl[0:3]" however you then need to use a different Shader to colour in the pixels using these values. The Shaders are the mat_flat, mat_bump, mat_reflect family. The latter two can have textures passed to them to act as normal map and reflected image.

Code: Select all

mysh = pi3d.Shader('mat_flat')
mycuboid = pi3d.Cuboid(shader=mysh)
mycuboid.set_material((127, 255, 0))
.. #while loop
  mycuboid.draw()
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
paddyg
Posts: 2555
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Pi3D Shape Surface Color

Sun Nov 23, 2014 12:00 am

OK so that was a quick post before I nipped out this afternoon. And had some wrong code. Sorry! Also the most useful shader for non textured 3D shapes is probably the one I didn't mention mat_light

Code: Select all

mysh = pi3d.Shader('mat_light')
mycuboid = pi3d.Cuboid(z=3.0)
mycuboid.set_material((0.5, 1.0, 0.0))
mycuboid.set_shader(mysh)
.. #while loop
  mycuboid.draw()
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

rosenfranz
Posts: 17
Joined: Sun Oct 26, 2014 11:19 am

Re: Pi3D Shape Surface Color

Mon Nov 24, 2014 10:11 am

Thanks Paddy!
Well I almost got there myself.
I figured that you would have to combine it with some sort of shader but I couldn't find a shader-list anywhere.
Is there one in the Pi3D documentation? Or how did you know those mat_shaders exist?

User avatar
paddyg
Posts: 2555
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Pi3D Shape Surface Color

Mon Nov 24, 2014 1:16 pm

OK, after a quick search it appears that the required information is not easy to find. This is where it's explained, but even though I knew it was there it took me a while to find it! I will add something in the FAQ and include as many likely search words as I can think of.

There isn't a list of shaders as they seem to fall outside the scope of the python module and have to be explicitly rolled up in the setup script as support files. Sphinx, which is used for the docs also seems to skip over them (as well as the demo files, which I tried to fill with docstrings to generate some documentation). The core shaders are included in pi3d/pi3d/shaders and there are demo ones in pi3d_demos/shaders
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

rosenfranz
Posts: 17
Joined: Sun Oct 26, 2014 11:19 am

Re: Pi3D Shape Surface Color

Mon Nov 24, 2014 2:35 pm

Thanks man.

Return to “Python”