Export Materials to GLSL Code

(vitorbalbio) #1

Hi guys

Blender 2.6 are bringing to us many new features, one of that is the possibility of get the pure GLSL code of one Blender material with the command “gpu.export_shader(Scene,mat)”. It´s a really great tool if you want to use the Amazing BGE material editor and export it for another game engine. Or if you’re just curious :stuck_out_tongue:

Its a simple command but it can be more simple for a end-user. So i did a little add-on to export the materials as GLSL (vert and frag) code.

Its really simple to use:

Turn On the Add on in the Preferences Panel ( File/User Preferences):

It will add a new panel in Material Properties:

Select one of this Three Options:

-> Active Material: Export Just the active material of this object
-> Selected Objects Materials : Export all materials of the selected objects
-> All Materials : Export all materials datablocks ( even it not linked to a object)

So click in Export

It will open the file browser, select the directory and click in Export.
It make two files per material: .frag to fragment shader and .vert to vertex shader
Names are not necessary. it always name the files with mat_<material_name>

A exemple with the default Material and one light in scene:

Vertex Shader

varying vec3 varposition;
varying vec3 varnormal;

void main()
	vec4 co = gl_ModelViewMatrix * gl_Vertex;

	varposition = co.xyz;
	varnormal = normalize(gl_NormalMatrix * gl_Normal);
	gl_Position = gl_ProjectionMatrix * co;


The fragment is too long to post here so i did it in pastall

The bad news is: now we can see how messy it is :frowning:
Especially if you add more lamps in the scene, i just hope the GLSL compiler make a good job to optimize it :smiley:

OBS: Only for blender 2.6 or above so use a graphicall build or the 2.6 RC



This is it, if there is some doubts just say. Suggestions are welcome too :slight_smile:

(SolarLune) #2

Great. That’s a really good idea - I recall there being a build of Blender that exported the material settings to GLSL code. I think I’m going to check this out. Thanks!

(Hperigo) #3

Awsome stuff!
can´t wait to test it with Processing =)

(Karakasimov) #4

Cool. although i am just curious. A New addon for bge. Thanks alot.:RocknRoll:

(Evil Moon MOose) #5

wait so we can export the material, then reimport it to blender and use it as GLSL? how would i do this?

(SolarLune) #6

It doesn’t appear to work with the BGE… That’s too bad.

(holyenigma74) #7

having good GLSL materials are nice for doing fast openGL animations…
over at blendswap someone has a pretty nice GLSL skin shader(that can also look like leather as well)

you can fake realtime reflections with glsl,
with spot lamps and buffer shadows you can have realtime shadows in the view port.

If someone was wanting to do 3d web tv show, on current events similar to
Taiwan animation OpenGL render is the way to go.

(aermartin) #8

can you export to GLSL to use with WebGL ?

(Ivaylo Gogov [ivaydesign]) #9

Yes this is very interesting question, I’m wondering same thing for a while also? If it’s possible can someone explain - how :slight_smile: Thanks in advance.

(unbreker) #10

@For Linux Users:

On the file, on the lines 61/62/85/86/99/100, put the slash “/” instead of the slash “”.
So this line:

frag = open(self.filepath + "\mat_" + mat.name + ".frag","w")
vertex = open(self.filepath + "\mat_" + mat.name + ".vert" ,"w")

become this:

frag = open(self.filepath + "/mat_" + mat.name + ".frag","w")
vertex = open(self.filepath + "/mat_" + mat.name + ".vert" ,"w")

(vitorbalbio) #11

Hi guys, sorry forget about this thread. There’s a while that i don’t check if it’s working yet but i will try to answer what i remember about it.

wait so we can export the material, then reimport it to blender and use it as GLSL? how would i do this?

I don’t think so… Not in a easy way mostly cause you need to pass to the shader some scene information such light position, for that you need a bit of python code too.

can you export to GLSL to use with WebGL ?

AFAIK WebGL shader language is similar to OpenGL 2.1 so it’s quite similar, but you will need to check all variables and pass the uniform values with the correct names that you are using in your program.

On the file, on the lines 61/62/85/86/99/100, put the slash “/” instead of the slash “”.

Thanks for the tip, never checked it on a Linux system.

(sybren) #12

I have updated the script at https://developer.blender.org/T28839 so that it’s Linux-friendly (and has some other small improvements).

(laishi) #13

PLZ update your addon , I use blender 2.60 and 2.73 in fedora 22 all does not work why? thanks my friend

(xendian) #14

I can’t get the script to work…I used both blender 2.6 and 2.77. I didn’t see a new planel when I enabled the the script from addon?. Can you please share some screenshot of that panel? thanks.