Simplex Noise shader

directxperlin-noiseshadersimplex-noise

I have a couple of questions about Simplex Noise. I'm using Simplex Noise to generate a terrain in directx but I'm currently doing it using classes and such. I will probably use this for textures as well, so is it necessary to change it into a shader implementation? And if so, is this easily done?

Also, for textures is it better to use 3D or 2D noise?

Best Answer

Yeah, you really should move this work to the GPU, it's what it's best at.

Simplex noise on the GPU is a solved problem. You can find a great paper on the subject here, or you can grab an implementation from here.

That implementation is in GLSL, but porting it is simply a matter of changing vec3's & vec4's to float3's and float4's.

Having used that, I have found that by far the fastest noise function implementation is an implementation of Perlin noise by inigo quilez (the implementation provided below is taken from code found on the shadertoy.com website. It's a great site, check it out!

#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
Related Topic