Skip to content

Commit 72dfcfb

Browse files
committed
Update shaders.
1 parent 82a4b70 commit 72dfcfb

19 files changed

+347
-74
lines changed

examples/openglviewer/main.cc

+141-24
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ struct GLTexParams {
9696
GLenum wrapS{GL_REPEAT};
9797
GLenum wrapT{GL_REPEAT};
9898
std::array<float, 4> borderCol{0.0f, 0.0f, 0.0f, 0.0f}; // transparent black
99+
100+
// Use 3x3 mat to support pivot transform.
101+
tinyusdz::tydra::mat3 uv_transform{tinyusdz::tydra::mat3::identity()};
99102
};
100103

101104
struct GLTexState {
@@ -104,6 +107,8 @@ struct GLTexState {
104107
uint32_t slot_id{0};
105108
GLuint tex_id; // glBindTexture id
106109
GLint u_tex{-1}; // sampler glUniform location
110+
111+
GLint u_transform; // texcoord transform
107112
};
108113

109114
template<typename T>
@@ -126,33 +131,21 @@ struct GLUniformFactor {
126131
struct GLUsdPreviewSurfaceState {
127132

128133
static constexpr auto kDiffuseColor = "diffuseColor";
129-
static constexpr auto kDiffuseColorTex = "diffuseColorTex";
130-
static constexpr auto kEmissionColor = "emissionColor";
131-
static constexpr auto kEmissionColorTex = "emissionColorTex";
134+
static constexpr auto kEmissiveColor = "emissiveColor";
132135
static constexpr auto kSpecularColor = "specularColor";
133-
static constexpr auto kSpecularColorTex = "specularColorTex";
134136
static constexpr auto kUseSpecularWorkflow = "useSpecularWorkflow";
135137
static constexpr auto kMetallic = "metallic";
136-
static constexpr auto kMetallicTex = "metallicTex";
137138
static constexpr auto kRoughness = "roughness";
138-
static constexpr auto kRoughnessTex = "roughnessTex";
139139
static constexpr auto kClearcoat = "clearcoat";
140-
static constexpr auto kClearcoatTex = "clearcoatTex";
141140
static constexpr auto kClearcoatRoughness = "clearcoatRoughness";
142-
static constexpr auto kClearcoatRoughnessTex = "clearcoatRoughnessTex";
143141
static constexpr auto kOpacity = "opacity";
144-
static constexpr auto kOpacityTex = "opacityTex";
145142
static constexpr auto kOpacityThreshold = "opacityThreshold";
146-
static constexpr auto kOpacityThresholdTex = "opacityThresholdTex";
147143
static constexpr auto kIor = "ior";
148-
static constexpr auto kIorTex = "iorTex";
149144
static constexpr auto kNormal = "normal";
150-
static constexpr auto kNormalTex = "normalTex";
151145
static constexpr auto kOcclusion = "occlusion";
152-
static constexpr auto kOcclusionTex = "occlusionTex";
153146

154147
GLTexOrFactor<tinyusdz::tydra::vec3> diffuseColor{{0.18f, 0.18f, 0.18f}};
155-
GLTexOrFactor<tinyusdz::tydra::vec3> emissionColor{{0.0f, 0.0f, 0.0f}};
148+
GLTexOrFactor<tinyusdz::tydra::vec3> emissiveColor{{0.0f, 0.0f, 0.0f}};
156149

157150
GLUniformFactor<int> useSpecularWorkflow{0}; // non-texturable
158151

@@ -176,24 +169,148 @@ struct GLUsdPreviewSurfaceState {
176169

177170
};
178171

179-
void SetupGLUsdPreviewSurface(
172+
template<typename T>
173+
bool SetupGLUsdPreviewSurfaceParam(
174+
const GLuint prog_id,
175+
const tinyusdz::tydra::RenderScene &scene,
176+
const std::string &base_shadername,
177+
const tinyusdz::tydra::ShaderParam<T> &s,
178+
179+
GLTexOrFactor<T> &dst)
180+
{
181+
if (s.is_texture()) {
182+
{
183+
std::string u_name = base_shadername + "Tex";
184+
GLint loc = glGetUniformLocation(prog_id, u_name.c_str());
185+
dst.tex.u_tex = loc;
186+
}
187+
188+
{
189+
std::string u_name = base_shadername + "TexTransform";
190+
GLint loc = glGetUniformLocation(prog_id, u_name.c_str());
191+
dst.tex.u_transform = loc;
192+
if (s.textureId < 0 || s.textureId >= scene.textures.size()) {
193+
std::cerr << "Invalid txtureId for " << base_shadername + "\n";
194+
} else {
195+
const tinyusdz::tydra::UVTexture &uvtex = scene.textures[size_t(s.textureId)];
196+
dst.tex.texParams.uv_transform = uvtex.transform;
197+
}
198+
}
199+
200+
} else {
201+
GLint loc = glGetUniformLocation(prog_id, base_shadername.c_str());
202+
if (loc < 0) {
203+
std::cerr << base_shadername << " uniform not found in the shader.\n";
204+
}
205+
dst.u_factor = loc;
206+
dst.factor = s.value;
207+
}
208+
209+
return true;
210+
}
211+
212+
bool ReloadShader(
213+
GLuint prog_id, const std::string &vert_filepath, const std::string &frag_filepath) {
214+
std::string vert_str;
215+
std::string frag_str;
216+
217+
if (vert_filepath.size() && tinyusdz::io::FileExists(vert_filepath)) {
218+
219+
std::vector<uint8_t> bytes;
220+
std::string err;
221+
if (!tinyusdz::io::ReadWholeFile(&bytes, &err, vert_filepath)) {
222+
std::cerr << "Read vertg shader failed: " << err << "\n";
223+
return false;
224+
}
225+
226+
vert_str = std::string(reinterpret_cast<char *>(bytes.data()), bytes.size());
227+
}
228+
229+
if (frag_filepath.size() && tinyusdz::io::FileExists(frag_filepath)) {
230+
231+
std::vector<uint8_t> bytes;
232+
std::string err;
233+
if (!tinyusdz::io::ReadWholeFile(&bytes, &err, frag_filepath)) {
234+
std::cerr << "Read frag shader failed: " << err << "\n";
235+
return false;
236+
}
237+
238+
frag_str = std::string(reinterpret_cast<char *>(bytes.data()), bytes.size());
239+
}
240+
241+
// TODO
242+
return true;
243+
}
244+
245+
246+
bool SetupGLUsdPreviewSurface(
180247
GLuint prog_id,
248+
tinyusdz::tydra::RenderScene &scene,
181249
tinyusdz::tydra::RenderMaterial &m,
182250
GLUsdPreviewSurfaceState &dst)
183251
{
184-
if (m.surfaceShader.diffuseColor.is_texture()) {
185-
GLint loc = glGetUniformLocation(prog_id, GLUsdPreviewSurfaceState::kDiffuseColorTex);
186-
dst.diffuseColor.tex.u_tex = loc;
187-
} else {
188-
GLint loc = glGetUniformLocation(prog_id, GLUsdPreviewSurfaceState::kDiffuseColor);
252+
const auto surfaceShader = m.surfaceShader;
253+
254+
if (!SetupGLUsdPreviewSurfaceParam(prog_id, scene, GLUsdPreviewSurfaceState::kDiffuseColor, surfaceShader.diffuseColor, dst.diffuseColor)) {
255+
return false;
256+
}
257+
258+
if (!SetupGLUsdPreviewSurfaceParam(prog_id, scene, GLUsdPreviewSurfaceState::kEmissiveColor, surfaceShader.emissiveColor, dst.emissiveColor)) {
259+
return false;
260+
}
261+
262+
if (!SetupGLUsdPreviewSurfaceParam(prog_id, scene, GLUsdPreviewSurfaceState::kSpecularColor, surfaceShader.specularColor, dst.specularColor)) {
263+
return false;
264+
}
265+
266+
if (!SetupGLUsdPreviewSurfaceParam(prog_id, scene, GLUsdPreviewSurfaceState::kMetallic, surfaceShader.metallic, dst.metallic)) {
267+
return false;
268+
}
269+
270+
if (!SetupGLUsdPreviewSurfaceParam(prog_id, scene, GLUsdPreviewSurfaceState::kRoughness, surfaceShader.roughness, dst.roughness)) {
271+
return false;
272+
}
273+
274+
if (!SetupGLUsdPreviewSurfaceParam(prog_id, scene, GLUsdPreviewSurfaceState::kClearcoat, surfaceShader.clearcoat, dst.clearcoat)) {
275+
return false;
276+
}
277+
278+
if (!SetupGLUsdPreviewSurfaceParam(prog_id, scene, GLUsdPreviewSurfaceState::kClearcoatRoughness, surfaceShader.clearcoatRoughness, dst.clearcoatRoughness)) {
279+
return false;
280+
}
281+
282+
if (!SetupGLUsdPreviewSurfaceParam(prog_id, scene, GLUsdPreviewSurfaceState::kOpacity, surfaceShader.opacity, dst.opacity)) {
283+
return false;
284+
}
285+
286+
if (!SetupGLUsdPreviewSurfaceParam(prog_id, scene, GLUsdPreviewSurfaceState::kOpacityThreshold, surfaceShader.opacityThreshold, dst.opacityThreshold)) {
287+
return false;
288+
}
289+
290+
if (!SetupGLUsdPreviewSurfaceParam(prog_id, scene, GLUsdPreviewSurfaceState::kIor, surfaceShader.ior, dst.ior)) {
291+
return false;
292+
}
293+
294+
if (!SetupGLUsdPreviewSurfaceParam(prog_id, scene, GLUsdPreviewSurfaceState::kOcclusion, surfaceShader.occlusion, dst.occlusion)) {
295+
return false;
296+
}
297+
298+
if (!SetupGLUsdPreviewSurfaceParam(prog_id, scene, GLUsdPreviewSurfaceState::kNormal, surfaceShader.normal, dst.normal)) {
299+
return false;
300+
}
301+
302+
{
303+
GLint loc = glGetUniformLocation(prog_id, GLUsdPreviewSurfaceState::kUseSpecularWorkflow);
189304
if (loc < 0) {
190-
std::cerr << GLUsdPreviewSurfaceState::kDiffuseColor << " uniform not found in the shader.\n";
305+
std::cerr << GLUsdPreviewSurfaceState::kUseSpecularWorkflow << " uniform not found in the shader.\n";
191306
}
192-
dst.diffuseColor.u_factor = loc;
193-
dst.diffuseColor.factor = m.surfaceShader.diffuseColor.value;
307+
dst.useSpecularWorkflow.factor = surfaceShader.useSpecularWorkFlow ? 1.0f : 0.0f;
308+
dst.useSpecularWorkflow.u_factor = loc;
194309
}
195310

196-
// TODO: Support shader params
311+
// TODO: `displacement` param
312+
313+
return true;
197314
}
198315

199316
struct GLVertexUniformState {

examples/openglviewer/shaders/base_color_map.frag_inc.hh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// input filename: shaders/base_color_map.frag
1+
// input filename: shaders\base_color_map.frag
22
unsigned char shaders_base_color_map_frag[] = {
33
0x69, 0x6e, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20, 0x69, 0x6e, 0x74, 0x65,
44
0x72, 0x70, 0x6f, 0x6c, 0x61, 0x74, 0x65, 0x64, 0x5f, 0x75, 0x76, 0x3b,

examples/openglviewer/shaders/draw_debug_color.frag_inc.hh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// input filename: shaders/draw_debug_color.frag
1+
// input filename: shaders\draw_debug_color.frag
22
unsigned char shaders_draw_debug_color_frag[] = {
33
0x6f, 0x75, 0x74, 0x20, 0x76, 0x65, 0x63, 0x34, 0x20, 0x6f, 0x75, 0x74,
44
0x70, 0x75, 0x74, 0x5f, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x3b, 0x0a, 0x75,

examples/openglviewer/shaders/emissive_map.frag_inc.hh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// input filename: shaders/emissive_map.frag
1+
// input filename: shaders\emissive_map.frag
22
unsigned char shaders_emissive_map_frag[] = {
33
0x69, 0x6e, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20, 0x69, 0x6e, 0x74, 0x65,
44
0x72, 0x70, 0x6f, 0x6c, 0x61, 0x74, 0x65, 0x64, 0x5f, 0x75, 0x76, 0x3b,

examples/openglviewer/shaders/metallic_roughness_map.frag_inc.hh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// input filename: shaders/metallic_roughness_map.frag
1+
// input filename: shaders\metallic_roughness_map.frag
22
unsigned char shaders_metallic_roughness_map_frag[] = {
33
0x69, 0x6e, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20, 0x69, 0x6e, 0x74, 0x65,
44
0x72, 0x70, 0x6f, 0x6c, 0x61, 0x74, 0x65, 0x64, 0x5f, 0x75, 0x76, 0x3b,

examples/openglviewer/shaders/no_skinning.vert_inc.hh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// input filename: shaders/no_skinning.vert
1+
// input filename: shaders\no_skinning.vert
22
unsigned char shaders_no_skinning_vert[] = {
33
0x0a, 0x6c, 0x61, 0x79, 0x6f, 0x75, 0x74, 0x20, 0x28, 0x6c, 0x6f, 0x63,
44
0x61, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x3d, 0x20, 0x30, 0x29, 0x20, 0x69,

examples/openglviewer/shaders/normal_map.frag_inc.hh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// input filename: shaders/normal_map.frag
1+
// input filename: shaders\normal_map.frag
22
unsigned char shaders_normal_map_frag[] = {
33
0x69, 0x6e, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20, 0x69, 0x6e, 0x74, 0x65,
44
0x72, 0x70, 0x6f, 0x6c, 0x61, 0x74, 0x65, 0x64, 0x5f, 0x75, 0x76, 0x3b,

examples/openglviewer/shaders/normals.frag_inc.hh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// input filename: shaders/normals.frag
1+
// input filename: shaders\normals.frag
22
unsigned char shaders_normals_frag[] = {
33
0x69, 0x6e, 0x20, 0x76, 0x65, 0x63, 0x33, 0x20, 0x69, 0x6e, 0x74, 0x65,
44
0x72, 0x70, 0x6f, 0x6c, 0x61, 0x74, 0x65, 0x64, 0x5f, 0x6e, 0x6f, 0x72,

examples/openglviewer/shaders/occlusion_map.frag_inc.hh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// input filename: shaders/occlusion_map.frag
1+
// input filename: shaders\occlusion_map.frag
22
unsigned char shaders_occlusion_map_frag[] = {
33
0x69, 0x6e, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20, 0x69, 0x6e, 0x74, 0x65,
44
0x72, 0x70, 0x6f, 0x6c, 0x61, 0x74, 0x65, 0x64, 0x5f, 0x75, 0x76, 0x3b,

examples/openglviewer/shaders/pbr_metallic_roughness.frag_inc.hh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// input filename: shaders/pbr_metallic_roughness.frag
1+
// input filename: shaders\pbr_metallic_roughness.frag
22
unsigned char shaders_pbr_metallic_roughness_frag[] = {
33
0x23, 0x64, 0x65, 0x66, 0x69, 0x6e, 0x65, 0x20, 0x41, 0x4c, 0x50, 0x48,
44
0x41, 0x5f, 0x4f, 0x50, 0x41, 0x51, 0x55, 0x45, 0x20, 0x30, 0x0a, 0x23,

examples/openglviewer/shaders/perturbed_normal.frag_inc.hh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// input filename: shaders/perturbed_normal.frag
1+
// input filename: shaders\perturbed_normal.frag
22
unsigned char shaders_perturbed_normal_frag[] = {
33
0x69, 0x6e, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20, 0x69, 0x6e, 0x74, 0x65,
44
0x72, 0x70, 0x6f, 0x6c, 0x61, 0x74, 0x65, 0x64, 0x5f, 0x75, 0x76, 0x3b,

examples/openglviewer/shaders/unlit.frag_inc.hh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// input filename: shaders/unlit.frag
1+
// input filename: shaders\unlit.frag
22
unsigned char shaders_unlit_frag[] = {
33
0x69, 0x6e, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20, 0x69, 0x6e, 0x74, 0x65,
44
0x72, 0x70, 0x6f, 0x6c, 0x61, 0x74, 0x65, 0x64, 0x5f, 0x75, 0x76, 0x3b,
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,65 @@
11
// Simple UsdPreviewSurface fragment shader.
22

33
// TODO
4-
// - [ ] UV transform
5-
4+
// - [ ] normal mapping
5+
// - [ ] displacement mapping(mesh shader?)
66

77
in vec2 interpolated_uv0;
8-
uniform sampler2D base_color_texture;
9-
uniform vec4 base_color_factor;
8+
9+
uniform vec3 baseColor;
10+
uniform sampler2D baseColorTex;
11+
uniform mat3 baseColorTexTransform;
12+
13+
uniform vec3 emissiveColor;
14+
uniform sampler2D emissiveColorTex;
15+
uniform mat3 emissiveColorTexTransform;
16+
17+
uniform vec3 specularColor;
18+
uniform sampler2D specularColorTex;
19+
uniform mat3 specularColorTexTransform;
20+
21+
uniform vec3 metallic;
22+
uniform sampler2D metallicTex;
23+
uniform mat3 metallicTexTransform;
24+
25+
uniform vec3 roughness;
26+
uniform sampler2D roughnessTex;
27+
uniform mat3 roughnessTexTransform;
28+
29+
uniform vec3 clearcoat;
30+
uniform sampler2D clearcoatTex;
31+
uniform mat3 clearcoatTexTransform;
32+
33+
uniform vec3 clearcoatRoughness;
34+
uniform sampler2D clearcoatRoughnessTex;
35+
uniform mat3 clearcoatRoughnessTexTransform;
36+
37+
uniform vec3 ior;
38+
uniform sampler2D iorTex;
39+
uniform mat3 iorTexTransform;
40+
41+
uniform float occlusion;
42+
uniform sampler2D occlusionTex;
43+
uniform mat3 occlusionTexTransform;
44+
45+
uniform float opacity;
46+
uniform sampler2D opacityTex;
47+
uniform mat3 opacityTexTransform;
48+
49+
uniform float opacityThreshold;
50+
uniform sampler2D opacityThresholdTex;
51+
uniform mat3 opacityThresholdTexTransform;
52+
53+
uniform int useSpecularWorkflow;
1054

1155
out vec4 output_color;
1256

1357
void main()
1458
{
15-
vec4 base_color_result = texture(base_color_texture, interpolated_uv0) + base_color_factor;
16-
output_color = base_color_result;
59+
vec3 uv0 = vec3(interpolated_uv0, 1.0);
60+
vec4 baseColorResult = texture(baseColorTex, (baseColorTexTransform * uv0).xy) + vec4(baseColor, 1.0);
61+
62+
output_color = baseColorResult;
1763
}
1864

1965

0 commit comments

Comments
 (0)