我主要是编写着色器的新手,这可能不是一个很好的起点,但我正在尝试制作着色器以使对象"闪闪发光"。我不会详细介绍它的外观,但要做到这一点,我需要一个值,该值会随着物体在房间中的位置而变化(或在屏幕上,因为相机是固定的(。我尝试了v_vTexcoord、in_Position、gl_Position等,但没有预期的结果。如果我用错了它们或错过了什么,我不会感到惊讶,但任何建议都是有帮助的。
我认为它们不会有所帮助,但这是我的顶点着色器:
//it's mostly the same as the default
// Simple passthrough vertex shader
//
attribute vec3 in_Position; // (x,y,z)
//attribute vec3 in_Normal; // (x,y,z) unused in this shader.
attribute vec4 in_Colour; // (r,g,b,a)
attribute vec2 in_TextureCoord; // (u,v)
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
varying vec3 v_inpos;
void main()
{
vec4 object_space_pos = vec4( in_Position.x, in_Position.y, in_Position.z, 1.0);
gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * object_space_pos;
v_vColour = in_Colour;
v_vTexcoord = in_TextureCoord;
v_inpos = /*this is the variable that i'd like to set to the x,y*/;
}
和我的片段着色器:
//
//
//
varying vec2 v_vTexcoord; //x is <1, >.5
varying vec4 v_vColour;
varying vec3 v_inpos;
uniform float pixelH; //unused, but left in for accuracy
uniform float pixelW; //see above
void main() //WHEN AN ERROR HAPPENS, THE SHADER JUST WON'T DO ANYTHING AT ALL.
{
vec2 offset = vec2 (pixelW, pixelH);
gl_FragColor = v_vColour * texture2D( gm_BaseTexture, v_vTexcoord );
/* i am planning on just testing different math until something works, but i can't
vec3 test = vec3 (v_inpos.x, v_inpos.x, v_inpos.x); //find the values i need to test it
test.x = mod(test.x,.08);
test.x = test.x - 4;
test.x = abs(test.x);
while (test.x > 1.0){
test.x = test.x/10;
}
test = vec3 (test.x, test.x, test.x);
gl_FragColor.a = test.x;
*/
//everything above doesn't cause an error when uncommented, i think
//if (v_inpos.x == 0.0){gl_FragColor.rgb = vec3 (1,0,0);}
//if (v_inpos.x > 1) {gl_FragColor.rgb = vec3 (0,1,0);}
//if (v_inpos.x < 1) {gl_FragColor.rgb = vec3 (0,0,1);}
}
如果这个问题没有意义,我会尝试在评论中澄清任何其他问题。
如果要在世界空间中获得位置,则必须将顶点坐标从模型空间转换为世界空间。
这可以通过模型(世界(矩阵(gm_Matrices[MATRIX_WORLD]
(来完成。请参阅游戏制作者工作室-2 - 矩阵。
例如:
vec4 object_space_pos = vec4(in_Position.xyz, 1.0);
vec4 world_space_pos = gm_Matrices[MATRIX_WORLD] * object_space_pos;
不是,笛卡尔世界空间位置可以通过:
(另见Swizzling
(
vec3 pos = world_space_pos.xyz;