浏览代码

Implement a higher quality uniform noise function

/RenderPassXR_Sandbox
Evgenii Golubev 7 年前
当前提交
e4e4bd7a
共有 4 个文件被更改,包括 83 次插入6 次删除
  1. 4
      Assets/ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/LitData.hlsl
  2. 9
      Assets/ScriptableRenderPipeline/ShaderLibrary/Common.hlsl
  3. 66
      Assets/ScriptableRenderPipeline/ShaderLibrary/Noise.hlsl
  4. 10
      Assets/ScriptableRenderPipeline/ShaderLibrary/Noise.hlsl.meta

4
Assets/ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/LitData.hlsl


void GetSurfaceAndBuiltinData(FragInputs input, float3 V, inout PositionInputs posInput, out SurfaceData surfaceData, out BuiltinData builtinData)
{
#ifdef LOD_FADE_CROSSFADE // enable dithering LOD transition if user select CrossFade transition in LOD group
LODDitheringTransition(posInput.unPositionSS, unity_LODFade.x);
LODDitheringTransition(posInput.unPositionSS, unity_LODFade.x, _Time.x);
#endif
ApplyDoubleSidedFlipOrMirror(input); // Apply double sided flip on the vertex normal

void GetSurfaceAndBuiltinData(FragInputs input, float3 V, inout PositionInputs posInput, out SurfaceData surfaceData, out BuiltinData builtinData)
{
#ifdef LOD_FADE_CROSSFADE // enable dithering LOD transition if user select CrossFade transition in LOD group
LODDitheringTransition(posInput.unPositionSS, unity_LODFade.x);
LODDitheringTransition(posInput.unPositionSS, unity_LODFade.x, _Time.x);
#endif
ApplyDoubleSidedFlipOrMirror(input); // Apply double sided flip on the vertex normal

9
Assets/ScriptableRenderPipeline/ShaderLibrary/Common.hlsl


#endif
#include "API/Validate.hlsl"
#include "Noise.hlsl"
// Some shader compiler don't support to do multiple ## for concatenation inside the same macro, it require an indirection.
// This is the purpose of this macro
#define MERGE_NAME(X, Y) X##Y

// LOD dithering transition helper
// LOD0 must use this function with ditherFactor 1..0
// LOD1 must use this function with ditherFactor 0..1
void LODDitheringTransition(uint2 unPositionSS, float ditherFactor)
void LODDitheringTransition(uint2 unPositionSS, float ditherFactor, float time)
// Generate a fixed pattern
float p = cos(dot(unPositionSS, float2(443.8975, 397.2973)));
p = frac(p * 491.1871);
// Generate a spatio-temporally varying pattern.
float p = GenerateHashedRandomFloat(uint3(unPositionSS, asuint(time)));
// We want to have a symmetry between 0..0.5 ditherFactor and 0.5..1 so no pixels are transparent during the transition
// this is handled by this test which reverse the pattern

66
Assets/ScriptableRenderPipeline/ShaderLibrary/Noise.hlsl


#ifndef UNITY_NOISE_INCLUDED
#define UNITY_NOISE_INCLUDED
// A single iteration of Bob Jenkins' One-At-A-Time hashing algorithm.
uint JenkinsHash(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.
uint JenkinsHash(uint2 v)
{
return JenkinsHash(v.x ^ JenkinsHash(v.y));
}
uint JenkinsHash(uint3 v)
{
return JenkinsHash(v.x ^ JenkinsHash(v.y) ^ JenkinsHash(v.z));
}
uint JenkinsHash(uint4 v)
{
return JenkinsHash(v.x ^ JenkinsHash(v.y) ^ JenkinsHash(v.z) ^ JenkinsHash(v.w));
}
// Construct a float with half-open range [0:1] using low 23 bits.
// All zeros yields 0, all ones yields the next smallest representable value below 1.
float ConstructFloat(uint m) {
const uint ieeeMantissa = 0x007FFFFFu; // Binary FP32 mantissa bitmask
const uint ieeeOne = 0x3F800000u; // 1.0 in FP32 IEEE
m &= ieeeMantissa; // Keep only mantissa bits (fractional part)
m |= ieeeOne; // Add fractional part to 1.0
float f = asfloat(m); // Range [1:2]
return f - 1; // Range [0:1]
}
// Pseudo-random value in half-open range [0:1]. The distribution is reasonably uniform.
// Ref: https://stackoverflow.com/a/17479300
float GenerateHashedRandomFloat(uint x)
{
return ConstructFloat(JenkinsHash(x));
}
float GenerateHashedRandomFloat(uint2 v)
{
return ConstructFloat(JenkinsHash(v));
}
float GenerateHashedRandomFloat(uint3 v)
{
return ConstructFloat(JenkinsHash(v));
}
float GenerateHashedRandomFloat(uint4 v)
{
return ConstructFloat(JenkinsHash(v));
}
#endif // UNITY_NOISE_INCLUDED

10
Assets/ScriptableRenderPipeline/ShaderLibrary/Noise.hlsl.meta


fileFormatVersion: 2
guid: 5918bc8b07f593546974c1961387db77
timeCreated: 1501167559
licenseType: Pro
ShaderImporter:
externalObjects: {}
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存