How to Pass in Matrix Programatically to Shader?


photo

Recommended Posts

Hi,

 

I was wondering if there are any examples of programmatically creating a shader, and more specifically, passing in a custom 4x4 matrix array to a shader? I see examples of vec4s throughout material examples, e.g.

<parameter name="normal_transform" type="expression" shared="1">vec4(1.0f,1.0f,0.0f,time)</parameter>

Are there any tips or examples that you can point me to do this? I have a 4x4 custom matrices (projection, rotation & translation) that I would like to pass into a shader that are generated only at runtime. 

 

Thanks!

Link to post

And a similar question for passing in an image. How do I pass in an image (1920 x 1080) that is generated dynamically at runtime (e.g., from a web camera) into a shader with the C++ API?

Link to post

Hi Rajinder,

 

Are there any tips or examples that you can point me to do this? I have a 4x4 custom matrices (projection, rotation & translation) that I would like to pass into a shader that are generated only at runtime. 

 

There is no mat4 structure inside material files, unfortuantely. You can pass matrix by splitting it by regular vec4 parameters (similar approach, but with 3x4 matrix you can find in core/shaders/default/mesh/vertex_base.shader). Also, you can use one of the available Shader::setParamterFloat() methods to pass up to 16 float variables directly to the shader.

 

And a similar question for passing in an image. How do I pass in an image (1920 x 1080) that is generated dynamically at runtime (e.g., from a web camera) into a shader with the C++ API?

 

For that purpose, I think one of the Material::setImageTexture() methods will be enough.

 

Thanks!

How to submit a good bug report
---
FTP server for test scenes and user uploads:

Link to post

Are you referring to setParameterFloatArray()? Can you give me an example of this or point me to a sample that uses this? 

 

I find it surprising that a mat4 structure isn't supported. Seems like a useful structure to include in a future release. 

Link to post