@@ -278,8 +278,37 @@ void distributePointsInTriangle(
278278
279279 int cancelCounter = 0 ;
280280
281+ bool hasDensityVar = false ;
282+ float finalDensity = textureDensity;
283+ float cornerDensities[3 ];
284+ if ( densityView )
285+ {
286+ hasDensityVar = true ;
287+ Imath::V3f bary;
288+ triangleCornerPrimVarValues (
289+ densityInterpolation, densityView, vertexIds, faceIdx,
290+ cornerDensities[0 ], cornerDensities[1 ], cornerDensities[2 ]
291+ );
292+ float maxDensity = std::max ( std::max ( cornerDensities[0 ], cornerDensities[1 ] ), cornerDensities[2 ] );
293+
294+ // This matches previous behaviour where a density primvar value over 1.0 does nothing, because it
295+ // cannot increase the number of points generated. Maybe this is valuable as a safety mechanism?
296+ // We could just delete this line, and then densities greater than 1 would work how you would expect.
297+ maxDensity = std::min ( 1 .0f , std::max ( 0 .0f , maxDensity ) );
298+
299+ // Apply the max density from the primvar to the density passed in to PointDistribution
300+ finalDensity *= maxDensity;
301+
302+ // Compensate the corner densities to account for the factor that is already handled when passed in
303+ // to PointDistribution
304+ float invMaxDensity = 1 .0f / maxDensity;
305+ cornerDensities[0 ] *= invMaxDensity;
306+ cornerDensities[1 ] *= invMaxDensity;
307+ cornerDensities[2 ] *= invMaxDensity;
308+ }
309+
281310 PointDistribution::defaultInstance ()(
282- uvBounds, textureDensity ,
311+ uvBounds, finalDensity ,
283312 [&]( const Imath::V2f pos, float densityThreshold )
284313 {
285314 cancelCounter++;
@@ -291,9 +320,13 @@ void distributePointsInTriangle(
291320 Imath::V3f bary;
292321 if ( triangleContainsPoint ( uv0, uv1, uv2, pos, bary ) )
293322 {
294- if ( densityView )
323+ if ( hasDensityVar )
295324 {
296- float d = triangleInterpolatedPrimVarValue ( densityInterpolation, densityView, vertexIds, faceIdx, bary );
325+ float d =
326+ bary[0 ] * cornerDensities[0 ] +
327+ bary[1 ] * cornerDensities[1 ] +
328+ bary[2 ] * cornerDensities[2 ];
329+
297330 if ( d <= densityThreshold )
298331 {
299332 return ;
0 commit comments