在哪个坐标空间中,顶点应该传递给碎片着色器?

发布于 2025-02-05 06:10:17 字数 2608 浏览 2 评论 0原文

我正在基于切断的顶点( v_cutpos )和当前顶点( v_currpos ),将片段着色器(将透明度设置为0/1)。从顶点着色器。这两个顶点通过世界协调

现在,切断的逻辑效果很好。但是切割本身并不光滑(必须遵循一定的形状)。当我转换为夹子空间之后,我经过相同的顶点时,剪切更加顺畅(或更细)。

对此有任何解释吗?

//碎片着色器

precision highp float;                                                     
varying mediump vec4 v_color;                                              
varying vec4 v_currPos;                                                  
varying vec4 v_cutPos;                                                     
                                                                           
/* returns 0 if pt is inside box otherwise 1 */                            
float insideCutArea(vec2 pt, vec2 cutPos)                                
{                                                                          
   return float(pt.y > cutPos.y);                                          
}                                                                          
                                                                           
void main(void)                                                            
{                                                                          
    float transparency = insideCutArea(v_currPos.xy, v_cutPos.xy);  
    gl_FragColor = vec4(v_color.xyz, v_color.w * transparency);            
}                       

//顶点着色器

varying mediump vec4 v_color; 
uniform vec3 cutPos;
varying vec4 v_currPos;                                                  
varying vec4 v_cutPos; 
void main(void)                                                                                  
{                                                                                                
                                           
//-------------------
other transformations 
-------------------//                                  
v_cutPos = myPMVMatrix * vec4(cutPos,1.0); //cut is not fine when not multiplying with the matrix           
gl_Position = myPMVMatrix * vec4(validVertex, 1.0);                                                
v_currPos = myPMVMatrix * vec4(validVertex, 1.0); //cut is not fine when not multiplying with the matrix                                            
v_color = color;                                                                            
                                                                                             
}

ps:由于没有太大的清晰度,此问题先前已关闭。我 通过解释我所做的工作再次创建了这个。

I am clipping off on the fragment shader (setting the transparency to 0/1) based on the cut off vertex (v_cutPos) and the current vertex (v_currPos) that I get from the vertex shader. These two vertices are passed as world coords.

Now, the cut off logic works fine. But the cut itself is not smooth (it has to follow a certain shape). And when I pass the same vertices after converting to clip space, the cut is much smoother (or finer.)

Is there any explanation to this?

//fragment shader

precision highp float;                                                     
varying mediump vec4 v_color;                                              
varying vec4 v_currPos;                                                  
varying vec4 v_cutPos;                                                     
                                                                           
/* returns 0 if pt is inside box otherwise 1 */                            
float insideCutArea(vec2 pt, vec2 cutPos)                                
{                                                                          
   return float(pt.y > cutPos.y);                                          
}                                                                          
                                                                           
void main(void)                                                            
{                                                                          
    float transparency = insideCutArea(v_currPos.xy, v_cutPos.xy);  
    gl_FragColor = vec4(v_color.xyz, v_color.w * transparency);            
}                       

//vertex shader

varying mediump vec4 v_color; 
uniform vec3 cutPos;
varying vec4 v_currPos;                                                  
varying vec4 v_cutPos; 
void main(void)                                                                                  
{                                                                                                
                                           
//-------------------
other transformations 
-------------------//                                  
v_cutPos = myPMVMatrix * vec4(cutPos,1.0); //cut is not fine when not multiplying with the matrix           
gl_Position = myPMVMatrix * vec4(validVertex, 1.0);                                                
v_currPos = myPMVMatrix * vec4(validVertex, 1.0); //cut is not fine when not multiplying with the matrix                                            
v_color = color;                                                                            
                                                                                             
}

PS: This question was previously closed due to not much clarity. I
have created this again with code explaining what I have done.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文