2012-02-09 06:38:54 +00:00
|
|
|
*******************
|
2011-09-09 11:55:38 +00:00
|
|
|
GPU functions (gpu)
|
2012-02-09 06:38:54 +00:00
|
|
|
*******************
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2011-10-13 06:59:09 +00:00
|
|
|
.. module:: gpu
|
|
|
|
|
|
|
|
This module provides access to materials GLSL shaders.
|
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
|
2011-09-09 11:55:38 +00:00
|
|
|
Intro
|
2012-02-09 06:38:54 +00:00
|
|
|
=====
|
2011-09-09 11:55:38 +00:00
|
|
|
|
|
|
|
Module to provide functions concerning the GPU implementation in Blender, in particular
|
|
|
|
the GLSL shaders that blender generates automatically to render materials in the 3D view
|
|
|
|
and in the game engine.
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
The API provided by this module is subject to change.
|
|
|
|
The data exposed by the API are are closely related to Blender's internal GLSL code
|
|
|
|
and may change if the GLSL code is modified (e.g. new uniform type).
|
2011-09-09 11:55:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
Constants
|
2012-02-09 06:38:54 +00:00
|
|
|
=========
|
|
|
|
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
GLSL Data Type
|
2011-09-09 11:55:38 +00:00
|
|
|
--------------
|
|
|
|
|
|
|
|
.. _data-type:
|
|
|
|
|
|
|
|
Type of GLSL data.
|
2015-09-07 06:02:46 +00:00
|
|
|
For shader uniforms, the data type determines which ``glUniform`` function
|
2011-09-09 11:55:38 +00:00
|
|
|
variant to use to send the uniform value to the GPU.
|
2015-09-07 06:02:46 +00:00
|
|
|
For vertex attributes, the data type determines which ``glVertexAttrib`` function
|
2011-09-09 11:55:38 +00:00
|
|
|
variant to use to send the vertex attribute to the GPU.
|
|
|
|
|
|
|
|
See export_shader_
|
|
|
|
|
|
|
|
.. data:: GPU_DATA_1I
|
|
|
|
|
|
|
|
one integer
|
|
|
|
|
|
|
|
.. data:: GPU_DATA_1F
|
|
|
|
|
|
|
|
one float
|
|
|
|
|
|
|
|
.. data:: GPU_DATA_2F
|
|
|
|
|
|
|
|
two floats
|
|
|
|
|
|
|
|
.. data:: GPU_DATA_3F
|
|
|
|
|
|
|
|
three floats
|
2012-02-09 06:38:54 +00:00
|
|
|
|
2011-09-09 11:55:38 +00:00
|
|
|
.. data:: GPU_DATA_4F
|
|
|
|
|
|
|
|
four floats
|
2012-02-09 06:38:54 +00:00
|
|
|
|
2011-09-09 11:55:38 +00:00
|
|
|
.. data:: GPU_DATA_9F
|
|
|
|
|
|
|
|
matrix 3x3 in column-major order
|
|
|
|
|
|
|
|
.. data:: GPU_DATA_16F
|
|
|
|
|
|
|
|
matrix 4x4 in column-major order
|
|
|
|
|
|
|
|
.. data:: GPU_DATA_4UB
|
|
|
|
|
|
|
|
four unsigned byte
|
|
|
|
|
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
GLSL Uniform Types
|
|
|
|
------------------
|
2011-09-09 11:55:38 +00:00
|
|
|
|
|
|
|
.. _uniform-type:
|
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
Constants that specify the type of uniform used in a GLSL shader.
|
2011-09-09 11:55:38 +00:00
|
|
|
The uniform type determines the data type, origin and method
|
2012-02-09 06:38:54 +00:00
|
|
|
of calculation used by Blender to compute the uniform value.
|
2011-09-09 11:55:38 +00:00
|
|
|
|
|
|
|
The calculation of some of the uniforms is based on matrices available in the scene:
|
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
.. _mat4_cam_to_world:
|
|
|
|
.. _mat4_world_to_cam:
|
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
``mat4_cam_to_world``
|
2012-02-09 06:38:54 +00:00
|
|
|
Model matrix of the camera. OpenGL 4x4 matrix that converts
|
|
|
|
camera local coordinates to world coordinates. In blender this is obtained from the
|
|
|
|
'matrix_world' attribute of the camera object.
|
|
|
|
|
|
|
|
Some uniform will need the *mat4_world_to_cam*
|
|
|
|
matrix computed as the inverse of this matrix.
|
|
|
|
|
|
|
|
.. _mat4_object_to_world:
|
|
|
|
.. _mat4_world_to_object:
|
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
``mat4_object_to_world``
|
2012-02-09 06:38:54 +00:00
|
|
|
Model matrix of the object that is being rendered. OpenGL 4x4 matric that converts
|
|
|
|
object local coordinates to world coordinates. In blender this is obtained from the
|
|
|
|
'matrix_world' attribute of the object.
|
|
|
|
|
|
|
|
Some uniform will need the *mat4_world_to_object* matrix, computed as the inverse of this matrix.
|
|
|
|
|
|
|
|
.. _mat4_lamp_to_world:
|
|
|
|
.. _mat4_world_to_lamp:
|
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
``mat4_lamp_to_world``
|
2012-02-09 06:38:54 +00:00
|
|
|
Model matrix of the lamp lighting the object. OpenGL 4x4 matrix that converts lamp
|
|
|
|
local coordinates to world coordinates. In blender this is obtained from the
|
|
|
|
'matrix_world' attribute of the lamp object.
|
|
|
|
|
|
|
|
Some uniform will need the *mat4_world_to_lamp* matrix
|
|
|
|
computed as the inverse of this matrix.
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
Any uniforms used for view projections or transformations (object, lamp matrices for eg),
|
|
|
|
can only be set once per frame.
|
|
|
|
|
|
|
|
|
|
|
|
GLSL Object Uniforms
|
|
|
|
^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
- Object transformations and color must be set before drawing the object.
|
|
|
|
- There is at most one uniform of these types per shader.
|
|
|
|
|
2011-09-09 11:55:38 +00:00
|
|
|
.. data:: GPU_DYNAMIC_OBJECT_VIEWMAT
|
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
A matrix that converts world coordinates to camera coordinates (see mat4_world_to_cam_).
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
:type: matrix4x4
|
2011-09-09 11:55:38 +00:00
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_OBJECT_MAT
|
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
A matrix that converts object coordinates to world coordinates (see mat4_object_to_world_).
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
:type: matrix4x4
|
2011-09-09 11:55:38 +00:00
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_OBJECT_VIEWIMAT
|
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
The uniform is a 4x4 GL matrix that converts coordinates
|
|
|
|
in camera space to world coordinates (see mat4_cam_to_world_).
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
:type: matrix4x4
|
2011-09-09 11:55:38 +00:00
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_OBJECT_IMAT
|
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
The uniform is a 4x4 GL matrix that converts world coodinates
|
|
|
|
to object coordinates (see mat4_world_to_object_).
|
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
:type: matrix4x4
|
2012-02-09 06:38:54 +00:00
|
|
|
|
2011-09-09 11:55:38 +00:00
|
|
|
.. data:: GPU_DYNAMIC_OBJECT_COLOR
|
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
An RGB color + alpha defined at object level.
|
|
|
|
Each values between 0.0 and 1.0.
|
2012-02-09 06:38:54 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
See :class:`bpy.types.Object.color`.
|
|
|
|
|
|
|
|
:type: float4
|
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_OBJECT_AUTOBUMPSCALE
|
|
|
|
|
|
|
|
Multiplier for bump-map scaling.
|
|
|
|
|
|
|
|
:type: float
|
2012-02-09 06:38:54 +00:00
|
|
|
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
GLSL Lamp Uniforms
|
|
|
|
^^^^^^^^^^^^^^^^^^
|
2012-02-09 06:38:54 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
.. note::
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
There is one uniform of that type per lamp lighting the material.
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
.. data:: GPU_DYNAMIC_LAMP_DYNVEC
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
Represents the direction of light in camera space.
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
Computed as:
|
|
|
|
mat4_world_to_cam_ * (-vec3_lamp_Z_axis)
|
2012-02-09 06:38:54 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
.. note::
|
2016-02-15 08:37:37 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
- The lamp Z axis points to the opposite direction of light.
|
|
|
|
- The norm of the vector should be unit length.
|
2012-02-09 06:38:54 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
:type: float3
|
2012-02-09 06:38:54 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
.. data:: GPU_DYNAMIC_LAMP_DYNCO
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
Represents the position of the light in camera space.
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
Computed as:
|
|
|
|
mat4_world_to_cam_ * vec3_lamp_pos
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
:type: float3
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
.. data:: GPU_DYNAMIC_LAMP_DYNIMAT
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
Matrix that converts vector in camera space to lamp space.
|
|
|
|
|
|
|
|
Computed as:
|
|
|
|
mat4_world_to_lamp_ * mat4_cam_to_world_
|
|
|
|
|
|
|
|
:type: matrix4x4
|
2011-09-09 11:55:38 +00:00
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_LAMP_DYNPERSMAT
|
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
Matrix that converts a vector in camera space to shadow buffer depth space.
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
Computed as:
|
|
|
|
mat4_perspective_to_depth_ * mat4_lamp_to_perspective_ * mat4_world_to_lamp_ * mat4_cam_to_world_.
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
.. _mat4_perspective_to_depth:
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
``mat4_perspective_to_depth`` is a fixed matrix defined as follow::
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
0.5 0.0 0.0 0.5
|
|
|
|
0.0 0.5 0.0 0.5
|
|
|
|
0.0 0.0 0.5 0.5
|
|
|
|
0.0 0.0 0.0 1.0
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
.. note::
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
- There is one uniform of that type per lamp casting shadow in the scene.
|
|
|
|
|
|
|
|
:type: matrix4x4
|
2011-09-09 11:55:38 +00:00
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_LAMP_DYNENERGY
|
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
See :class:`bpy.types.Lamp.energy`.
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
:type: float
|
2011-09-09 11:55:38 +00:00
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_LAMP_DYNCOL
|
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
See :class:`bpy.types.Lamp.color`.
|
|
|
|
|
|
|
|
:type: float3
|
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_LAMP_DISTANCE
|
|
|
|
|
|
|
|
See :class:`bpy.types.Lamp.distance`.
|
|
|
|
|
|
|
|
:type: float
|
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_LAMP_ATT1
|
|
|
|
|
|
|
|
See
|
|
|
|
:class:`bpy.types.PointLamp.linear_attenuation`,
|
|
|
|
:class:`bpy.types.SpotLamp.linear_attenuation`.
|
|
|
|
|
|
|
|
:type: float
|
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_LAMP_ATT2
|
|
|
|
|
|
|
|
See
|
|
|
|
:class:`bpy.types.PointLamp.quadratic_attenuation`,
|
|
|
|
:class:`bpy.types.SpotLamp.quadratic_attenuation`.
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
:type: float
|
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_LAMP_SPOTSIZE
|
|
|
|
|
|
|
|
See :class:`bpy.types.SpotLamp.spot_size`.
|
|
|
|
|
|
|
|
:type: float
|
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_LAMP_SPOTBLEND
|
|
|
|
|
|
|
|
See :class:`bpy.types.SpotLamp.spot_blend`.
|
|
|
|
|
|
|
|
:type: float
|
|
|
|
|
2015-10-15 11:36:31 +00:00
|
|
|
.. data:: GPU_DYNAMIC_LAMP_SPOTSCALE
|
|
|
|
|
|
|
|
Represents the SpotLamp local scale.
|
|
|
|
|
|
|
|
:type: float2
|
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
|
|
|
|
GLSL Sampler Uniforms
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^
|
2011-09-09 11:55:38 +00:00
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_SAMPLER_2DBUFFER
|
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
Represents an internal texture used for certain effect
|
2012-02-09 06:38:54 +00:00
|
|
|
(color band, etc).
|
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
:type: integer
|
2011-09-09 11:55:38 +00:00
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_SAMPLER_2DIMAGE
|
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
Represents a texture loaded from an image file.
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
:type: integer
|
2011-09-09 11:55:38 +00:00
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_SAMPLER_2DSHADOW
|
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
Represents a texture loaded from a shadow buffer file.
|
2012-11-22 05:15:52 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
:type: integer
|
2012-11-22 05:15:52 +00:00
|
|
|
|
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
GLSL Mist Uniforms
|
|
|
|
^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_MIST_ENABLE:
|
|
|
|
|
|
|
|
See :class:`bpy.types.WorldMistSettings.use_mist`.
|
|
|
|
|
|
|
|
:type: float (0 or 1)
|
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_MIST_START
|
2012-02-09 06:38:54 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
See :class:`bpy.types.WorldMistSettings.start`.
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
:type: float
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
See :class:`bpy.types.WorldMistSettings.depth`.
|
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_MIST_DISTANCE
|
|
|
|
|
|
|
|
:type: float
|
|
|
|
|
|
|
|
See :class:`bpy.types.WorldMistSettings.intensity`.
|
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_MIST_INTENSITY
|
|
|
|
|
|
|
|
:type: float
|
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_MIST_TYPE
|
|
|
|
|
|
|
|
See :class:`bpy.types.WorldMistSettings.falloff`.
|
|
|
|
|
|
|
|
:type: float (used as an index into the type)
|
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_MIST_COLOR
|
|
|
|
|
|
|
|
|
|
|
|
GLSL World Uniforms
|
|
|
|
^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_HORIZON_COLOR
|
|
|
|
|
|
|
|
See :class:`bpy.types.World.horizon_color`.
|
|
|
|
|
|
|
|
:type: float3
|
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_AMBIENT_COLOR
|
|
|
|
|
|
|
|
See :class:`bpy.types.World.ambient_color`.
|
|
|
|
|
|
|
|
:type: float3
|
|
|
|
|
|
|
|
|
|
|
|
GLSL Material Uniforms
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_MAT_DIFFRGB
|
|
|
|
|
|
|
|
See :class:`bpy.types.Material.diffuse_color`.
|
|
|
|
|
|
|
|
:type: float3
|
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_MAT_REF
|
|
|
|
|
|
|
|
See :class:`bpy.types.Material.diffuse_intensity`.
|
|
|
|
|
|
|
|
:type: float
|
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_MAT_SPECRGB
|
|
|
|
|
|
|
|
See :class:`bpy.types.Material.specular_color`.
|
|
|
|
|
|
|
|
:type: float3
|
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_MAT_SPEC
|
|
|
|
|
|
|
|
See :class:`bpy.types.Material.specular_intensity`.
|
|
|
|
|
|
|
|
:type: float
|
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_MAT_HARD
|
|
|
|
|
|
|
|
See :class:`bpy.types.Material.specular_hardness`.
|
|
|
|
|
|
|
|
:type: float
|
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_MAT_EMIT
|
|
|
|
|
|
|
|
See :class:`bpy.types.Material.emit`.
|
|
|
|
|
|
|
|
:type: float
|
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_MAT_AMB
|
|
|
|
|
|
|
|
See :class:`bpy.types.Material.ambient`.
|
|
|
|
|
|
|
|
:type: float
|
|
|
|
|
|
|
|
.. data:: GPU_DYNAMIC_MAT_ALPHA
|
|
|
|
|
|
|
|
See :class:`bpy.types.Material.alpha`.
|
|
|
|
|
|
|
|
:type: float
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GLSL Attribute Type
|
2011-09-09 11:55:38 +00:00
|
|
|
-------------------
|
|
|
|
|
|
|
|
.. _attribute-type:
|
|
|
|
|
|
|
|
Type of the vertex attribute used in the GLSL shader. Determines the mesh custom data
|
2012-02-09 06:38:54 +00:00
|
|
|
layer that contains the vertex attribute.
|
2011-09-09 11:55:38 +00:00
|
|
|
|
|
|
|
.. data:: CD_MTFACE
|
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
Vertex attribute is a UV Map. Data type is vector of 2 float.
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
There can be more than one attribute of that type, they are differenciated by name.
|
|
|
|
In blender, you can retrieve the attribute data with:
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
.. code-block:: python
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
mesh.uv_layers[attribute["name"]]
|
2011-09-09 11:55:38 +00:00
|
|
|
|
|
|
|
.. data:: CD_MCOL
|
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
Vertex attribute is color layer. Data type is vector 4 unsigned byte (RGBA).
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
There can be more than one attribute of that type, they are differenciated by name.
|
|
|
|
In blender you can retrieve the attribute data with:
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
.. code-block:: python
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
mesh.vertex_colors[attribute["name"]]
|
2011-09-09 11:55:38 +00:00
|
|
|
|
|
|
|
.. data:: CD_ORCO
|
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
Vertex attribute is original coordinates. Data type is vector 3 float.
|
|
|
|
|
|
|
|
There can be only 1 attribute of that type per shader.
|
|
|
|
In blender you can retrieve the attribute data with:
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
.. code-block:: python
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
mesh.vertices
|
2011-09-09 11:55:38 +00:00
|
|
|
|
|
|
|
.. data:: CD_TANGENT
|
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
Vertex attribute is the tangent vector. Data type is vector 4 float.
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
There can be only 1 attribute of that type per shader.
|
|
|
|
There is currently no way to retrieve this attribute data via the RNA API but a standalone
|
|
|
|
C function to compute the tangent layer from the other layers can be obtained from
|
|
|
|
blender.org.
|
|
|
|
|
2011-09-09 11:55:38 +00:00
|
|
|
|
|
|
|
Functions
|
2012-02-09 06:38:54 +00:00
|
|
|
=========
|
2011-09-09 11:55:38 +00:00
|
|
|
|
|
|
|
.. _export_shader:
|
|
|
|
|
|
|
|
.. function:: export_shader(scene,material)
|
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
Extracts the GLSL shader producing the visual effect of material in scene for the purpose of
|
2015-09-07 06:02:46 +00:00
|
|
|
reusing the shader in an external engine.
|
|
|
|
|
|
|
|
This function is meant to be used in material exporter
|
|
|
|
so that the GLSL shader can be exported entirely.
|
|
|
|
|
|
|
|
The return value is a dictionary containing the
|
2012-02-09 06:38:54 +00:00
|
|
|
shader source code and all associated data.
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
:arg scene: the scene in which the material in rendered.
|
|
|
|
:type scene: :class:`bpy.types.Scene`
|
|
|
|
:arg material: the material that you want to export the GLSL shader
|
|
|
|
:type material: :class:`bpy.types.Material`
|
|
|
|
:return: the shader source code and all associated data in a dictionary
|
|
|
|
:rtype: dictionary
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
The dictionary contains the following elements:
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
- ``["fragment"]``: string
|
2012-02-09 06:38:54 +00:00
|
|
|
fragment shader source code.
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
- ``["vertex"]``: string
|
2012-02-09 06:38:54 +00:00
|
|
|
vertex shader source code.
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
- ``["uniforms"]``: sequence
|
2012-02-09 06:38:54 +00:00
|
|
|
list of uniforms used in fragment shader, can be empty list. Each element of the
|
|
|
|
sequence is a dictionary with the following elements:
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
- ``["varname"]``: string
|
2012-02-09 06:38:54 +00:00
|
|
|
name of the uniform in the fragment shader. Always of the form 'unf<number>'.
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
- ``["datatype"]``: integer
|
2012-02-09 06:38:54 +00:00
|
|
|
data type of the uniform variable. Can be one of the following:
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
.. hlist::
|
|
|
|
:columns: 2
|
|
|
|
|
|
|
|
- :data:`gpu.GPU_DATA_1I` : use ``glUniform1i``
|
|
|
|
- :data:`gpu.GPU_DATA_1F` : use ``glUniform1fv``
|
|
|
|
- :data:`gpu.GPU_DATA_2F` : use ``glUniform2fv``
|
|
|
|
- :data:`gpu.GPU_DATA_3F` : use ``glUniform3fv``
|
|
|
|
- :data:`gpu.GPU_DATA_4F` : use ``glUniform4fv``
|
|
|
|
- :data:`gpu.GPU_DATA_9F` : use ``glUniformMatrix3fv``
|
|
|
|
- :data:`gpu.GPU_DATA_16F` : use ``glUniformMatrix4fv``
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
- ``["type"]``: integer
|
2012-02-09 06:38:54 +00:00
|
|
|
type of uniform, determines the origin and method of calculation. See uniform-type_.
|
|
|
|
Depending on the type, more elements will be be present.
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
- ``["lamp"]``: :class:`bpy.types.Object`
|
|
|
|
Reference to the lamp object from which the uniforms value are extracted.
|
|
|
|
Set for the following uniforms types:
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
.. hlist::
|
2015-09-07 06:02:46 +00:00
|
|
|
:columns: 2
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
- :data:`gpu.GPU_DYNAMIC_LAMP_DYNVEC`
|
|
|
|
- :data:`gpu.GPU_DYNAMIC_LAMP_DYNCO`
|
|
|
|
- :data:`gpu.GPU_DYNAMIC_LAMP_DYNIMAT`
|
|
|
|
- :data:`gpu.GPU_DYNAMIC_LAMP_DYNPERSMAT`
|
|
|
|
- :data:`gpu.GPU_DYNAMIC_LAMP_DYNENERGY`
|
|
|
|
- :data:`gpu.GPU_DYNAMIC_LAMP_DYNCOL`
|
|
|
|
- :data:`gpu.GPU_DYNAMIC_SAMPLER_2DSHADOW`
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
Notes:
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
- 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`
|
|
|
|
refer to the lamp object position and orientation,
|
|
|
|
both of can be derived from the object world matrix:
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
.. code-block:: python
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
obmat = uniform["lamp"].matrix_world
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
where obmat is the mat4_lamp_to_world_ matrix of the lamp as a 2 dimensional array,
|
2015-09-07 06:02:46 +00:00
|
|
|
the lamp world location location is in ``obmat[3]``.
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
- 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:
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
.. code-block:: python
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
la = uniform["lamp"].data
|
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
from which you get ``lamp.energy`` and ``lamp.color``
|
2012-02-09 06:38:54 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
- Lamp duplication is not supported: if you have duplicated lamps in your scene
|
2012-02-09 06:38:54 +00:00
|
|
|
(i.e. lamp that are instantiated by dupligroup, etc), this element will only
|
|
|
|
give you a reference to the orignal lamp and you will not know which instance
|
|
|
|
of the lamp it is refering too. You can still handle that case in the exporter
|
|
|
|
by distributing the uniforms amongst the duplicated lamps.
|
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
- ``["image"]``: :class:`bpy.types.Image`
|
|
|
|
Reference to the image databloc.
|
|
|
|
Set for uniform type
|
|
|
|
:data:`gpu.GPU_DYNAMIC_SAMPLER_2DIMAGE`.
|
|
|
|
You can get the image data from:
|
2012-02-09 06:38:54 +00:00
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
# full path to image file
|
|
|
|
uniform["image"].filepath
|
|
|
|
# image size as a 2-dimensional array of int
|
|
|
|
uniform["image"].size
|
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
- ``["texnumber"]``: integer
|
2012-02-09 06:38:54 +00:00
|
|
|
Channel number to which the texture is bound when drawing the object.
|
2015-09-07 06:02:46 +00:00
|
|
|
Set for uniform types
|
|
|
|
:data:`gpu.GPU_DYNAMIC_SAMPLER_2DBUFFER`,
|
|
|
|
:data:`gpu.GPU_DYNAMIC_SAMPLER_2DIMAGE` and
|
|
|
|
:data:`gpu.GPU_DYNAMIC_SAMPLER_2DSHADOW`.
|
2012-02-09 06:38:54 +00:00
|
|
|
|
|
|
|
This is provided for information only: when reusing the shader outside blencer,
|
|
|
|
you are free to assign the textures to the channel of your choice and to pass
|
|
|
|
that number channel to the GPU in the uniform.
|
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
- ``["texpixels"]``: byte array
|
|
|
|
texture data for uniform type :data:`gpu.GPU_DYNAMIC_SAMPLER_2DBUFFER`.
|
|
|
|
Although the corresponding uniform is a 2D sampler,
|
|
|
|
the texture is always a 1D texture of n x 1 pixel.
|
|
|
|
The texture size n is provided in ["texsize"] element.
|
2012-02-09 06:38:54 +00:00
|
|
|
These texture are only used for computer generated texture (colorband, etc).
|
2015-09-07 06:02:46 +00:00
|
|
|
The texture data is provided so that you can make a real image out of it in the exporter.
|
2012-02-09 06:38:54 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
- ``["texsize"]``: integer
|
2012-02-09 06:38:54 +00:00
|
|
|
horizontal size of texture for uniform type :data:`gpu.GPU_DYNAMIC_SAMPLER_2DBUFFER`.
|
|
|
|
The texture data is in ["texpixels"].
|
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
- ``["attributes"]``: sequence
|
2012-02-09 06:38:54 +00:00
|
|
|
list of attributes used in vertex shader, can be empty. Blender doesn't use
|
|
|
|
standard attributes except for vertex position and normal. All other vertex
|
2015-09-07 06:02:46 +00:00
|
|
|
attributes must be passed using the generic ``glVertexAttrib`` functions.
|
2012-02-09 06:38:54 +00:00
|
|
|
The attribute data can be found in the derived mesh custom data using RNA.
|
|
|
|
Each element of the sequence is a dictionary containing the following elements:
|
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
- ``["varname"]``: string
|
2012-02-09 06:38:54 +00:00
|
|
|
name of the uniform in the vertex shader. Always of the form 'att<number>'.
|
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
- ``["datatype"]``: integer
|
2012-02-09 06:38:54 +00:00
|
|
|
data type of vertex attribute, can be one of the following:
|
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
- :data:`gpu.GPU_DATA_2F`: use ``glVertexAttrib2fv``
|
|
|
|
- :data:`gpu.GPU_DATA_3F`: use ``glVertexAttrib3fv``
|
|
|
|
- :data:`gpu.GPU_DATA_4F`: use ``glVertexAttrib4fv``
|
|
|
|
- :data:`gpu.GPU_DATA_4UB`: use ``glVertexAttrib4ubv``
|
2012-02-09 06:38:54 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
- ``["number"]``: integer
|
|
|
|
Generic attribute number. This is provided for information only.
|
|
|
|
Blender doesn't use ``glBindAttribLocation`` to place generic attributes at specific location,
|
2012-02-09 06:38:54 +00:00
|
|
|
it lets the shader compiler place the attributes automatically and query the
|
2015-09-07 06:02:46 +00:00
|
|
|
placement with ``glGetAttribLocation``.
|
|
|
|
The result of this placement is returned in this element.
|
2012-02-09 06:38:54 +00:00
|
|
|
|
|
|
|
When using this shader in a render engine, you should either use
|
2015-09-07 06:02:46 +00:00
|
|
|
``glBindAttribLocation`` to force the attribute at this location or use
|
|
|
|
``glGetAttribLocation`` to get the placement chosen by the compiler of your GPU.
|
2012-02-09 06:38:54 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
- ``["type"]``: integer
|
2012-02-09 06:38:54 +00:00
|
|
|
type of the mesh custom data from which the vertex attribute is loaded.
|
|
|
|
See attribute-type_.
|
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
- ``["name"]``: string or integer
|
2012-02-09 06:38:54 +00:00
|
|
|
custom data layer name, used for attribute type :data:`gpu.CD_MTFACE` and :data:`gpu.CD_MCOL`.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
import gpu
|
|
|
|
# get GLSL shader of material Mat.001 in scene Scene.001
|
|
|
|
scene = bpy.data.scenes["Scene.001"]
|
|
|
|
material = bpy.data.materials["Mat.001"]
|
|
|
|
shader = gpu.export_shader(scene,material)
|
|
|
|
# scan the uniform list and find the images used in the shader
|
|
|
|
for uniform in shader["uniforms"]:
|
|
|
|
if uniform["type"] == gpu.GPU_DYNAMIC_SAMPLER_2DIMAGE:
|
|
|
|
print("uniform {0} is using image {1}".format(uniform["varname"], uniform["image"].filepath))
|
|
|
|
# scan the attribute list and find the UV Map used in the shader
|
|
|
|
for attribute in shader["attributes"]:
|
|
|
|
if attribute["type"] == gpu.CD_MTFACE:
|
|
|
|
print("attribute {0} is using UV Map {1}".format(attribute["varname"], attribute["name"]))
|
2011-09-09 11:55:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
Notes
|
2012-02-09 06:38:54 +00:00
|
|
|
=====
|
2011-09-09 11:55:38 +00:00
|
|
|
|
|
|
|
.. _mat4_lamp_to_perspective:
|
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
#. Calculation of the ``mat4_lamp_to_perspective`` matrix for a spot lamp.
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
The following pseudo code shows how the ``mat4_lamp_to_perspective`` matrix is computed
|
2013-12-23 06:24:23 +00:00
|
|
|
in blender for uniforms of :data:`gpu.GPU_DYNAMIC_LAMP_DYNPERSMAT` type:
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
.. code-block:: python
|
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
# Get the lamp datablock with:
|
2012-02-09 06:38:54 +00:00
|
|
|
lamp = bpy.data.objects[uniform["lamp"]].data
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
# Compute the projection matrix:
|
|
|
|
# You will need these lamp attributes:
|
|
|
|
# lamp.clipsta : near clip plane in world unit
|
|
|
|
# lamp.clipend : far clip plane in world unit
|
|
|
|
# lamp.spotsize : angle in degree of the spot light
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2012-02-09 06:38:54 +00:00
|
|
|
# The size of the projection plane is computed with the usual formula:
|
|
|
|
wsize = lamp.clista * tan(lamp.spotsize/2)
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
# And the projection matrix:
|
2012-02-09 06:38:54 +00:00
|
|
|
mat4_lamp_to_perspective = glFrustum(-wsize, wsize, -wsize, wsize, lamp.clista, lamp.clipend)
|
2011-09-09 11:55:38 +00:00
|
|
|
|
2015-09-07 06:02:46 +00:00
|
|
|
#. Creation of the shadow map for a spot lamp.
|
2011-09-09 11:55:38 +00:00
|
|
|
|
|
|
|
The shadow map is the depth buffer of a render performed by placing the camera at the
|
2015-09-07 06:02:46 +00:00
|
|
|
spot light position. The size of the shadow map is given by the attribute ``lamp.bufsize``:
|
2011-09-09 11:55:38 +00:00
|
|
|
shadow map size in pixel, same size in both dimensions.
|