<script id="shader-triangle-fs" type="x-shader/x-fragment">
precision mediump float;
uniform vec4 colorVec;
uniform sampler2D uSampler;
varying highp vec2 vTextureCoord;
varying vec3 vVertexPosition;
void main(void) {
float dist = length(vVertexPosition.xy - gl_FragCoord.xy);
float att = 100.0/dist;
vec4 color = vec4(att,att,att, 1);
vec4 texture = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
gl_FragColor = color;
}
</script>
<script id="shader-triangle-vs" type="x-shader/x-vertex">
attribute vec3 aVertexPosition;
attribute vec2 aTextureCoord;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
varying highp vec2 vTextureCoord;
varying vec3 vVertexPosition;
void main(void) {
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
vTextureCoord = aTextureCoord;
vVertexPosition = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
}
</script>
In float dist = length(vVertexPosition.xy - gl_FragCoord.xy);
, What I want to get is the distance from current Vertex coordinate to Fragment coordinate.
But always what i get is this.
http://puu.sh/6rbva.jpg
http://puu.sh/6rbzM.jpg
(I used triangleStrip.)
These images show vVertexPosition.xy - gl_FragCoord.xy = 0
so vVertexPosition.xy == gl_FragCoord.xy
.
Why this thing happens?
And how to get distance from current Vertex Position to current FragCoord?
You declare vVertexPosition
as varying
, so it interpolated between the three vertexes in the triangle. Since the values set for it at each vertex are the vertex position, it will thus end up being the same as the fragment position.
The question is, what do you want? You say you want 'the distance from the current vertex position', but that makes no sense as there is no single "current" vertex in a fragment shader -- there are multiple vertexes that define the primitive containing the fragment.
With later versions of OGL/GLSL, you could define it as flat varying
(or just flat
depending on version), in which case it would be the position of the provoking vertex.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With