make random dice work
This commit is contained in:
parent
d39bf73fac
commit
047474ab84
@ -9,7 +9,8 @@ export default async function inpaint(
|
|||||||
settings: Settings,
|
settings: Settings,
|
||||||
croperRect: Rect,
|
croperRect: Rect,
|
||||||
prompt?: string,
|
prompt?: string,
|
||||||
sizeLimit?: string
|
sizeLimit?: string,
|
||||||
|
seed?: number
|
||||||
) {
|
) {
|
||||||
// 1080, 2000, Original
|
// 1080, 2000, Original
|
||||||
const fd = new FormData()
|
const fd = new FormData()
|
||||||
@ -42,7 +43,8 @@ export default async function inpaint(
|
|||||||
fd.append('sdSteps', settings.sdSteps.toString())
|
fd.append('sdSteps', settings.sdSteps.toString())
|
||||||
fd.append('sdGuidanceScale', settings.sdGuidanceScale.toString())
|
fd.append('sdGuidanceScale', settings.sdGuidanceScale.toString())
|
||||||
fd.append('sdSampler', settings.sdSampler.toString())
|
fd.append('sdSampler', settings.sdSampler.toString())
|
||||||
fd.append('sdSeed', settings.sdSeedFixed ? settings.sdSeed.toString() : '-1')
|
// fd.append('sdSeed', settings.sdSeedFixed ? settings.sdSeed.toString() : '-1')
|
||||||
|
fd.append('sdSeed', seed ? seed.toString() : '-1')
|
||||||
|
|
||||||
if (sizeLimit === undefined) {
|
if (sizeLimit === undefined) {
|
||||||
fd.append('sizeLimit', '1080')
|
fd.append('sizeLimit', '1080')
|
||||||
@ -57,8 +59,8 @@ export default async function inpaint(
|
|||||||
})
|
})
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
const blob = await res.blob()
|
const blob = await res.blob()
|
||||||
const seed = res.headers.get('x-seed')
|
const newSeed = res.headers.get('x-seed')
|
||||||
return { blob: URL.createObjectURL(blob), seed }
|
return { blob: URL.createObjectURL(blob), seed: newSeed }
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
throw new Error('Something went wrong on server side.')
|
throw new Error('Something went wrong on server side.')
|
||||||
|
@ -41,7 +41,7 @@ import {
|
|||||||
} from '../../store/Atoms'
|
} from '../../store/Atoms'
|
||||||
import useHotKey from '../../hooks/useHotkey'
|
import useHotKey from '../../hooks/useHotkey'
|
||||||
import Croper from '../Croper/Croper'
|
import Croper from '../Croper/Croper'
|
||||||
import emitter, { EVENT_PROMPT } from '../../event'
|
import emitter, { EVENT_PROMPT, EVENT_RERUN } from '../../event'
|
||||||
|
|
||||||
const TOOLBAR_SIZE = 200
|
const TOOLBAR_SIZE = 200
|
||||||
const BRUSH_COLOR = '#ffcc00bb'
|
const BRUSH_COLOR = '#ffcc00bb'
|
||||||
@ -102,6 +102,7 @@ export default function Editor(props: EditorProps) {
|
|||||||
return document.createElement('canvas')
|
return document.createElement('canvas')
|
||||||
})
|
})
|
||||||
const [lineGroups, setLineGroups] = useState<LineGroup[]>([])
|
const [lineGroups, setLineGroups] = useState<LineGroup[]>([])
|
||||||
|
const [lastLineGroup, setLastLineGroup] = useState<LineGroup>([])
|
||||||
const [curLineGroup, setCurLineGroup] = useState<LineGroup>([])
|
const [curLineGroup, setCurLineGroup] = useState<LineGroup>([])
|
||||||
const [{ x, y }, setCoords] = useState({ x: -1, y: -1 })
|
const [{ x, y }, setCoords] = useState({ x: -1, y: -1 })
|
||||||
const [showBrush, setShowBrush] = useState(false)
|
const [showBrush, setShowBrush] = useState(false)
|
||||||
@ -182,26 +183,53 @@ export default function Editor(props: EditorProps) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
const runInpainting = useCallback(
|
const runInpainting = useCallback(
|
||||||
async (prompt?: string) => {
|
async (prompt?: string, useLastLineGroup?: boolean) => {
|
||||||
|
// useLastLineGroup 的影响
|
||||||
|
// 1. 使用上一次的 mask
|
||||||
|
// 2. 结果替换当前 render
|
||||||
console.log('runInpainting')
|
console.log('runInpainting')
|
||||||
|
|
||||||
|
let maskLineGroup = []
|
||||||
|
if (useLastLineGroup === true) {
|
||||||
|
if (lastLineGroup.length === 0) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
maskLineGroup = lastLineGroup
|
||||||
|
} else {
|
||||||
if (!hadDrawSomething()) {
|
if (!hadDrawSomething()) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
console.log(prompt)
|
|
||||||
|
|
||||||
const newLineGroups = [...lineGroups, curLineGroup]
|
setLastLineGroup(curLineGroup)
|
||||||
|
maskLineGroup = curLineGroup
|
||||||
|
}
|
||||||
|
|
||||||
|
const newLineGroups = [...lineGroups, maskLineGroup]
|
||||||
|
|
||||||
setCurLineGroup([])
|
setCurLineGroup([])
|
||||||
setIsDraging(false)
|
setIsDraging(false)
|
||||||
setIsInpainting(true)
|
setIsInpainting(true)
|
||||||
if (settings.graduallyInpainting) {
|
if (settings.graduallyInpainting) {
|
||||||
drawLinesOnMask([curLineGroup])
|
drawLinesOnMask([maskLineGroup])
|
||||||
} else {
|
} else {
|
||||||
drawLinesOnMask(newLineGroups)
|
drawLinesOnMask(newLineGroups)
|
||||||
}
|
}
|
||||||
|
|
||||||
let targetFile = file
|
let targetFile = file
|
||||||
if (settings.graduallyInpainting === true && renders.length > 0) {
|
if (settings.graduallyInpainting === true) {
|
||||||
|
if (useLastLineGroup === true) {
|
||||||
|
// renders.length == 1 还是用原来的
|
||||||
|
if (renders.length > 1) {
|
||||||
|
const lastRender = renders[renders.length - 2]
|
||||||
|
targetFile = await srcToFile(
|
||||||
|
lastRender.currentSrc,
|
||||||
|
file.name,
|
||||||
|
file.type
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else if (renders.length > 0) {
|
||||||
console.info('gradually inpainting on last result')
|
console.info('gradually inpainting on last result')
|
||||||
|
|
||||||
const lastRender = renders[renders.length - 1]
|
const lastRender = renders[renders.length - 1]
|
||||||
targetFile = await srcToFile(
|
targetFile = await srcToFile(
|
||||||
lastRender.currentSrc,
|
lastRender.currentSrc,
|
||||||
@ -209,6 +237,12 @@ export default function Editor(props: EditorProps) {
|
|||||||
file.type
|
file.type
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let sdSeed = settings.sdSeedFixed ? settings.sdSeed : -1
|
||||||
|
if (useLastLineGroup === true) {
|
||||||
|
sdSeed = -1
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await inpaint(
|
const res = await inpaint(
|
||||||
@ -217,7 +251,8 @@ export default function Editor(props: EditorProps) {
|
|||||||
settings,
|
settings,
|
||||||
croperRect,
|
croperRect,
|
||||||
prompt,
|
prompt,
|
||||||
sizeLimit.toString()
|
sizeLimit.toString(),
|
||||||
|
sdSeed
|
||||||
)
|
)
|
||||||
if (!res) {
|
if (!res) {
|
||||||
throw new Error('empty response')
|
throw new Error('empty response')
|
||||||
@ -228,8 +263,16 @@ export default function Editor(props: EditorProps) {
|
|||||||
}
|
}
|
||||||
const newRender = new Image()
|
const newRender = new Image()
|
||||||
await loadImage(newRender, blob)
|
await loadImage(newRender, blob)
|
||||||
|
|
||||||
|
if (useLastLineGroup === true) {
|
||||||
|
const prevRenders = renders.slice(0, -1)
|
||||||
|
const newRenders = [...prevRenders, newRender]
|
||||||
|
setRenders(newRenders)
|
||||||
|
} else {
|
||||||
const newRenders = [...renders, newRender]
|
const newRenders = [...renders, newRender]
|
||||||
setRenders(newRenders)
|
setRenders(newRenders)
|
||||||
|
}
|
||||||
|
|
||||||
draw(newRender, [])
|
draw(newRender, [])
|
||||||
// Only append new LineGroup after inpainting success
|
// Only append new LineGroup after inpainting success
|
||||||
setLineGroups(newLineGroups)
|
setLineGroups(newLineGroups)
|
||||||
@ -280,6 +323,26 @@ export default function Editor(props: EditorProps) {
|
|||||||
}
|
}
|
||||||
}, [hadDrawSomething, runInpainting, prompt])
|
}, [hadDrawSomething, runInpainting, prompt])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
emitter.on(EVENT_RERUN, () => {
|
||||||
|
if (hadDrawSomething()) {
|
||||||
|
runInpainting(promptVal)
|
||||||
|
} else if (lastLineGroup.length !== 0) {
|
||||||
|
runInpainting(promptVal, true)
|
||||||
|
} else {
|
||||||
|
setToastState({
|
||||||
|
open: true,
|
||||||
|
desc: 'Please draw mask on picture',
|
||||||
|
state: 'error',
|
||||||
|
duration: 1500,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return () => {
|
||||||
|
emitter.off(EVENT_RERUN)
|
||||||
|
}
|
||||||
|
}, [lastLineGroup, hadDrawSomething, runInpainting, prompt])
|
||||||
|
|
||||||
const hadRunInpainting = () => {
|
const hadRunInpainting = () => {
|
||||||
return renders.length !== 0
|
return renders.length !== 0
|
||||||
}
|
}
|
||||||
@ -547,6 +610,7 @@ export default function Editor(props: EditorProps) {
|
|||||||
if (curLineGroup.length === 0) {
|
if (curLineGroup.length === 0) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
setLastLineGroup([])
|
||||||
|
|
||||||
const lastLine = curLineGroup.pop()!
|
const lastLine = curLineGroup.pop()!
|
||||||
const newRedoCurLines = [...redoCurLines, lastLine]
|
const newRedoCurLines = [...redoCurLines, lastLine]
|
||||||
@ -563,8 +627,8 @@ export default function Editor(props: EditorProps) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// save line Group
|
// save line Group
|
||||||
const lastLineGroup = lineGroups.pop()!
|
const latestLineGroup = lineGroups.pop()!
|
||||||
setRedoLineGroups([...redoLineGroups, lastLineGroup])
|
setRedoLineGroups([...redoLineGroups, latestLineGroup])
|
||||||
// If render is undo, clear strokes
|
// If render is undo, clear strokes
|
||||||
setRedoCurLines([])
|
setRedoCurLines([])
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@ import SettingBlock from '../Settings/SettingBlock'
|
|||||||
import Selector from '../shared/Selector'
|
import Selector from '../shared/Selector'
|
||||||
import { Switch, SwitchThumb } from '../shared/Switch'
|
import { Switch, SwitchThumb } from '../shared/Switch'
|
||||||
import Button from '../shared/Button'
|
import Button from '../shared/Button'
|
||||||
import emitter, { EVENT_PROMPT } from '../../event'
|
import emitter, { EVENT_RERUN } from '../../event'
|
||||||
|
|
||||||
const INPUT_WIDTH = 30
|
const INPUT_WIDTH = 30
|
||||||
|
|
||||||
@ -18,7 +18,7 @@ const SidePanel = () => {
|
|||||||
const [setting, setSettingState] = useRecoilState(settingState)
|
const [setting, setSettingState] = useRecoilState(settingState)
|
||||||
|
|
||||||
const onReRunBtnClick = () => {
|
const onReRunBtnClick = () => {
|
||||||
emitter.emit(EVENT_PROMPT)
|
emitter.emit(EVENT_RERUN)
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import mitt from 'mitt'
|
import mitt from 'mitt'
|
||||||
|
|
||||||
export const EVENT_PROMPT = 'prompt'
|
export const EVENT_PROMPT = 'prompt'
|
||||||
|
export const EVENT_RERUN = 'rerun_last_mask'
|
||||||
|
|
||||||
const emitter = mitt()
|
const emitter = mitt()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user