Merge pull request #301 from HifiExperiments/modelBackface

Models support flipped normals on double sided geometry
This commit is contained in:
kasenvr 2020-05-13 16:06:26 -04:00 committed by GitHub
commit c5fdacf975
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 34 additions and 10 deletions

View file

@ -19,6 +19,8 @@
<@include paintStroke.slh@> <@include paintStroke.slh@>
<$declarePolyLineBuffers()$> <$declarePolyLineBuffers()$>
<@include CullFace.slh@>
LAYOUT(binding=0) uniform sampler2D _texture; LAYOUT(binding=0) uniform sampler2D _texture;
<@if not HIFI_USE_FORWARD@> <@if not HIFI_USE_FORWARD@>
@ -35,9 +37,9 @@ void main(void) {
<@if not HIFI_USE_FORWARD@> <@if not HIFI_USE_FORWARD@>
<@if HIFI_USE_TRANSLUCENT@> <@if HIFI_USE_TRANSLUCENT@>
packDeferredFragmentTranslucent((2.0 * float(gl_FrontFacing) - 1.0) * _normalWS, texel.a, texel.rgb, DEFAULT_ROUGHNESS); packDeferredFragmentTranslucent(evalFrontOrBackFaceNormal(_normalWS), texel.a, texel.rgb, DEFAULT_ROUGHNESS);
<@else@> <@else@>
packDeferredFragmentUnlit((2.0 * float(gl_FrontFacing) - 1.0) * _normalWS, texel.a, texel.rgb); packDeferredFragmentUnlit(evalFrontOrBackFaceNormal(_normalWS), texel.a, texel.rgb);
<@endif@> <@endif@>
<@else@> <@else@>
_fragColor0 = texel; _fragColor0 = texel;

View file

@ -13,7 +13,6 @@
<@include graphics/ShaderConstants.h@> <@include graphics/ShaderConstants.h@>
const int MAX_TEXCOORDS = 2; const int MAX_TEXCOORDS = 2;
struct TexMapArray { struct TexMapArray {

View file

@ -0,0 +1,20 @@
<!
// CullFace.slh
// libraries/render-utils/src
//
// Created by HifiExperiments on 4/16/2020.
// Copyright 2020 Vircadia
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
!>
<@if not CULL_FACE_SLH@>
<@def CULL_FACE_SLH@>
// NOTE: this calculation happens once per fragment. this could be optimized by creating different shaders (via defines)
// for front, back, and double-sided. for front/back-only triangles, this will simplify to always 1 or always -1
vec3 evalFrontOrBackFaceNormal(vec3 normal) {
return (2.0 * float(gl_FrontFacing) - 1.0) * normal;
}
<@endif@>

View file

@ -13,6 +13,7 @@
<@include graphics/Material.slh@> <@include graphics/Material.slh@>
<@include graphics/MaterialTextures.slh@> <@include graphics/MaterialTextures.slh@>
<@include render-utils/ShaderConstants.h@> <@include render-utils/ShaderConstants.h@>
<@include CullFace.slh@>
<@if not HIFI_USE_SHADOW@> <@if not HIFI_USE_SHADOW@>
<@if HIFI_USE_FORWARD or HIFI_USE_TRANSLUCENT@> <@if HIFI_USE_FORWARD or HIFI_USE_TRANSLUCENT@>
@ -129,7 +130,7 @@ void main(void) {
_fragColor0 = vec4(albedo * isUnlitEnabled(), opacity); _fragColor0 = vec4(albedo * isUnlitEnabled(), opacity);
<@else@> <@else@>
packDeferredFragmentUnlit( packDeferredFragmentUnlit(
normalize(_normalWS), evalFrontOrBackFaceNormal(normalize(_normalWS)),
opacity, opacity,
albedo * isUnlitEnabled()); albedo * isUnlitEnabled());
<@endif@> <@endif@>
@ -195,7 +196,7 @@ void main(void) {
<@else@> <@else@>
vec3 fragNormalWS = _normalWS; vec3 fragNormalWS = _normalWS;
<@endif@> <@endif@>
fragNormalWS = normalize(fragNormalWS); fragNormalWS = evalFrontOrBackFaceNormal(normalize(fragNormalWS));
<@if HIFI_USE_FORWARD@> <@if HIFI_USE_FORWARD@>
TransformCamera cam = getTransformCamera(); TransformCamera cam = getTransformCamera();

View file

@ -13,6 +13,7 @@
<@include gpu/Color.slh@> <@include gpu/Color.slh@>
<@include DefaultMaterials.slh@> <@include DefaultMaterials.slh@>
<@include render-utils/ShaderConstants.h@> <@include render-utils/ShaderConstants.h@>
<@include CullFace.slh@>
<@if HIFI_USE_FORWARD or HIFI_USE_TRANSLUCENT@> <@if HIFI_USE_FORWARD or HIFI_USE_TRANSLUCENT@>
<@if not HIFI_USE_UNLIT@> <@if not HIFI_USE_UNLIT@>
@ -94,7 +95,7 @@ void main(void) {
1.0, 1.0,
DEFAULT_OCCLUSION, DEFAULT_OCCLUSION,
fragPosition, fragPosition,
normalize(_normalWS) * (2.0 * float(gl_FrontFacing) - 1.0), evalFrontOrBackFaceNormal(normalize(_normalWS)),
texel.rgb, texel.rgb,
fresnel, fresnel,
metallic, metallic,
@ -111,7 +112,7 @@ void main(void) {
1.0, 1.0,
DEFAULT_OCCLUSION, DEFAULT_OCCLUSION,
fragPosition, fragPosition,
normalize(_normalWS) * (2.0 * float(gl_FrontFacing) - 1.0), evalFrontOrBackFaceNormal(normalize(_normalWS)),
texel.rgb, texel.rgb,
fresnel, fresnel,
metallic, metallic,
@ -119,7 +120,7 @@ void main(void) {
texel.a); texel.a);
<@else@> <@else@>
packDeferredFragment( packDeferredFragment(
normalize(_normalWS) * (2.0 * float(gl_FrontFacing) - 1.0), evalFrontOrBackFaceNormal(normalize(_normalWS)),
1.0, 1.0,
texel.rgb, texel.rgb,
DEFAULT_ROUGHNESS, DEFAULT_ROUGHNESS,
@ -141,7 +142,7 @@ void main(void) {
, texel.a); , texel.a);
<@else@> <@else@>
packDeferredFragmentUnlit( packDeferredFragmentUnlit(
normalize(_normalWS) * (2.0 * float(gl_FrontFacing) - 1.0), evalFrontOrBackFaceNormal(normalize(_normalWS)),
1.0, 1.0,
texel.rgb texel.rgb
<@if HIFI_USE_FADE@> <@if HIFI_USE_FADE@>

View file

@ -24,6 +24,7 @@
<@include gpu/Transform.slh@> <@include gpu/Transform.slh@>
<$declareStandardCameraTransform()$> <$declareStandardCameraTransform()$>
<@include CullFace.slh@>
<@include render-utils/ShaderConstants.h@> <@include render-utils/ShaderConstants.h@>
layout(location=RENDER_UTILS_ATTR_POSITION_MS) in vec4 _positionMS; layout(location=RENDER_UTILS_ATTR_POSITION_MS) in vec4 _positionMS;
@ -66,7 +67,7 @@ float getProceduralFragmentWithPosition(inout ProceduralFragmentWithPosition pro
#line 2030 #line 2030
void main(void) { void main(void) {
vec3 normal = normalize(_normalWS.xyz) * (2.0 * float(gl_FrontFacing) - 1.0); vec3 normal = evalFrontOrBackFaceNormal(normalize(_normalWS.xyz));
vec3 diffuse = _color.rgb; vec3 diffuse = _color.rgb;
vec3 fresnel = DEFAULT_FRESNEL; vec3 fresnel = DEFAULT_FRESNEL;
float roughness = DEFAULT_ROUGHNESS; float roughness = DEFAULT_ROUGHNESS;