Skip to content

Commit

Permalink
gltf:
Browse files Browse the repository at this point in the history
- Refactor shader includes a little. First pass at making the shared code more usable.
- Expose setShaderParams functions for the materials. Add a nextTextureUnit parameter to manage texture slot allocation.
- Add gltf-material header file.
  • Loading branch information
marcel303 committed Apr 28, 2020
1 parent 7f59a77 commit 79378a3
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 16 deletions.
2 changes: 1 addition & 1 deletion chibi-root.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ push_group libraries

add_root 1stparty

add users/marcel/gltf1
add users/marcel/framework-gltf

add 3rdparty

Expand Down
2 changes: 1 addition & 1 deletion refine/chibi.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ app refine
depend_library audiograph
depend_library audiograph-nodes-4dsound-1.6
depend_library framework
depend_library gltf
depend_library framework-gltf
depend_library ies-loader
depend_library imgui-framework
depend_library ImGuiColorTextEdit
Expand Down
2 changes: 2 additions & 0 deletions users/marcel/chibi-root.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ add fourier
add fluidCube
add geo
add geopart1
add gltf1
add gltf2
add gpuParticles1
add gpupath1
push_group thegrooop
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// material based on: https://github.com/SaschaWillems/Vulkan-glTF-PBR/blob/master/data/shaders/pbr.frag

include gltf/shaders/pbr-lighting-utils.txt
include gltf/shaders/normal-mapping-utils.txt

#define kMinRoughness 0.04

// material maps
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// material based on: https://github.com/SaschaWillems/Vulkan-glTF-PBR/blob/master/data/shaders/pbr.frag

include gltf/shaders/pbr-lighting-utils.txt
include gltf/shaders/normal-mapping-utils.txt

#define kMinRoughness 0.04

// material maps
Expand Down
Binary file not shown.
Binary file not shown.
30 changes: 16 additions & 14 deletions users/marcel/framework-gltf/gltf-draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace gltf
return true;
}

static void setShaderParams_metallicRoughness(Shader & shader, const Material & material, const Scene & scene, const bool hasVertexColors)
void setShaderParams_metallicRoughness(Shader & shader, const Material & material, const Scene & scene, const bool hasVertexColors, int & nextTextureUnit)
{
// PBR metallic roughness material

Expand Down Expand Up @@ -101,24 +101,24 @@ namespace gltf
const GxTextureId occlusionTextureId = tryGetTextureId(scene, material.occlusionTexture.index);
const GxTextureId emissiveTextureId = tryGetTextureId(scene, material.emissiveTexture.index);

shader.setTexture("baseColorTexture", 0, baseColorTextureId, true, false);
shader.setTexture("baseColorTexture", nextTextureUnit++, baseColorTextureId, true, false);
shader.setImmediate("baseColorTextureCoord", baseColorTextureId == 0 ? -1 : material.pbrMetallicRoughness.baseColorTexture.texCoord);

shader.setTexture("metallicRoughnessTexture", 1, metallicRoughnessTextureId, true, false);
shader.setTexture("metallicRoughnessTexture", nextTextureUnit++, metallicRoughnessTextureId, true, false);
shader.setImmediate("metallicRoughnessTextureCoord", metallicRoughnessTextureId == 0 ? -1 : material.pbrMetallicRoughness.metallicRoughnessTexture.texCoord);

shader.setTexture("normalTexture", 2, normalTextureId, true, false);
shader.setTexture("normalTexture", nextTextureUnit++, normalTextureId, true, false);
shader.setImmediate("normalTextureCoord", normalTextureId == 0 ? -1 : material.normalTexture.texCoord);

shader.setTexture("occlusionTexture", 3, occlusionTextureId, true, false);
shader.setTexture("occlusionTexture", nextTextureUnit++, occlusionTextureId, true, false);
shader.setImmediate("occlusionTextureCoord", occlusionTextureId == 0 ? -1 : material.occlusionTexture.texCoord);
shader.setImmediate("u_occlusionStrength", material.occlusionTexture.strength);

shader.setTexture("emissiveTexture", 4, emissiveTextureId, true, false);
shader.setTexture("emissiveTexture", nextTextureUnit++, emissiveTextureId, true, false);
shader.setImmediate("emissiveTextureCoord", emissiveTextureId == 0 ? -1 : material.emissiveTexture.texCoord);
}

static void setShaderParams_specularGlossiness(Shader & shader, const Material & material, const Scene & scene, const bool hasVertexColors)
void setShaderParams_specularGlossiness(Shader & shader, const Material & material, const Scene & scene, const bool hasVertexColors, int & nextTextureUnit)
{
// PBR specular glossiness material

Expand Down Expand Up @@ -156,20 +156,20 @@ namespace gltf
const GxTextureId occlusionTextureId = tryGetTextureId(scene, material.occlusionTexture.index);
const GxTextureId emissiveTextureId = tryGetTextureId(scene, material.emissiveTexture.index);

shader.setTexture("diffuseTexture", 0, diffuseTextureId, true, false);
shader.setTexture("diffuseTexture", nextTextureUnit++, diffuseTextureId, true, false);
shader.setImmediate("diffuseTextureCoord", diffuseTextureId == 0 ? -1 : material.pbrSpecularGlossiness.diffuseTexture.texCoord);

shader.setTexture("specularGlossinessTexture", 1, specularGlossinessTextureId, true, false);
shader.setTexture("specularGlossinessTexture", nextTextureUnit++, specularGlossinessTextureId, true, false);
shader.setImmediate("specularGlossinessTextureCoord", specularGlossinessTextureId == 0 ? -1 : material.pbrSpecularGlossiness.specularGlossinessTexture.texCoord);

shader.setTexture("normalTexture", 2, normalTextureId, true, false);
shader.setTexture("normalTexture", nextTextureUnit++, normalTextureId, true, false);
shader.setImmediate("normalTextureCoord", normalTextureId == 0 ? -1 : material.normalTexture.texCoord);

shader.setTexture("occlusionTexture", 3, occlusionTextureId, true, false);
shader.setTexture("occlusionTexture", nextTextureUnit++, occlusionTextureId, true, false);
shader.setImmediate("occlusionTextureCoord", occlusionTextureId == 0 ? -1 : material.occlusionTexture.texCoord);
shader.setImmediate("u_occlusionStrength", material.occlusionTexture.strength);

shader.setTexture("emissiveTexture", 4, emissiveTextureId, true, false);
shader.setTexture("emissiveTexture", nextTextureUnit++, emissiveTextureId, true, false);
shader.setImmediate("emissiveTextureCoord", emissiveTextureId == 0 ? -1 : material.emissiveTexture.texCoord);
}

Expand Down Expand Up @@ -224,11 +224,13 @@ namespace gltf
// set material parameters

const bool hasVertexColors = primitive.attributes.count("COLOR_0") != 0;

int nextTextureUnit = 0;

if (material.pbrSpecularGlossiness.isSet)
setShaderParams_specularGlossiness(shader, material, scene, hasVertexColors);
setShaderParams_specularGlossiness(shader, material, scene, hasVertexColors, nextTextureUnit);
else
setShaderParams_metallicRoughness(shader, material, scene, hasVertexColors);
setShaderParams_metallicRoughness(shader, material, scene, hasVertexColors, nextTextureUnit);

// draw

Expand Down
22 changes: 22 additions & 0 deletions users/marcel/framework-gltf/gltf-material.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include "gltf.h"

class Shader;

namespace gltf
{
void setShaderParams_metallicRoughness(
Shader & shader,
const Material & material,
const Scene & scene,
const bool hasVertexColors,
int & nextTextureUnit);

void setShaderParams_specularGlossiness(
Shader & shader,
const Material & material,
const Scene & scene,
const bool hasVertexColors,
int & nextTextureUnit);
}

0 comments on commit 79378a3

Please sign in to comment.