给定世界坐标(均匀)与GLSL中呈现的当前像素之间的距离?

时间:2016-08-22 21:51:54

标签: opengl glsl shader distance

我正试图让像素来自光源的距离越大。

如何计算我的lightsource(u_lightSourcePosition)和正在渲染的像素之间的距离?

我试过这样做:

float distance_from_point_to_pixel = distance(gl_FragCoord,v_lightSourcePosition) 

但它没有用。

顶点着色器

attribute vec3 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;

varying vec4 v_color;
varying vec2 v_texCoord0;
varying vec2 v_lightSourcePosition;


uniform mat4 u_projTrans;
uniform vec2 u_lightSourcePosition;

void main() {
    vec4 position = u_projTrans * vec4(a_position, 1.0);
    gl_Position = position;
    v_color = a_color;
    v_texCoord0 = a_texCoord0;
    v_lightSourcePosition = u_lightSourcePosition ;

}

片段着色器

varying vec4 v_color;
varying vec2 v_texCoord0;

varying vec2 v_lightSourcePosition;

uniform sampler2D u_sampler2D;


void main() {
    vec4 color = texture2D(u_sampler2D, v_texCoord0) * v_color;
    float lightRadius = 400.0;
    float distance_from_point_to_pixel = distance(gl_FragCoord,v_lightSourcePosition);
    color.a = distance_from_point_to_pixel / lightRadius;
    gl_FragColor = color;
}

1 个答案:

答案 0 :(得分:0)

你需要片段的世界空间。将它从顶点着色器传递到像素着色器作为另一个<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <input type="button" data-toggle="modal" data-target="#myModal" value="Launch Modal"> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-body"> Hello, below is some text in a div that should start scrolling if the height of the modal exceeds the browser. <p><div id="scrollbox"> the<br>quick<br>brown<br>fox<br> the<br>quick<br>brown<br>fox<br> the<br>quick<br>brown<br>fox<br> the<br>quick<br>brown<br>fox<br> the<br>quick<br>brown<br>fox<br> the<br>quick<br>brown<br>fox<br> the<br>quick<br>brown<br>fox<br> the<br>quick<br>brown<br>fox<br> the<br>quick<br>brown<br>fox<br> the<br>quick<br>brown<br>fox<br> </div> </div> </div> </div> </div>,您可能希望传入vec3矩阵将world/model从对象空间转换为世界空间。 a_position

相关问题