Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenlib/intern/noise.cc
| Show First 20 Lines • Show All 1,542 Lines • ▼ Show 20 Lines | void voronoi_smooth_f1(const float w, | ||||
| const float smoothness, | const float smoothness, | ||||
| const float randomness, | const float randomness, | ||||
| float *r_distance, | float *r_distance, | ||||
| float3 *r_color, | float3 *r_color, | ||||
| float *r_w) | float *r_w) | ||||
| { | { | ||||
| const float cellPosition = floorf(w); | const float cellPosition = floorf(w); | ||||
| const float localPosition = w - cellPosition; | const float localPosition = w - cellPosition; | ||||
| const float smoothFactor = max_ff(smoothness, FLT_MIN); | |||||
| float smoothDistance = 8.0f; | float smoothDistance = 8.0f; | ||||
| float smoothPosition = 0.0f; | float smoothPosition = 0.0f; | ||||
| float3 smoothColor = float3(0.0f, 0.0f, 0.0f); | float3 smoothColor = float3(0.0f, 0.0f, 0.0f); | ||||
| for (int i = -2; i <= 2; i++) { | for (int i = -2; i <= 2; i++) { | ||||
| const float cellOffset = i; | const float cellOffset = i; | ||||
| const float pointPosition = cellOffset + | const float pointPosition = cellOffset + | ||||
| hash_float_to_float(cellPosition + cellOffset) * randomness; | hash_float_to_float(cellPosition + cellOffset) * randomness; | ||||
| const float distanceToPoint = voronoi_distance(pointPosition, localPosition); | const float distanceToPoint = voronoi_distance(pointPosition, localPosition); | ||||
| const float h = smoothstep( | const float h = smoothstep( | ||||
| 0.0f, 1.0f, 0.5f + 0.5f * (smoothDistance - distanceToPoint) / smoothness); | 0.0f, 1.0f, 0.5f + 0.5f * (smoothDistance - distanceToPoint) / smoothFactor); | ||||
JacquesLucke: Dividing by a "factor" is always a bit weird..
Maybe use `smoothness_clamped` instead.
Might… | |||||
| float correctionFactor = smoothness * h * (1.0f - h); | float correctionFactor = smoothFactor * h * (1.0f - h); | ||||
JacquesLuckeUnsubmitted Done Inline ActionsDoes this really have to be changed as well? JacquesLucke: Does this really have to be changed as well? | |||||
| smoothDistance = mix(smoothDistance, distanceToPoint, h) - correctionFactor; | smoothDistance = mix(smoothDistance, distanceToPoint, h) - correctionFactor; | ||||
| if (r_color != nullptr || r_w != nullptr) { | if (r_color != nullptr || r_w != nullptr) { | ||||
| correctionFactor /= 1.0f + 3.0f * smoothness; | correctionFactor /= 1.0f + 3.0f * smoothFactor; | ||||
| if (r_color != nullptr) { | if (r_color != nullptr) { | ||||
| const float3 cellColor = hash_float_to_float3(cellPosition + cellOffset); | const float3 cellColor = hash_float_to_float3(cellPosition + cellOffset); | ||||
| smoothColor = float3::interpolate(smoothColor, cellColor, h) - correctionFactor; | smoothColor = float3::interpolate(smoothColor, cellColor, h) - correctionFactor; | ||||
| } | } | ||||
| if (r_w != nullptr) { | if (r_w != nullptr) { | ||||
| smoothPosition = mix(smoothPosition, pointPosition, h) - correctionFactor; | smoothPosition = mix(smoothPosition, pointPosition, h) - correctionFactor; | ||||
| } | } | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 173 Lines • ▼ Show 20 Lines | void voronoi_smooth_f1(const float2 coord, | ||||
| const float randomness, | const float randomness, | ||||
| const int metric, | const int metric, | ||||
| float *r_distance, | float *r_distance, | ||||
| float3 *r_color, | float3 *r_color, | ||||
| float2 *r_position) | float2 *r_position) | ||||
| { | { | ||||
| const float2 cellPosition = float2::floor(coord); | const float2 cellPosition = float2::floor(coord); | ||||
| const float2 localPosition = coord - cellPosition; | const float2 localPosition = coord - cellPosition; | ||||
| const float smoothFactor = max_ff(smoothness, FLT_MIN); | |||||
| float smoothDistance = 8.0f; | float smoothDistance = 8.0f; | ||||
| float3 smoothColor = float3(0.0f, 0.0f, 0.0f); | float3 smoothColor = float3(0.0f, 0.0f, 0.0f); | ||||
| float2 smoothPosition = float2(0.0f, 0.0f); | float2 smoothPosition = float2(0.0f, 0.0f); | ||||
| for (int j = -2; j <= 2; j++) { | for (int j = -2; j <= 2; j++) { | ||||
| for (int i = -2; i <= 2; i++) { | for (int i = -2; i <= 2; i++) { | ||||
| const float2 cellOffset = float2(i, j); | const float2 cellOffset = float2(i, j); | ||||
| const float2 pointPosition = cellOffset + | const float2 pointPosition = cellOffset + | ||||
| hash_float_to_float2(cellPosition + cellOffset) * randomness; | hash_float_to_float2(cellPosition + cellOffset) * randomness; | ||||
| const float distanceToPoint = voronoi_distance( | const float distanceToPoint = voronoi_distance( | ||||
| pointPosition, localPosition, metric, exponent); | pointPosition, localPosition, metric, exponent); | ||||
| const float h = smoothstep( | const float h = smoothstep( | ||||
| 0.0f, 1.0f, 0.5f + 0.5f * (smoothDistance - distanceToPoint) / smoothness); | 0.0f, 1.0f, 0.5f + 0.5f * (smoothDistance - distanceToPoint) / smoothFactor); | ||||
| float correctionFactor = smoothness * h * (1.0f - h); | float correctionFactor = smoothFactor * h * (1.0f - h); | ||||
| smoothDistance = mix(smoothDistance, distanceToPoint, h) - correctionFactor; | smoothDistance = mix(smoothDistance, distanceToPoint, h) - correctionFactor; | ||||
| if (r_color != nullptr || r_position != nullptr) { | if (r_color != nullptr || r_position != nullptr) { | ||||
| correctionFactor /= 1.0f + 3.0f * smoothness; | correctionFactor /= 1.0f + 3.0f * smoothFactor; | ||||
| if (r_color != nullptr) { | if (r_color != nullptr) { | ||||
| const float3 cellColor = hash_float_to_float3(cellPosition + cellOffset); | const float3 cellColor = hash_float_to_float3(cellPosition + cellOffset); | ||||
| smoothColor = float3::interpolate(smoothColor, cellColor, h) - correctionFactor; | smoothColor = float3::interpolate(smoothColor, cellColor, h) - correctionFactor; | ||||
| } | } | ||||
| if (r_position != nullptr) { | if (r_position != nullptr) { | ||||
| smoothPosition = float2::interpolate(smoothPosition, pointPosition, h) - | smoothPosition = float2::interpolate(smoothPosition, pointPosition, h) - | ||||
| correctionFactor; | correctionFactor; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 214 Lines • ▼ Show 20 Lines | void voronoi_smooth_f1(const float3 coord, | ||||
| const float randomness, | const float randomness, | ||||
| const int metric, | const int metric, | ||||
| float *r_distance, | float *r_distance, | ||||
| float3 *r_color, | float3 *r_color, | ||||
| float3 *r_position) | float3 *r_position) | ||||
| { | { | ||||
| const float3 cellPosition = float3::floor(coord); | const float3 cellPosition = float3::floor(coord); | ||||
| const float3 localPosition = coord - cellPosition; | const float3 localPosition = coord - cellPosition; | ||||
| const float smoothFactor = max_ff(smoothness, FLT_MIN); | |||||
| float smoothDistance = 8.0f; | float smoothDistance = 8.0f; | ||||
| float3 smoothColor = float3(0.0f, 0.0f, 0.0f); | float3 smoothColor = float3(0.0f, 0.0f, 0.0f); | ||||
| float3 smoothPosition = float3(0.0f, 0.0f, 0.0f); | float3 smoothPosition = float3(0.0f, 0.0f, 0.0f); | ||||
| for (int k = -2; k <= 2; k++) { | for (int k = -2; k <= 2; k++) { | ||||
| for (int j = -2; j <= 2; j++) { | for (int j = -2; j <= 2; j++) { | ||||
| for (int i = -2; i <= 2; i++) { | for (int i = -2; i <= 2; i++) { | ||||
| const float3 cellOffset = float3(i, j, k); | const float3 cellOffset = float3(i, j, k); | ||||
| const float3 pointPosition = cellOffset + | const float3 pointPosition = cellOffset + | ||||
| hash_float_to_float3(cellPosition + cellOffset) * randomness; | hash_float_to_float3(cellPosition + cellOffset) * randomness; | ||||
| const float distanceToPoint = voronoi_distance( | const float distanceToPoint = voronoi_distance( | ||||
| pointPosition, localPosition, metric, exponent); | pointPosition, localPosition, metric, exponent); | ||||
| const float h = smoothstep( | const float h = smoothstep( | ||||
| 0.0f, 1.0f, 0.5f + 0.5f * (smoothDistance - distanceToPoint) / smoothness); | 0.0f, 1.0f, 0.5f + 0.5f * (smoothDistance - distanceToPoint) / smoothFactor); | ||||
| float correctionFactor = smoothness * h * (1.0f - h); | float correctionFactor = smoothFactor * h * (1.0f - h); | ||||
| smoothDistance = mix(smoothDistance, distanceToPoint, h) - correctionFactor; | smoothDistance = mix(smoothDistance, distanceToPoint, h) - correctionFactor; | ||||
| if (r_color != nullptr || r_position != nullptr) { | if (r_color != nullptr || r_position != nullptr) { | ||||
| correctionFactor /= 1.0f + 3.0f * smoothness; | correctionFactor /= 1.0f + 3.0f * smoothFactor; | ||||
| if (r_color != nullptr) { | if (r_color != nullptr) { | ||||
| const float3 cellColor = hash_float_to_float3(cellPosition + cellOffset); | const float3 cellColor = hash_float_to_float3(cellPosition + cellOffset); | ||||
| smoothColor = float3::interpolate(smoothColor, cellColor, h) - correctionFactor; | smoothColor = float3::interpolate(smoothColor, cellColor, h) - correctionFactor; | ||||
| } | } | ||||
| if (r_position != nullptr) { | if (r_position != nullptr) { | ||||
| smoothPosition = float3::interpolate(smoothPosition, pointPosition, h) - | smoothPosition = float3::interpolate(smoothPosition, pointPosition, h) - | ||||
| correctionFactor; | correctionFactor; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 229 Lines • ▼ Show 20 Lines | void voronoi_smooth_f1(const float4 coord, | ||||
| const float randomness, | const float randomness, | ||||
| const int metric, | const int metric, | ||||
| float *r_distance, | float *r_distance, | ||||
| float3 *r_color, | float3 *r_color, | ||||
| float4 *r_position) | float4 *r_position) | ||||
| { | { | ||||
| const float4 cellPosition = float4::floor(coord); | const float4 cellPosition = float4::floor(coord); | ||||
| const float4 localPosition = coord - cellPosition; | const float4 localPosition = coord - cellPosition; | ||||
| const float smoothFactor = max_ff(smoothness, FLT_MIN); | |||||
| float smoothDistance = 8.0f; | float smoothDistance = 8.0f; | ||||
| float3 smoothColor = float3(0.0f, 0.0f, 0.0f); | float3 smoothColor = float3(0.0f, 0.0f, 0.0f); | ||||
| float4 smoothPosition = float4(0.0f, 0.0f, 0.0f, 0.0f); | float4 smoothPosition = float4(0.0f, 0.0f, 0.0f, 0.0f); | ||||
| for (int u = -2; u <= 2; u++) { | for (int u = -2; u <= 2; u++) { | ||||
| for (int k = -2; k <= 2; k++) { | for (int k = -2; k <= 2; k++) { | ||||
| for (int j = -2; j <= 2; j++) { | for (int j = -2; j <= 2; j++) { | ||||
| for (int i = -2; i <= 2; i++) { | for (int i = -2; i <= 2; i++) { | ||||
| const float4 cellOffset = float4(i, j, k, u); | const float4 cellOffset = float4(i, j, k, u); | ||||
| const float4 pointPosition = cellOffset + | const float4 pointPosition = cellOffset + | ||||
| hash_float_to_float4(cellPosition + cellOffset) * | hash_float_to_float4(cellPosition + cellOffset) * | ||||
| randomness; | randomness; | ||||
| const float distanceToPoint = voronoi_distance( | const float distanceToPoint = voronoi_distance( | ||||
| pointPosition, localPosition, metric, exponent); | pointPosition, localPosition, metric, exponent); | ||||
| const float h = smoothstep( | const float h = smoothstep( | ||||
| 0.0f, 1.0f, 0.5f + 0.5f * (smoothDistance - distanceToPoint) / smoothness); | 0.0f, 1.0f, 0.5f + 0.5f * (smoothDistance - distanceToPoint) / smoothFactor); | ||||
| float correctionFactor = smoothness * h * (1.0f - h); | float correctionFactor = smoothFactor * h * (1.0f - h); | ||||
| smoothDistance = mix(smoothDistance, distanceToPoint, h) - correctionFactor; | smoothDistance = mix(smoothDistance, distanceToPoint, h) - correctionFactor; | ||||
| if (r_color != nullptr || r_position != nullptr) { | if (r_color != nullptr || r_position != nullptr) { | ||||
| correctionFactor /= 1.0f + 3.0f * smoothness; | correctionFactor /= 1.0f + 3.0f * smoothFactor; | ||||
| if (r_color != nullptr) { | if (r_color != nullptr) { | ||||
| const float3 cellColor = hash_float_to_float3(cellPosition + cellOffset); | const float3 cellColor = hash_float_to_float3(cellPosition + cellOffset); | ||||
| smoothColor = float3::interpolate(smoothColor, cellColor, h) - correctionFactor; | smoothColor = float3::interpolate(smoothColor, cellColor, h) - correctionFactor; | ||||
| } | } | ||||
| if (r_position != nullptr) { | if (r_position != nullptr) { | ||||
| smoothPosition = float4::interpolate(smoothPosition, pointPosition, h) - | smoothPosition = float4::interpolate(smoothPosition, pointPosition, h) - | ||||
| correctionFactor; | correctionFactor; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 173 Lines • Show Last 20 Lines | |||||
Dividing by a "factor" is always a bit weird..
Maybe use smoothness_clamped instead.
Might be interesting if there is a measurable performance improvement by replacing division with multiplication here, but that should be done separately.