查找由半径定义的磁盘内部的点

发布于 2025-01-23 04:45:55 字数 713 浏览 0 评论 0原文

我遇到了一个我试图理解在半径定义的磁盘中找到点的着色器。

此函数diskpoint如何工作?

float HASHVALUE = 0.0f;
vec2 RandomHashValue()
{
    return fract(sin(vec2(HASHVALUE += 0.1, HASHVALUE += 0.1)) * vec2(43758.5453123, 22578.1459123));
}

vec2 DiskPoint(in float radius, in float x1, in float x2)
{
    float P = radius * sqrt(1.0f - x1);
    float theta = x2 * 2.0 * PI;
    return vec2(P * cos(theta), P * sin(theta));
}

void main()
{
    HASHVALUE = (uvCoords.x * uvCoords.y) * 64.0;
    HASHVALUE += fract(time) * 64.0f;

    for (int i = 0; i < samples; ++i)
    {
        vec2 hash = RandomHashValue();
        vec2 disk = DiskPoint(sampleRadius, hash.x, hash.y);
        //....
    }

}

I have come across a shader that I am trying to understand where a point is found within a disk defined by a radius.

How does this function DiskPoint work?

float HASHVALUE = 0.0f;
vec2 RandomHashValue()
{
    return fract(sin(vec2(HASHVALUE += 0.1, HASHVALUE += 0.1)) * vec2(43758.5453123, 22578.1459123));
}

vec2 DiskPoint(in float radius, in float x1, in float x2)
{
    float P = radius * sqrt(1.0f - x1);
    float theta = x2 * 2.0 * PI;
    return vec2(P * cos(theta), P * sin(theta));
}

void main()
{
    HASHVALUE = (uvCoords.x * uvCoords.y) * 64.0;
    HASHVALUE += fract(time) * 64.0f;

    for (int i = 0; i < samples; ++i)
    {
        vec2 hash = RandomHashValue();
        vec2 disk = DiskPoint(sampleRadius, hash.x, hash.y);
        //....
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋叶绚丽 2025-01-30 04:45:55

我这样认为:

vec2 DiskPoint(in float radius, in float x1, in float x2)
    {
    // x1,x2 are "uniform randoms" in range <0,+1>
    float P = radius * sqrt(1.0f - x1); // this will scale x1 into P with range <0,radius> but change the distribution to uniform number of points inside disc
    float theta = x2 * 2.0 * PI; // this will scale x2 to theta in range <0,6.28>
    return vec2(P * cos(theta), P * sin(theta)); // and this just use the above as polar coordinates with parametric circle equation ...
    // returning "random" point inside disc with uniform density ...
    }

但是我只是猜测了,没有测试过,所以请记住这一点

I see it like this:

vec2 DiskPoint(in float radius, in float x1, in float x2)
    {
    // x1,x2 are "uniform randoms" in range <0,+1>
    float P = radius * sqrt(1.0f - x1); // this will scale x1 into P with range <0,radius> but change the distribution to uniform number of points inside disc
    float theta = x2 * 2.0 * PI; // this will scale x2 to theta in range <0,6.28>
    return vec2(P * cos(theta), P * sin(theta)); // and this just use the above as polar coordinates with parametric circle equation ...
    // returning "random" point inside disc with uniform density ...
    }

but I just guessed and not tested it so take that in mind

迷爱 2025-01-30 04:45:55

根据 @spektre的建议,我在画布上提取了此功能的结果。

代码看起来像这样:

        var canvas = document.getElementById("canvas");
        var ctx = canvas.getContext("2d");
        var canvasWidth = canvas.width;
        var canvasHeight = canvas.height;
        
        
        class vec2{
            constructor(x, y){
                this.x = x;
                this.y = y;
            }
        }
        
        function DiskPoint(radius, x1, x2){
            var p = radius * Math.sqrt(1.0 - x1);
            var theta = x2 * 2.0 * 3.14;
            return new vec2(p * Math.cos(theta), p * Math.sin(theta));
        }
        
        
        ctx.fillStyle = "#FF0000";
        ctx.fillRect(100, 100, 1, 1);
        
        for(var i = 0; i < 100; i++){
            for(var j = 0; j < 100; j++){
                var point = DiskPoint(0.5, i/100, j/100);
                ctx.fillRect(point.x * 100 + 100, point.y * 100 + 100, 1, 1);
                console.log(point.x, point.y);
            }
        }

这是创建的模式:

”由上面的代码创建的模式“

它看起来都可以找到给定半径的圆圈中的每个(x1,x2)。

希望这会有所帮助!

Based on @Spektre´s suggestion I drew the outcome of this function on a Canvas.

the code looks like this:

        var canvas = document.getElementById("canvas");
        var ctx = canvas.getContext("2d");
        var canvasWidth = canvas.width;
        var canvasHeight = canvas.height;
        
        
        class vec2{
            constructor(x, y){
                this.x = x;
                this.y = y;
            }
        }
        
        function DiskPoint(radius, x1, x2){
            var p = radius * Math.sqrt(1.0 - x1);
            var theta = x2 * 2.0 * 3.14;
            return new vec2(p * Math.cos(theta), p * Math.sin(theta));
        }
        
        
        ctx.fillStyle = "#FF0000";
        ctx.fillRect(100, 100, 1, 1);
        
        for(var i = 0; i < 100; i++){
            for(var j = 0; j < 100; j++){
                var point = DiskPoint(0.5, i/100, j/100);
                ctx.fillRect(point.x * 100 + 100, point.y * 100 + 100, 1, 1);
                console.log(point.x, point.y);
            }
        }

This is the created Pattern:

Created pattern by Code above

It looks like it finds for every (x1,x2) a point in the circle of given radius.

Hope this will help!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文