Files
XCEngine/docs/api/rhi/shader/set-mat4.md

40 lines
1.1 KiB
Markdown
Raw Normal View History

# RHIShader::SetMat4
```cpp
virtual void SetMat4(const char* name, const float* value) = 0;
```
2026-03-20 02:35:45 +08:00
设置 4x4 矩阵 uniform 变量。
通过名称查找并设置着色器中的 4x4 变换矩阵 uniform 变量。常见用途包括设置模型矩阵、视图矩阵、投影矩阵及其组合MVP、法线矩阵等。
**参数:**
2026-03-20 02:35:45 +08:00
- `name` - uniform 变量名称
- `value` - 指向 16 个 float 元素的数组指针
**线程安全:** ❌(需要在渲染线程调用)
**复杂度:** O(1)(通常为哈希表查找)
**示例:**
2026-03-20 02:35:45 +08:00
```cpp
RHIShader* shader = device->CreateShader();
shader->CompileFromFile(L"shaders/vertex.cso", "VSMain", "vs_5_0");
shader->Bind();
// 设置变换矩阵
float modelMatrix[16] = { /* 4x4 模型矩阵数据 */ };
float viewMatrix[16] = { /* 4x4 视图矩阵数据 */ };
float projMatrix[16] = { /* 4x4 投影矩阵数据 */ };
shader->SetMat4("u_model", modelMatrix);
shader->SetMat4("u_view", viewMatrix);
shader->SetMat4("u_proj", projMatrix);
```
## 相关文档
2026-03-20 02:35:45 +08:00
- [RHIShader](shader.md) - 返回类总览
- [`SetVec4`](set-vec4.md) - 设置四维向量 uniform