From b6d74a9e66e0fdd15f0c93ba8a633bb51bfb976b Mon Sep 17 00:00:00 2001 From: David Thompson Date: Fri, 7 May 2021 08:52:18 -0400 Subject: model: Add crude support for secondary textures in glTF models. --- chickadee/graphics/model.scm | 12 +++++---- chickadee/graphics/pbr.scm | 64 +++++++++++++++++++++++++++++--------------- 2 files changed, 49 insertions(+), 27 deletions(-) diff --git a/chickadee/graphics/model.scm b/chickadee/graphics/model.scm index 58204d6..55374fe 100644 --- a/chickadee/graphics/model.scm +++ b/chickadee/graphics/model.scm @@ -896,7 +896,7 @@ 1.0)) (metallic-roughness-texture (match (object-ref/optional pbrmr "metallicRoughnessTexture") - (#f null-texture) + (#f #f) (obj (vector-ref textures (number-ref obj "index"))))) (normal-factor @@ -905,7 +905,7 @@ (vec3 (vector-ref v 0) (vector-ref v 1) (vector-ref v 2)))) (normal-texture (match (object-ref/optional obj "normalTexture") - (#f null-texture) + (#f #f) (obj (vector-ref textures (number-ref obj "index"))))) (occlusion-factor (let ((v (or (array-ref/optional obj "occlusionFactor") @@ -913,7 +913,7 @@ (vec3 (vector-ref v 0) (vector-ref v 1) (vector-ref v 2)))) (occlusion-texture (match (object-ref/optional obj "occlusionTexture") - (#f null-texture) + (#f #f) (obj (vector-ref textures (number-ref obj "index"))))) (emissive-factor (let ((v (or (array-ref/optional obj "emissiveFactor") @@ -921,7 +921,7 @@ (vec3 (vector-ref v 0) (vector-ref v 1) (vector-ref v 2)))) (emissive-texture (match (object-ref/optional obj "emissiveTexture") - (#f null-texture) + (#f #f) (obj (vector-ref textures (number-ref obj "index"))))) (alpha-mode (match (or (string-ref/optional obj "alphaMode") "BLEND") @@ -958,7 +958,9 @@ ("TEXCOORD_0" (attribute-location (hash-ref (shader-attributes shader) "texcoord0"))) - ("TEXCOORD_1" 11) + ("TEXCOORD_1" + (attribute-location + (hash-ref (shader-attributes shader) "texcoord1"))) ("COLOR_0" (attribute-location (hash-ref (shader-attributes shader) "color0"))) diff --git a/chickadee/graphics/pbr.scm b/chickadee/graphics/pbr.scm index 73f057b..01a330c 100644 --- a/chickadee/graphics/pbr.scm +++ b/chickadee/graphics/pbr.scm @@ -75,13 +75,13 @@ #:base-color-texture null-texture #:metallic-factor 1.0 #:roughness-factor 1.0 - #:metallic-roughness-texture null-texture + #:metallic-roughness-texture #f #:normal-factor #v(1.0 1.0 1.0) - #:normal-texture null-texture + #:normal-texture #f #:occlusion-factor #v(1.0 1.0 1.0) - #:occlusion-texture null-texture + #:occlusion-texture #f #:emissive-factor #v(1.0 1.0 1.0) - #:emissive-texture null-texture + #:emissive-texture #f #:alpha-mode 'opaque #:alpha-cutoff 0.5 #:double-sided? #f)) @@ -94,56 +94,69 @@ #ifdef GLSL330 layout (location = 0) in vec3 position; layout (location = 1) in vec2 texcoord0; -layout (location = 2) in vec4 color0; +layout (location = 2) in vec2 texcoord1; +layout (location = 3) in vec4 color0; #elif defined(GLSL130) in vec3 position; in vec2 texcoord0; +in vec2 texcoord1; in vec4 color0; #elif defined(GLSL120) attribute vec3 position; attribute vec2 texcoord0; +attribute vec2 texcoord1; attribute vec4 color0; #endif #ifdef GLSL120 -varying vec2 fragTex; -varying vec4 fragVertColor; +varying vec2 fragTexcoord0; +varying vec2 fragTexcoord1; +varying vec4 fragColor0; #else -out vec2 fragTex; -out vec4 fragVertColor; +out vec2 fragTexcoord0; +out vec2 fragTexcoord1; +out vec4 fragColor0; #endif uniform mat4 model; uniform mat4 view; uniform mat4 projection; void main(void) { - fragTex = texcoord0; - fragVertColor = color0; + fragTexcoord0 = texcoord0; + fragTexcoord1 = texcoord1; + fragColor0 = color0; gl_Position = projection * view * model * vec4(position.xyz, 1.0); } " " #ifdef GLSL120 -attribute vec2 fragTex; -attribute vec4 fragVertColor; +attribute vec2 fragTexcoord0; +attribute vec2 fragTexcoord1 +attribute vec4 fragColor0; #else -in vec2 fragTex; -in vec4 fragVertColor; +in vec2 fragTexcoord0; +in vec2 fragTexcoord1; +in vec4 fragColor0; #endif #ifdef GLSL330 out vec4 fragColor; #endif -uniform bool textured; +uniform bool textured0; +uniform bool textured1; uniform bool vertexColored; uniform vec3 baseColorFactor; -uniform sampler2D baseColorTexture; +uniform sampler2D texture0; +uniform sampler2D texture1; void main (void) { vec4 finalColor = vec4(baseColorFactor, 1.0); if(vertexColored) { - finalColor *= fragVertColor; + finalColor *= fragColor0; } - if(textured) { - finalColor *= texture(baseColorTexture, fragTex); + if(textured0) { + finalColor *= texture(texture0, fragTexcoord0); + } + if(textured1) { + finalColor += texture(texture1, fragTexcoord1); } #ifdef GLSL330 fragColor = finalColor; @@ -156,14 +169,21 @@ void main (void) { (define (shader-apply/pbr vertex-array material model-matrix view-matrix) (let* ((shader (graphics-variable-ref pbr-shader)) (base-color-texture (pbr-material-base-color-texture material)) + (secondary-texture (or (pbr-material-metallic-roughness-texture material) + (pbr-material-normal-texture material) + (pbr-material-occlusion-texture material) + (pbr-material-emissive-texture material) + null-texture)) (vattrs (vertex-array-attributes vertex-array)) (sattrs (shader-attributes shader))) - (with-graphics-state ((g:texture-0 base-color-texture)) + (with-graphics-state ((g:texture-0 base-color-texture) + (g:texture-1 secondary-texture)) (shader-apply shader vertex-array #:model model-matrix #:view view-matrix #:projection (current-projection) - #:textured (not (eq? base-color-texture null-texture)) + #:textured0 (not (eq? base-color-texture null-texture)) + #:textured1 (not (eq? secondary-texture null-texture)) #:vertex-colored (buffer-view? (assv-ref vattrs (attribute-location -- cgit v1.2.3