-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaterial.h
More file actions
216 lines (179 loc) · 4.78 KB
/
Material.h
File metadata and controls
216 lines (179 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#pragma once
#define GLM_ENABLE_EXPERIMENTAL
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include<iostream>
#include<vector>
#include<algorithm>
#include<glm/glm.hpp>
#include <glm/gtx/hash.hpp>
#include"GpuBufferFactory.h"
#include"DescriptorSetFactory.h"
#include"Object.h"
//シェーダ上のテクスチャの参照用番号
//マテリアルがそのテクスチャを持たない場合は、-1が記録される
struct TexCoordSets
{
int baseColor;
int metallicRoughness;
int specularGlossiness;
int normal;
int occlusion;
int emissive;
void init()
{
baseColor = -1;
metallicRoughness = -1;
specularGlossiness = -1;
normal = -1;
occlusion = -1;
emissive = -1;
};
};
struct MaterialProperty
{
TexCoordSets texCoordSet;
std::shared_ptr<Texture> baseColorTexture;
std::shared_ptr<Texture> metallicRoughnessTexture;
std::shared_ptr<Texture> normalTexture;
std::shared_ptr<Texture> occlusionTexture;
std::shared_ptr<Texture> emissiveTexture;
glm::vec4 baseColorFactor;
glm::vec4 emissiveFactor;
float metallicFactor;
float roughnessFactor;
float alphaMaskCutoff;
float emissiveStrength;
void initProperty()
{
texCoordSet.init();
//透明度の最大値の設定
alphaMaskCutoff = 1.0f;
//メタリックの初期値
metallicFactor = 1.0f;
//粗さの初期値
roughnessFactor = 1.0f;
//自己放射の強さ
emissiveStrength = 1.0f;
//基本色の設定
baseColorFactor = glm::vec4(1.0f);
//発光
emissiveFactor = glm::vec4(0.0f);
baseColorTexture = nullptr;
metallicRoughnessTexture = nullptr;
normalTexture = nullptr;
occlusionTexture = nullptr;
emissiveTexture = nullptr;
}
void Build(std::shared_ptr<Texture> emptyTexture)
{
if (!baseColorTexture)
{
baseColorTexture = emptyTexture;
}
if (!metallicRoughnessTexture)
{
metallicRoughnessTexture = emptyTexture;
}
if (!normalTexture)
{
normalTexture = emptyTexture;
}
if (!occlusionTexture)
{
occlusionTexture = emptyTexture;
}
if (!emissiveTexture)
{
emissiveTexture = emptyTexture;
}
}
};
//シェーダ上でアクセスるためのマテリアル
//テクスチャに対する係数がメイン
struct ShaderMaterial
{
glm::vec4 baseColorFactor;
glm::vec4 emissiveFactor;
glm::vec4 diffuseFactor;
glm::vec4 specularFactor;
int colorTextureSet;
int PhysicalDescriptorTextureSet;
int normalTextureSet;
int occlusionTextureSet;
int emissiveTextureSet;
float metallicFactor;
float roughnessFactor;
float alphaMask;
float alphaMaskCutoff;
float emissiveStrength;
void init(const MaterialProperty& prop)
{
baseColorFactor = prop.baseColorFactor;
emissiveFactor = prop.emissiveFactor;
diffuseFactor = glm::vec4(1.0f);
specularFactor = glm::vec4(1.0f);
colorTextureSet = prop.texCoordSet.baseColor;
PhysicalDescriptorTextureSet = prop.texCoordSet.metallicRoughness;
normalTextureSet = prop.texCoordSet.normal;
occlusionTextureSet = prop.texCoordSet.occlusion;
emissiveTextureSet = prop.texCoordSet.emissive;
metallicFactor = prop.metallicFactor;
roughnessFactor = prop.roughnessFactor;
alphaMaskCutoff = prop.alphaMaskCutoff;
alphaMask = 1.0f - alphaMaskCutoff; //透明度の最大値
emissiveStrength = prop.emissiveStrength;
}
};
//gltfモデルのマテリアル
class Material
{
std::shared_ptr<GpuBufferFactory> bufferFactory;
std::shared_ptr<DescriptorSetLayoutFactory> layoutFactory;
std::shared_ptr<DescriptorSetFactory> descriptorSetFactory;
//gpu上のテクスチャに対する係数用のバッファ
std::shared_ptr<GpuBuffer> shaderMaterialBuffer;
//gpu上のマテリアルに関するデータを紐づけるもの
//レンダリング時に使用
std::shared_ptr<DescriptorSet> descriptorSet;
//シェーダー上のマテリアルデータを記録するバッファを作成
void createBuffer();
//ディスクリプタセットを作成
void createDescriptorSet(const MaterialProperty& prop);
public:
Material(const MaterialProperty& prop, std::shared_ptr<GpuBufferFactory> bf,
std::shared_ptr<DescriptorSetLayoutFactory> lf, std::shared_ptr<DescriptorSetFactory> df)
{
bufferFactory = bf;
layoutFactory = lf;
descriptorSetFactory = df;
shaderMaterial.init(prop);
texCoordSets = prop.texCoordSet;
//gpuリソースの作成
createBuffer();
createDescriptorSet(prop);
//更新した構造体の値をgpu上のバッファにコピー
bufferFactory->copyMemory(
sizeof(ShaderMaterial),
&shaderMaterial,
shaderMaterialBuffer
);
}
enum AlphaMode { ALPHAMODE_OPAQUE, ALPHAMODE_MASK, ALPHAMODE_BLEND };
AlphaMode alphaMode = ALPHAMODE_OPAQUE;
//gltfモデル内のマテリアルの番号
//gltfモデルのメッシュが持つマテリアルを参照する際に使用
int index = 0;
//テクスチャが従うuvの種類を記録,-1の場合はそのテクスチャを持たないことを示す
TexCoordSets texCoordSets;
//テクスチャに対する係数
ShaderMaterial shaderMaterial;
std::shared_ptr<DescriptorSet> getDescriptorSet() const
{
return descriptorSet;
}
std::shared_ptr<GpuBuffer> getShaderMaterialBuffer() const
{
return shaderMaterialBuffer;
}
};