浏览代码
HDRenderPipeline: Another pass on adding surface gradient code
HDRenderPipeline: Another pass on adding surface gradient code
- miss the init of vT1/Vb1 etc.../Branch_Batching2
Sebastien Lagarde
8 年前
当前提交
01072951
共有 10 个文件被更改,包括 253 次插入 和 92 次删除
-
15Assets/ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/LitData.hlsl
-
31Assets/ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/LitDataInternal.hlsl
-
2Assets/ScriptableRenderPipeline/HDRenderPipeline/Material/MaterialUtilities.hlsl
-
39Assets/ScriptableRenderPipeline/HDRenderPipeline/Material/SampleLayer.hlsl
-
88Assets/ScriptableRenderPipeline/HDRenderPipeline/Material/SampleLayerInternal.hlsl
-
9Assets/ScriptableRenderPipeline/ShaderLibrary/CommonMaterial.hlsl
-
55Assets/ScriptableRenderPipeline/HDRenderPipeline/Material/SampleLayerNormalInternal.hlsl
-
9Assets/ScriptableRenderPipeline/HDRenderPipeline/Material/SampleLayerNormalInternal.hlsl.meta
-
88Assets/ScriptableRenderPipeline/ShaderLibrary/NormalSurfaceGradient.hlsl
-
9Assets/ScriptableRenderPipeline/ShaderLibrary/NormalSurfaceGradient.hlsl.meta
|
|||
float3 ADD_FUNC_SUFFIX(ADD_NORMAL_FUNC_SUFFIX(SampleLayerNormal))(TEXTURE2D_ARGS(layerTex, layerSampler), LayerUV layerUV, CommonLayerUV common, float scale, float param) |
|||
{ |
|||
if (layerUV.isTriplanar) |
|||
{ |
|||
float3 triplanarWeights = common.triplanarWeights; |
|||
|
|||
#ifdef SURFACE_GRADIENT |
|||
float2 derivXplane; |
|||
float2 derivYPlane; |
|||
float2 derivZPlane; |
|||
derivXplane = derivYPlane = derivZPlane = float2(0.0, 0.0); |
|||
|
|||
if (triplanarWeights.x > 0.0) |
|||
derivXplane = triplanarWeights.x * UNPACK_DERIVATIVE_FUNC(SAMPLE_TEXTURE_FUNC(layerTex, layerSampler, layerUV.uvZY, param), scale); |
|||
if (triplanarWeights.y > 0.0) |
|||
derivYPlane = triplanarWeights.y * UNPACK_DERIVATIVE_FUNC(SAMPLE_TEXTURE_FUNC(layerTex, layerSampler, layerUV.uvXZ, param), scale); |
|||
if (triplanarWeights.z > 0.0) |
|||
derivZPlane = triplanarWeights.z * UNPACK_DERIVATIVE_FUNC(SAMPLE_TEXTURE_FUNC(layerTex, layerSampler, layerUV.uvXY, param), scale); |
|||
|
|||
// Assume derivXplane, derivYPlane and derivZPlane sampled using (z,y), (z,x) and (x,y) respectively. |
|||
// TODO: Check with morten convention! Do it follow ours ? |
|||
float3 volumeGrad = float3(derivZPlane.x + derivYPlane.y, derivZPlane.y + derivXplane.y, derivXplane.x + derivYPlane.x); |
|||
return surfgradFromVolumeGradient(layerUV.vertexNormalWS, volumeGrad); |
|||
#else |
|||
float3 val = float3(0.0, 0.0, 0.0); |
|||
|
|||
if (triplanarWeights.x > 0.0) |
|||
val += triplanarWeights.x * UNPACK_NORMAL_FUNC(SAMPLE_TEXTURE_FUNC(layerTex, layerSampler, layerUV.uvZY, param), scale); |
|||
if (triplanarWeights.y > 0.0) |
|||
val += triplanarWeights.y * UNPACK_NORMAL_FUNC(SAMPLE_TEXTURE_FUNC(layerTex, layerSampler, layerUV.uvXZ, param), scale); |
|||
if (triplanarWeights.z > 0.0) |
|||
val += triplanarWeights.z * UNPACK_NORMAL_FUNC(SAMPLE_TEXTURE_FUNC(layerTex, layerSampler, layerUV.uvXY, param), scale); |
|||
|
|||
return normalize(val); |
|||
#endif |
|||
} |
|||
#ifdef SURFACE_GRADIENT |
|||
else if (layerUV.isPlanar) |
|||
{ |
|||
float2 derivYPlane = UNPACK_DERIVATIVE_FUNC(SAMPLE_TEXTURE_FUNC(layerTex, layerSampler, layerUV.uvXZ, param), scale); |
|||
// See comment above |
|||
float3 volumeGrad = float3(derivYPlane.y, 0.0, derivYPlane.x); |
|||
return surfgradFromVolumeGradient(layerUV.vertexNormalWS, volumeGrad); |
|||
} |
|||
#endif |
|||
else |
|||
{ |
|||
#ifdef SURFACE_GRADIENT |
|||
float2 deriv = UNPACK_DERIVATIVE_FUNC(SAMPLE_TEXTURE_FUNC(layerTex, layerSampler, layerUV.uv, param), scale); |
|||
return SurfaceGradientFromTBN(deriv, layerUV.vT, layerUV.vB); |
|||
#else |
|||
return UNPACK_NORMAL_FUNC(SAMPLE_TEXTURE_FUNC(layerTex, layerSampler, layerUV.uv, param), scale); |
|||
#endif |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: f941b2119184e624a8ecd126735c5ed0 |
|||
timeCreated: 1487475828 |
|||
licenseType: Pro |
|||
ShaderImporter: |
|||
defaultTextures: [] |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
// this produces an orthonormal basis of the tangent and bitangent WITHOUT vertex level tangent/bitangent for any UV including procedurally generated |
|||
// method released with the demo for publication of "bump mapping unparametrized surfaces on the GPU" |
|||
// http://mmikkelsen3d.blogspot.com/2011/07/derivative-maps.html |
|||
void SurfaceGradientGenBasisTB(float3 nrmVertexNormal, float3 sigmaX, float3 sigmaY, float flip sign, out float3 vT, out float3 vB, float2 texST) |
|||
{ |
|||
float2 dSTdx = ddx_fine(texST), dSTdy = ddy_fine(texST); |
|||
|
|||
float det = dot(dSTdx, float2(dSTdy.y, -dSTdy.x)); |
|||
float sign_det = det < 0 ? -1 : 1; |
|||
|
|||
// invC0 represents (dXds, dYds); but we don't divide by determinant (scale by sign instead) |
|||
float2 invC0 = sign_det * float2(dSTdy.y, -dSTdx.y); |
|||
vT = sigmaX * invC0.x + sigmaY * invC0.y; |
|||
if (abs(det) > 0.0) |
|||
vT = normalize(vT); |
|||
vB = (sign_det * flip_sign) * cross(nrmVertexNormal, vT); |
|||
} |
|||
|
|||
// surface gradient from an on the fly TBN (deriv obtained using tspaceNormalToDerivative()) or from conventional vertex level TBN (mikktspace compliant and deriv obtained using tspaceNormalToDerivative()) |
|||
float3 SurfaceGradientFromTBN(float2 deriv, float3 vT, float3 vB) |
|||
{ |
|||
return deriv.x * vT + deriv.y * vB; |
|||
} |
|||
|
|||
// surface gradient from an already generated "normal" such as from an object space normal map |
|||
// this allows us to mix the contribution together with a series of other contributions including tangent space normals |
|||
// v does not need to be unit length as long as it establishes the direction. |
|||
float3 SurfaceGradientFromPerturbedNormal(float3 nrmVertexNormal, float3 v) |
|||
{ |
|||
float3 n = nrmVertexNormal; |
|||
float s = 1.0 / max(FLT_EPSILON, abs(dot(n, v))); |
|||
return s * (dot(n, v) * n - v); |
|||
} |
|||
|
|||
// used to produce a surface gradient from the gradient of a volume bump function such as a volume of perlin noise. |
|||
// equation 2. in "bump mapping unparametrized surfaces on the GPU". |
|||
// Observe the difference in figure 2. between using the gradient vs. the surface gradient to do bump mapping (the original method is proved wrong in the paper!). |
|||
float3 SurfaceGradientFromVolumeGradient(float3 nrmVertexNormal, float3 grad) |
|||
{ |
|||
return grad - dot(nrmVertexNormal, grad) * nrmVertexNormal; |
|||
} |
|||
|
|||
// triplanar projection considered special case of volume bump map |
|||
// described here: http://mmikkelsen3d.blogspot.com/2013/10/volume-height-maps-and-triplanar-bump.html |
|||
// derivs obtained using tspaceNormalToDerivative() and weights using computeTriplanarWeights(). |
|||
float3 SurfaceGradientFromTriplanarProjection(float3 nrmVertexNormal, float3 triplanarWeights, float2 deriv_xplane, float2 deriv_yplane, float2 deriv_zplane) |
|||
{ |
|||
const float w0 = triplanarWeights.x, w1 = triplanarWeights.y, w2 = triplanarWeights.z; |
|||
|
|||
// assume deriv_xplane, deriv_yplane and deriv_zplane sampled using (z,y), (z,x) and (x,y) respectively. |
|||
// positive scales of the look-up coordinate will work as well but for negative scales the derivative components will need to be negated accordingly. |
|||
float3 volumeGrad = float3(w2 * deriv_zplane.x + w1 * deriv_yplane.y, w2 * deriv_zplane.y + w0 * deriv_xplane.y, w0 * deriv_xplane.x + w1 * deriv_yplane.x); |
|||
|
|||
return SurfaceGradientFromVolumeGradient(nrmVertexNormal, volumeGrad); |
|||
} |
|||
|
|||
float3 SurfaceGradientResolveNormal(float3 nrmVertexNormal, float3 surfGrad) |
|||
{ |
|||
return normalize(nrmVertexNormal - surfGrad); |
|||
} |
|||
|
|||
// The 128 means the derivative will come out no greater than 128 numerically (where 1 is 45 degrees so 128 is very steap). You can increase it if u like of course |
|||
// Basically tan(angle) limited to 128 |
|||
// So a max angle of 89.55 degrees ;) id argue thats close enough to the vertical limit at 90 degrees |
|||
// vT is channels.xy of a tangent space normal in[-1; 1] |
|||
// out: convert vT to a derivative |
|||
float2 UnpackDerivativeNormalAG(float4 packedNormal, float scale = 1.0) |
|||
{ |
|||
const float fS = 1.0 / (128.0 * 128.0); |
|||
float2 vT = packedNormal.wy * 2.0 - 1.0; |
|||
float2 vTsq = vT * vT; |
|||
float nz_sq = 1 - vTsq.x - vTsq.y; |
|||
float maxcompxy_sq = fS * max(vTsq.x, vTsq.y); |
|||
float z_inv = rsqrt(max(nz_sq, maxcompxy_sq)); |
|||
float2 deriv = -z_inv * float2(vT.x, vT.y); |
|||
return deriv * scale; |
|||
} |
|||
|
|||
float2 UnpackDerivativeNormalRGB(float4 packedNormal, float scale = 1.0) |
|||
{ |
|||
const float fS = 1.0 / (128.0 * 128.0); |
|||
float3 vT = packedNormal.xyz * 2.0 - 1.0; |
|||
float3 vTsq = vT * vT; |
|||
float maxcompxy_sq = fS * max(vTsq.x, vTsq.y); |
|||
float z_inv = rsqrt(max(vTsq.z, maxcompxy_sq)); |
|||
float2 deriv = -z_inv * float2(vT.x, vT.y); |
|||
return deriv * scale; |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: b6ff088bebec9f941ae19a0086e4f097 |
|||
timeCreated: 1487475828 |
|||
licenseType: Pro |
|||
ShaderImporter: |
|||
defaultTextures: [] |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue