首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Three.js/GLSL -将像素坐标转换为世界坐标

Three.js/GLSL -将像素坐标转换为世界坐标
EN

Stack Overflow用户
提问于 2013-12-01 00:51:29
回答 1查看 2.9K关注 0票数 4

我的Three.js应用程序中有一个简单的着色器,将屏幕涂成红色。但是,我想把所有像素都涂到给定的世界位置的右边,以不同的颜色。

我见过一些 答案建议使用varying vec4 worldCoord = gl_ModelViewMatrix * gl_Vertex;,但是由于WebGL使用GLSLES,所以像gl_Vertex这样的变量对我来说是不可用的。

顶点着色器

代码语言:javascript
复制
<script type="x-shader/x-vertex" id="vertexshader">
    #ifdef GL_ES
    precision highp float;
    #endif

    void main()
    {
        gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);
    }
</script>

片段着色器

代码语言:javascript
复制
<script type="x-shader/x-fragment" id="fragmentshader">
    #ifdef GL_ES
    precision highp float;
    #endif

    float worldX = 10.0; //Or some other position in the WebGL world

    void main()
    {
        if(gl_FragCoord.x > worldX) //FragCoord gives me coordinates relative to the screen
        {
            gl_FragColor = vec4(0.0, 1.0, 0.0, 1);
        }

        else
        {
            gl_FragColor = vec4(1.0, 0.0, 0.0, 1);
        }
    }
</script>

问:如何使用Three.js?将像素的位置转换为在WebGL中的世界位置

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-01 02:27:57

答案来自韦斯特兰利的Fiddle这里:http://jsfiddle.net/ST4aM/2/

顶点着色器

代码语言:javascript
复制
<script type="x-shader/x-vertex" id="vertexshader">
    #ifdef GL_ES
    precision highp float;
    #endif

    varying float x;
    varying float y;
    void main()
    {
        gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);
        x = position.x;
        y = position.y;
    }
</script>

片段着色器

代码语言:javascript
复制
<script type="x-shader/x-fragment" id="fragmentshader">
    #ifdef GL_ES
    precision highp float;
    #endif

    varying float x;
    varying float y;

    void main()
    {
        if(x > somePosition)
        {
            gl_FragColor = vec4(0.0, 1.0, 0.0, 1);
        }

        else
        {
            gl_FragColor = vec4(1.0, 0.0, 0.0, 1);
        }
    }
</script>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20307489

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档