{"record":{"id":"f7193b8c86190cc9","repo":"BabylonJS/Babylon.js","slug":"constant-value-must-be-between-0-0-and-1-0","errorCode":null,"errorMessage":"Constant value must be between 0.0 and 1.0","messagePattern":"Constant value must be between 0\\.0 and 1\\.0","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/dev/core/src/Materials/Textures/textureMerger.ts","lineNumber":124,"sourceCode":"        const channel = channels[channelIndex];\n        if (channel) {\n            if (IsTextureInput(channel)) {\n                // Validate source channel\n                if (channel.sourceChannel < 0 || channel.sourceChannel > 3) {\n                    throw new Error(\"Source channel must be between 0 and 3 (R, G, B, A)\");\n                }\n\n                // Find or add texture to inputs\n                let textureIndex = textureInputs.indexOf(channel.texture);\n                if (textureIndex === -1) {\n                    textureIndex = textureInputs.length;\n                    textureInputs.push(channel.texture);\n                }\n                textureInputMap[channelIndex] = textureIndex;\n            } else if (IsConstantInput(channel)) {\n                // Validate constant value\n                if (channel.value < 0 || channel.value > 1) {\n                    throw new Error(\"Constant value must be between 0.0 and 1.0\");\n                }\n                textureInputMap[channelIndex] = -1;\n            } else {\n                throw new Error(\"Invalid channel input configuration\");\n            }\n        } else {\n            textureInputMap[channelIndex] = -1;\n        }\n    }\n\n    // Determine output size\n    let outputSize = config.outputSize;\n    if (!outputSize && textureInputs.length > 0) {\n        // Use the largest texture size\n        let maxSize = 0;\n        for (const texture of textureInputs) {\n            const size = texture.getSize();\n            const currentSize = Math.max(size.width, size.height);","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/BabylonJS/Babylon.js/blob/0592b347b8a4ee0236089ea86a749cacfdb266d8/packages/dev/core/src/Materials/Textures/textureMerger.ts#L106-L142","documentation":"In MergeTexturesAsync, a constant channel input fills an output channel with a fixed scalar that is written into an 8-bit texture, so it must be between 0.0 and 1.0. This error is thrown when channel.value falls outside that range.","triggerScenarios":"Passing a constant input like { value: 255 } (authoring in 0-255) or a negative value to MergeTexturesAsync / CreateMergedAnisotropyTexture / metalRough conversion.","commonSituations":"Porting code that used byte values (0-255), computing a factor with NaN or a bad division producing out-of-range numbers, glTF material factor units confusion.","solutions":["Convert byte-scale constants: divide by 255 (e.g. 255 -> 1.0).","Clamp the value: Math.min(1, Math.max(0, value)) before passing it.","Check upstream computations (factors, sliders) for units that are not normalized."],"exampleFix":"// before\n{ value: 255 }\n// after\n{ value: 1.0 } // or 255 / 255","handlingStrategy":"validation","validationCode":"function clamp01(v: number): number {\n  return Math.min(1, Math.max(0, v));\n}\nconst safeInput = { value: clamp01(rawFactor) };","typeGuard":"function isUnitValue(v: unknown): v is number {\n  return typeof v === \"number\" && Number.isFinite(v) && v >= 0 && v <= 1;\n}","tryCatchPattern":"try {\n  const tex = await MergeTexturesAsync(channels, w, h, scene);\n} catch (e) {\n  if (String(e.message).includes(\"Constant value must be between 0.0 and 1.0\")) {\n    console.error(\"Constant is out of 0-1 range; check 0-255 scale confusion.\");\n  }\n}","preventionTips":["Always normalize constants to 0-1 (divide byte values by 255).","Clamp computed factors before building channel inputs.","Guard against NaN/Infinity reaching the value field."],"tags":["texture-merger","validation","range-check"],"backgroundTag":"value-out-of-range","analyzedSha":"0592b347b8a4ee0236089ea86a749cacfdb266d8","analyzedAt":"2026-08-30T15:11:20.442Z","schemaVersion":2},"datasetVersion":"2026-08-30T18:17:15.746Z"}