7
This module provides access to materials GLSL shaders.
13
Module to provide functions concerning the GPU implementation in Blender, in particular
14
the GLSL shaders that blender generates automatically to render materials in the 3D view
15
and in the game engine.
19
The API provided by this module should be consider unstable. The data exposed by the API
20
are are closely related to Blender's internal GLSL code and may change if the GLSL code
21
is modified (e.g. new uniform type).
34
For shader uniforms, the data type determines which glUniform function
35
variant to use to send the uniform value to the GPU.
36
For vertex attributes, the data type determines which glVertexAttrib function
37
variant to use to send the vertex attribute to the GPU.
73
matrix 3x3 in column-major order
77
.. data:: GPU_DATA_16F
79
matrix 4x4 in column-major order
83
.. data:: GPU_DATA_4UB
95
Constants that specify the type of uniform used in a GLSL shader.
96
The uniform type determines the data type, origin and method
97
of calculation used by Blender to compute the uniform value.
99
The calculation of some of the uniforms is based on matrices available in the scene:
101
.. _mat4_cam_to_world:
102
.. _mat4_world_to_cam:
105
Model matrix of the camera. OpenGL 4x4 matrix that converts
106
camera local coordinates to world coordinates. In blender this is obtained from the
107
'matrix_world' attribute of the camera object.
109
Some uniform will need the *mat4_world_to_cam*
110
matrix computed as the inverse of this matrix.
112
.. _mat4_object_to_world:
113
.. _mat4_world_to_object:
115
*mat4_object_to_world*
116
Model matrix of the object that is being rendered. OpenGL 4x4 matric that converts
117
object local coordinates to world coordinates. In blender this is obtained from the
118
'matrix_world' attribute of the object.
120
Some uniform will need the *mat4_world_to_object* matrix, computed as the inverse of this matrix.
122
.. _mat4_lamp_to_world:
123
.. _mat4_world_to_lamp:
126
Model matrix of the lamp lighting the object. OpenGL 4x4 matrix that converts lamp
127
local coordinates to world coordinates. In blender this is obtained from the
128
'matrix_world' attribute of the lamp object.
130
Some uniform will need the *mat4_world_to_lamp* matrix
131
computed as the inverse of this matrix.
133
.. data:: GPU_DYNAMIC_OBJECT_VIEWMAT
135
The uniform is a 4x4 GL matrix that converts world coordinates to
136
camera coordinates (see mat4_world_to_cam_). Can be set once per frame.
137
There is at most one uniform of that type per shader.
141
.. data:: GPU_DYNAMIC_OBJECT_MAT
143
The uniform is a 4x4 GL matrix that converts object coordinates
144
to world coordinates (see mat4_object_to_world_). Must be set before drawing the object.
145
There is at most one uniform of that type per shader.
149
.. data:: GPU_DYNAMIC_OBJECT_VIEWIMAT
151
The uniform is a 4x4 GL matrix that converts coordinates
152
in camera space to world coordinates (see mat4_cam_to_world_).
153
Can be set once per frame.
154
There is at most one uniform of that type per shader.
158
.. data:: GPU_DYNAMIC_OBJECT_IMAT
160
The uniform is a 4x4 GL matrix that converts world coodinates
161
to object coordinates (see mat4_world_to_object_).
162
Must be set before drawing the object.
163
There is at most one uniform of that type per shader.
167
.. data:: GPU_DYNAMIC_OBJECT_COLOR
169
The uniform is a vector of 4 float representing a RGB color + alpha defined at object level.
170
Each values between 0.0 and 1.0. In blender it corresponds to the 'color' attribute of the object.
171
Must be set before drawing the object.
172
There is at most one uniform of that type per shader.
176
.. data:: GPU_DYNAMIC_LAMP_DYNVEC
178
The uniform is a vector of 3 float representing the direction of light in camera space.
179
In Blender, this is computed by
181
mat4_world_to_cam_ * (-vec3_lamp_Z_axis)
183
as the lamp Z axis points to the opposite direction of light.
184
The norm of the vector should be unity. Can be set once per frame.
185
There is one uniform of that type per lamp lighting the material.
189
.. data:: GPU_DYNAMIC_LAMP_DYNCO
191
The uniform is a vector of 3 float representing the position of the light in camera space.
194
mat4_world_to_cam_ * vec3_lamp_pos
196
Can be set once per frame.
197
There is one uniform of that type per lamp lighting the material.
201
.. data:: GPU_DYNAMIC_LAMP_DYNIMAT
203
The uniform is a 4x4 GL matrix that converts vector in camera space to lamp space.
206
mat4_world_to_lamp_ * mat4_cam_to_world_
208
Can be set once per frame.
209
There is one uniform of that type per lamp lighting the material.
213
.. data:: GPU_DYNAMIC_LAMP_DYNPERSMAT
215
The uniform is a 4x4 GL matrix that converts a vector in camera space to shadow buffer depth space.
218
mat4_perspective_to_depth_ * mat4_lamp_to_perspective_ * mat4_world_to_lamp_ * mat4_cam_to_world_.
220
.. _mat4_perspective_to_depth:
222
*mat4_perspective_to_depth* is a fixed matrix defined as follow::
229
This uniform can be set once per frame. There is one uniform of that type per lamp casting shadow in the scene.
233
.. data:: GPU_DYNAMIC_LAMP_DYNENERGY
235
The uniform is a single float representing the lamp energy. In blender it corresponds
236
to the 'energy' attribute of the lamp data block.
237
There is one uniform of that type per lamp lighting the material.
241
.. data:: GPU_DYNAMIC_LAMP_DYNCOL
243
The uniform is a vector of 3 float representing the lamp color.
244
Color elements are between 0.0 and 1.0. In blender it corresponds
245
to the 'color' attribute of the lamp data block.
246
There is one uniform of that type per lamp lighting the material.
250
.. data:: GPU_DYNAMIC_SAMPLER_2DBUFFER
252
The uniform is an integer representing an internal texture used for certain effect
257
.. data:: GPU_DYNAMIC_SAMPLER_2DIMAGE
259
The uniform is an integer representing a texture loaded from an image file.
263
.. data:: GPU_DYNAMIC_SAMPLER_2DSHADOW
265
The uniform is an integer representing a shadow buffer corresponding to a lamp
276
Type of the vertex attribute used in the GLSL shader. Determines the mesh custom data
277
layer that contains the vertex attribute.
281
Vertex attribute is a UV Map. Data type is vector of 2 float.
283
There can be more than one attribute of that type, they are differenciated by name.
284
In blender, you can retrieve the attribute data with:
286
.. code-block:: python
288
mesh.uv_textures[attribute["name"]]
294
Vertex attribute is color layer. Data type is vector 4 unsigned byte (RGBA).
296
There can be more than one attribute of that type, they are differenciated by name.
297
In blender you can retrieve the attribute data with:
299
.. code-block:: python
301
mesh.vertex_colors[attribute["name"]]
307
Vertex attribute is original coordinates. Data type is vector 3 float.
309
There can be only 1 attribute of that type per shader.
310
In blender you can retrieve the attribute data with:
312
.. code-block:: python
320
Vertex attribute is the tangent vector. Data type is vector 4 float.
322
There can be only 1 attribute of that type per shader.
323
There is currently no way to retrieve this attribute data via the RNA API but a standalone
324
C function to compute the tangent layer from the other layers can be obtained from
335
.. function:: export_shader(scene,material)
337
Extracts the GLSL shader producing the visual effect of material in scene for the purpose of
338
reusing the shader in an external engine. This function is meant to be used in material exporter
339
so that the GLSL shader can be exported entirely. The return value is a dictionary containing the
340
shader source code and all associated data.
342
:arg scene: the scene in which the material in rendered.
343
:type scene: :class:`bpy.types.Scene`
344
:arg material: the material that you want to export the GLSL shader
345
:type material: :class:`bpy.types.Material`
346
:return: the shader source code and all associated data in a dictionary
349
The dictionary contains the following elements:
351
* ["fragment"] : string
352
fragment shader source code.
354
* ["vertex"] : string
355
vertex shader source code.
357
* ["uniforms"] : sequence
358
list of uniforms used in fragment shader, can be empty list. Each element of the
359
sequence is a dictionary with the following elements:
361
* ["varname"] : string
362
name of the uniform in the fragment shader. Always of the form 'unf<number>'.
364
* ["datatype"] : integer
365
data type of the uniform variable. Can be one of the following:
367
* :data:`gpu.GPU_DATA_1I` : use glUniform1i
368
* :data:`gpu.GPU_DATA_1F` : use glUniform1fv
369
* :data:`gpu.GPU_DATA_2F` : use glUniform2fv
370
* :data:`gpu.GPU_DATA_3F` : use glUniform3fv
371
* :data:`gpu.GPU_DATA_4F` : use glUniform4fv
372
* :data:`gpu.GPU_DATA_9F` : use glUniformMatrix3fv
373
* :data:`gpu.GPU_DATA_16F` : use glUniformMatrix4fv
376
type of uniform, determines the origin and method of calculation. See uniform-type_.
377
Depending on the type, more elements will be be present.
379
* ["lamp"] : :class:`bpy.types.Object`
380
Reference to the lamp object from which the uniforms value are extracted. Set for the following uniforms types:
385
* :data:`gpu.GPU_DYNAMIC_LAMP_DYNVEC`
386
* :data:`gpu.GPU_DYNAMIC_LAMP_DYNCO`
387
* :data:`gpu.GPU_DYNAMIC_LAMP_DYNIMAT`
388
* :data:`gpu.GPU_DYNAMIC_LAMP_DYNPERSMAT`
389
* :data:`gpu.GPU_DYNAMIC_LAMP_DYNENERGY`
390
* :data:`gpu.GPU_DYNAMIC_LAMP_DYNCOL`
391
* :data:`gpu.GPU_DYNAMIC_SAMPLER_2DSHADOW`
395
* The uniforms :data:`gpu.GPU_DYNAMIC_LAMP_DYNVEC`, :data:`gpu.GPU_DYNAMIC_LAMP_DYNCO`, :data:`gpu.GPU_DYNAMIC_LAMP_DYNIMAT` and :data:`gpu.GPU_DYNAMIC_LAMP_DYNPERSMAT`
396
refer to the lamp object position and orientation, both of can be derived from the object world matrix:
398
.. code-block:: python
400
obmat = uniform["lamp"].matrix_world
402
where obmat is the mat4_lamp_to_world_ matrix of the lamp as a 2 dimensional array,
403
the lamp world location location is in obmat[3].
405
* The uniform types :data:`gpu.GPU_DYNAMIC_LAMP_DYNENERGY` and :data:`gpu.GPU_DYNAMIC_LAMP_DYNCOL` refer to the lamp data bloc that you get from:
407
.. code-block:: python
409
la = uniform["lamp"].data
411
from which you get la.energy and la.color
413
* Lamp duplication is not supported: if you have duplicated lamps in your scene
414
(i.e. lamp that are instantiated by dupligroup, etc), this element will only
415
give you a reference to the orignal lamp and you will not know which instance
416
of the lamp it is refering too. You can still handle that case in the exporter
417
by distributing the uniforms amongst the duplicated lamps.
419
* ["image"] : :class:`bpy.types.Image`
420
Reference to the image databloc. Set for uniform type :data:`gpu.GPU_DYNAMIC_SAMPLER_2DIMAGE`. You can get the image data from:
422
.. code-block:: python
424
# full path to image file
425
uniform["image"].filepath
426
# image size as a 2-dimensional array of int
427
uniform["image"].size
429
* ["texnumber"] : integer
430
Channel number to which the texture is bound when drawing the object.
431
Set for uniform types :data:`gpu.GPU_DYNAMIC_SAMPLER_2DBUFFER`, :data:`gpu.GPU_DYNAMIC_SAMPLER_2DIMAGE` and :data:`gpu.GPU_DYNAMIC_SAMPLER_2DSHADOW`.
433
This is provided for information only: when reusing the shader outside blencer,
434
you are free to assign the textures to the channel of your choice and to pass
435
that number channel to the GPU in the uniform.
437
* ["texpixels"] : byte array
438
texture data for uniform type :data:`gpu.GPU_DYNAMIC_SAMPLER_2DBUFFER`. Although
439
the corresponding uniform is a 2D sampler, the texture is always a 1D texture
440
of n x 1 pixel. The texture size n is provided in ["texsize"] element.
441
These texture are only used for computer generated texture (colorband, etc).
442
The texture data is provided so that you can make a real image out of it in the
445
* ["texsize"] : integer
446
horizontal size of texture for uniform type :data:`gpu.GPU_DYNAMIC_SAMPLER_2DBUFFER`.
447
The texture data is in ["texpixels"].
449
* ["attributes"] : sequence
450
list of attributes used in vertex shader, can be empty. Blender doesn't use
451
standard attributes except for vertex position and normal. All other vertex
452
attributes must be passed using the generic glVertexAttrib functions.
453
The attribute data can be found in the derived mesh custom data using RNA.
454
Each element of the sequence is a dictionary containing the following elements:
456
* ["varname"] : string
457
name of the uniform in the vertex shader. Always of the form 'att<number>'.
459
* ["datatype"] : integer
460
data type of vertex attribute, can be one of the following:
462
* :data:`gpu.GPU_DATA_2F` : use glVertexAttrib2fv
463
* :data:`gpu.GPU_DATA_3F` : use glVertexAttrib3fv
464
* :data:`gpu.GPU_DATA_4F` : use glVertexAttrib4fv
465
* :data:`gpu.GPU_DATA_4UB` : use glVertexAttrib4ubv
467
* ["number"] : integer
468
generic attribute number. This is provided for information only. Blender
469
doesn't use glBindAttribLocation to place generic attributes at specific location,
470
it lets the shader compiler place the attributes automatically and query the
471
placement with glGetAttribLocation. The result of this placement is returned in
474
When using this shader in a render engine, you should either use
475
glBindAttribLocation to force the attribute at this location or use
476
glGetAttribLocation to get the placement chosen by the compiler of your GPU.
479
type of the mesh custom data from which the vertex attribute is loaded.
482
* ["name"] : string or integer
483
custom data layer name, used for attribute type :data:`gpu.CD_MTFACE` and :data:`gpu.CD_MCOL`.
487
.. code-block:: python
490
# get GLSL shader of material Mat.001 in scene Scene.001
491
scene = bpy.data.scenes["Scene.001"]
492
material = bpy.data.materials["Mat.001"]
493
shader = gpu.export_shader(scene,material)
494
# scan the uniform list and find the images used in the shader
495
for uniform in shader["uniforms"]:
496
if uniform["type"] == gpu.GPU_DYNAMIC_SAMPLER_2DIMAGE:
497
print("uniform {0} is using image {1}".format(uniform["varname"], uniform["image"].filepath))
498
# scan the attribute list and find the UV Map used in the shader
499
for attribute in shader["attributes"]:
500
if attribute["type"] == gpu.CD_MTFACE:
501
print("attribute {0} is using UV Map {1}".format(attribute["varname"], attribute["name"]))
507
.. _mat4_lamp_to_perspective:
509
1. Calculation of the *mat4_lamp_to_perspective* matrix for a spot lamp.
511
The following pseudo code shows how the *mat4_lamp_to_perspective* matrix is computed
512
in blender for uniforms of :data:`gpu.GPU_DYNAMIC_LAMP_DYNPERSMAT` type::
514
.. code-block:: python
516
#Get the lamp datablock with:
517
lamp = bpy.data.objects[uniform["lamp"]].data
519
# Compute the projection matrix:
520
# You will need these lamp attributes:
521
# lamp.clipsta : near clip plane in world unit
522
# lamp.clipend : far clip plane in world unit
523
# lamp.spotsize : angle in degree of the spot light
525
# The size of the projection plane is computed with the usual formula:
526
wsize = lamp.clista * tan(lamp.spotsize/2)
528
#And the projection matrix:
529
mat4_lamp_to_perspective = glFrustum(-wsize, wsize, -wsize, wsize, lamp.clista, lamp.clipend)
531
2. Creation of the shadow map for a spot lamp.
533
The shadow map is the depth buffer of a render performed by placing the camera at the
534
spot light position. The size of the shadow map is given by the attribute lamp.bufsize :
535
shadow map size in pixel, same size in both dimensions.