由于 GPU 驱动程序供应商通常不会费心去实现 noiseX 在 GLSL 中,我正在寻找 “图形随机化瑞士军刀” 实用函数集,最好优化为在 GPU 着色器中使用。我更喜欢 GLSL,但是任何语言的代码都适合我,我可以自己将其翻译为 GLSL。

具体来说,我期望:

A) 伪随机函数 - N 维,[-1,1] 或 [0,1] 上的均匀分布,从 M 维种子计算(理想情况下是任何值,但我可以将种子限制为 0)。 .1 用于均匀结果分布)。就像是:

float random  (T seed);
vec2  random2 (T seed);
vec3  random3 (T seed);
vec4  random4 (T seed);
// T being either float, vec2, vec3, vec4 - ideally.

b) 持续噪音 像 Perlin Noise - 再次,N 维,+- 均匀分布,具有受约束的值集,并且看起来不错(一些配置外观的选项,如 Perlin 级别也可能很有用)。我期望签名如下:

float noise  (T coord, TT seed);
vec2  noise2 (T coord, TT seed);
// ...

我不太喜欢随机数生成理论,所以我非常热切地想要一个 预制解决方案, ,但我也很感激像这样的答案 “这是一个非常好的、高效的一维 rand(),让我向您解释如何在它的基础上创建一个好的 N 维 rand()...” .

有帮助吗?

解决方案

有关非常简单的伪随机寻找的东西,我用这个oneliner,我在互联网上发现某处:

float rand(vec2 co){
    return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}

您也可以使用产生噪声纹理PRNG任何你喜欢的,然后以正常的方式上传这一点,并在样品着色器的值;我可以在以后挖一个代码示例,如果你想。

此外,检查出为培林和单纯形噪声的GLSL实现这个文件由斯特凡克古斯达夫逊。

其他提示

  

克古斯达夫逊的实现使用1D纹理

没有没有,不是因为2005年它只是人们坚持下载旧版本。即链路上的版本你提供的用途仅8位2D纹理。

新版本由阿诗玛的伊恩•麦克尤恩和我不使用纹理,但是对有很多纹理带宽的典型桌面平台的速度大约一半运行。在移动平台上,因为纹理往往是一个显著瓶颈的无纹理的版本可能会更快。

我们的积极维护源存储库是:

https://github.com/ashima/webgl-noise

这里(仅使用2D纹理)的无纹理和噪声二者的纹理使用版本的集合:

http://www.itn.liu.se/~stegu /simplexnoise/GLSL-noise-vs-noise.zip

如果您有任何具体问题,随时直接发邮件给我(我的电子邮件地址,可以在classicnoise*.glsl来源找到。)

它发生,我认为你可以使用一个简单的整数哈希函数,并将结果插入到一个浮动的尾数。 IIRC的GLSL规格保证32位无符号整数,IEEE binary32浮点表示所以它应该是完全便携式的。

我这个刚才给了一个尝试。结果是非常好的:它看起来就像每输入我试过了,没有可见的图处于静止状态。与此相反,流行正弦/ FRACT段已相当显着的对我的GPU对角线给定相同的输入。

的一个缺点是,它需要GLSL V3.30。虽然它似乎不够快,我还没有经验量化其性能。 AMD的着色器分析仪声称每个时钟13.33像素上HD5870的VEC 2版本。相反,每个时钟的16个像素的正弦/ FRACT片段。因此,它是肯定慢一点。

下面是我的实现。我把它的想法的各种排列,使其更容易从派生自己的功能。

/*
    static.frag
    by Spatial
    05 July 2013
*/

#version 330 core

uniform float time;
out vec4 fragment;



// A single iteration of Bob Jenkins' One-At-A-Time hashing algorithm.
uint hash( uint x ) {
    x += ( x << 10u );
    x ^= ( x >>  6u );
    x += ( x <<  3u );
    x ^= ( x >> 11u );
    x += ( x << 15u );
    return x;
}



// Compound versions of the hashing algorithm I whipped together.
uint hash( uvec2 v ) { return hash( v.x ^ hash(v.y)                         ); }
uint hash( uvec3 v ) { return hash( v.x ^ hash(v.y) ^ hash(v.z)             ); }
uint hash( uvec4 v ) { return hash( v.x ^ hash(v.y) ^ hash(v.z) ^ hash(v.w) ); }



// Construct a float with half-open range [0:1] using low 23 bits.
// All zeroes yields 0.0, all ones yields the next smallest representable value below 1.0.
float floatConstruct( uint m ) {
    const uint ieeeMantissa = 0x007FFFFFu; // binary32 mantissa bitmask
    const uint ieeeOne      = 0x3F800000u; // 1.0 in IEEE binary32

    m &= ieeeMantissa;                     // Keep only mantissa bits (fractional part)
    m |= ieeeOne;                          // Add fractional part to 1.0

    float  f = uintBitsToFloat( m );       // Range [1:2]
    return f - 1.0;                        // Range [0:1]
}



// Pseudo-random value in half-open range [0:1].
float random( float x ) { return floatConstruct(hash(floatBitsToUint(x))); }
float random( vec2  v ) { return floatConstruct(hash(floatBitsToUint(v))); }
float random( vec3  v ) { return floatConstruct(hash(floatBitsToUint(v))); }
float random( vec4  v ) { return floatConstruct(hash(floatBitsToUint(v))); }





void main()
{
    vec3  inputs = vec3( gl_FragCoord.xy, time ); // Spatial and temporal inputs
    float rand   = random( inputs );              // Random per-pixel value
    vec3  luma   = vec3( rand );                  // Expand to RGB

    fragment = vec4( luma, 1.0 );
}

截图:

“在static.frag随机(VEC

我在图像编辑程序检查的屏幕截图。有256种颜色和所述平均值是127,这意味着分布是均匀的,并覆盖预期范围。

<强>黄金噪声

// Gold Noise ©2015 dcerisano@standard3d.com 
//  - based on the Golden Ratio, PI and Square Root of Two
//  - superior distribution
//  - fastest noise generator function
//  - works with all chipsets (including low precision)

float PHI = 1.61803398874989484820459 * 00000.1; // Golden Ratio   
float PI  = 3.14159265358979323846264 * 00000.1; // PI
float SQ2 = 1.41421356237309504880169 * 10000.0; // Square Root of Two

float gold_noise(in vec2 coordinate, in float seed){
    return fract(tan(distance(coordinate*(seed+PHI), vec2(PHI, PI)))*SQ2);
}

看到黄金噪声在你的浏览器现在!

“在这里输入的图像描述”

这个函数在@appas'回答过电流功能改进的随机分布作为2017年9月9日的:

“在这里输入的图像描述”

在@appas功能也是不完全的,因为没有提供种子(UV不是种子 - 同每帧),并且不与低精度的芯片组的工作。在低精度默认(快得多)金噪音运行。

还有一个很好的实现描述 这里 由 McEwan 和 @StefanGustavson 编写,看起来像 Perlin 噪声,但“不需要任何设置,即不是纹理也不是统一数组。只需将其添加到您的着色器源代码中并在您想要的任何地方调用它即可”。

这非常方便,特别是考虑到 @dep 链接到的 Gustavson 的早期实现使用 1D 纹理,即 GLSL ES 不支持 (WebGL 的着色器语言)。

刚刚发现这个版本的GPU 3D噪声的,它alledgedly是最快的一个可用的:

#ifndef __noise_hlsl_
#define __noise_hlsl_

// hash based 3d value noise
// function taken from https://www.shadertoy.com/view/XslGRr
// Created by inigo quilez - iq/2013
// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

// ported from GLSL to HLSL

float hash( float n )
{
    return frac(sin(n)*43758.5453);
}

float noise( float3 x )
{
    // The noise function returns a value in the range -1.0f -> 1.0f

    float3 p = floor(x);
    float3 f = frac(x);

    f       = f*f*(3.0-2.0*f);
    float n = p.x + p.y*57.0 + 113.0*p.z;

    return lerp(lerp(lerp( hash(n+0.0), hash(n+1.0),f.x),
                   lerp( hash(n+57.0), hash(n+58.0),f.x),f.y),
               lerp(lerp( hash(n+113.0), hash(n+114.0),f.x),
                   lerp( hash(n+170.0), hash(n+171.0),f.x),f.y),f.z);
}

#endif

1d Perlin 的直锯齿版本,本质上是随机低频振荡器之字形。

half  rn(float xx){         
    half x0=floor(xx);
    half x1=x0+1;
    half v0 = frac(sin (x0*.014686)*31718.927+x0);
    half v1 = frac(sin (x1*.014686)*31718.927+x1);          

    return (v0*(1-frac(xx))+v1*(frac(xx)))*2-1*sin(xx);
}

我还在 Shadertoy 所有者 inigo quilez perlin 教程网站上找到了 1-2-3-4d perlin 噪声,以及 voronoi 等,他有完整的快速实现和代码。

散列: 如今webGL2.0有这么整数是在(W)GLSL可用。 - >质量便携式哈希(比丑陋的浮散列类似费用),我们现在可以用“严重”散列技术。 IQ执行一些 https://www.shadertoy.com/view/XlXcW4 (更)

E.g:

  const uint k = 1103515245U;  // GLIB C
//const uint k = 134775813U;   // Delphi and Turbo Pascal
//const uint k = 20170906U;    // Today's date (use three days ago's dateif you want a prime)
//const uint k = 1664525U;     // Numerical Recipes

vec3 hash( uvec3 x )
{
    x = ((x>>8U)^x.yzx)*k;
    x = ((x>>8U)^x.yzx)*k;
    x = ((x>>8U)^x.yzx)*k;

    return vec3(x)*(1.0/float(0xffffffffU));
}

请使用这个:

highp float rand(vec2 co)
{
    highp float a = 12.9898;
    highp float b = 78.233;
    highp float c = 43758.5453;
    highp float dt= dot(co.xy ,vec2(a,b));
    highp float sn= mod(dt,3.14);
    return fract(sin(sn) * c);
}

不要使用这个:

float rand(vec2 co){
    return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}

您可以在中找到解释 针对 OpenGL ES 2.0 的规范单行 GLSL rand() 的改进

请参阅下面的示例,了解如何向渲染的纹理添加白噪声。解决方案是使用两个纹理:原始的纯白噪声,就像这个: 维基白噪声

private static final String VERTEX_SHADER =
    "uniform mat4 uMVPMatrix;\n" +
    "uniform mat4 uMVMatrix;\n" +
    "uniform mat4 uSTMatrix;\n" +
    "attribute vec4 aPosition;\n" +
    "attribute vec4 aTextureCoord;\n" +
    "varying vec2 vTextureCoord;\n" +
    "varying vec4 vInCamPosition;\n" +
    "void main() {\n" +
    "    vTextureCoord = (uSTMatrix * aTextureCoord).xy;\n" +
    "    gl_Position = uMVPMatrix * aPosition;\n" +
    "}\n";

private static final String FRAGMENT_SHADER =
        "precision mediump float;\n" +
        "uniform sampler2D sTextureUnit;\n" +
        "uniform sampler2D sNoiseTextureUnit;\n" +
        "uniform float uNoseFactor;\n" +
        "varying vec2 vTextureCoord;\n" +
        "varying vec4 vInCamPosition;\n" +
        "void main() {\n" +
                "    gl_FragColor = texture2D(sTextureUnit, vTextureCoord);\n" +
                "    vec4 vRandChosenColor = texture2D(sNoiseTextureUnit, fract(vTextureCoord + uNoseFactor));\n" +
                "    gl_FragColor.r += (0.05 * vRandChosenColor.r);\n" +
                "    gl_FragColor.g += (0.05 * vRandChosenColor.g);\n" +
                "    gl_FragColor.b += (0.05 * vRandChosenColor.b);\n" +
        "}\n";

共享的片段包含参数 uNoiseFactor,该参数在主应用程序的每次渲染上更新:

float noiseValue = (float)(mRand.nextInt() % 1000)/1000;
int noiseFactorUniformHandle = GLES20.glGetUniformLocation( mProgram, "sNoiseTextureUnit");
GLES20.glUniform1f(noiseFactorUniformHandle, noiseFactor);

我已经翻译肯培林的Java实现成GLSL之一,并在ShaderToy几个项目中使用它。

下面是解释GLSL我做:

int b(int N, int B) { return N>>B & 1; }
int T[] = int[](0x15,0x38,0x32,0x2c,0x0d,0x13,0x07,0x2a);
int A[] = int[](0,0,0);

int b(int i, int j, int k, int B) { return T[b(i,B)<<2 | b(j,B)<<1 | b(k,B)]; }

int shuffle(int i, int j, int k) {
    return b(i,j,k,0) + b(j,k,i,1) + b(k,i,j,2) + b(i,j,k,3) +
        b(j,k,i,4) + b(k,i,j,5) + b(i,j,k,6) + b(j,k,i,7) ;
}

float K(int a, vec3 uvw, vec3 ijk)
{
    float s = float(A[0]+A[1]+A[2])/6.0;
    float x = uvw.x - float(A[0]) + s,
        y = uvw.y - float(A[1]) + s,
        z = uvw.z - float(A[2]) + s,
        t = 0.6 - x * x - y * y - z * z;
    int h = shuffle(int(ijk.x) + A[0], int(ijk.y) + A[1], int(ijk.z) + A[2]);
    A[a]++;
    if (t < 0.0)
        return 0.0;
    int b5 = h>>5 & 1, b4 = h>>4 & 1, b3 = h>>3 & 1, b2= h>>2 & 1, b = h & 3;
    float p = b==1?x:b==2?y:z, q = b==1?y:b==2?z:x, r = b==1?z:b==2?x:y;
    p = (b5==b3 ? -p : p); q = (b5==b4 ? -q : q); r = (b5!=(b4^b3) ? -r : r);
    t *= t;
    return 8.0 * t * t * (p + (b==0 ? q+r : b2==0 ? q : r));
}

float noise(float x, float y, float z)
{
    float s = (x + y + z) / 3.0;  
    vec3 ijk = vec3(int(floor(x+s)), int(floor(y+s)), int(floor(z+s)));
    s = float(ijk.x + ijk.y + ijk.z) / 6.0;
    vec3 uvw = vec3(x - float(ijk.x) + s, y - float(ijk.y) + s, z - float(ijk.z) + s);
    A[0] = A[1] = A[2] = 0;
    int hi = uvw.x >= uvw.z ? uvw.x >= uvw.y ? 0 : 1 : uvw.y >= uvw.z ? 1 : 2;
    int lo = uvw.x <  uvw.z ? uvw.x <  uvw.y ? 0 : 1 : uvw.y <  uvw.z ? 1 : 2;
    return K(hi, uvw, ijk) + K(3 - hi - lo, uvw, ijk) + K(lo, uvw, ijk) + K(0, uvw, ijk);
}

我翻译它从附录B从肯培林的噪声硬件的第2章在此源:

https://www.csee.umbc.edu/~olano /s2002c36/ch02.pdf

下面是一个公共遮阳我上着色玩具那样使用所述张贴的噪声函数:

https://www.shadertoy.com/view/3slXzM

我在我的研究过程中的噪音的对象体内发现的一些其他的好来源包括:

https://thebookofshaders.com/11/

https://mzucker.github.io/html/perlin -noise-数学的faq.html

https://rmarcus.info/blog/2018/03 /04/perlin-noise.html

http://flafla2.github.io/2014/08/09 /perlinnoise.html

https://mrl.nyu.edu/~perlin/noise/

https://rmarcus.info/blog/assets/perlin/perlin_paper.pdf

https://developer.nvidia.com/gpugems/GPUGems/gpugems_ch05.html

我强烈建议着色器的书,因为它不仅提供了噪声的伟大互动的解释,但还有其他着色器的概念。

编辑:

可能能够通过利用一些在GLSL可用的硬件加速功能,以优化转换的代码。如果我最终会做这将更新这个帖子。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top