forked from BilalY/Rasagar
2.3 KiB
2.3 KiB
Normal From Height Node
Description
Creates a normal map from a height value defined by input Input with a strength defined by input Strength.
Ports
Name | Direction | Type | Description |
---|---|---|---|
In | Input | Float | Input height value |
Strength | Input | Float | The strength of the output normal. Considered in real-world units, recommended range is 0 - 0.1 . |
Out | Output | Vector 3 | Output value |
Controls
Name | Type | Options | Description |
---|---|---|---|
Output Space | Dropdown | Tangent, World | Sets the coordinate space of the output normal. |
Generated Code Example
The following example code represents one possible outcome of this node per Output Space mode.
Tangent
void Unity_NormalFromHeight_Tangent_float(float In, float Strength, float3 Position, float3x3 TangentMatrix, out float3 Out)
{
float3 worldDerivativeX = ddx(Position);
float3 worldDerivativeY = ddy(Position);
float3 crossX = cross(TangentMatrix[2].xyz, worldDerivativeX);
float3 crossY = cross(worldDerivativeY, TangentMatrix[2].xyz);
float d = dot(worldDerivativeX, crossY);
float sgn = d < 0.0 ? (-1.0f) : 1.0f;
float surface = sgn / max(0.000000000000001192093f, abs(d));
float dHdx = ddx(In);
float dHdy = ddy(In);
float3 surfGrad = surface * (dHdx*crossY + dHdy*crossX);
Out = normalize(TangentMatrix[2].xyz - (Strength * surfGrad));
Out = TransformWorldToTangent(Out, TangentMatrix);
}
World
void Unity_NormalFromHeight_World_float(float In, float Strength, float3 Position, float3x3 TangentMatrix, out float3 Out)
{
float3 worldDerivativeX = ddx(Position);
float3 worldDerivativeY = ddy(Position);
float3 crossX = cross(TangentMatrix[2].xyz, worldDerivativeX);
float3 crossY = cross(worldDerivativeY, TangentMatrix[2].xyz);
float d = dot(worldDerivativeX, crossY);
float sgn = d < 0.0 ? (-1.0f) : 1.0f;
float surface = sgn / max(0.000000000000001192093f, abs(d));
float dHdx = ddx(In);
float dHdy = ddy(In);
float3 surfGrad = surface * (dHdx*crossY + dHdy*crossX);
Out = normalize(TangentMatrix[2].xyz - (Strength * surfGrad));
}