openspades/Resources/Shaders/SoftSprite.fs

72 lines
1.8 KiB
Forth
Raw Normal View History

2013-08-29 11:45:22 +09:00
/*
Copyright (c) 2013 yvt
This file is part of OpenSpades.
OpenSpades is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenSpades is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenSpades. If not, see <http://www.gnu.org/licenses/>.
*/
2013-08-18 16:18:06 +09:00
uniform sampler2D depthTexture;
2016-11-18 15:40:10 +02:00
uniform sampler2D mainTexture;
2013-08-18 16:18:06 +09:00
uniform vec3 fogColor;
uniform vec2 zNearFar;
varying vec4 color;
varying vec4 texCoord;
varying vec4 fogDensity;
varying vec4 depthRange;
float decodeDepth(float w, float near, float far){
2013-09-16 09:32:16 +09:00
return 1. /*far * near*/ / mix(far, near, w);
2013-08-18 16:18:06 +09:00
}
float depthAt(vec2 pt){
float w = texture2D(depthTexture, pt).x;
return decodeDepth(w, zNearFar.x, zNearFar.y);
}
void main() {
// get depth
float depth = depthAt(texCoord.zw);
2013-09-16 09:32:16 +09:00
if(depth < /*depthRange.x*/ depthRange.y){
2013-08-18 16:18:06 +09:00
discard;
}
2016-11-18 15:40:10 +02:00
gl_FragColor = texture2D(mainTexture, texCoord.xy);
2014-04-08 03:36:07 +09:00
#if LINEAR_FRAMEBUFFER
gl_FragColor.xyz *= gl_FragColor.xyz;
#endif
2013-08-18 16:18:06 +09:00
gl_FragColor.xyz *= gl_FragColor.w; // premultiplied alpha
gl_FragColor *= color;
2013-09-16 09:32:16 +09:00
vec3 fogColorPremuld = fogColor;
2013-08-18 16:18:06 +09:00
fogColorPremuld *= gl_FragColor.w;
2013-09-16 09:32:16 +09:00
gl_FragColor.xyz = mix(gl_FragColor.xyz, fogColorPremuld, fogDensity.xyz);
2013-08-18 16:18:06 +09:00
2013-09-16 09:32:16 +09:00
//float soft = (depth - depthRange.x) / (depthRange.y - depthRange.w);
float soft = depth * depthRange.z + depthRange.x;
2014-03-23 22:48:16 +09:00
soft = smoothstep(0., 1., soft);
2013-08-18 16:18:06 +09:00
gl_FragColor *= soft;
if(dot(gl_FragColor, vec4(1.)) < .002)
discard;
}