Fix shadercode
This commit is contained in:
Binary file not shown.
|
Before Width: | Height: | Size: 216 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 274 KiB |
@@ -1,7 +1,8 @@
|
|||||||
|
#version 150
|
||||||
|
|
||||||
// Samplers for textures
|
// Samplers for textures
|
||||||
uniform sampler2D m_Texture;
|
uniform sampler2D m_Texture;
|
||||||
uniform sampler2D m_OutlineDepthTexture;
|
uniform sampler2D m_OutlineDepthTexture;
|
||||||
uniform sampler2D m_DepthTexture;
|
|
||||||
|
|
||||||
// Input texture coordinates from the vertex shader
|
// Input texture coordinates from the vertex shader
|
||||||
in vec2 texCoord;
|
in vec2 texCoord;
|
||||||
@@ -15,26 +16,25 @@ uniform float m_OutlineWidth;
|
|||||||
out vec4 fragColor;
|
out vec4 fragColor;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
// Sample depth textures
|
// Sample depth textures at various offsets
|
||||||
vec4 depth = texture(m_OutlineDepthTexture, texCoord);
|
vec4 depth = texture(m_OutlineDepthTexture, texCoord);
|
||||||
vec4 depth1 = texture(m_OutlineDepthTexture, ((texCoord * m_Resolution) + vec2(m_OutlineWidth, m_OutlineWidth)) / m_Resolution);
|
vec4 depth1 = texture(m_OutlineDepthTexture, (texCoord * m_Resolution + vec2(m_OutlineWidth, m_OutlineWidth)) / m_Resolution);
|
||||||
vec4 depth2 = texture(m_OutlineDepthTexture, ((texCoord * m_Resolution) + vec2(m_OutlineWidth, -m_OutlineWidth)) / m_Resolution);
|
vec4 depth2 = texture(m_OutlineDepthTexture, (texCoord * m_Resolution + vec2(m_OutlineWidth, -m_OutlineWidth)) / m_Resolution);
|
||||||
vec4 depth3 = texture(m_OutlineDepthTexture, ((texCoord * m_Resolution) + vec2(-m_OutlineWidth, m_OutlineWidth)) / m_Resolution);
|
vec4 depth3 = texture(m_OutlineDepthTexture, (texCoord * m_Resolution + vec2(-m_OutlineWidth, m_OutlineWidth)) / m_Resolution);
|
||||||
vec4 depth4 = texture(m_OutlineDepthTexture, ((texCoord * m_Resolution) + vec2(-m_OutlineWidth, -m_OutlineWidth)) / m_Resolution);
|
vec4 depth4 = texture(m_OutlineDepthTexture, (texCoord * m_Resolution + vec2(-m_OutlineWidth, -m_OutlineWidth)) / m_Resolution);
|
||||||
vec4 depth5 = texture(m_OutlineDepthTexture, ((texCoord * m_Resolution) + vec2(0.0, m_OutlineWidth)) / m_Resolution);
|
vec4 depth5 = texture(m_OutlineDepthTexture, (texCoord * m_Resolution + vec2(0.0, m_OutlineWidth)) / m_Resolution);
|
||||||
vec4 depth6 = texture(m_OutlineDepthTexture, ((texCoord * m_Resolution) + vec2(0.0, -m_OutlineWidth)) / m_Resolution);
|
vec4 depth6 = texture(m_OutlineDepthTexture, (texCoord * m_Resolution + vec2(0.0, -m_OutlineWidth)) / m_Resolution);
|
||||||
vec4 depth7 = texture(m_OutlineDepthTexture, ((texCoord * m_Resolution) + vec2(m_OutlineWidth, 0.0)) / m_Resolution);
|
vec4 depth7 = texture(m_OutlineDepthTexture, (texCoord * m_Resolution + vec2(m_OutlineWidth, 0.0)) / m_Resolution);
|
||||||
vec4 depth8 = texture(m_OutlineDepthTexture, ((texCoord * m_Resolution) + vec2(-m_OutlineWidth, 0.0)) / m_Resolution);
|
vec4 depth8 = texture(m_OutlineDepthTexture, (texCoord * m_Resolution + vec2(-m_OutlineWidth, 0.0)) / m_Resolution);
|
||||||
|
|
||||||
// Sample the main texture
|
// Sample the main texture
|
||||||
vec4 color = texture(m_Texture, texCoord);
|
vec4 color = texture(m_Texture, texCoord);
|
||||||
|
|
||||||
// Determine whether to apply the outline color
|
// Check if an outline should be applied
|
||||||
if (depth == vec4(0.0) &&
|
bool isEdge = (depth == vec4(0.0)) &&
|
||||||
(depth1 != depth || depth2 != depth || depth3 != depth || depth4 != depth ||
|
(depth1 != depth || depth2 != depth || depth3 != depth || depth4 != depth ||
|
||||||
depth5 != depth || depth6 != depth || depth7 != depth || depth8 != depth)) {
|
depth5 != depth || depth6 != depth || depth7 != depth || depth8 != depth);
|
||||||
fragColor = m_OutlineColor; // Apply outline color
|
|
||||||
} else {
|
// Output the final color
|
||||||
fragColor = color; // Use the original texture color
|
fragColor = isEdge ? m_OutlineColor : color;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
|
#version 150
|
||||||
|
|
||||||
// Use 'in' instead of 'varying' for inputs from the vertex shader
|
// Input texture coordinates from the vertex shader
|
||||||
in vec2 texCoord;
|
in vec2 texCoord;
|
||||||
|
|
||||||
// Declare a custom output variable for the fragment color
|
// Output variable for the fragment color
|
||||||
out vec4 fragColor;
|
out vec4 fragColor;
|
||||||
|
|
||||||
// Uniform samplers
|
// Uniform samplers for textures
|
||||||
uniform sampler2D m_Texture;
|
uniform sampler2D m_Texture;
|
||||||
uniform sampler2D m_NormalsTexture;
|
uniform sampler2D m_NormalsTexture;
|
||||||
uniform sampler2D m_DepthTexture;
|
uniform sampler2D m_DepthTexture;
|
||||||
@@ -13,6 +14,7 @@ uniform sampler2D m_DepthTexture;
|
|||||||
void main() {
|
void main() {
|
||||||
// Sample the texture at the given texture coordinates
|
// Sample the texture at the given texture coordinates
|
||||||
vec4 color = texture(m_Texture, texCoord);
|
vec4 color = texture(m_Texture, texCoord);
|
||||||
|
|
||||||
// Assign the color to the output variable
|
// Assign the color to the output variable
|
||||||
fragColor = color;
|
fragColor = color;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,109 +1,80 @@
|
|||||||
|
#version 150
|
||||||
|
|
||||||
|
// Uniform samplers
|
||||||
uniform sampler2D m_Texture;
|
uniform sampler2D m_Texture;
|
||||||
uniform sampler2D m_OutlineDepthTexture;
|
uniform sampler2D m_OutlineDepthTexture;
|
||||||
uniform sampler2D m_DepthTexture;
|
uniform sampler2D m_DepthTexture;
|
||||||
varying vec2 texCoord;
|
|
||||||
|
|
||||||
|
// Input texture coordinates from the vertex shader
|
||||||
|
in vec2 texCoord;
|
||||||
|
|
||||||
|
// Uniforms for resolution, outline color, and width
|
||||||
uniform vec2 m_Resolution;
|
uniform vec2 m_Resolution;
|
||||||
uniform vec4 m_OutlineColor;
|
uniform vec4 m_OutlineColor;
|
||||||
uniform float m_OutlineWidth;
|
uniform float m_OutlineWidth;
|
||||||
|
|
||||||
|
// Output variable for fragment color
|
||||||
|
out vec4 fragColor;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
vec4 depth = texture2D(m_OutlineDepthTexture, texCoord);
|
vec4 depth = texture(m_OutlineDepthTexture, texCoord);
|
||||||
vec4 depth1 = texture2D(m_OutlineDepthTexture, ((texCoord*m_Resolution)+vec2(m_OutlineWidth,m_OutlineWidth))/m_Resolution);
|
vec4 depth1 = texture(m_OutlineDepthTexture, (texCoord * m_Resolution + vec2(m_OutlineWidth, m_OutlineWidth)) / m_Resolution);
|
||||||
vec4 depth2 = texture2D(m_OutlineDepthTexture, ((texCoord*m_Resolution)+vec2(m_OutlineWidth,-m_OutlineWidth))/m_Resolution);
|
vec4 depth2 = texture(m_OutlineDepthTexture, (texCoord * m_Resolution + vec2(m_OutlineWidth, -m_OutlineWidth)) / m_Resolution);
|
||||||
vec4 depth3 = texture2D(m_OutlineDepthTexture, ((texCoord*m_Resolution)+vec2(-m_OutlineWidth,m_OutlineWidth))/m_Resolution);
|
vec4 depth3 = texture(m_OutlineDepthTexture, (texCoord * m_Resolution + vec2(-m_OutlineWidth, m_OutlineWidth)) / m_Resolution);
|
||||||
vec4 depth4 = texture2D(m_OutlineDepthTexture, ((texCoord*m_Resolution)+vec2(-m_OutlineWidth,-m_OutlineWidth))/m_Resolution);
|
vec4 depth4 = texture(m_OutlineDepthTexture, (texCoord * m_Resolution + vec2(-m_OutlineWidth, -m_OutlineWidth)) / m_Resolution);
|
||||||
vec4 depth5 = texture2D(m_OutlineDepthTexture, ((texCoord*m_Resolution)+vec2(0.,m_OutlineWidth))/m_Resolution);
|
vec4 depth5 = texture(m_OutlineDepthTexture, (texCoord * m_Resolution + vec2(0.0, m_OutlineWidth)) / m_Resolution);
|
||||||
vec4 depth6 = texture2D(m_OutlineDepthTexture, ((texCoord*m_Resolution)+vec2(0.,-m_OutlineWidth))/m_Resolution);
|
vec4 depth6 = texture(m_OutlineDepthTexture, (texCoord * m_Resolution + vec2(0.0, -m_OutlineWidth)) / m_Resolution);
|
||||||
vec4 depth7 = texture2D(m_OutlineDepthTexture, ((texCoord*m_Resolution)+vec2(m_OutlineWidth,0.))/m_Resolution);
|
vec4 depth7 = texture(m_OutlineDepthTexture, (texCoord * m_Resolution + vec2(m_OutlineWidth, 0.0)) / m_Resolution);
|
||||||
vec4 depth8 = texture2D(m_OutlineDepthTexture, ((texCoord*m_Resolution)+vec2(-m_OutlineWidth,0.))/m_Resolution);
|
vec4 depth8 = texture(m_OutlineDepthTexture, (texCoord * m_Resolution + vec2(-m_OutlineWidth, 0.0)) / m_Resolution);
|
||||||
vec4 color = texture2D(m_Texture, texCoord);
|
|
||||||
//如果是背景
|
vec4 color = texture(m_Texture, texCoord);
|
||||||
float ratio=0.;
|
|
||||||
if(depth==vec4(0.) && (depth1 != depth || depth2 != depth || depth3 != depth || depth4 != depth||depth5 != depth || depth6 != depth || depth7 != depth || depth8 != depth)){
|
float ratio = 0.0;
|
||||||
float dist=m_OutlineWidth;
|
if (depth == vec4(0.0) &&
|
||||||
//距离边的像素
|
(depth1 != depth || depth2 != depth || depth3 != depth || depth4 != depth ||
|
||||||
vec4 nearDepth;
|
depth5 != depth || depth6 != depth || depth7 != depth || depth8 != depth)) {
|
||||||
if(depth1 != depth){
|
float dist = m_OutlineWidth;
|
||||||
for(float i=0.;i<m_OutlineWidth;i++){
|
vec4 nearDepth;
|
||||||
nearDepth = texture2D(m_OutlineDepthTexture, ((texCoord*m_Resolution)+vec2(i,i))/m_Resolution);
|
|
||||||
if(nearDepth != depth){
|
// Iterate to find the distance to the nearest edge
|
||||||
dist = i;
|
for (float i = 0.0; i < m_OutlineWidth; i++) {
|
||||||
break;
|
if (depth1 != depth) {
|
||||||
}
|
nearDepth = texture(m_OutlineDepthTexture, (texCoord * m_Resolution + vec2(i, i)) / m_Resolution);
|
||||||
}
|
if (nearDepth != depth) { dist = i; break; }
|
||||||
}else
|
} else if (depth2 != depth) {
|
||||||
if(depth2 != depth){
|
nearDepth = texture(m_OutlineDepthTexture, (texCoord * m_Resolution + vec2(i, -i)) / m_Resolution);
|
||||||
for(float i=0.;i<m_OutlineWidth;i++){
|
if (nearDepth != depth) { dist = i; break; }
|
||||||
nearDepth = texture2D(m_OutlineDepthTexture, ((texCoord*m_Resolution)+vec2(i,-i))/m_Resolution);
|
} else if (depth3 != depth) {
|
||||||
if(nearDepth != depth){
|
nearDepth = texture(m_OutlineDepthTexture, (texCoord * m_Resolution + vec2(-i, i)) / m_Resolution);
|
||||||
dist = i;
|
if (nearDepth != depth) { dist = i; break; }
|
||||||
break;
|
} else if (depth4 != depth) {
|
||||||
}
|
nearDepth = texture(m_OutlineDepthTexture, (texCoord * m_Resolution + vec2(-i, -i)) / m_Resolution);
|
||||||
}
|
if (nearDepth != depth) { dist = i; break; }
|
||||||
}else
|
} else if (depth5 != depth) {
|
||||||
if(depth3 != depth){
|
nearDepth = texture(m_OutlineDepthTexture, (texCoord * m_Resolution + vec2(0.0, i)) / m_Resolution);
|
||||||
for(float i=0.;i<m_OutlineWidth;i++){
|
if (nearDepth != depth) { dist = i; break; }
|
||||||
nearDepth = texture2D(m_OutlineDepthTexture, ((texCoord*m_Resolution)+vec2(-i,i))/m_Resolution);
|
} else if (depth6 != depth) {
|
||||||
if(nearDepth != depth){
|
nearDepth = texture(m_OutlineDepthTexture, (texCoord * m_Resolution + vec2(0.0, -i)) / m_Resolution);
|
||||||
dist = i;
|
if (nearDepth != depth) { dist = i; break; }
|
||||||
break;
|
} else if (depth7 != depth) {
|
||||||
}
|
nearDepth = texture(m_OutlineDepthTexture, (texCoord * m_Resolution + vec2(i, 0.0)) / m_Resolution);
|
||||||
}
|
if (nearDepth != depth) { dist = i; break; }
|
||||||
}else
|
} else if (depth8 != depth) {
|
||||||
if(depth4 != depth){
|
nearDepth = texture(m_OutlineDepthTexture, (texCoord * m_Resolution + vec2(-i, 0.0)) / m_Resolution);
|
||||||
for(float i=0.;i<m_OutlineWidth;i++){
|
if (nearDepth != depth) { dist = i; break; }
|
||||||
nearDepth = texture2D(m_OutlineDepthTexture, ((texCoord*m_Resolution)+vec2(-i,-i))/m_Resolution);
|
}
|
||||||
if(nearDepth != depth){
|
}
|
||||||
dist = i;
|
|
||||||
break;
|
// Calculate ratio for outline blending
|
||||||
}
|
ratio = clamp(1.0 - dist / m_OutlineWidth, 0.0, 1.0);
|
||||||
}
|
|
||||||
}else
|
// Blend the outline color with the base color
|
||||||
if(depth5 != depth){
|
fragColor = color * (1.0 - ratio) + m_OutlineColor * ratio;
|
||||||
for(float i=0.;i<m_OutlineWidth;i++){
|
} else {
|
||||||
nearDepth = texture2D(m_OutlineDepthTexture, ((texCoord*m_Resolution)+vec2(0.,i))/m_Resolution);
|
// No outline, use the base texture color
|
||||||
if(nearDepth != depth){
|
fragColor = color;
|
||||||
dist = i;
|
}
|
||||||
break;
|
|
||||||
}
|
// Optional: Debugging outline visualization
|
||||||
}
|
// fragColor = vec4(0.0, 1.0 - ratio, 0.0, 1.0);
|
||||||
}else
|
|
||||||
if(depth6 != depth){
|
|
||||||
for(float i=0.;i<m_OutlineWidth;i++){
|
|
||||||
nearDepth = texture2D(m_OutlineDepthTexture, ((texCoord*m_Resolution)+vec2(0.,-i))/m_Resolution);
|
|
||||||
if(nearDepth != depth){
|
|
||||||
dist = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}else
|
|
||||||
if(depth7 != depth){
|
|
||||||
for(float i=0.;i<m_OutlineWidth;i++){
|
|
||||||
nearDepth = texture2D(m_OutlineDepthTexture, ((texCoord*m_Resolution)+vec2(i,0.))/m_Resolution);
|
|
||||||
if(nearDepth != depth){
|
|
||||||
dist = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}else
|
|
||||||
if(depth8 != depth){
|
|
||||||
for(float i=0.;i<m_OutlineWidth;i++){
|
|
||||||
nearDepth = texture2D(m_OutlineDepthTexture, ((texCoord*m_Resolution)+vec2(-i,0.))/m_Resolution);
|
|
||||||
if(nearDepth != depth){
|
|
||||||
dist = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//0:场景颜色 1:outline颜色
|
|
||||||
ratio = clamp(1.- dist/m_OutlineWidth,0.,1.);
|
|
||||||
//float off = (1.-ratio*ratio)*(1.-ratio*ratio);
|
|
||||||
gl_FragColor = color*(1.-ratio) +m_OutlineColor*ratio;
|
|
||||||
//gl_FragColor = m_OutlineColor;
|
|
||||||
}else{
|
|
||||||
gl_FragColor = color;
|
|
||||||
}
|
|
||||||
//debug
|
|
||||||
//gl_FragColor = vec4(0.,(1.-ratio),0.,1.);
|
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
// Use 'in' for vertex attributes
|
#version 150
|
||||||
|
|
||||||
|
// Vertex attributes
|
||||||
in vec4 inPosition;
|
in vec4 inPosition;
|
||||||
in vec2 inTexCoord;
|
in vec2 inTexCoord;
|
||||||
|
|
||||||
// Use 'out' for passing data to the fragment shader
|
// Output to fragment shader
|
||||||
out vec2 texCoord;
|
out vec2 texCoord;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
|||||||
Reference in New Issue
Block a user