{"record":{"id":"6bdac102475d1b69","repo":"BabylonJS/Babylon.js","slug":"sample2drgbatoref-widthpx-and-heightpx-must-be-po","errorCode":null,"errorMessage":"Sample2DRgbaToRef: widthPx and heightPx must be positive.","messagePattern":"Sample2DRgbaToRef: widthPx and heightPx must be positive\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/dev/addons/src/atmosphere/sampling.ts","lineNumber":46,"sourceCode":" * @param v - The v coordinate to sample.\r\n * @param widthPx - The width of the texture in texels.\r\n * @param heightPx - The height of the texture in texels.\r\n * @param data - The texture data to sample.\r\n * @param result - The color to store the sample.\r\n * @param normalizeFunc - The function to normalize the texel values. Default is to divide by 255. Pass null for no normalization.\r\n * @returns The result color.\r\n */\r\nexport function Sample2DRgbaToRef<T extends IColor4Like>(\r\n    u: number,\r\n    v: number,\r\n    widthPx: number,\r\n    heightPx: number,\r\n    data: Uint8Array | Uint16Array | Float32Array,\r\n    result: T,\r\n    normalizeFunc: ((value: number) => number) | null = DefaultNormalize\r\n): T {\r\n    if (widthPx <= 0 || heightPx <= 0) {\r\n        throw new Error(\"Sample2DRgbaToRef: widthPx and heightPx must be positive.\");\r\n    }\r\n\r\n    const expectedLength = widthPx * heightPx * 4;\r\n    if (data.length < expectedLength) {\r\n        throw new Error(`Sample2DRgbaToRef: data length (${data.length}) is less than required (${expectedLength}).`);\r\n    }\r\n\r\n    // Default to clamping behavior, but could support others.\r\n    u = Clamp(u);\r\n    v = Clamp(v);\r\n\r\n    // Compute 4 nearest neighbor texels.\r\n    const fractionalTexelX = Math.max(u * widthPx - 0.5, 0);\r\n    const fractionalTexelY = Math.max(v * heightPx - 0.5, 0);\r\n    const xLeft = Math.floor(fractionalTexelX);\r\n    const xRight = Math.min(xLeft + 1, widthPx - 1);\r\n    const yBottom = Math.floor(fractionalTexelY);\r\n    const yTop = Math.min(yBottom + 1, heightPx - 1);\r","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/BabylonJS/Babylon.js/blob/0592b347b8a4ee0236089ea86a749cacfdb266d8/packages/dev/addons/src/atmosphere/sampling.ts#L28-L64","documentation":"Sample2DRgbaToRef samples an RGBA LUT stored in a flat typed array sized widthPx*heightPx*4. Non-positive dimensions make sampling math meaningless, so the function validates them up front and throws. Callers such as SampleLutToRef and getDiffuseSkyIrradianceToRef pass LUT dimensions that must come from valid texture descriptors.","triggerScenarios":"Calling Sample2DRgbaToRef (directly or via SampleLutToRef / getDiffuseSkyIrradianceToRef) with widthPx <= 0 or heightPx <= 0, e.g. a transmittance/multiscattering LUT created with zero size or dimension read before initialization.","commonSituations":"LUT texture created with size 0 because options had invalid dimensions or a compute pass failed; reading width/height from an uninitialized object; integer truncation producing 0 for very small configured sizes.","solutions":["Validate LUT dimensions before sampling: ensure widthPx > 0 && heightPx > 0 and fall back to configured defaults (e.g. transmittance 256x64).","Fix where the dimensions come from — check the texture creation options passed to the Atmosphere physical properties / LUT generation.","Guard call sites: skip sampling until the LUT has been generated with valid dimensions."],"exampleFix":"// before\nsample.Sample2DRgbaToRef(u, v, lut.width, lut.height, lutData, result); // width may be 0\n// after\nif (lut.width > 0 && lut.height > 0) {\n  sample.Sample2DRgbaToRef(u, v, lut.width, lut.height, lutData, result);\n}","handlingStrategy":"validation","validationCode":"function assertValidLutDims(w: number, h: number): void {\n  if (!(w > 0) || !(h > 0)) throw new Error(`bad LUT dims ${w}x${h}`);\n}\nassertValidLutDims(lut.width, lut.height);","typeGuard":"const arePositiveDims = (d: { width: number; height: number }): d is { width: number; height: number } =>\n  d.width > 0 && d.height > 0;","tryCatchPattern":"try {\n  Sample2DRgbaToRef(u, v, w, h, data, out);\n} catch (e) {\n  if (String(e).includes(\"must be positive\")) {\n    // regenerate LUT with default dimensions before retrying\n  } else throw e;\n}","preventionTips":["Never construct LUTs with computed sizes without validating > 0.","Delay sampling until the LUT generation pass has completed.","Keep configured LUT resolutions as named constants, not derived values."],"tags":["sampling","validation","atmosphere","lut","argument-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"0592b347b8a4ee0236089ea86a749cacfdb266d8","analyzedAt":"2026-08-30T15:11:20.442Z","schemaVersion":2},"datasetVersion":"2026-08-30T18:17:15.746Z"}