{"record":{"id":"13d9e4c418411c5f","repo":"BabylonJS/Babylon.js","slug":"kernel-size-must-be-odd","errorCode":null,"errorMessage":"Kernel size must be odd.","messagePattern":"Kernel size must be odd\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/dev/core/src/Misc/areaLightsTextureTools.ts","lineNumber":170,"sourceCode":"                generateDepthBuffer: false,\r\n                generateMipMaps: true,\r\n                generateStencilBuffer: false,\r\n                samplingMode: Constants.TEXTURE_TRILINEAR_SAMPLINGMODE,\r\n                type: Constants.TEXTURETYPE_UNSIGNED_BYTE,\r\n                format: Constants.TEXTUREFORMAT_RGBA,\r\n            }\r\n        );\r\n\r\n        this._source = source;\r\n        const engineDepthMask = this._engine.getDepthWrite(); // for some reasons, depthWrite is not restored by EffectRenderer.restoreStates\r\n        this._renderer.render(this._effectWrapper, renderTarget);\r\n        this._engine.setDepthWrite(engineDepthMask);\r\n        return new BaseTexture(this._engine, renderTarget.texture);\r\n    }\r\n\r\n    private _generateGaussianKernel(size: number, sigma: number): KernelData {\r\n        if (size % 2 === 0) {\r\n            throw new Error(\"Kernel size must be odd.\");\r\n        }\r\n\r\n        const kernel = new Float32Array(size);\r\n        let sum = 0.0;\r\n        const halfSize = Math.floor(size / 2);\r\n\r\n        for (let i = -halfSize; i <= halfSize; ++i) {\r\n            const value = Math.exp(-(i * i) / (2.0 * sigma * sigma));\r\n            const index = i + halfSize;\r\n            kernel[index] = value;\r\n            sum += value;\r\n        }\r\n\r\n        for (let i = 0; i < kernel.length; i++) {\r\n            kernel[i] /= sum;\r\n        }\r\n\r\n        return { kernel, kernelSize: size, kernelHalfSize: halfSize };\r","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/BabylonJS/Babylon.js/blob/0592b347b8a4ee0236089ea86a749cacfdb266d8/packages/dev/core/src/Misc/areaLightsTextureTools.ts#L152-L188","documentation":"The area lights texture tool builds a Gaussian blur kernel via _generateGaussianKernel, which requires an odd size so the kernel has a unique center element (halfSize = floor(size/2)); an even size would blur asymmetrically. It deliberately rejects even sizes at construction time with this error.","triggerScenarios":"Constructing the area lights texture tool (e.g. via AreaLightsTextureToolsFactory or helpers like AreaLightsLCDTexture) with options.kernel set to an even number such as 8 or 16.","commonSituations":"Choosing a power-of-two size 'for performance' and forgetting blurs need odd sizes; kernel size read from config or a slider that permits even values; reusing a size constant from elsewhere (e.g. texture dimensions) as the blur kernel size.","solutions":["Pass an odd kernel size in the options, e.g. { kernel: 9 } instead of 8.","If the size comes from input/config, coerce it before constructing: size % 2 === 0 ? size + 1 : size.","Prefer typical odd sizes like 3, 5, 7, 9 unless a larger odd kernel is specifically required.","Check the call site (constructor / AreaLightsLCDTexture options) to find where the even value originates."],"exampleFix":"// before\nconst tool = createTool({ kernel: 8 }); // throws\n// after\nconst kernel = 8 % 2 === 0 ? 9 : 8; // ensure odd\nconst tool = createTool({ kernel });","handlingStrategy":"validation","validationCode":"function assertOddKernel(size) {\n  if (size % 2 === 0) {\n    throw new Error('Kernel size must be odd, got ' + size);\n  }\n  return size;\n}\nconst tool = createAreaLightsTool({ kernel: assertOddKernel(options.kernel) });","typeGuard":"function isOddKernelSize(size) {\n  return Number.isInteger(size) && size > 0 && size % 2 === 1;\n}","tryCatchPattern":"try {\n  const tool = new AreaLightsTextureTool(engine, options);\n} catch (e) {\n  if (String(e.message).includes('Kernel size must be odd')) {\n    options.kernel = options.kernel % 2 === 0 ? options.kernel + 1 : options.kernel;\n    // retry with corrected options\n  } else { throw e; }\n}","preventionTips":["Always use odd sizes (3, 5, 7, 9...) for Gaussian blur kernels.","Coerce user/config-supplied sizes with size % 2 === 0 ? size + 1 : size before use.","Don't reuse power-of-two texture dimensions as blur kernel sizes.","Clamp UI sliders controlling kernel size to odd values only.","Add a startup assert when the kernel size comes from configuration."],"tags":["post-process","gaussian-blur","validation","area-lights"],"backgroundTag":"kernel-size-must-be-odd","analyzedSha":"0592b347b8a4ee0236089ea86a749cacfdb266d8","analyzedAt":"2026-08-30T15:11:20.442Z","schemaVersion":2},"datasetVersion":"2026-08-30T18:17:15.746Z"}