mirror of
https://github.com/HifiExperiments/overte.git
synced 2025-06-17 16:20:34 +02:00
168 lines
No EOL
5.7 KiB
Text
168 lines
No EOL
5.7 KiB
Text
// Generated on <$_SCRIBE_DATE$>
|
|
//
|
|
// Created by Olivier Prat on 04/12/17.
|
|
// Copyright 2017 High Fidelity, Inc.
|
|
//
|
|
// 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 FADE_SLH@>
|
|
<@def FADE_SLH@>
|
|
|
|
<@include render-utils/ShaderConstants.h@>
|
|
|
|
<@func declareFadeFragmentCommon()@>
|
|
|
|
#define CATEGORY_COUNT 5
|
|
|
|
<@include Fade_shared.slh@>
|
|
<@include FadeObjectParams.shared.slh@>
|
|
|
|
// See ShapePipeline::Slot::BUFFER in ShapePipeline.h
|
|
layout(std140, binding=RENDER_UTILS_BUFFER_FADE_PARAMS) uniform fadeParametersBuffer {
|
|
FadeParameters fadeParameters[CATEGORY_COUNT];
|
|
};
|
|
|
|
// See ShapePipeline::Slot::MAP in ShapePipeline.h
|
|
layout(binding=RENDER_UTILS_TEXTURE_FADE_MASK) uniform sampler2D fadeMaskMap;
|
|
|
|
vec3 getNoiseInverseSize(int category) {
|
|
return fadeParameters[category]._noiseInvSizeAndLevel.xyz;
|
|
}
|
|
|
|
float getNoiseLevel(int category) {
|
|
return fadeParameters[category]._noiseInvSizeAndLevel.w;
|
|
}
|
|
|
|
vec2 hash2D(vec3 position) {
|
|
return position.xy* vec2(0.1677, 0.221765) + position.z*0.561;
|
|
}
|
|
|
|
float noise3D(vec3 position) {
|
|
float n = textureLod(fadeMaskMap, hash2D(position), 0.0).r;
|
|
return pow(n, 1.0/2.2); // Remove sRGB. Need to fix this later directly in the texture
|
|
}
|
|
|
|
float evalFadeNoiseGradient(FadeObjectParams params, vec3 position) {
|
|
// Do tri-linear interpolation
|
|
vec3 noisePosition = position * getNoiseInverseSize(params.category) + params.noiseOffset.xyz;
|
|
vec3 noisePositionFloored = floor(noisePosition);
|
|
vec3 noisePositionFraction = fract(noisePosition);
|
|
|
|
noisePositionFraction = noisePositionFraction*noisePositionFraction*(3.0 - 2.0*noisePositionFraction);
|
|
|
|
float noiseLowXLowYLowZ = noise3D(noisePositionFloored);
|
|
float noiseLowXHighYLowZ = noise3D(noisePositionFloored+vec3(0,1,0));
|
|
float noiseHighXLowYLowZ = noise3D(noisePositionFloored+vec3(1,0,0));
|
|
float noiseHighXHighYLowZ = noise3D(noisePositionFloored+vec3(1,1,0));
|
|
float noiseLowXLowYHighZ = noise3D(noisePositionFloored+vec3(0,0,1));
|
|
float noiseLowXHighYHighZ = noise3D(noisePositionFloored+vec3(0,1,1));
|
|
float noiseHighXLowYHighZ = noise3D(noisePositionFloored+vec3(1,0,1));
|
|
float noiseHighXHighYHighZ = noise3D(noisePositionFloored+vec3(1,1,1));
|
|
vec4 maskLowZ = vec4(noiseLowXLowYLowZ, noiseLowXHighYLowZ, noiseHighXLowYLowZ, noiseHighXHighYLowZ);
|
|
vec4 maskHighZ = vec4(noiseLowXLowYHighZ, noiseLowXHighYHighZ, noiseHighXLowYHighZ, noiseHighXHighYHighZ);
|
|
vec4 maskXY = mix(maskLowZ, maskHighZ, noisePositionFraction.z);
|
|
vec2 maskY = mix(maskXY.xy, maskXY.zw, noisePositionFraction.x);
|
|
|
|
float noise = mix(maskY.x, maskY.y, noisePositionFraction.y);
|
|
noise -= 0.5; // Center on value 0
|
|
return noise * getNoiseLevel(params.category);
|
|
}
|
|
|
|
float evalFadeBaseGradient(FadeObjectParams params, vec3 position) {
|
|
float gradient = length((position - params.baseOffset.xyz) * params.baseInvSize.xyz);
|
|
gradient = gradient-0.5; // Center on value 0.5
|
|
gradient *= fadeParameters[params.category]._baseLevel;
|
|
return gradient;
|
|
}
|
|
|
|
float evalFadeGradient(FadeObjectParams params, vec3 position) {
|
|
float baseGradient = evalFadeBaseGradient(params, position);
|
|
float noiseGradient = evalFadeNoiseGradient(params, position);
|
|
float gradient = noiseGradient+baseGradient+0.5;
|
|
|
|
return gradient;
|
|
}
|
|
|
|
float evalFadeAlpha(FadeObjectParams params, vec3 position) {
|
|
return evalFadeGradient(params, position)-params.threshold;
|
|
}
|
|
|
|
void applyFadeClip(FadeObjectParams params, vec3 position) {
|
|
if (evalFadeAlpha(params, position) < 0.0) {
|
|
discard;
|
|
}
|
|
}
|
|
|
|
void applyFade(FadeObjectParams params, vec3 position, out vec3 emissive) {
|
|
float alpha = evalFadeAlpha(params, position);
|
|
if (fadeParameters[params.category]._isInverted!=0) {
|
|
alpha = -alpha;
|
|
}
|
|
|
|
if (alpha < 0.0) {
|
|
discard;
|
|
}
|
|
|
|
float edgeMask = alpha * fadeParameters[params.category]._edgeWidthInvWidth.y;
|
|
float edgeAlpha = 1.0-clamp(edgeMask, 0.0, 1.0);
|
|
|
|
edgeMask = step(edgeMask, 1.0);
|
|
edgeAlpha *= edgeAlpha; // Square to have a nice ease out
|
|
vec4 color = mix(fadeParameters[params.category]._innerEdgeColor, fadeParameters[params.category]._outerEdgeColor, edgeAlpha);
|
|
emissive = color.rgb * edgeMask * color.a;
|
|
}
|
|
|
|
<@endfunc@>
|
|
|
|
<@func declareFadeFragmentUniform()@>
|
|
|
|
layout(std140, binding=RENDER_UTILS_BUFFER_FADE_OBJECT_PARAMS) uniform fadeObjectParametersBuffer {
|
|
FadeObjectParams fadeObjectParams;
|
|
};
|
|
|
|
<@endfunc@>
|
|
|
|
<@func fetchFadeObjectParams(fadeParams)@>
|
|
<$fadeParams$> = fadeObjectParams;
|
|
<@endfunc@>
|
|
|
|
<@func declareFadeFragmentVertexInput()@>
|
|
|
|
layout(location=RENDER_UTILS_ATTR_FADE1) in vec4 _fadeData1;
|
|
layout(location=RENDER_UTILS_ATTR_FADE2) in vec4 _fadeData2;
|
|
layout(location=RENDER_UTILS_ATTR_FADE3) in vec4 _fadeData3;
|
|
|
|
<@endfunc@>
|
|
|
|
<@func fetchFadeObjectParamsInstanced(fadeParams)@>
|
|
<$fadeParams$>.category = int(_fadeData1.w);
|
|
<$fadeParams$>.threshold = _fadeData2.w;
|
|
<$fadeParams$>.noiseOffset = _fadeData1;
|
|
<$fadeParams$>.baseOffset = _fadeData2;
|
|
<$fadeParams$>.baseInvSize = _fadeData3;
|
|
<@endfunc@>
|
|
|
|
<@func declareFadeFragment()@>
|
|
<$declareFadeFragmentCommon()$>
|
|
<$declareFadeFragmentUniform()$>
|
|
<@endfunc@>
|
|
|
|
<@func declareFadeFragmentInstanced()@>
|
|
<$declareFadeFragmentCommon()$>
|
|
<$declareFadeFragmentVertexInput()$>
|
|
<@endfunc@>
|
|
|
|
<@func declareFadeVertexInstanced()@>
|
|
layout(location=RENDER_UTILS_ATTR_FADE1) out vec4 _fadeData1;
|
|
layout(location=RENDER_UTILS_ATTR_FADE2) out vec4 _fadeData2;
|
|
layout(location=RENDER_UTILS_ATTR_FADE3) out vec4 _fadeData3;
|
|
<@endfunc@>
|
|
|
|
<@func passThroughFadeObjectParams()@>
|
|
_fadeData1 = inTexCoord2;
|
|
_fadeData2 = inTexCoord3;
|
|
_fadeData3 = inTexCoord4;
|
|
<@endfunc@>
|
|
|
|
<@endif@> |