我一直试图让这个工作过去几天,我真的开始变得绝望了.我真的很感激你的建议.
我一直在努力做的事情:
问题:
如果我启用片段着色器(水平模糊),我会得到一个模糊的图像渲染到我的四边形第一帧左右,之后它全部变黑.
我怀疑我将纹理传递给着色器的方式有问题:
horizontalBlurVertex.enable();
horizontalBlurFragment.enable();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, fboTexture);
glUniform1i(glGetUniformLocation(horizontalBlurFragment.program, "RTScene"), 0);
Run Code Online (Sandbox Code Playgroud)
编辑: 另外一条信息:如果我删除glUniform1i(glGetUniformLocation(horizontalBlurFragment.program,"RTScene"),0); 我得到完全相同的结果.我猜这表明我的着色器从未获得任何纹理数据.
来源:
顶点着色器:
varying vec2 vTexCoord;
void main(void)
{
gl_FrontColor = gl_Color;
gl_Position = ftransform();
vec2 Pos;
Pos = sign(gl_Vertex.xy);
gl_Position = vec4(Pos, 0.0, 1.0);
vTexCoord = Pos * 0.5 + 0.5;
}
Run Code Online (Sandbox Code Playgroud)
片段着色器:
uniform sampler2D RTScene;
varying vec2 vTexCoord;
const float blurSize = 1.0/800.0;
const float weightSum = 70.0 + 2.0 * (1.0 + 8.0 + 28.0 + 56.0);
void main(void) …Run Code Online (Sandbox Code Playgroud) 
我有一个片段着色器,它基本上读取颜色alpha并将其转换为像素上的抖动效果.
但是,对于所有mod和if语句来说,它是处理器密集型的.有没有人对优化下面的代码有任何建议?
varying vec2 the_uv;
varying vec4 color;
void main()
{
// The pixel color will correspond
// to the uv coords of the texture
// for the given vertice, retrieved
// by the Vertex shader through varying vec2 the_uv
gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
vec4 tex = texture2D(_MainTex, the_uv);
tex = tex * color ;
float r = tex.a;
if ( r > 0.1 ) {
if ( ( mod(gl_FragCoord.x, 4.001) + mod(gl_FragCoord.y, 4.0) ) > 6.00 ) …Run Code Online (Sandbox Code Playgroud) 我正在尝试在Unity中创建线框顶点/片段着色器.根据这篇论文,似乎有可能.一般的想法似乎是您将在顶点着色器中计算的距离向量传递给片段着色器中的每个片段,它可以用来根据多边形中的位置确定绘制线框线的亮度.
但是,从我读过的其他内容来看,您似乎只能访问顶点着色器中的单个顶点.我需要访问多边形中的所有相邻顶点以获得ecah顶点.该论文似乎暗示几何着色器不是必需的,这很好,因为Unity还不支持它们.
我错过了什么?是否永远不可能访问顶点着色器中的相邻顶点?没有几何着色器,我正在尝试做什么?
gl_FragCoord变量存储四个组件:(x,y,z,1/w)
什么是w坐标,为什么它存储为1/w?
所以我有一个三角形:
我有一个顶点着色器:
uniform mat4 uViewProjection;
attribute vec3 aVertexPosition;
attribute vec2 aTextureCoords;
varying vec2 vTextureCoords;
void main(void) {
vTextureCoords = aTextureCoords;
gl_Position = uViewProjection * vec4(aVertexPosition, 1.0);
}
Run Code Online (Sandbox Code Playgroud)
我有一个片段着色器:
precision mediump float;
uniform sampler2D uMyTexture;
varying vec2 vTextureCoords;
void main(void) {
gl_FragColor = texture2D(uMyTexture, vTextureCoords);
}
Run Code Online (Sandbox Code Playgroud)
我提供了三组顶点和UV,交错:
# x, y, z, s, t
0.0, 1.0, 0.0, 0.5, 1.0
-1.0, -1.0, 0.0, 0.0, 0.0
1.0, -1.0, 0.0, 1.0, 0.0
Run Code Online (Sandbox Code Playgroud)
片段着色器如何知道以不同于像素B的方式绘制像素A?有什么变化?
我正在尝试创建一个着色器以在Three.js中产生发光的光晕效果.我目前的尝试是在这里:http://stemkoski.github.io/Three.js/Shader-Halo.html
着色器代码目前是:
<script id="vertexShader" type="x-shader/x-vertex">
varying vec3 vNormal;
void main()
{
vNormal = normalize( normalMatrix * normal );
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}
</script>
<script id="fragmentShader" type="x-shader/x-vertex">
varying vec3 vNormal;
void main()
{
float intensity = pow( 0.7 - dot( vNormal, vec3( 0.0, 0.0, 1.0 ) ), 4.0 );
gl_FragColor = vec4( 1.0, 1.0, 1.0, 1.0 ) * intensity;
}
</script>
Run Code Online (Sandbox Code Playgroud)
只要对象保持在场景的中心,这就可以正常工作.在缩放或平移之后,光晕效果似乎分别改变强度或看起来不平衡.
我想我理解为什么这是数学上 - 在这段代码中,发光效果的强度是由网格法线向量的z坐标决定的,因为这是面向观察者的向量,所以我应该以某种方式在计算强度之前,将类似的变换应用于(0,0,1).
于是我到了
<script id="vertexShader" type="x-shader/x-vertex">
varying vec3 …Run Code Online (Sandbox Code Playgroud) 我对如何使用片段着色器绘制具有特定宽度(或多行)的线感兴趣.我偶然发现这篇文章似乎在解释它.
我所面临的挑战是理解它背后的逻辑.
几个问题:
我有一个像这样实现阴影贴图的着色器:
#version 430 core
out vec4 color;
in VS_OUT {
vec3 N;
vec3 L;
vec3 V;
vec4 shadow_coord;
} fs_in;
layout(binding = 0) uniform sampler2DShadow shadow_tex;
uniform vec3 light_ambient_albedo = vec3(1.0);
uniform vec3 light_diffuse_albedo = vec3(1.0);
uniform vec3 light_specular_albedo = vec3(1.0);
uniform vec3 ambient_albedo = vec3(0.1, 0.1, 0.2);
uniform vec3 diffuse_albedo = vec3(0.4, 0.4, 0.8);
uniform vec3 specular_albedo = vec3(0.0, 0.0, 0.0);
uniform float specular_power = 128.0;
void main(void) {
//color = vec4(0.4, 0.4, 0.8, 1.0);
//normalize
vec3 N = …Run Code Online (Sandbox Code Playgroud) 我的目标是将一个点数组传递给着色器,计算它们与碎片的距离,并用一个渐变的圆圈绘制它们,这个圆形取决于该计算.
例如:
(从我在着色器玩具上设置的工作示例)
不幸的是,我不清楚如何计算和转换在着色器内传递的处理坐标.
我目前正在尝试将两个浮点数 - 一个用于x位置,一个用于每个点的y位置 - 通过一个制服传递到着色器.然后在着色器中迭代遍历每个点,如下所示:
#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif
uniform float sourceX[100];
uniform float sourceY[100];
uniform vec2 resolution;
in vec4 gl_FragCoord;
varying vec4 vertColor;
varying vec2 center;
varying vec2 pos;
void main()
{
float intensity = 0.0;
for(int i=0; i<100; i++)
{
vec2 source = vec2(sourceX[i],sourceY[i]);
vec2 position = ( gl_FragCoord.xy / resolution.xy );
float d = distance(position, source);
intensity += exp(-0.5*d*d);
}
intensity=3.0*pow(intensity,0.02);
if (intensity<=1.0)
gl_FragColor=vec4(0.0,intensity*0.5,0.0,1.0); …Run Code Online (Sandbox Code Playgroud) 有没有像Voronoi图这样的简单算法将任何矩形平面划分为三角形,最终使用预先定义的点数.
说实话,我已经写了一个非常简单的片段着色器像这样.
从理论上讲,这个Voronoii着色器可以通过Delaunay三角测量"升级",但想找到更优雅的解决方案.