Blender as a GLSL editor

It seems it’s possible to export Blender materials as GLSL code since 2.6 and someone has already written an exporter.
wiki.blender.org/index.php/Exten … xport_GLSL

I created a quick toon material and exported it.

I got a simple vertex shader but a 2200 line fragment…
pastebin.com/bk9bhiZe
pastebin.com/vgzH6iMj

I’m not a shader programmer, but if this can be cleaned up to make it work with Panda properly, then that will be very nice considering there is nothing like a GLSL,etc editor right now, at least a free one. Which always seemed weird to me, as I think artists should create the shading of their artwork.

That’s really cool!

It looks like the shader defines a lot of unnecessary functions, though, assuming that the GLSL compiler inlines them. For instance, math_cosine just calls the built-in function cos(), and math_subtract just returns the subtraction of its parameters. This is not a big deal, though, as the compiler surely optimises this out.

In any case, have you tried using this shader in Panda? Looking at it, it should work as-is; though it does define three uniforms, unf9, unf23 and unf37, which you need to pass using setShaderInput.

Judging by the shader, it seems that unf37 is the specular colour, unf23 is the light colour, and unf9 is the lamp vector.

This is a good point, and we’ve been discussing potential ways to improve this. Perhaps a future Panda release will feature a new shader system that does empower artists to do this.

Is it possible to somewhere read more about the new shader system?
I just started to adapt themselves to the current ))

The design hasn’t been worked out yet, but it’ll probably have to do with connecting nodes together by input and output similar to Blender’s node-based shader editor. This enables non-programmers to create Panda shaders, since no knowledge of programming will be required to design a shader.

In the long term, we may work towards supporting FX shaders and allow people to use tools like NVIDIA FX Composer 2, but that’s a different story.

Anyway, if Blender allows exporting this node tree, then perhaps we could also extend the design to allow people to import the Blender material nodes into Panda.

Regardless, your old shaders will of course continue to work.

Ok. Thanks!

BTW. I try to use shader above and got a strange behaviour. Shader works through time.

And sometimes it’s a fully red or yellow teapot without lighting. Every time I get the message that “Vertex shader was successfully compiled to run on hardware.”

Panda 1.9.0 cvs20121130, Linux Mint 14 (cinnamon), ATI Radeon HD4850

class Game(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)
        self.model = self.loader.loadModel('teapot')
        self.model.reparentTo(render)
        self.shader = Shader.make(Shader.SLGLSL, VSHADER, FSHADER)
        self.model.setShader(self.shader)
        self.model.setShaderInput('unf23', Vec4(0.6, 0.6, 0.6, 1.0))
        self.model.setShaderInput('unf37', Vec4(1.0, 1.0, 1.0, 1.0))
        self.model.setShaderInput('unf9', Vec3(1.0, 1.0, 0.0))
        self.cam.setPos(0, -10, 0)

game = Game()
game.run()

Sorry, but I’m just getting a pretty teapot when I run that. I’m guessing that some kind of driver bug has to be involved.

What exactly do you need to do to get the first result?

I didn’t do anything, no any changes in source - just pressed “run” again. I’ll try update my videodriver if possible.

Hmm, I just ran it again and reproduced the issue. This is odd. I’ll have to investigate.

I don’t see anything at all.

from panda3d.core import *
from direct.showbase.ShowBase import *

class Game(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)
        self.model = self.loader.loadModel('teapot')
        self.model.reparentTo(render)
        self.shader = Shader.load(Shader.SLGLSL, 'vert.glsl', 'frag.glsl')
        self.model.setShader(self.shader)
        self.model.setShaderInput('unf23', Vec4(0.6, 0.6, 0.6, 1.0))
        self.model.setShaderInput('unf37', Vec4(1.0, 1.0, 1.0, 1.0))
        self.model.setShaderInput('unf9', Vec3(1.0, 1.0, 0.0))
        self.cam.setPos(0, -10, 0)

game = Game()
game.run()

@redpanda As I know - it’s a bug in 1.8.0

@ninth: hmm, if you remove the setShaderInput lines and replace the three uniform declarations in the fragment shader with this:

const vec4 unf23 = vec4(0.6, 0.6, 0.6, 1.0);
const vec3 unf37 = vec3(1.0, 1.0, 1.0);
const vec3 unf9 = vec3(1.0, 1.0, 0.0);

does it still show the issue? For me, I haven’t been able to reproduce it with this change, but since it takes many, many runs for me to even reproduce it in the original I cannot be certain.

If this does indeed fix it, that would establish that the issue is in how the parameters are passed or bound to the shader, which is an important step toward diagnosing what’s going on here.

Yes, after changes i can get stabile result, but it looks like a second image. As far as I can see, the more or less right result is on the first image (it’s should be a toon shader).

Both look nothing like the material in Blender. The color might be right but nobody needs a whole shader for that. Are the Blender devs terrible at jokes or are we doing something wrong?

Then it would help if you gave the original Blender file.

Don’t see why as posted an image, but sure: 2shared.com/file/LDI81RX8/example.html

I ran the code with the shaders in your example.zip file (which are, for the record, not the same as the ones you linked in the first post), and after setting the settings more or less as they were set in the blend file:

const vec4 unf30 = vec4(0.8, 0.06699, 0.0, 1.0);
const vec3 unf48 = vec3(1.0, 1.0, 1.0);
const vec3 unf10 = vec3(1.0, 1.0, 0.0);

(and of course uncommenting the corresponding uniforms)
I get the same shading effect as I get when opening the .blend file:
rdb.name/glsl_toon_teapot.png

I guess ninth is doing something wrong or his drivers are buggy. All I meant was that none of his screenshots showed identical shading.

Since I’m done with my projects I’ll experiment with these myself and see if node-based materials export as smoothly. If that works too then Blender is pretty much a GLSL material editor already.

ninth was running the shader you had posted originally, which gives an effect that is very different from the one you had just posted.

I’m still investigating why setShaderInput sometimes fails, though.

Well, when i run shaders from example.zip, I get the same result as rdb (if I set vars in the shader file). But if I try to pass vars by setShaderInput, I get the strange behaviour as in the first case.

May be It’s ATI “feature”? Before I’ve had problems with Cg on ATI when I passed too many vars in shader, although on GeForce it’s does not appears.

I’m reproducing the problem on a GeForce 320M, so it’s not ATI’s fault.