From c7f8b2b2ed3d2064ce03cf93748b651b76a81e01 Mon Sep 17 00:00:00 2001 From: "Alex \"mcmonkey\" Goodwin" Date: Mon, 24 Jun 2024 15:57:32 -0700 Subject: [PATCH] rework wildcard random handling to have trackable seeds - for https://github.com/mcmonkeyprojects/SwarmUI/discussions/6 --- src/Text2Image/T2IParamInput.cs | 47 +++++++++++++++++++++----------- src/wwwroot/js/genpage/params.js | 4 ++- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/Text2Image/T2IParamInput.cs b/src/Text2Image/T2IParamInput.cs index 912a7b26..576364e9 100644 --- a/src/Text2Image/T2IParamInput.cs +++ b/src/Text2Image/T2IParamInput.cs @@ -44,8 +44,6 @@ public class T2IParamInput public class PromptTagContext { - public Random Random; - public T2IParamInput Input; public string Param; @@ -158,7 +156,7 @@ public class T2IParamInput List vals = [.. rawVals]; for (int i = 0; i < count; i++) { - int index = context.Random.Next(vals.Count); + int index = context.Input.GetWildcardRandom().Next(vals.Count); string choice = vals[index]; if (TryInterpretNumberRange(choice, out string number)) { @@ -243,7 +241,7 @@ public class T2IParamInput List vals = [.. wildcard.Options]; for (int i = 0; i < count; i++) { - int index = context.Random.Next(vals.Count); + int index = context.Input.GetWildcardRandom().Next(vals.Count); string choice = vals[index]; result += context.Parse(choice).Trim() + partSeparator; if (vals.Count == 1) @@ -598,6 +596,34 @@ public class T2IParamInput proc(T2IParamTypes.NegativePrompt); } + /// Random instance for . + public Random WildcardRandom = null; + + /// Gets the random instance for , initializing it if needed. + public Random GetWildcardRandom() + { + if (WildcardRandom is not null) + { + return WildcardRandom; + } + long backupSeed = Get(T2IParamTypes.Seed) + Get(T2IParamTypes.VariationSeed, 0) + 17; + if (!TryGet(T2IParamTypes.WildcardSeed, out long wildcardSeed)) + { + wildcardSeed = backupSeed; + } + if (wildcardSeed > int.MaxValue) + { + wildcardSeed %= int.MaxValue; + } + if (wildcardSeed == -1) + { + wildcardSeed = Random.Shared.Next(int.MaxValue); + } + Set(T2IParamTypes.WildcardSeed, wildcardSeed); + WildcardRandom = new((int)wildcardSeed); + return WildcardRandom; + } + /// Special utility to process prompt inputs before the request is executed (to parse wildcards, embeddings, etc). public string ProcessPromptLike(T2IRegisteredParam param) { @@ -607,18 +633,7 @@ public class T2IParamInput return ""; } string fixedVal = val.Replace('\0', '\a').Replace("\a", ""); - long backupSeed = Get(T2IParamTypes.Seed) + Get(T2IParamTypes.VariationSeed, 0) + param.Type.Name.Length; - long wildcardSeed = Get(T2IParamTypes.WildcardSeed, backupSeed); - if (wildcardSeed > int.MaxValue) - { - wildcardSeed %= int.MaxValue; - } - if (wildcardSeed == -1) - { - wildcardSeed = Random.Shared.Next(int.MaxValue); - } - Random rand = new((int)wildcardSeed); - PromptTagContext context = new() { Input = this, Random = rand, Param = param.Type.ID }; + PromptTagContext context = new() { Input = this, Param = param.Type.ID }; fixedVal = ProcessPromptLike(fixedVal, context); if (fixedVal != val) { diff --git a/src/wwwroot/js/genpage/params.js b/src/wwwroot/js/genpage/params.js index 2930f214..7bb17261 100644 --- a/src/wwwroot/js/genpage/params.js +++ b/src/wwwroot/js/genpage/params.js @@ -775,7 +775,9 @@ function reuseLastParamVal(paramId) { } let params = JSON.parse(currentMetadataVal).sui_image_params; if (pid in params) { - getRequiredElementById(paramId).value = params[pid]; + let elem = getRequiredElementById(paramId); + elem.value = params[pid]; + triggerChangeFor(elem); } }