Skip to content

ProCraft for Android: An Android native painting app written in Rust

Rust

feat(3d): Lighting Studio, material maps and turntable export

Josh66b7c8ad6acf
diff --git a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/CanvasSurfaceView.kt b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/CanvasSurfaceView.kt
index ea50567..1c706bf 100644
--- a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/CanvasSurfaceView.kt
+++ b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/CanvasSurfaceView.kt
@@ -474,7 +474,7 @@ class CanvasSurfaceView(context: Context, private val vm: CanvasViewModel) : Sur
return true
}
// on a model, a finger turns it; the pen paints
- if (vm.modelOpen && kindOf(e, 0) == PointerKind.FINGER) {
+ if (vm.modelOpen && !vm.modelFlat && kindOf(e, 0) == PointerKind.FINGER) {
mode = Mode.TURN
lastX = e.x
lastY = e.y
diff --git a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/CanvasViewModel.kt b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/CanvasViewModel.kt
index da96f8e..627d8dc 100644
--- a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/CanvasViewModel.kt
+++ b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/CanvasViewModel.kt
@@ -989,10 +989,263 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {
var textureSet by mutableStateOf(0)
private set
+ /** The texture set painted on shows flat, as a canvas, in place of the model (Show 2D texture). */
+ var modelFlat by mutableStateOf(false)
+ private set
+
+ fun changeModelFlat(on: Boolean) {
+ val s = session ?: return
+ s.setModelFlat(on)
+ modelFlat = s.modelFlat()
+ }
+
+ /** The Lighting Studio is open over the model (Actions > 3D > Edit lighting & environment). */
+ var lightingStudioOpen by mutableStateOf(false)
+ private set
+
+ /** The environment the model is lit by (an asset in environments/), how bright, and whether it shows behind. */
+ var modelEnvironment by mutableStateOf(DEFAULT_ENVIRONMENT)
+ private set
+ var modelExposure by mutableStateOf(1f)
+ private set
+ var modelEnvironmentShown by mutableStateOf(true)
+ private set
+
+ /** The lights the model is lit by, as the studio shows them. */
+ var modelLights by mutableStateOf<List<io.github.artcraftandroid.procraft.core.ModelLight>>(emptyList())
+ private set
+
+ /** The lighting as the studio found it, put back by Cancel. */
+ private class StudioKept(val environment: String, val exposure: Float, val shown: Boolean, val lights: List<io.github.artcraftandroid.procraft.core.ModelLight>)
+
+ private var studioKept: StudioKept? = null
+
+ fun openLightingStudio() {
+ val s = session ?: return
+ if (!modelOpen || modelFlat) return
+ closePanels()
+ modelLights = s.modelLights()
+ studioKept = StudioKept(modelEnvironment, modelExposure, modelEnvironmentShown, modelLights)
+ s.setLightingStudio(true)
+ lightingStudioOpen = true
+ }
+
+ /** Leaves the studio, keeping what was changed or ([keep] false) putting the lighting back. */
+ fun closeLightingStudio(keep: Boolean) {
+ val s = session
+ if (!keep) studioKept?.let { kept ->
+ if (kept.environment != modelEnvironment) chooseEnvironment(kept.environment)
+ changeExposure(kept.exposure)
+ changeEnvironmentShown(kept.shown)
+ s?.setModelLights(kept.lights)
+ }
+ s?.setLightingStudio(false)
+ modelLights = s?.modelLights() ?: emptyList()
+ studioKept = null
+ lightingStudioOpen = false
+ }
+
+ /** Adds a light above the model; at the most there can be, says so instead. */
+ fun addLight() {
+ val s = session ?: return
+ if (!s.addModelLight()) message = "A model can have four lights at most."
+ modelLights = s.modelLights()
+ }
+
+ fun duplicateLight(index: Int) {
+ val s = session ?: return
+ if (!s.duplicateModelLight(index.toUInt())) message = "A model can have four lights at most."
+ modelLights = s.modelLights()
+ }
+
+ fun deleteLight(index: Int) {
+ val s = session ?: return
+ s.deleteModelLight(index.toUInt())
+ modelLights = s.modelLights()
+ }
+
+ /** Light [index] changed to [light] (its colour or intensity). */
+ fun changeLight(index: Int, light: io.github.artcraftandroid.procraft.core.ModelLight) {
+ val s = session ?: return
+ s.setModelLight(index.toUInt(), light)
+ modelLights = s.modelLights()
+ }
+
+ /** The light whose cube is under window point ([x], [y]) in the studio, if one is. */
+ fun lightAt(x: Float, y: Float): Int? = session?.modelLightAt(x, y)?.toInt()
+
+ /** Light [index] in the studio: its middle on the window and how far its cube reaches (pixels). */
+ fun lightOnScreen(index: Int): List<Float>? = session?.modelLightOnScreen(index.toUInt())
+
+ fun moveLight(index: Int, dx: Float, dy: Float) {
+ val s = session ?: return
+ s.moveModelLight(index.toUInt(), dx, dy)
+ modelLights = s.modelLights()
+ }
+
+ /** A drag across the studio: its camera goes round the model and up or down. */
+ fun studioTurn(dx: Float, dy: Float) {
+ session?.studioTurn(dx, dy)
+ }
+
+ fun studioZoom(scale: Float) {
+ session?.studioZoom(scale)
+ }
+
+ /** Lights the model by the environment [name] (an asset in environments/), read off the main thread. */
+ fun chooseEnvironment(name: String) {
+ val s = session ?: return
+ modelEnvironment = name
+ viewModelScope.launch {
+ val assets = getApplication<Application>().assets
+ val hdr = withContext(Dispatchers.IO) { runCatching { assets.open("environments/$name.hdr").use { it.readBytes() } }.getOrNull() }
+ if (hdr != null && session === s && modelEnvironment == name) s.setModelEnvironment(name, hdr)
+ }
+ }
+
+ fun changeExposure(exposure: Float) {
+ val s = session ?: return
+ s.setModelExposure(exposure)
+ modelExposure = s.modelExposure()
+ }
+
+ fun changeEnvironmentShown(shown: Boolean) {
+ val s = session ?: return
+ s.setModelEnvironmentShown(shown)
+ modelEnvironmentShown = s.modelEnvironmentShown()
+ }
+
+ /** One finger across the studio's view: the model turns. */
+ fun turnModel(dx: Float, dy: Float) {
+ session?.turnModel(dx, dy)
+ }
+
+ /** Strokes on the model reach every surface under them, not only the nearest. */
+ var paintThrough by mutableStateOf(false)
+ private set
+
+ fun changePaintThrough(on: Boolean) {
+ val s = session ?: return
+ s.setPaintThrough(on)
+ paintThrough = s.paintThrough()
+ }
+
+ /** The model's meshes, each in its texture set; which one is open in the layers panel; which are hidden; their pictures. */
+ var submeshes by mutableStateOf<List<io.github.artcraftandroid.procraft.core.SubmeshInfo>>(emptyList())
+ private set
+ var openSubmesh by mutableStateOf<Int?>(null)
+ private set
+ var submeshesHidden by mutableStateOf<List<Boolean>>(emptyList())
+ private set
+ val submeshThumbnails = androidx.compose.runtime.mutableStateMapOf<Int, ImageBitmap>()
+
+ /** Opens submesh [index] in the layers panel (its texture set's layers under it), or closes it again. */
+ fun openSubmesh(index: Int) {
+ if (openSubmesh == index) {
+ openSubmesh = null
+ return
+ }
+ val m = submeshes.getOrNull(index) ?: return
+ openSubmesh = index
+ chooseTextureSet(m.textureSet.toInt())
+ }
+
+ /** Hides submesh [index] (or shows it again), saying which. */
+ fun toggleSubmeshHidden(index: Int) {
+ val s = session ?: return
+ val hide = submeshesHidden.getOrNull(index) != true
+ s.setSubmeshHidden(index.toUInt(), hide)
+ submeshesHidden = s.submeshesHidden()
+ showNotice(if (hide) "Hide Submesh" else "Show Submesh")
+ }
+
+ /** Draws each submesh's picture again, as the model is seen now. */
+ fun refreshSubmeshThumbnails() {
+ val s = session ?: return
+ for (i in submeshes.indices) {
+ viewModelScope.launch {
+ val image = withContext(Dispatchers.Default) {
+ val t = s.submeshThumbnail(i.toUInt(), THUMB_SIDE.toUInt()) ?: return@withContext null
+ val bitmap = io.github.artcraftandroid.procraft.ui.CanvasSpace.tag(Bitmap.createBitmap(t.width.toInt(), t.height.toInt(), Bitmap.Config.ARGB_8888))
+ bitmap.copyPixelsFromBuffer(ByteBuffer.wrap(t.rgba))
+ bitmap.asImageBitmap()
+ }
+ if (image != null && session === s) submeshThumbnails[i] = image
+ }
+ }
+ }
+
+ /** The channel painted on the model's texture set: 0 its colour, 1 roughness, 2 metalness. */
+ var modelChannel by mutableStateOf(0)
+ private set
+
+ /** Each channel's own colour (hue, saturation, brightness), kept while another is painted. */
+ private val channelColors = arrayOfNulls<FloatArray>(3)
+
+ /** A channel's tile was chosen (blue, the layer's row not): painting goes to that channel of the current layer. */
+ var channelTileChosen by mutableStateOf(false)
+ private set
+
+ /** Chooses a layer's channel tile: [channel] painted from now on. */
+ fun chooseChannelTile(channel: Int) {
+ chooseChannel(channel)
+ channelTileChosen = true
+ }
+
+ /** A layer's row tapped on a model: the layer itself (its colour) is painted, no tile chosen. */
+ fun leaveChannelTiles() {
+ chooseChannel(0)
+ channelTileChosen = false
+ }
+
+ /** The layer whose channels show under it in the layers panel, if one's do; and their pictures. */
+ var channelTrayFor by mutableStateOf<ULong?>(null)
+ val channelThumbnails = androidx.compose.runtime.mutableStateMapOf<Pair<Int, ULong>, ImageBitmap>()
+
+ /**
+ * Paints on [channel] of the texture set from now on (its layers stay the layers), with that
+ * channel's own colour: roughness and metalness start black.
+ */
+ fun chooseChannel(channel: Int) {
+ val s = session ?: return
+ if (channel == modelChannel || channel !in 0..2) return
+ channelColors[modelChannel] = floatArrayOf(hue, saturation, brightness)
+ s.setModelChannel(channel.toUInt())
+ modelChannel = s.modelChannel().toInt()
+ val c = channelColors[modelChannel] ?: if (modelChannel == 0) null else floatArrayOf(0f, 0f, 0f)
+ c?.let { setColor(it[0], it[1], it[2]) }
+ refresh()
+ refreshThumbnails()
+ }
+
+ /** Shows layer [id]'s channels under it (or hides them), drawn afresh. */
+ fun toggleChannelTray(id: ULong) {
+ channelTrayFor = if (channelTrayFor == id) null else id
+ if (channelTrayFor == id) refreshChannelThumbnails(id)
+ }
+
+ private fun refreshChannelThumbnails(id: ULong) {
+ val s = session ?: return
+ for (c in 0..2) {
+ viewModelScope.launch {
+ val image = withContext(Dispatchers.Default) {
+ val t = s.channelLayerThumbnail(c.toUInt(), id, THUMB_SIDE.toUInt()) ?: return@withContext null
+ val bitmap = io.github.artcraftandroid.procraft.ui.CanvasSpace.tag(Bitmap.createBitmap(t.width.toInt(), t.height.toInt(), Bitmap.Config.ARGB_8888))
+ bitmap.copyPixelsFromBuffer(ByteBuffer.wrap(t.rgba))
+ bitmap.asImageBitmap()
+ }
+ if (image != null && session === s) channelThumbnails[c to id] = image
+ }
+ }
+ }
+
/** Paints on texture set [index] from now on: the layers become its layers. */
fun chooseTextureSet(index: Int) {
val s = session ?: return
if (index == textureSet || index !in textureSets.indices) return
+ // a texture set is opened on its colour
+ leaveChannelTiles()
+ channelTrayFor = null
s.chooseTextureSet(index.toUInt())
textureSet = index
refresh()
@@ -1005,7 +1258,7 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {
val s = withContext(Dispatchers.IO) {
runCatching {
val file = resolver.openInputStream(uri)?.use { it.readBytes() } ?: return@runCatching null
- CanvasSession.openModel(file, name, null, name.substringBeforeLast('.').ifBlank { "Untitled Artwork" }, 0u)
+ CanvasSession.openModel(file, name, null, name.substringBeforeLast('.').ifBlank { "Untitled Artwork" }, 0u).also(::loadModelTextures)
}.onFailure { android.util.Log.e("ProCraft", "opening a model", it) }.getOrNull()
}
if (s == null) {
@@ -1016,6 +1269,42 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {
startedNew()
}
+ /**
+ * The images a model's file brings (a USDZ's), decoded and scaled to its texture sets' side
+ * into their Base Layers: colour as it is, roughness and metalness from the component they
+ * name, as grey.
+ */
+ private fun loadModelTextures(s: CanvasSession) {
+ val side = s.modelInfo()?.textureSize?.toInt() ?: return
+ val options = BitmapFactory.Options().apply {
+ inPreferredConfig = Bitmap.Config.ARGB_8888
+ inPreferredColorSpace = android.graphics.ColorSpace.get(android.graphics.ColorSpace.Named.SRGB)
+ }
+ // an image that can't be read is left out, the rest still come in
+ for (f in s.modelTextureFiles()) runCatching {
+ val decoded = BitmapFactory.decodeByteArray(f.bytes, 0, f.bytes.size, options) ?: return@runCatching
+ var scaled = Bitmap.createScaledBitmap(decoded, side, side, true)
+ if (scaled !== decoded) decoded.recycle()
+ // a 16-bit image decodes to half floats whatever is asked: made 8-bit
+ if (scaled.config != Bitmap.Config.ARGB_8888) scaled = scaled.copy(Bitmap.Config.ARGB_8888, true).also { scaled.recycle() }
+ if (f.component >= 0) {
+ val px = IntArray(side * side)
+ scaled.getPixels(px, 0, side, 0, 0, side, side)
+ val shift = when (f.component) { 0 -> 16; 1 -> 8; 2 -> 0; else -> 24 }
+ for (i in px.indices) {
+ val v = (px[i] ushr shift) and 0xFF
+ px[i] = (0xFF shl 24) or (v shl 16) or (v shl 8) or v
+ }
+ scaled.setPixels(px, 0, side, 0, 0, side, side)
+ }
+ // RGBA, premultiplied
+ val buffer = ByteBuffer.allocate(side * side * 4)
+ scaled.copyPixelsToBuffer(buffer)
+ scaled.recycle()
+ s.setModelTexture(f.textureSet, f.channel, buffer.array())
+ }.onFailure { android.util.Log.e("ProCraft", "a model's texture", it) }
+ }
+
/** A file from elsewhere into the gallery: an artwork file, or a picture as a new artwork. */
fun importFile(uri: android.net.Uri) {
val resolver = getApplication<Application>().contentResolver
@@ -1408,16 +1697,23 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {
modelOpen = model != null
textureSets = model?.textureSets ?: emptyList()
textureSet = s.textureSet().toInt()
+ modelFlat = s.modelFlat()
+ paintThrough = s.paintThrough()
+ submeshes = model?.submeshes ?: emptyList()
+ submeshesHidden = s.submeshesHidden()
+ openSubmesh = null
+ submeshThumbnails.clear()
+ modelChannel = s.modelChannel().toInt()
+ channelTileChosen = false
+ channelColors.fill(null)
+ channelTrayFor = null
+ channelThumbnails.clear()
+ modelExposure = s.modelExposure()
+ modelEnvironmentShown = s.modelEnvironmentShown()
+ modelLights = s.modelLights()
+ lightingStudioOpen = false
// a model is lit by the environment it was last lit by, else the default
- if (model != null) {
- val name = s.modelEnvironment() ?: DEFAULT_ENVIRONMENT
- viewModelScope.launch {
- val assets = getApplication<Application>().assets
- val read = { n: String -> runCatching { assets.open("environments/$n.hdr").use { it.readBytes() } }.getOrNull() }
- val chosen = withContext(Dispatchers.IO) { read(name)?.let { name to it } ?: read(DEFAULT_ENVIRONMENT)?.let { DEFAULT_ENVIRONMENT to it } }
- if (chosen != null) s.setModelEnvironment(chosen.first, chosen.second)
- }
- }
+ if (model != null) chooseEnvironment(s.modelEnvironment()?.takeIf { it in ENVIRONMENTS } ?: DEFAULT_ENVIRONMENT)
// brush files that came while no canvas was open
addPendingBrushes(s)
pushSecondary()
@@ -3039,11 +3335,10 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {
MODEL_USDZ("usdz", "model/vnd.usdz+zip"),
/** A model's texture sets, a PNG each. */
MODEL_TEXTURES("png", "image/png"),
- /** The model as shown. */
+ /** The model as shown, as a PNG, a JPEG or a TIFF. */
MODEL_RENDER("png", "image/png"),
- /** The model on a turntable, turning once round or swinging, as a video. */
- MODEL_ROTATE("mp4", "video/mp4"),
- MODEL_SWING("mp4", "video/mp4"),
+ MODEL_JPEG("jpg", "image/jpeg"),
+ MODEL_TIFF("tif", "image/tiff"),
}
/** The artwork written as [kind] into the cache, to share: one file, or a PNG per layer. */
@@ -3061,9 +3356,14 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {
Export.MODEL_OBJ -> s.exportModelObj(file.path)
Export.MODEL_USDZ -> s.exportModelUsdz(file.path)
Export.MODEL_RENDER -> s.exportModelRender(file.path, false)
- Export.MODEL_ROTATE, Export.MODEL_SWING -> {
- if (!writeTurntable(s, file, kind == Export.MODEL_SWING)) return@withContext emptyList()
+ Export.MODEL_JPEG -> {
+ val png = File(dir, "$base.png")
+ s.exportModelRender(png.path, false)
+ val ok = io.github.artcraftandroid.procraft.gallery.pngToJpeg(png, file)
+ png.delete()
+ if (!ok) return@withContext emptyList()
}
+ Export.MODEL_TIFF -> s.exportModelTiff(file.path)
Export.MODEL_TEXTURES -> {
val folder = File(dir, base).apply { deleteRecursively(); mkdirs() }
return@withContext s.exportModelTextures(folder.path).map { File(it) }
@@ -3082,22 +3382,61 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {
}
}
- /**
- * The model on a turntable as an MP4 at [file]: [TURNTABLE_SECONDS] at [TURNTABLE_FPS], the
- * window's shape, as large as the encoder takes up to [TURNTABLE_SIDE]. False if it fails.
- */
- private fun writeTurntable(s: CanvasSession, file: File, swing: Boolean): Boolean {
- val (w, h) = VideoWriter.fit(windowPx.width.coerceAtLeast(2), windowPx.height.coerceAtLeast(2), TURNTABLE_SIDE) ?: return false
- val count = TURNTABLE_SECONDS * TURNTABLE_FPS
- val started = System.nanoTime()
- val ok = VideoWriter.write(file, w, h, TURNTABLE_FPS, count) { i, pixels ->
- val t = s.modelTurntableFrame(i.toUInt(), count.toUInt(), swing, w.toUInt(), h.toUInt()) ?: return@write false
- // already ARGB, little-endian
- java.nio.ByteBuffer.wrap(t.rgba).order(java.nio.ByteOrder.LITTLE_ENDIAN).asIntBuffer().get(pixels, 0, w * h)
- true
+ /** The model's turntable being set up to share (Share Image > Animated ...), if it is. */
+ var modelAnimatedExport by mutableStateOf<Animated?>(null)
+
+ /** How the model's turntable goes, and its size: 720p, 1080p, 4K or Square (kept while the app runs). */
+ var turntable by mutableStateOf(io.github.artcraftandroid.procraft.core.Turntable(swing = false, seconds = 2f, zoom = 0.5f, ease = 1f, environment = true))
+ var turntableSize by mutableStateOf(0)
+
+ /** The turntable's picture size for [turntableSize]: video's 16:9 sizes across, or a square. */
+ fun turntablePixels(): Pair<Int, Int> = TURNTABLE_SIZES.getOrElse(turntableSize) { TURNTABLE_SIZES[0] }
+
+ /** The turntable's frames, each [side] square, [fps] a second, for the preview. */
+ suspend fun turntablePreview(side: Int, fps: Int): List<Bitmap> {
+ val s = session ?: return emptyList()
+ val t = turntable
+ return withContext(Dispatchers.Default) {
+ val count = (t.seconds * fps).roundToInt().coerceAtLeast(1)
+ (0 until count).mapNotNull { i ->
+ val p = s.modelTurntablePicture(i.toUInt(), count.toUInt(), t, side.toUInt(), side.toUInt(), false) ?: return@mapNotNull null
+ Bitmap.createBitmap(p.width.toInt(), p.height.toInt(), Bitmap.Config.ARGB_8888).apply { copyPixelsFromBuffer(ByteBuffer.wrap(p.rgba)) }
+ }
+ }
+ }
+
+ /** The model's turntable written as [format] into the cache, to share; null if it can't be made. */
+ suspend fun exportModelAnimation(format: Animated): File? {
+ val s = session ?: return null
+ val t = turntable
+ val (w, h) = turntablePixels()
+ return withContext(Dispatchers.IO) {
+ val dir = File(getApplication<Application>().cacheDir, "exports").apply { mkdirs() }
+ val base = s.name().filter { it.isLetterOrDigit() || it in " _-" }.trim().ifEmpty { "Untitled Artwork" }
+ val file = File(dir, "$base.${format.extension}")
+ try {
+ when (format) {
+ Animated.GIF -> s.exportModelGif(file.path, t, w.toUInt(), h.toUInt(), TURNTABLE_FPS.toFloat())
+ Animated.PNG -> s.exportModelApng(file.path, t, w.toUInt(), h.toUInt(), TURNTABLE_FPS.toFloat())
+ Animated.MP4, Animated.HEVC -> {
+ val mime = if (format == Animated.HEVC) android.media.MediaFormat.MIMETYPE_VIDEO_HEVC else android.media.MediaFormat.MIMETYPE_VIDEO_AVC
+ val (vw, vh) = VideoWriter.fit(w, h, maxOf(w, h), mime) ?: return@withContext null
+ val count = (t.seconds * TURNTABLE_FPS).roundToInt().coerceAtLeast(1)
+ val ok = VideoWriter.write(file, vw, vh, TURNTABLE_FPS, count, mime) { i, pixels ->
+ val p = s.modelTurntablePicture(i.toUInt(), count.toUInt(), t, vw.toUInt(), vh.toUInt(), true) ?: return@write false
+ // already ARGB, little-endian
+ ByteBuffer.wrap(p.rgba).order(java.nio.ByteOrder.LITTLE_ENDIAN).asIntBuffer().get(pixels, 0, vw * vh)
+ true
+ }
+ if (!ok) return@withContext null
+ }
+ }
+ file
+ } catch (e: MobileException) {
+ Log.e(TAG, "exporting the turntable", e)
+ null
+ }
}
- Log.i(TAG, "turntable ${w}x$h, $count frames in ${(System.nanoTime() - started) / 1_000_000} ms")
- return ok
}
/** The layers as an animation, a frame each: the formats, by name, extension and type. */
@@ -4654,10 +4993,9 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {
}
companion object {
- /** A model's turntable video: its length and frame rate. */
- const val TURNTABLE_SECONDS = 5
+ /** A model's turntable: its frame rate, and its sizes (720p, 1080p, 4K, Square). */
const val TURNTABLE_FPS = 30
- const val TURNTABLE_SIDE = 1920
+ val TURNTABLE_SIZES = listOf(1280 to 720, 1920 to 1080, 3840 to 2160, 1080 to 1080)
/** Model files the gallery's Import opens to paint on. */
val MODEL_EXTENSIONS = listOf("obj", "usdz", "usdc", "usda", "usd")
@@ -4665,6 +5003,9 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {
/** The environment a model is lit by unless another is chosen (an asset in environments/). */
const val DEFAULT_ENVIRONMENT = "mountain"
+ /** The environments to light a model by, as the Lighting Studio lists them (assets in environments/). */
+ val ENVIRONMENTS = listOf("studio", "coast", "auditorium", "city", "nightlife", "portside", "industrial", "sunrise", "beach", "mountain", "daytime")
+
/** While the view settles, what is drawn over the canvas is moved this often (ms), and a little past its end (ns). */
const val FRAME_MS = 16L
const val SETTLE_MARGIN_NS = 50_000_000L
diff --git a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/ActionsPanel.kt b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/ActionsPanel.kt
index 97b829c..1206a8a 100644
--- a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/ActionsPanel.kt
+++ b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/ActionsPanel.kt
@@ -96,6 +96,9 @@ private val Tabs = listOf(
Tab("Help", PcIcons.TabHelp),
)
+/** On a model, its own tab takes Video's place. */
+private val ModelTabs = Tabs.map { if (it.label == "Video") Tab("3D", PcIcons.Tab3D) else it }
+
/** The actions panel: six tabs of grouped rows (adding, the canvas, sharing, video, preferences, help). */
@Composable
fun ActionsPanel(vm: CanvasViewModel, arrowX: Dp, modifier: Modifier = Modifier) {
@@ -150,7 +153,12 @@ fun ActionsPanel(vm: CanvasViewModel, arrowX: Dp, modifier: Modifier = Modifier)
val takePhoto = androidx.activity.compose.rememberLauncherForActivityResult(androidx.activity.result.contract.ActivityResultContracts.TakePicture()) { taken ->
if (taken) vm.insertPicture(photo)
}
- val groups: List<List<ActionRow>> = when (shownTab) {
+ val model = vm.modelOpen
+ val tabs = if (model) ModelTabs else Tabs
+ val groups: List<List<ActionRow>> = if (model && shownTab <= 3) modelGroups(vm, shownTab, { k, q -> share(k, q) }, pickFile = { pickFile.launch(arrayOf("image/*")) },
+ pickPhoto = { pickPhoto.launch(androidx.activity.result.PickVisualMediaRequest(androidx.activity.result.contract.ActivityResultContracts.PickVisualMedia.ImageOnly)) },
+ takePhoto = { runCatching { takePhoto.launch(photo) }.onFailure { dialog = "Take a photo" to "No camera app is available." } },
+ ) else when (shownTab) {
0 -> listOf(
listOf(
Act("Insert a file") { pickFile.launch(arrayOf("image/*")) },
@@ -176,21 +184,7 @@ fun ActionsPanel(vm: CanvasViewModel, arrowX: Dp, modifier: Modifier = Modifier)
listOf(Act("Flip horizontal") { vm.flipCanvas(horizontal = true) }, Act("Flip vertical") { vm.flipCanvas(horizontal = false) }),
listOf(Act("Canvas information") { vm.canvasInfoOpen = true }),
)
- 2 -> listOfNotNull(
- // a model shares as a model too
- if (vm.modelOpen) {
- listOf(
- Heading("Share 3D"),
- Act("OBJ") { share(CanvasViewModel.Export.MODEL_OBJ) },
- Act("USDZ") { share(CanvasViewModel.Export.MODEL_USDZ) },
- Act("Render") { share(CanvasViewModel.Export.MODEL_RENDER) },
- Act("PNG Textures") { share(CanvasViewModel.Export.MODEL_TEXTURES) },
- Act("Animated rotate") { share(CanvasViewModel.Export.MODEL_ROTATE) },
- Act("Animated swing") { share(CanvasViewModel.Export.MODEL_SWING) },
- )
- } else {
- null
- },
+ 2 -> listOf(
listOf(
Heading("Share Image"),
Act(CanvasViewModel.PACKAGE_LABEL) {
@@ -285,7 +279,7 @@ fun ActionsPanel(vm: CanvasViewModel, arrowX: Dp, modifier: Modifier = Modifier)
modifier = Modifier.padding(start = 24.dp, top = 22.dp).height(34.dp),
)
Row(Modifier.padding(start = 10.dp).height(60.dp)) {
- Tabs.forEachIndexed { i, tab ->
+ tabs.forEachIndexed { i, tab ->
// a tab lights up as soon as it is touched; the old one goes out when it is let go
val touch = remember { androidx.compose.foundation.interaction.MutableInteractionSource() }
val held = heldOf(touch)
@@ -306,7 +300,7 @@ fun ActionsPanel(vm: CanvasViewModel, arrowX: Dp, modifier: Modifier = Modifier)
modifier = Modifier.align(Alignment.TopCenter).padding(top = 41.dp),
)
}
- if (i < Tabs.size - 1) Spacer(Modifier.width(10.dp))
+ if (i < tabs.size - 1) Spacer(Modifier.width(10.dp))
}
}
Column(
@@ -370,6 +364,63 @@ fun ActionsPanel(vm: CanvasViewModel, arrowX: Dp, modifier: Modifier = Modifier)
}
}
+/**
+ * The first four tabs' rows on a model: adding without text, the canvas's reference and
+ * information, sharing the model, pictures of it and its textures, and the 3D tab (the 2D
+ * texture, painting through, lighting, AR). Prefs and Help are as on a canvas.
+ */
+private fun modelGroups(
+ vm: CanvasViewModel,
+ tab: Int,
+ share: (CanvasViewModel.Export, Int) -> Unit,
+ pickFile: () -> Unit,
+ pickPhoto: () -> Unit,
+ takePhoto: () -> Unit,
+): List<List<ActionRow>> = when (tab) {
+ 0 -> listOf(
+ listOf(Act("Insert a file") { pickFile() }, Act("Insert a photo") { pickPhoto() }, Act("Take a photo") { takePhoto() }),
+ listOf(Act("Cut") { vm.cutLayer() }, Act("Copy") { vm.copyLayer() }, Act("Copy canvas") { vm.copyCanvas() }, Act("Paste", onClick = if (vm.canPaste) ({ vm.paste() }) else null)),
+ )
+ 1 -> listOf(
+ listOf(Toggle("Reference", vm.referenceOn) { vm.changeReferenceOn(it) }),
+ listOf(Act("Canvas information") { vm.canvasInfoOpen = true }),
+ )
+ 2 -> listOf(
+ listOf(
+ Heading("Share Model"),
+ // the native package doesn't hold a model yet
+ Act(CanvasViewModel.PACKAGE_LABEL),
+ Act("USDZ") { share(CanvasViewModel.Export.MODEL_USDZ, 100) },
+ Act("OBJ") { share(CanvasViewModel.Export.MODEL_OBJ, 100) },
+ ),
+ listOf(
+ Heading("Share Image"),
+ Act("JPEG") { share(CanvasViewModel.Export.MODEL_JPEG, 100) },
+ Act("PNG") { share(CanvasViewModel.Export.MODEL_RENDER, 100) },
+ Act("TIFF") { share(CanvasViewModel.Export.MODEL_TIFF, 100) },
+ Act("Animated GIF") { vm.modelAnimatedExport = CanvasViewModel.Animated.GIF },
+ Act("Animated PNG") { vm.modelAnimatedExport = CanvasViewModel.Animated.PNG },
+ Act("Animated MP4") { vm.modelAnimatedExport = CanvasViewModel.Animated.MP4 },
+ Act("Animated HEVC") { vm.modelAnimatedExport = CanvasViewModel.Animated.HEVC },
+ ),
+ listOf(Heading("Share Textures"), Act("PNG") { share(CanvasViewModel.Export.MODEL_TEXTURES, 100) }),
+ )
+ 3 -> {
+ // showing the 2D texture, the rest is for the model
+ val shown = !vm.modelFlat
+ listOf(
+ listOf(
+ Toggle("Show 2D texture", vm.modelFlat) { vm.changeModelFlat(it) },
+ Toggle("Paint through mesh", vm.paintThrough, onChange = if (shown) ({ vm.changePaintThrough(it) }) else null),
+ Act("Edit lighting & environment", onClick = if (shown) ({ vm.openLightingStudio() }) else null),
+ ),
+ // no AR on Android yet
+ listOf(Act("View in AR")),
+ )
+ }
+ else -> emptyList()
+}
+
/** A small menu beside a row: its choices, the chosen one marked with a blue dot. */
@Composable
private fun ChoiceMenu(look: Modifier, choices: List<String>, chosen: Int, offsetY: Dp, onPick: (Int) -> Unit, onDismiss: () -> Unit) {
diff --git a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/AnimatedExportUi.kt b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/AnimatedExportUi.kt
index 27037b4..4eb2110 100644
--- a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/AnimatedExportUi.kt
+++ b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/AnimatedExportUi.kt
@@ -6,6 +6,8 @@ import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
+import androidx.compose.foundation.gestures.awaitEachGesture
+import androidx.compose.foundation.gestures.awaitFirstDown
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
@@ -14,6 +16,7 @@ import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
@@ -50,6 +53,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
+import kotlin.math.roundToInt
private val ScreenFill = Color(0xFF1F1F1F)
private val TitleInk = Color(0xFFAAAAAA)
@@ -176,3 +180,158 @@ private fun SettingRow(label: String, on: Boolean, onChange: (Boolean) -> Unit)
Switch(on, enabled = true)
}
}
+
+private val ChoiceFill = Color(0xFF2E2E2E)
+private val SliderTrack = Color(0xFF3E3E3E)
+
+/**
+ * Sharing a model on a turntable: it turning in a square as it will, Export Settings (the size,
+ * once round or swinging, how long a turn takes, how far the camera pulls back, the easing, and
+ * whether the environment shows) in a panel on the left, Cancel, and the round Done that exports
+ * and shares.
+ */
+@Composable
+fun ModelAnimatedExport(vm: CanvasViewModel, format: CanvasViewModel.Animated) {
+ val context = LocalContext.current
+ val scope = rememberCoroutineScope()
+ var settingsOpen by remember { mutableStateOf(false) }
+ var exporting by remember { mutableStateOf(false) }
+ fun cancel() {
+ if (settingsOpen) settingsOpen = false else vm.modelAnimatedExport = null
+ }
+ BackHandler { cancel() }
+ BoxWithConstraints(Modifier.fillMaxSize().background(ScreenFill).pointerInput(Unit) { detectTapGestures { } }) {
+ val box = minOf(maxWidth - 48.dp, maxHeight - 120.dp)
+ val side = with(LocalDensity.current) { box.roundToPx() }
+ // the turntable's frames, made again whenever its settings change, played over and over
+ var frames by remember { mutableStateOf<List<Bitmap>>(emptyList()) }
+ var shown by remember { mutableIntStateOf(0) }
+ LaunchedEffect(vm.turntable, side) {
+ frames = vm.turntablePreview(side / 2, PREVIEW_FPS)
+ shown = 0
+ }
+ LaunchedEffect(frames) {
+ while (frames.size > 1) {
+ delay(1000L / PREVIEW_FPS)
+ shown = (shown + 1) % frames.size
+ }
+ }
+ Box(Modifier.align(Alignment.TopCenter).offset(y = 96.dp).size(box), contentAlignment = Alignment.Center) {
+ frames.getOrNull(shown)?.let { Image(it.asImageBitmap(), contentDescription = "Preview", contentScale = ContentScale.Fit, modifier = Modifier.fillMaxSize()) }
+ }
+ Row(Modifier.offset(x = 14.dp, y = 20.dp).height(44.dp).clickable { settingsOpen = !settingsOpen }.padding(end = 12.dp), verticalAlignment = Alignment.CenterVertically) {
+ Box(Modifier.width(60.dp), contentAlignment = Alignment.Center) {
+ Icon(PcIcons.ChevronLeft, contentDescription = null, tint = TitleInk, modifier = Modifier.size(28.dp))
+ }
+ Text("Export Settings", color = TitleInk, fontSize = 24.sp, fontWeight = FontWeight.Bold, maxLines = 1)
+ }
+ Row(Modifier.align(Alignment.TopEnd).padding(top = 16.dp, end = 16.dp), verticalAlignment = Alignment.CenterVertically) {
+ Box(
+ Modifier.height(43.dp).clip(CircleShape).background(CapsuleFill).border(1.dp, CapsuleEdge, CircleShape).clickable { cancel() }.padding(horizontal = 13.dp),
+ contentAlignment = Alignment.Center,
+ ) {
+ Text("Cancel", color = Color(0xFFA5A5A5), fontSize = 17.sp)
+ }
+ Spacer(Modifier.width(13.dp))
+ Box(
+ Modifier.size(44.dp, 45.dp).clip(CircleShape).background(DoneBlue).clickable(enabled = !exporting) {
+ exporting = true
+ scope.launch {
+ val file = vm.exportModelAnimation(format)
+ exporting = false
+ if (file != null) {
+ vm.modelAnimatedExport = null
+ shareFile(context, file, format.mime)
+ } else {
+ vm.message = "The animation could not be exported."
+ }
+ }
+ },
+ contentAlignment = Alignment.Center,
+ ) {
+ if (exporting) CircularProgressIndicator(color = Color.White, strokeWidth = 2.dp, modifier = Modifier.size(24.dp))
+ else Icon(PcIcons.DoneTick, contentDescription = "Export", tint = Color.White, modifier = Modifier.requiredSize(40.dp))
+ }
+ }
+ if (settingsOpen) {
+ Box(Modifier.fillMaxSize().pointerInput(Unit) { detectTapGestures { settingsOpen = false } })
+ val t = vm.turntable
+ Column(
+ Modifier.padding(10.dp).width(400.dp).fillMaxHeight().clip(RoundedCornerShape(20.dp)).background(PanelFill).border(1.dp, PanelEdge, RoundedCornerShape(20.dp))
+ .pointerInput(Unit) { detectTapGestures { } },
+ ) {
+ Text(format.title, color = TitleInk, fontSize = 24.sp, fontWeight = FontWeight.Bold, maxLines = 1, modifier = Modifier.padding(start = 24.dp, top = 15.dp).height(36.dp))
+ Spacer(Modifier.height(8.dp))
+ Segments(listOf("720p", "1080p", "4K", "Square"), vm.turntableSize) { vm.turntableSize = it }
+ Spacer(Modifier.height(23.dp))
+ Column(Modifier.padding(horizontal = 16.dp).clip(RoundedCornerShape(14.dp)).background(ChoiceFill)) {
+ ChoiceRow("Animated rotate", !t.swing) { vm.turntable = t.copy(swing = false) }
+ ChoiceRow("Animated swing", t.swing) { vm.turntable = t.copy(swing = true) }
+ }
+ Spacer(Modifier.height(26.dp))
+ val seconds = if (t.seconds < 1f) "%.1f".format(t.seconds) else "${t.seconds.roundToInt()}"
+ TurntableSlider("Animation duration", seconds, (t.seconds - 0.5f) / 4.5f) { vm.turntable = vm.turntable.copy(seconds = 0.5f + it * 4.5f) }
+ TurntableSlider("Zoom distance", "${(t.zoom * 100).roundToInt()}%", t.zoom) { vm.turntable = vm.turntable.copy(zoom = it) }
+ TurntableSlider("Ease", "${(t.ease * 100).roundToInt()}%", t.ease) { vm.turntable = vm.turntable.copy(ease = it) }
+ Spacer(Modifier.height(10.dp))
+ SettingRow("Show environment", t.environment) { vm.turntable = vm.turntable.copy(environment = it) }
+ }
+ }
+ }
+}
+
+/** The turntable preview's frame rate. */
+private const val PREVIEW_FPS = 15
+
+/** One of two choices in a group, 40 tall: the chosen one blue with a tick. */
+@Composable
+private fun ChoiceRow(label: String, chosen: Boolean, onChoose: () -> Unit) {
+ Row(
+ Modifier.fillMaxWidth().height(40.dp).background(if (chosen) Pc.Accent else Color.Transparent).clickable(onClick = onChoose).padding(start = 16.dp, end = 12.dp),
+ verticalAlignment = Alignment.CenterVertically,
+ ) {
+ Text(label, color = if (chosen) Color.White else RowInk, fontSize = 17.sp, maxLines = 1, modifier = Modifier.weight(1f))
+ if (chosen) {
+ Box(Modifier.size(20.dp).clip(CircleShape).background(Color.White), contentAlignment = Alignment.Center) {
+ Icon(PcIcons.Tick, contentDescription = null, tint = Pc.Accent, modifier = Modifier.size(14.dp))
+ }
+ }
+ }
+}
+
+/** A setting from 0 to 1, 66 tall: its name and value over a track filled blue up to its white knob. */
+@Composable
+private fun TurntableSlider(label: String, value: String, fraction: Float, onChange: (Float) -> Unit) {
+ val change by androidx.compose.runtime.rememberUpdatedState(onChange)
+ val current by androidx.compose.runtime.rememberUpdatedState(fraction)
+ Box(Modifier.padding(start = 8.dp, end = 8.dp).fillMaxWidth().height(66.dp)) {
+ Text(label, color = RowInk, fontSize = 17.sp, maxLines = 1, modifier = Modifier.padding(start = 16.dp, top = 6.dp))
+ Text(value, color = RowInk, fontSize = 17.sp, maxLines = 1, modifier = Modifier.align(Alignment.TopEnd).padding(end = 16.dp, top = 6.dp))
+ androidx.compose.foundation.Canvas(
+ Modifier.fillMaxWidth().height(28.dp).align(Alignment.BottomStart).offset(y = (-8).dp).pointerInput(Unit) {
+ awaitEachGesture {
+ val down = awaitFirstDown()
+ down.consume()
+ val travel = size.width - 2 * 16.dp.toPx()
+ var last = down.position.x
+ while (true) {
+ val ev = awaitPointerEvent().changes.firstOrNull { it.id == down.id } ?: break
+ if (!ev.pressed) break
+ change((current + (ev.position.x - last) / travel).coerceIn(0f, 1f))
+ last = ev.position.x
+ ev.consume()
+ }
+ }
+ },
+ ) {
+ val y = size.height / 2f
+ val inset = 16.dp.toPx()
+ val thick = 3.dp.toPx()
+ val x = inset + (size.width - 2 * inset) * current.coerceIn(0f, 1f)
+ drawRoundRect(SliderTrack, androidx.compose.ui.geometry.Offset(inset, y - thick / 2f), androidx.compose.ui.geometry.Size(size.width - 2 * inset, thick), androidx.compose.ui.geometry.CornerRadius(thick / 2f))
+ drawRoundRect(Pc.Accent, androidx.compose.ui.geometry.Offset(inset, y - thick / 2f), androidx.compose.ui.geometry.Size(x - inset, thick), androidx.compose.ui.geometry.CornerRadius(thick / 2f))
+ val (w, h) = 26.dp.toPx() to 16.dp.toPx()
+ drawRoundRect(Color.White, androidx.compose.ui.geometry.Offset(x - w / 2f, y - h / 2f), androidx.compose.ui.geometry.Size(w, h), androidx.compose.ui.geometry.CornerRadius(h / 2f))
+ }
+ }
+}
diff --git a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/AppRoot.kt b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/AppRoot.kt
index 40863f4..7b3f735 100644
--- a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/AppRoot.kt
+++ b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/AppRoot.kt
@@ -52,6 +52,7 @@ fun AppRoot(vm: CanvasViewModel) {
BackHandler {
when {
vm.guideEditing -> vm.closeGuideEditor(keep = false)
+ vm.lightingStudioOpen -> vm.closeLightingStudio(keep = false)
vm.studioOpen -> vm.closeStudio(keep = false)
vm.transformPopup != TransformPopup.NONE -> vm.transformPopup = TransformPopup.NONE
vm.selectionPopup != SelectionPopup.NONE -> vm.selectionPopup = SelectionPopup.NONE
@@ -115,7 +116,7 @@ fun AppRoot(vm: CanvasViewModel) {
TopBar(vm, Modifier.align(Alignment.TopCenter).then(up).then(fade))
// a mode taking the screen slides it off its edge, and back when it ends
androidx.compose.animation.AnimatedVisibility(
- vm.showSideBar && !vm.transforming && !vm.selecting && !vm.guideEditing && vm.cropRect == null && (vm.adjustment == null || vm.adjustPencil),
+ vm.showSideBar && !vm.transforming && !vm.selecting && !vm.guideEditing && !vm.lightingStudioOpen && vm.cropRect == null && (vm.adjustment == null || vm.adjustPencil),
Modifier.align(if (vm.rightHanded) Alignment.TopEnd else Alignment.TopStart).offset(y = maxHeight * Pc.SIDE_BAR_CENTRE - Pc.SideBarHeight / 2),
enter = Motion.sideBarIn(vm.rightHanded),
exit = Motion.sideBarOut(vm.rightHanded),
@@ -188,6 +189,7 @@ fun AppRoot(vm: CanvasViewModel) {
if (vm.textInfo != null) TextEditor(vm)
RasterizePrompt(vm)
vm.animatedExport?.let { AnimatedExport(vm, it) }
+ vm.modelAnimatedExport?.let { ModelAnimatedExport(vm, it) }
// full-screen pages slide up over everything, and back down
androidx.compose.animation.AnimatedVisibility(
vm.canvasInfoOpen,
@@ -264,6 +266,7 @@ fun AppRoot(vm: CanvasViewModel) {
SelectionPopover(vm, maxWidth, maxHeight)
}
GuideEditor(vm)
+ LightingStudio(vm)
BrushStudio(vm)
PadMenu(vm)
}
diff --git a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/Icons.kt b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/Icons.kt
index 22ed581..2c84275 100644
--- a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/Icons.kt
+++ b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/Icons.kt
@@ -354,6 +354,72 @@ object PcIcons {
val TabCanvas by lazy { shape("tabcanvas", 50f, "M14.7 33.6C13.3 33.4 12.1 32.3 11.8 30.9C11.7 30.3 11.7 17.7 11.8 17.2C12.1 16 13.2 14.9 14.4 14.6C14.9 14.5 35.4 14.4 36 14.6C37.1 14.8 38.1 15.7 38.6 17C38.8 17.5 38.8 30 38.7 30.8C38.4 32.2 37.3 33.3 36 33.6C35.6 33.7 15.2 33.7 14.7 33.6zM28.6 30.6C29.4 30.4 30 29.9 30.3 29.2C30.6 28.5 30.6 28.3 30.6 25.8C30.6 23.6 30.6 23.6 30.4 23.3C30 22.8 29.2 22.8 28.8 23.4C28.7 23.6 28.7 23.7 28.6 25.9C28.6 28.5 28.6 28.5 28.1 28.7C27.8 28.8 27.2 28.8 27 28.6C26.5 28.3 26.5 28.6 26.5 24.4C26.5 20.8 26.5 20.7 26.3 20.4C25.9 19.1 24.9 18.5 23.5 18.5C22 18.5 21 19.3 20.6 20.7C20.4 21.4 20.5 25.1 20.7 25.4C21.1 26 22 25.9 22.4 25.3C22.5 25 22.5 24.9 22.5 23.1C22.5 21 22.5 20.8 22.9 20.6C23.2 20.5 23.8 20.4 24.1 20.6C24.5 20.7 24.5 20.7 24.5 24.6C24.5 28.4 24.5 28.6 24.9 29.3C25.6 30.5 27.1 31.1 28.6 30.6z") }
val TabShare by lazy { shape("tabshare", 50f, "M18.8 34.2C17.5 34 16.4 33 16.1 31.6C16 31 16 20.7 16.1 20C16.3 18.9 17.1 18 18.3 17.6C18.7 17.4 18.8 17.4 20.8 17.4L22.8 17.4L22.8 21.4C22.8 25.4 22.8 25.4 23 25.8C23.6 27.3 25.7 27.7 26.9 26.5C27.6 25.9 27.5 26.1 27.6 21.5L27.6 17.4L29.4 17.4C31.5 17.4 31.8 17.4 32.5 17.8C33.4 18.2 34 18.9 34.3 19.8C34.4 20.4 34.4 31.3 34.3 31.8C34 32.9 33.1 33.8 32 34.2C31.6 34.3 19.6 34.4 18.8 34.2zM24.9 25.5C24.7 25.4 24.7 25.4 24.7 18.4L24.7 11.4L23.1 12.9C22.1 13.9 21.5 14.5 21.4 14.5C20.9 14.6 20.5 14.2 20.6 13.7C20.7 13.6 21.4 12.8 22.8 11.4C24.9 9.3 24.9 9.3 25.2 9.3C25.5 9.3 25.5 9.3 27.6 11.5C30 13.8 29.9 13.8 29.7 14.2C29.6 14.4 29.4 14.5 29.1 14.5C28.9 14.5 28.8 14.4 27.3 12.9L25.7 11.3L25.7 18.3C25.7 25.4 25.7 25.4 25.5 25.5C25.3 25.7 25.1 25.7 24.9 25.5z") }
val TabVideo by lazy { shape("tabvideo", 50f, "M18.4 34.2C18 34.1 17.6 33.8 17.3 33.5C17.1 33 17 15.6 17.3 15C17.6 14.5 17.9 14.3 18.5 14.3C19 14.3 19 14.3 26.9 18.5C35.8 23.3 35.3 23 35.5 23.2C36 23.8 36 24.8 35.4 25.4C35.3 25.5 20.7 33.3 19.3 34C19 34.2 18.7 34.2 18.4 34.2z") }
+ /** The Lighting Studio's environments, each its own small picture (24 grid). */
+ // a studio lamp: a round light in its square housing
+ val EnvStudio by lazy {
+ icon("env-studio", Part("M6.5 5h11A1.5 1.5 0 0 1 19 6.5v11a1.5 1.5 0 0 1-1.5 1.5h-11A1.5 1.5 0 0 1 5 17.5v-11A1.5 1.5 0 0 1 6.5 5z", 1.7f, false), Part("M12 8.2a3.8 3.8 0 1 1 0 7.6a3.8 3.8 0 1 1 0-7.6z", 0f, true))
+ }
+ // waves along a coast under a low sun
+ val EnvCoast by lazy {
+ icon("env-coast", Part("M3 14.5c1.5-1.4 3-1.4 4.5 0s3 1.4 4.5 0 3-1.4 4.5 0 3 1.4 4.5 0", 1.7f, false), Part("M3 18.5c1.5-1.4 3-1.4 4.5 0s3 1.4 4.5 0 3-1.4 4.5 0 3 1.4 4.5 0", 1.7f, false), Part("M12 6.5a3 3 0 1 1 0 6h-.01a3 3 0 0 1 0-6z", 0f, true))
+ }
+ // a star on a stage's backdrop
+ val EnvAuditorium by lazy {
+ icon("env-auditorium", Part("M6 4.5h12A1.5 1.5 0 0 1 19.5 6v12a1.5 1.5 0 0 1-1.5 1.5H6A1.5 1.5 0 0 1 4.5 18V6A1.5 1.5 0 0 1 6 4.5zM12 7.6l1.3 2.7 3 .4-2.2 2.1.5 3-2.6-1.4-2.6 1.4.5-3-2.2-2.1 3-.4z", 0f, true, evenOdd = true))
+ }
+ // three towers
+ val EnvCity by lazy {
+ icon("env-city", Part("M4 20V9.5a3 3 0 0 1 6 0V20zM10.5 20V6.5a3 3 0 0 1 6 0V20zM17 20v-8a1.5 1.5 0 0 1 3 0v8z", 0f, true))
+ }
+ // towers under a twinkle
+ val EnvNightlife by lazy {
+ icon("env-nightlife", Part("M4 20v-7h4v7zM9 20V10h4v10zM14 20v-6h4v6z", 0f, true), Part("M17.5 3.5v5M15 6h5", 1.5f, false), Part("M6 6.5v3M4.5 8h3", 1.2f, false))
+ }
+ // a sailing boat
+ val EnvPortside by lazy {
+ icon("env-portside", Part("M11.5 3.5v11.5H5.5z", 0f, true), Part("M13 6.5c2.8 2.3 4.4 5 4.9 8.5H13z", 0f, true), Part("M3.5 16.8h17l-1.8 3.2H5.3z", 0f, true))
+ }
+ // two gears
+ val EnvIndustrial by lazy {
+ icon(
+ "env-industrial",
+ Part("M9 8.3a4.7 4.7 0 1 1 0 9.4a4.7 4.7 0 1 1 0-9.4z", 2.2f, false),
+ Part("M9 5.6v2M9 18.4v2M3.6 13h2M12.4 13h2M5.2 9.2l1.4 1.4M11.4 15.4l1.4 1.4M5.2 16.8l1.4-1.4M11.4 10.6l1.4-1.4", 1.8f, false),
+ Part("M17 3.6a2.6 2.6 0 1 1 0 5.2a2.6 2.6 0 1 1 0-5.2z", 1.6f, false),
+ Part("M17 1.9v1M17 9.5v1M13.6 6.2h1M19.4 6.2h1", 1.4f, false),
+ )
+ }
+ // the sun half over the horizon
+ val EnvSunrise by lazy {
+ icon("env-sunrise", Part("M7 17a5 5 0 0 1 10 0z", 0f, true), Part("M3.5 19.5h17M12 4.5v3M5.6 9.6l2 1.8M18.4 9.6l-2 1.8", 1.7f, false))
+ }
+ // a scallop shell
+ val EnvBeach by lazy {
+ icon("env-beach", Part("M12 20.5L5.2 11a6.8 6.8 0 0 1 13.6 0z", 0f, true), Part("M12 19.5V6M12 19.5L8.3 6.8M12 19.5l3.7-12.7M12 19.5L5.6 9.5M12 19.5l6.4-10", 0.9f, false))
+ }
+ // peaks
+ val EnvMountain by lazy {
+ icon("env-mountain", Part("M2.5 19.5L9 9l3.2 5 2.3-3.2 7 8.7z", 0f, true))
+ }
+ // the sun
+ val EnvDaytime by lazy {
+ icon(
+ "env-daytime",
+ Part("M12 8.4a3.6 3.6 0 1 1 0 7.2a3.6 3.6 0 1 1 0-7.2z", 0f, true),
+ Part("M12 2.8v2.4M12 18.8v2.4M2.8 12h2.4M18.8 12h2.4M5.5 5.5l1.7 1.7M16.8 16.8l1.7 1.7M5.5 18.5l1.7-1.7M16.8 7.2l1.7-1.7", 1.7f, false),
+ )
+ }
+
+ /** A see-through cube: its outline, the three edges in front, the three behind dashed. */
+ val Tab3D by lazy {
+ icon(
+ "tab-3d",
+ Part("M25 12.9L34.6 18.4V28.6L25 34.1L15.4 28.6V18.4Z", 1.6f, false),
+ Part("M15.4 18.4L25 23.5L34.6 18.4M25 23.5V34.1", 1.4f, false),
+ Part("M25 14.6V16.6M25 18.9V20.9M22.9 24.6L21 25.6M19.2 26.6L17.3 27.6M27.1 24.6L29 25.6M30.8 26.6L32.7 27.6", 1.4f, false),
+ viewport = 50f,
+ )
+ }
val TabPrefs by lazy { shape("tabprefs", 50f, "M17.5 32.7C14.8 32.3 12.4 30.5 11.1 28.1C8.1 22.4 11.9 15.6 18.3 14.9C18.7 14.9 21.1 14.9 25.5 14.9C32.1 14.9 32.1 14.9 32.8 15.1C36.2 15.8 38.9 18.3 39.8 21.6C41.1 26.7 37.8 31.9 32.6 32.8C31.5 32.9 18.5 32.9 17.5 32.7zM32.7 31.2C37.4 30.2 39.9 24.9 37.8 20.6C35.3 15.6 28.4 14.9 25.1 19.4C21.7 23.8 24.2 30.2 29.8 31.3C30.4 31.4 32.1 31.4 32.7 31.2z") }
val TabHelp by lazy { shape("tabhelp", 50f, "M24 34.2C19.6 33.8 15.8 30.5 14.8 26.1C13.1 18.8 19.6 12 27 13.4C33.2 14.7 36.9 21 35 27C33.5 31.6 28.9 34.6 24 34.2zM25.3 30C26.1 29.8 26.4 28.6 25.8 28C25 27.1 23.7 27.7 23.7 28.8C23.7 29.7 24.4 30.2 25.3 30zM25.3 26.7C25.6 26.5 25.7 26.4 25.9 25.7C26 25 26.1 24.9 27.1 23.9C28.3 22.9 28.5 22.7 28.7 22.3C29.5 21.1 29.3 19.6 28.2 18.6C26.8 17.1 23.9 17.1 22.2 18.4C20.7 19.7 20.4 21.7 21.7 21.9C22.4 22 22.7 21.8 23.1 20.9C23.6 19.6 24 19.2 25.1 19.2C26.2 19.2 27 19.9 26.9 20.9C26.8 21.4 26.5 21.8 25.7 22.6C24.2 23.9 23.8 24.5 23.8 25.6C23.8 26.6 24.5 27.1 25.3 26.7z") }
val Tick by lazy { outline("tick", 3.2f, "M5.5 12.5l4 4 9-9") }
diff --git a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/LayersPanel.kt b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/LayersPanel.kt
index e9143f1..83a6420 100644
--- a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/LayersPanel.kt
+++ b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/LayersPanel.kt
@@ -28,6 +28,7 @@ import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
+import androidx.compose.foundation.lazy.LazyItemScope
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
@@ -152,6 +153,8 @@ fun LayersPanel(vm: CanvasViewModel, arrowX: Dp, modifier: Modifier = Modifier)
// where each row is in the window: for dropping a dragged row and placing the options
val rows = remember { mutableMapOf<ULong, Rect>() }
+ // a model's meshes are pictured as the model is seen when the panel opens
+ if (vm.modelOpen) LaunchedEffect(Unit) { vm.refreshSubmeshThumbnails() }
Popover(LayersWidth, arrowX, modifier) {
// kept while they fade out
val open = options?.let { id -> vm.layers.firstOrNull { it.id == id }?.let { l -> rows[id]?.let { l to it } } }
@@ -194,20 +197,10 @@ fun LayersPanel(vm: CanvasViewModel, arrowX: Dp, modifier: Modifier = Modifier)
) {
// a closed group's layers are left out
val shown = vm.layers.filter { !vm.hidden(it) }
- // on a model, the texture set painted on heads its layers
- vm.textureSets.getOrNull(vm.textureSet)?.let { name ->
- item(key = "texture set") { TextureSetRow(name, onTap = {}) }
- }
- itemsIndexed(shown, key = { _, l -> l.id.toLong() }) { index, l ->
+ // a layer's row: in the list, or on a model under its submesh, indented
+ val layerItem: @Composable LazyItemScope.(Int, LayerInfo, Shape, Dp, String?) -> Unit = { index, l, shape, indent, subtitle ->
val dragging = dragId == l.id
DisposableEffect(l.id) { onDispose { rows.remove(l.id) } }
- // the list's ends round; the rows between are square
- val shape = RoundedCornerShape(
- topStart = if (index == 0) ListCorner else 0.dp,
- topEnd = if (index == 0) ListCorner else 0.dp,
- bottomStart = if (index == shown.size - 1) ListCorner else 0.dp,
- bottomEnd = if (index == shown.size - 1) ListCorner else 0.dp,
- )
Column(
Modifier.animateItem(Motion.rowFade(), if (dragging || blendAt >= 0) null else Motion.rowMove(), Motion.rowFade())
.onGloballyPositioned { rows[l.id] = it.boundsInWindow() }
@@ -241,7 +234,11 @@ fun LayersPanel(vm: CanvasViewModel, arrowX: Dp, modifier: Modifier = Modifier)
) {
val onTap = {
swiped = null
- if (vm.anyPicked) {
+ // on a model the row means the layer itself: its colour, no channel's tile
+ if (vm.modelOpen && vm.channelTileChosen && l.kind != LayerType.GROUP) {
+ vm.leaveChannelTiles()
+ if (!l.current) vm.selectLayer(l.id)
+ } else if (vm.anyPicked) {
// picks give way to the layer tapped, the current one too
vm.selectLayer(l.id)
options = null
@@ -266,7 +263,7 @@ fun LayersPanel(vm: CanvasViewModel, arrowX: Dp, modifier: Modifier = Modifier)
}
SwipeRow(vm, l, open = swiped == l.id, enabled = dragId == null, onOpen = { swiped = if (it) l.id else null }, onPick = { vm.pickLayer(l) }) {
if (l.kind == LayerType.GROUP) {
- GroupRow(vm, l, shape, name, onTap) { vm.toggleCollapsed(l) }
+ GroupRow(vm, l, shape, name, onTap, indent) { vm.toggleCollapsed(l) }
} else LayerRow(
vm,
l,
@@ -278,8 +275,15 @@ fun LayersPanel(vm: CanvasViewModel, arrowX: Dp, modifier: Modifier = Modifier)
changeBlend(if (blendFor == l.id) null else l.id)
options = null
},
+ indent = indent,
+ subtitle = subtitle,
+ // on a model: its channels, and the row giving its blue to the channel painted
+ onCube = if (vm.modelOpen && l.kind != LayerType.MASK) ({ vm.toggleChannelTray(l.id) }) else null,
+ cubeLit = vm.modelOpen && l.current && vm.channelTileChosen,
+ selected = l.current && !(vm.modelOpen && vm.channelTileChosen),
)
}
+ if (vm.modelOpen && vm.channelTrayFor == l.id) ChannelTray(vm, l)
// the blend modes open under the row, the panel growing as they fade in, and close back
androidx.compose.animation.AnimatedVisibility(
blendFor == l.id,
@@ -291,9 +295,51 @@ fun LayersPanel(vm: CanvasViewModel, arrowX: Dp, modifier: Modifier = Modifier)
) { BlendEditor(vm, l) }
}
}
- // the model's other texture sets, a tap away
- vm.textureSets.forEachIndexed { i, name ->
- if (i != vm.textureSet) item(key = "texture set $i") { TextureSetRow(name, onTap = { vm.chooseTextureSet(i) }) }
+ if (!vm.modelOpen) {
+ itemsIndexed(shown, key = { _, l -> l.id.toLong() }) { index, l ->
+ // the list's ends round; the rows between are square
+ val shape = RoundedCornerShape(
+ topStart = if (index == 0) ListCorner else 0.dp,
+ topEnd = if (index == 0) ListCorner else 0.dp,
+ bottomStart = if (index == shown.size - 1) ListCorner else 0.dp,
+ bottomEnd = if (index == shown.size - 1) ListCorner else 0.dp,
+ )
+ layerItem(index, l, shape, 0.dp, null)
+ }
+ } else {
+ // on a model: each texture set headed by its name, its meshes under it; the open
+ // mesh's texture set's layers under that, shared by every mesh in the set
+ vm.textureSets.forEachIndexed { set, setName ->
+ val meshes = vm.submeshes.withIndex().filter { it.value.textureSet.toInt() == set }
+ if (meshes.isEmpty()) return@forEachIndexed
+ item(key = "set $set") { SetHeader(setName, first = set == 0) }
+ meshes.forEachIndexed { k, (m, info) ->
+ val open = vm.openSubmesh == m
+ val lastMesh = k == meshes.size - 1
+ val layersUnder = open && set == vm.textureSet && shown.isNotEmpty()
+ item(key = "mesh $m") {
+ SubmeshRow(
+ vm,
+ m,
+ info.name,
+ open = layersUnder,
+ shape = RoundedCornerShape(
+ topStart = if (k == 0) ListCorner else 0.dp,
+ topEnd = if (k == 0) ListCorner else 0.dp,
+ bottomStart = if (lastMesh && !layersUnder) ListCorner else 0.dp,
+ bottomEnd = if (lastMesh && !layersUnder) ListCorner else 0.dp,
+ ),
+ )
+ }
+ if (layersUnder) {
+ itemsIndexed(shown, key = { _, l -> "layer ${l.id}" }) { index, l ->
+ val end = lastMesh && index == shown.size - 1
+ val shape = RoundedCornerShape(bottomStart = if (end) ListCorner else 0.dp, bottomEnd = if (end) ListCorner else 0.dp)
+ layerItem(index, l, shape, GroupIndent, if (meshes.size > 1) "Shared" else null)
+ }
+ }
+ }
+ }
}
// it moves down and up with the rows above it
if (!vm.modelOpen) item(key = "background") {
@@ -499,12 +545,20 @@ private fun LockedName(l: LayerInfo, ink: Color, name: @Composable (Color, Modif
/** A group's row: a line where a thumbnail would be, its name, its open or closed arrow and its box. */
@Composable
-private fun GroupRow(vm: CanvasViewModel, l: LayerInfo, shape: Shape, name: @Composable (Color, Modifier) -> Unit, onTap: () -> Unit, onToggle: () -> Unit) {
+private fun GroupRow(
+ vm: CanvasViewModel,
+ l: LayerInfo,
+ shape: Shape,
+ name: @Composable (Color, Modifier) -> Unit,
+ onTap: () -> Unit,
+ indent: Dp,
+ onToggle: () -> Unit,
+) {
Row(
Modifier.fillMaxWidth().height(GroupHeight).clip(shape)
.background(if (l.current) Pc.RowSelected else if (l.picked) PickedRow else Pc.LayerRow)
.clickable(onClick = onTap)
- .padding(start = GroupIndent * l.depth.toInt()),
+ .padding(start = indent + GroupIndent * l.depth.toInt()),
verticalAlignment = Alignment.CenterVertically,
) {
val ink = if (l.current || l.picked) Color.White else Pc.PanelTitle
@@ -529,18 +583,23 @@ private fun LayerRow(
owner: Boolean,
onTap: () -> Unit,
onBlend: () -> Unit,
+ indent: Dp = 0.dp,
+ subtitle: String? = null,
+ onCube: (() -> Unit)? = null,
+ cubeLit: Boolean = false,
+ selected: Boolean = l.current,
) {
val mask = l.kind == LayerType.MASK
val masked = vm.hasMask(l)
Row(
Modifier.fillMaxWidth().height(if (mask || masked) MaskedRowHeight else RowHeight).clip(shape)
// the layer a selected mask belongs to shows a darker blue
- .background(if (l.current) Pc.RowSelected else if (l.picked) PickedRow else if (owner) MaskOwnerRow else Pc.LayerRow)
+ .background(if (selected) Pc.RowSelected else if (l.picked) PickedRow else if (owner) MaskOwnerRow else Pc.LayerRow)
.clickable(onClick = onTap)
- .padding(start = (if (l.clipped) 18.dp else 0.dp) + GroupIndent * l.depth.toInt()),
+ .padding(start = indent + (if (l.clipped) 18.dp else 0.dp) + GroupIndent * l.depth.toInt()),
verticalAlignment = Alignment.CenterVertically,
) {
- val ink = if (l.current || l.picked) Color.White else Pc.PanelTitle
+ val ink = if (selected || l.picked) Color.White else Pc.PanelTitle
if (l.clipped) Icon(PcIcons.Clip, contentDescription = "Clipped", tint = ink, modifier = Modifier.size(16.dp))
// a mask's thumbnail and its layer's join into one, rounded only at the outer corners
val corner = 4.dp
@@ -555,7 +614,21 @@ private fun LayerRow(
else -> 0.dp
}
Box(Modifier.padding(start = 4.dp).offset(y = shift)) { Thumb(vm, l, thumbShape) }
- LockedName(l, ink, name, Modifier.weight(1f))
+ if (subtitle == null) {
+ LockedName(l, ink, name, Modifier.weight(1f))
+ } else {
+ // a model's layer shared by its texture set's meshes says so under its name
+ Column(Modifier.weight(1f)) {
+ LockedName(l, ink, name, Modifier)
+ Text(subtitle, color = ink, fontSize = 13.sp, maxLines = 1, modifier = Modifier.padding(start = 29.dp))
+ }
+ }
+ // a model's layer: the button that shows its channels, blue while one of them is painted
+ if (onCube != null) {
+ Box(Modifier.offset(x = 16.dp).size(44.dp, 47.dp).clickable(onClick = onCube), contentAlignment = Alignment.Center) {
+ Icon(PcIcons.Tab3D, contentDescription = "Channels", tint = if (cubeLit) Pc.Accent else ink, modifier = Modifier.requiredSize(48.dp))
+ }
+ }
// the blend and visibility buttons overlap by 8, as on the reference; a mask has no blend
if (mask) {
Spacer(Modifier.width(36.dp))
@@ -567,12 +640,41 @@ private fun LayerRow(
}
}
Box(Modifier.size(44.dp, 47.dp).clickable { vm.toggleVisible(l) }, contentAlignment = Alignment.Center) {
- CheckBox(l.visible, if (l.current || l.picked) Color.White else Pc.CheckFill)
+ CheckBox(l.visible, if (selected || l.picked) Color.White else Pc.CheckFill)
}
Spacer(Modifier.width(8.dp))
}
}
+private val TrayFill = Color(0xFF303032)
+private val TileFill = Color(0xFF3B3B3D)
+
+/**
+ * A model's layer's channels under its row: Color, Roughness and Metallic, each its picture of
+ * the layer; the one painted on (of the current layer) blue. A tap paints on it.
+ */
+@Composable
+private fun ChannelTray(vm: CanvasViewModel, l: LayerInfo) {
+ Row(Modifier.fillMaxWidth().height(159.dp).background(TrayFill).padding(4.dp), horizontalArrangement = Arrangement.spacedBy(6.dp)) {
+ listOf("Color", "Roughness", "Metallic").forEachIndexed { c, label ->
+ val chosen = l.current && vm.channelTileChosen && vm.modelChannel == c
+ Column(
+ Modifier.weight(1f).fillMaxHeight().clip(RoundedCornerShape(6.dp)).background(if (chosen) Pc.Accent else TileFill)
+ .clickable {
+ if (!l.current) vm.selectLayer(l.id)
+ vm.chooseChannelTile(c)
+ }
+ .padding(6.dp),
+ ) {
+ Box(Modifier.fillMaxWidth().height(102.dp).clip(RoundedCornerShape(3.dp)).background(Pc.ThumbBackground)) {
+ vm.channelThumbnails[c to l.id]?.let { Image(it, contentDescription = null, contentScale = ContentScale.Fit, modifier = Modifier.fillMaxSize()) }
+ }
+ Text(label, color = if (chosen) Color.White else Pc.PanelTitle, fontSize = 15.sp, maxLines = 1, modifier = Modifier.padding(start = 2.dp, top = 9.dp))
+ }
+ }
+ }
+}
+
/** A layer's visibility box: filled with a check when shown, an outline when hidden. */
@Composable
private fun CheckBox(checked: Boolean, fill: Color) {
@@ -822,11 +924,44 @@ private fun Option(label: String, checked: Boolean? = null, enabled: Boolean = t
if (checked == true) Icon(PcIcons.Tick, contentDescription = null, tint = color, modifier = Modifier.size(18.dp))
}
}
-/** A model's texture set: its name heading the layers painted into it. */
+/** A model's texture set's name, heading its meshes: 41 tall, 16 under the set before it. */
+@Composable
+private fun SetHeader(name: String, first: Boolean) {
+ Column {
+ if (!first) Spacer(Modifier.height(16.dp))
+ Box(Modifier.fillMaxWidth().height(41.dp), contentAlignment = Alignment.TopStart) {
+ Text(name, color = Pc.PanelTitle, fontSize = 14.sp, fontWeight = FontWeight.SemiBold, maxLines = 1, modifier = Modifier.padding(start = 16.dp, top = 12.dp))
+ }
+ }
+}
+
+/** A model's mesh opened in the layers panel: its row darkened blue. */
+private val SubmeshOpen = Color(0xFF25344A)
+private val SubmeshTile = Color(0xFF434246)
+
+/**
+ * One of a model's meshes: its picture, its name, the arrow that opens it (its texture set's
+ * layers under it) and its box (shown or hidden). A tap anywhere but the box opens or closes it.
+ */
@Composable
-private fun TextureSetRow(name: String, onTap: () -> Unit) {
- Box(Modifier.fillMaxWidth().height(RowHeight).clickable(onClick = onTap), contentAlignment = Alignment.CenterStart) {
- Text(name, color = Pc.PanelTitle, fontSize = 17.sp, fontWeight = FontWeight.Medium, maxLines = 1, modifier = Modifier.padding(start = 16.dp))
+private fun SubmeshRow(vm: CanvasViewModel, index: Int, name: String, open: Boolean, shape: Shape) {
+ val hidden = vm.submeshesHidden.getOrNull(index) == true
+ Row(
+ Modifier.fillMaxWidth().height(RowHeight).clip(shape).background(if (open) SubmeshOpen else Pc.LayerRow)
+ .clickable { vm.openSubmesh(index) },
+ verticalAlignment = Alignment.CenterVertically,
+ ) {
+ Box(Modifier.padding(start = 4.dp).size(93.dp, 61.dp).clip(RoundedCornerShape(4.dp)).background(SubmeshTile), contentAlignment = Alignment.Center) {
+ vm.submeshThumbnails[index]?.let { Image(it, contentDescription = null, contentScale = ContentScale.Fit, modifier = Modifier.fillMaxSize().padding(4.dp)) }
+ }
+ Text(name, color = Pc.PanelTitle, fontSize = 17.sp, maxLines = 1, overflow = TextOverflow.Ellipsis, modifier = Modifier.weight(1f).padding(start = 29.dp, end = 6.dp))
+ Box(Modifier.offset(x = 8.dp).size(44.dp, 51.dp).clickable { vm.openSubmesh(index) }, contentAlignment = Alignment.Center) {
+ Icon(if (open) PcIcons.ChevronDown else PcIcons.ChevronRight, contentDescription = if (open) "Close" else "Open", tint = Pc.PanelTitle, modifier = Modifier.size(16.dp))
+ }
+ Box(Modifier.size(44.dp, 47.dp).clickable { vm.toggleSubmeshHidden(index) }, contentAlignment = Alignment.Center) {
+ CheckBox(!hidden, Pc.CheckFill)
+ }
+ Spacer(Modifier.width(8.dp))
}
}
diff --git a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/LightingStudioUi.kt b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/LightingStudioUi.kt
new file mode 100644
index 0000000..161fe76
--- /dev/null
+++ b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/LightingStudioUi.kt
@@ -0,0 +1,432 @@
+package io.github.artcraftandroid.procraft.ui
+
+import androidx.activity.compose.BackHandler
+import androidx.compose.foundation.Canvas
+import androidx.compose.foundation.background
+import androidx.compose.foundation.border
+import androidx.compose.foundation.clickable
+import androidx.compose.foundation.gestures.awaitEachGesture
+import androidx.compose.foundation.gestures.awaitFirstDown
+import androidx.compose.foundation.gestures.detectTapGestures
+import androidx.compose.foundation.layout.Box
+import androidx.compose.foundation.layout.BoxWithConstraints
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.Row
+import androidx.compose.foundation.layout.Spacer
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.layout.fillMaxWidth
+import androidx.compose.foundation.layout.height
+import androidx.compose.foundation.layout.heightIn
+import androidx.compose.foundation.layout.offset
+import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.layout.requiredSize
+import androidx.compose.foundation.layout.size
+import androidx.compose.foundation.layout.width
+import androidx.compose.foundation.rememberScrollState
+import androidx.compose.foundation.shape.CircleShape
+import androidx.compose.foundation.shape.RoundedCornerShape
+import androidx.compose.foundation.verticalScroll
+import androidx.compose.material3.Icon
+import androidx.compose.material3.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.remember
+import androidx.compose.runtime.rememberUpdatedState
+import androidx.compose.runtime.setValue
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.draw.clip
+import androidx.compose.ui.geometry.CornerRadius
+import androidx.compose.ui.geometry.Offset
+import androidx.compose.ui.geometry.Size
+import androidx.compose.ui.graphics.Brush
+import androidx.compose.ui.graphics.Color
+import androidx.compose.ui.graphics.vector.ImageVector
+import androidx.compose.ui.input.pointer.pointerInput
+import androidx.compose.ui.text.font.FontWeight
+import androidx.compose.ui.unit.dp
+import androidx.compose.ui.unit.sp
+import io.github.artcraftandroid.procraft.canvas.CanvasViewModel
+import io.github.artcraftandroid.procraft.core.ModelLight
+import kotlin.math.abs
+import kotlin.math.roundToInt
+
+private val StudioBar = Color(0xFF26292A)
+private val StudioTitle = Color(0xFFD8D8D8)
+private val PillFill = Color(0xFF1D2021)
+private val PillEdge = Color(0xFF3B3E3F)
+private val PillLit = Color(0xFF474747)
+private val PillInk = Color(0xFFC1C1C1)
+private val DoneBlue = Color(0xFF0190FF)
+private val PopoverFill = Color(0xFF282A2D)
+private val GroupFill = Color(0xFF232528)
+private val RowLine = Color(0xFF2C2E31)
+private val LabelInk = Color(0xFFB7B7B7)
+private val DimInk = Color(0xFF8A8D8E)
+
+/** How bright the light can be taken: Min to Max on the exposure slider. */
+private const val MOST_EXPOSURE = 5f
+
+/** The environments as the studio lists them: their names, assets and pictures. */
+private class Environment(val label: String, val asset: String, val icon: ImageVector)
+
+private val Environments by lazy {
+ listOf(
+ Environment("Studio", "studio", PcIcons.EnvStudio),
+ Environment("Coast", "coast", PcIcons.EnvCoast),
+ Environment("Auditorium", "auditorium", PcIcons.EnvAuditorium),
+ Environment("City", "city", PcIcons.EnvCity),
+ Environment("Nightlife", "nightlife", PcIcons.EnvNightlife),
+ Environment("Portside", "portside", PcIcons.EnvPortside),
+ Environment("Industrial", "industrial", PcIcons.EnvIndustrial),
+ Environment("Sunrise", "sunrise", PcIcons.EnvSunrise),
+ Environment("Beach", "beach", PcIcons.EnvBeach),
+ Environment("Mountain", "mountain", PcIcons.EnvMountain),
+ Environment("Daytime", "daytime", PcIcons.EnvDaytime),
+ )
+}
+
+/** The hue bar's colours, evenly spaced along it (as the lights take them). */
+private val HueStops = listOf(0xFFFF0000, 0xFFFF6E00, 0xFFFFDC00, 0xFF35FF00, 0xFF00FFEC, 0xFF0034FF, 0xFFAA00FF, 0xFFFF0000).map { Color(it) }
+private val SaturationStart = Color(0xFF808080)
+private val SettingsFill = Color(0xFF29292B)
+private val SettingsGroup = Color(0xFF222427)
+
+/** The hue bar's colour at [t] along it (0 to 1). */
+private fun hueColor(t: Float): Color {
+ val at = t.coerceIn(0f, 1f) * (HueStops.size - 1)
+ val i = at.toInt().coerceAtMost(HueStops.size - 2)
+ return androidx.compose.ui.graphics.lerp(HueStops[i], HueStops[i + 1], at - i)
+}
+
+/**
+ * The Lighting Studio over a model: its bar (Cancel, Add Light, Environment and Done), the model
+ * on its floor among its lights, seen through the studio's camera, which a finger takes round
+ * the model and two fingers nearer; a light's cube is dragged across the view, and tapped, its
+ * settings show. The Environment popover: whether the environment shows, the exposure, and which
+ * environment lights the model.
+ */
+@Composable
+fun LightingStudio(vm: CanvasViewModel) {
+ if (!vm.lightingStudioOpen) return
+ var environmentOpen by remember { mutableStateOf(false) }
+ // the light whose settings are showing
+ var lightOpen by remember { mutableStateOf<Int?>(null) }
+ BackHandler {
+ when {
+ lightOpen != null -> lightOpen = null
+ environmentOpen -> environmentOpen = false
+ else -> vm.closeLightingStudio(keep = false)
+ }
+ }
+ BoxWithConstraints(Modifier.fillMaxSize()) {
+ // the view: a touch with a popover open only closes it
+ Box(
+ Modifier.fillMaxSize().pointerInput(Unit) {
+ val slop = viewConfiguration.touchSlop
+ awaitEachGesture {
+ val down = awaitFirstDown()
+ down.consume()
+ if (environmentOpen || lightOpen != null) {
+ environmentOpen = false
+ lightOpen = null
+ do {
+ val ev = awaitPointerEvent()
+ ev.changes.forEach { it.consume() }
+ } while (ev.changes.any { it.pressed })
+ return@awaitEachGesture
+ }
+ val grabbed = vm.lightAt(down.position.x, down.position.y)
+ var last = down.position
+ var travelled = 0f
+ var pinch: Float? = null
+ while (true) {
+ val ev = awaitPointerEvent()
+ val pressed = ev.changes.filter { it.pressed }
+ if (pressed.isEmpty()) break
+ if (pressed.size >= 2) {
+ // two fingers: nearer or farther, never a tap
+ val apart = (pressed[0].position - pressed[1].position).getDistance()
+ pinch?.let { if (it > 0f) vm.studioZoom(apart / it) }
+ pinch = apart
+ travelled = Float.MAX_VALUE
+ } else {
+ val c = ev.changes.firstOrNull { it.id == down.id && it.pressed } ?: pressed[0]
+ val (dx, dy) = c.position.x - last.x to c.position.y - last.y
+ last = c.position
+ pinch = null
+ // still for a tap until it goes past the slop
+ val before = travelled
+ travelled += abs(dx) + abs(dy)
+ if (travelled > slop && before != Float.MAX_VALUE) {
+ if (grabbed != null) vm.moveLight(grabbed, dx, dy) else vm.studioTurn(dx, dy)
+ }
+ }
+ ev.changes.forEach { it.consume() }
+ }
+ if (grabbed != null && travelled <= slop) lightOpen = grabbed
+ }
+ },
+ )
+ Box(Modifier.fillMaxWidth().height(85.dp).background(StudioBar).pointerInput(Unit) { detectTapGestures { environmentOpen = false } }) {
+ Text("Lighting Studio", color = StudioTitle, fontSize = 20.sp, fontWeight = FontWeight.Bold, maxLines = 1, modifier = Modifier.offset(32.dp, 27.dp))
+ Row(Modifier.align(Alignment.TopEnd).padding(top = 21.dp, end = 20.dp), verticalAlignment = Alignment.CenterVertically) {
+ Row(Modifier.height(43.dp).clip(CircleShape).background(PillFill).border(1.dp, PillEdge, CircleShape)) {
+ PillButton("Cancel", 73.dp, lit = false) { vm.closeLightingStudio(keep = false) }
+ PillButton("Add Light", 92.dp, lit = false) {
+ environmentOpen = false
+ lightOpen = null
+ vm.addLight()
+ }
+ PillButton("Environment", 113.dp, lit = environmentOpen) { environmentOpen = !environmentOpen }
+ }
+ Spacer(Modifier.width(13.dp))
+ Box(Modifier.size(44.dp, 45.dp).clip(CircleShape).background(DoneBlue).clickable { vm.closeLightingStudio(keep = true) }, contentAlignment = Alignment.Center) {
+ Icon(PcIcons.DoneTick, contentDescription = "Done", tint = Color.White, modifier = Modifier.requiredSize(40.dp))
+ }
+ }
+ }
+ if (environmentOpen) {
+ EnvironmentPopover(vm, Modifier.align(Alignment.TopEnd).padding(top = 85.dp, end = 12.dp).heightIn(max = maxHeight - 97.dp))
+ }
+ lightOpen?.let { index ->
+ val light = vm.modelLights.getOrNull(index)
+ val at = vm.lightOnScreen(index)
+ if (light == null || at == null || at.size < 3) {
+ lightOpen = null
+ } else {
+ val density = androidx.compose.ui.platform.LocalDensity.current
+ val (x, y, reach) = with(density) { Triple(at[0].toDp(), at[1].toDp(), at[2].toDp()) }
+ LightSettings(vm, index, light, x, y, reach, maxWidth, onDone = { lightOpen = null })
+ }
+ }
+ }
+}
+
+/** The popover's size: its body and the arrow under it. */
+private val SettingsWidth = 280.dp
+private val SettingsHeight = 347.dp
+
+/**
+ * Light Settings, pointing at the light from above it (below it when there's no room): its hue,
+ * how much of that colour it has, how strong it is; Delete and Duplicate.
+ */
+@Composable
+private fun LightSettings(
+ vm: CanvasViewModel,
+ index: Int,
+ light: ModelLight,
+ x: androidx.compose.ui.unit.Dp,
+ y: androidx.compose.ui.unit.Dp,
+ reach: androidx.compose.ui.unit.Dp,
+ screenWidth: androidx.compose.ui.unit.Dp,
+ onDone: () -> Unit,
+) {
+ val above = y - reach - Pc.ArrowHeight - SettingsHeight > 85.dp + 8.dp
+ val left = (x - SettingsWidth / 2).coerceIn(12.dp, (screenWidth - SettingsWidth - 12.dp).coerceAtLeast(12.dp))
+ val top = if (above) y - reach - Pc.ArrowHeight - SettingsHeight else y + reach
+ Popover(
+ SettingsWidth,
+ (x - left).coerceIn(28.dp, SettingsWidth - 28.dp),
+ Modifier.offset(left, top).pointerInput(Unit) { detectTapGestures { } },
+ fill = SettingsFill,
+ side = if (above) ArrowSide.BOTTOM else ArrowSide.TOP,
+ ) {
+ Text("Light Settings", color = Pc.PanelTitle, fontSize = 24.sp, fontWeight = FontWeight.Bold, maxLines = 1, modifier = Modifier.padding(start = 16.dp, top = 5.dp).height(40.dp))
+ Column(Modifier.padding(horizontal = 10.dp)) {
+ Spacer(Modifier.height(10.dp))
+ Column(Modifier.clip(RoundedCornerShape(14.dp)).background(SettingsGroup).padding(vertical = 5.dp)) {
+ val hue = hueColor(light.hue)
+ val degrees = run {
+ val hsv = FloatArray(3)
+ android.graphics.Color.RGBToHSV((hue.red * 255).roundToInt(), (hue.green * 255).roundToInt(), (hue.blue * 255).roundToInt(), hsv)
+ if (light.hue >= 1f) 360 else hsv[0].roundToInt()
+ }
+ LightSlider(
+ "Hue",
+ if (light.saturation <= 0f) "None" else "$degrees°",
+ light.hue,
+ Brush.horizontalGradient(HueStops),
+ hue,
+ ) { vm.changeLight(index, light.copy(hue = it)) }
+ LightSlider(
+ "Saturation",
+ level(light.saturation),
+ light.saturation,
+ Brush.horizontalGradient(listOf(SaturationStart, hue)),
+ androidx.compose.ui.graphics.lerp(SaturationStart, hue, light.saturation),
+ ) { vm.changeLight(index, light.copy(saturation = it)) }
+ LightSlider(
+ "Intensity",
+ level(light.intensity),
+ light.intensity,
+ Brush.horizontalGradient(listOf(Color.Black, Color.White)),
+ Color(light.intensity, light.intensity, light.intensity),
+ ) { vm.changeLight(index, light.copy(intensity = it)) }
+ }
+ Spacer(Modifier.height(10.dp))
+ Column(Modifier.clip(RoundedCornerShape(14.dp)).background(SettingsGroup)) {
+ Box(Modifier.fillMaxWidth().height(44.dp).clickable { vm.deleteLight(index); onDone() }.padding(start = 16.dp), contentAlignment = Alignment.CenterStart) {
+ Text("Delete", color = LabelInk, fontSize = 17.sp, maxLines = 1)
+ }
+ Box(Modifier.fillMaxWidth().height(1.dp).background(RowLine))
+ Box(Modifier.fillMaxWidth().height(44.dp).clickable { vm.duplicateLight(index); onDone() }.padding(start = 16.dp), contentAlignment = Alignment.CenterStart) {
+ Text("Duplicate", color = LabelInk, fontSize = 17.sp, maxLines = 1)
+ }
+ }
+ Spacer(Modifier.height(10.dp))
+ }
+ }
+}
+
+/** A share shown as the light's sliders show it: None, a percentage, or Max. */
+private fun level(v: Float) = when {
+ v <= 0f -> "None"
+ v >= 1f -> "Max"
+ else -> "${(v * 100f).roundToInt()}%"
+}
+
+/**
+ * One of a light's settings, 56 tall: its name and value over a thin track drawn with [track],
+ * whose capsule knob takes [knob], the track's colour where it is. Dragged by as far as the
+ * finger goes.
+ */
+@Composable
+private fun LightSlider(label: String, value: String, fraction: Float, track: Brush, knob: Color, onChange: (Float) -> Unit) {
+ val change by rememberUpdatedState(onChange)
+ val current by rememberUpdatedState(fraction)
+ Box(Modifier.fillMaxWidth().height(56.dp)) {
+ Text(label, color = DimInk, fontSize = 17.sp, maxLines = 1, modifier = Modifier.padding(start = 16.dp, top = 6.dp))
+ Text(value, color = Color.White, fontSize = 17.sp, maxLines = 1, modifier = Modifier.align(Alignment.TopEnd).padding(end = 16.dp, top = 6.dp))
+ Canvas(
+ Modifier.fillMaxWidth().height(24.dp).align(Alignment.BottomStart).pointerInput(Unit) {
+ awaitEachGesture {
+ val down = awaitFirstDown()
+ down.consume()
+ val travel = size.width - 2 * 18.5.dp.toPx()
+ var last = down.position.x
+ while (true) {
+ val ev = awaitPointerEvent().changes.firstOrNull { it.id == down.id } ?: break
+ if (!ev.pressed) break
+ val moved = (ev.position.x - last) / travel
+ last = ev.position.x
+ change((current + moved).coerceIn(0f, 1f))
+ ev.consume()
+ }
+ }
+ },
+ ) {
+ val y = size.height / 2f
+ val inset = 14.dp.toPx()
+ val thick = 3.dp.toPx()
+ drawRoundRect(track, Offset(inset, y - thick / 2f), Size(size.width - 2 * inset, thick), CornerRadius(thick / 2f))
+ val ends = 18.5.dp.toPx()
+ val x = ends + (size.width - 2 * ends) * fraction.coerceIn(0f, 1f)
+ val (w, h) = 16.dp.toPx() to 10.dp.toPx()
+ drawRoundRect(knob, Offset(x - w / 2f, y - h / 2f), Size(w, h), CornerRadius(h / 2f))
+ }
+ }
+}
+
+/** One of the bar's capsule buttons, lit while what it opened is showing. */
+@Composable
+private fun PillButton(label: String, width: androidx.compose.ui.unit.Dp, lit: Boolean, enabled: Boolean = true, onClick: () -> Unit) {
+ Box(
+ Modifier.width(width).height(43.dp).padding(2.dp).clip(CircleShape).background(if (lit) PillLit else Color.Transparent)
+ .clickable(enabled = enabled, onClick = onClick),
+ contentAlignment = Alignment.Center,
+ ) {
+ Text(label, color = if (enabled) PillInk else PillInk.copy(alpha = 0.4f), fontSize = 17.sp, maxLines = 1)
+ }
+}
+
+/** The Environment popover: Show Environment, Exposure, and the environments, the one lighting the model blue. */
+@Composable
+private fun EnvironmentPopover(vm: CanvasViewModel, modifier: Modifier) {
+ Popover(350.dp, 0.dp, modifier.pointerInput(Unit) { detectTapGestures { } }, fill = PopoverFill, side = ArrowSide.NONE) {
+ Text("Environment", color = Pc.PanelTitle, fontSize = 24.sp, fontWeight = FontWeight.Bold, maxLines = 1, modifier = Modifier.padding(start = 26.dp, top = 8.dp).height(40.dp))
+ Column(Modifier.padding(horizontal = 10.dp)) {
+ Spacer(Modifier.height(13.dp))
+ Row(
+ Modifier.fillMaxWidth().height(44.dp).clip(RoundedCornerShape(14.dp)).background(GroupFill)
+ .clickable { vm.changeEnvironmentShown(!vm.modelEnvironmentShown) }.padding(start = 16.dp, end = 8.dp),
+ verticalAlignment = Alignment.CenterVertically,
+ ) {
+ Text("Show Environment", color = LabelInk, fontSize = 17.sp, maxLines = 1, modifier = Modifier.weight(1f))
+ Switch(vm.modelEnvironmentShown, enabled = true)
+ }
+ Spacer(Modifier.height(10.dp))
+ ExposureSlider(vm.modelExposure) { vm.changeExposure(it) }
+ Spacer(Modifier.height(12.dp))
+ Column(Modifier.clip(RoundedCornerShape(14.dp)).background(GroupFill).verticalScroll(rememberScrollState())) {
+ Environments.forEachIndexed { i, env ->
+ if (i > 0) Box(Modifier.fillMaxWidth().height(1.dp).background(RowLine))
+ val chosen = env.asset == vm.modelEnvironment
+ Row(
+ Modifier.fillMaxWidth().height(44.dp).background(if (chosen) Pc.Accent else Color.Transparent)
+ .clickable { if (!chosen) vm.chooseEnvironment(env.asset) }.padding(start = 11.dp, end = 16.dp),
+ verticalAlignment = Alignment.CenterVertically,
+ ) {
+ Text(env.label, color = if (chosen) Color.White else LabelInk, fontSize = 16.sp, maxLines = 1, modifier = Modifier.weight(1f))
+ Icon(env.icon, contentDescription = null, tint = Color.White, modifier = Modifier.size(20.dp))
+ }
+ }
+ }
+ Spacer(Modifier.height(12.dp))
+ }
+ }
+}
+
+/**
+ * Exposure, from Min (none) to Max (5 times): its value on the right and a black-to-white track
+ * whose knob is filled with the grey the exposure makes (white from 100% up). Dragging moves it
+ * by as far as the finger goes.
+ */
+@Composable
+private fun ExposureSlider(value: Float, onChange: (Float) -> Unit) {
+ val change by rememberUpdatedState(onChange)
+ val current by rememberUpdatedState(value)
+ val shown = when {
+ value <= 0f -> "Min"
+ value >= MOST_EXPOSURE -> "Max"
+ else -> "${(value * 100f).roundToInt()}%"
+ }
+ Box(Modifier.fillMaxWidth().height(84.dp).clip(RoundedCornerShape(14.dp)).background(GroupFill)) {
+ Text("Exposure", color = DimInk, fontSize = 17.sp, maxLines = 1, modifier = Modifier.padding(start = 16.dp, top = 19.dp))
+ Text(shown, color = Color.White, fontSize = 17.sp, maxLines = 1, modifier = Modifier.align(Alignment.TopEnd).padding(end = 16.dp, top = 19.dp))
+ Canvas(
+ Modifier.fillMaxWidth().height(30.dp).align(Alignment.BottomStart).offset(y = (-13).dp).pointerInput(Unit) {
+ awaitEachGesture {
+ val down = awaitFirstDown()
+ down.consume()
+ val travel = size.width - 2 * 18.5.dp.toPx()
+ var last = down.position.x
+ while (true) {
+ val ev = awaitPointerEvent().changes.firstOrNull { it.id == down.id } ?: break
+ if (!ev.pressed) break
+ val moved = (ev.position.x - last) / travel * MOST_EXPOSURE
+ last = ev.position.x
+ change((current + moved).coerceIn(0f, MOST_EXPOSURE))
+ ev.consume()
+ }
+ }
+ },
+ ) {
+ val y = size.height / 2f
+ val inset = 16.dp.toPx()
+ val track = 2.dp.toPx()
+ drawRoundRect(
+ Brush.horizontalGradient(listOf(Color.Black, Color.White), startX = inset, endX = size.width - inset),
+ Offset(inset, y - track / 2f),
+ Size(size.width - 2 * inset, track),
+ CornerRadius(track / 2f),
+ )
+ val ends = 18.5.dp.toPx()
+ val x = ends + (size.width - 2 * ends) * (value / MOST_EXPOSURE).coerceIn(0f, 1f)
+ val (w, h) = 16.dp.toPx() to 9.dp.toPx()
+ val grey = value.coerceIn(0f, 1f)
+ drawRoundRect(Color(grey, grey, grey), Offset(x - w / 2f, y - h / 2f), Size(w, h), CornerRadius(h / 2f))
+ }
+ }
+}
diff --git a/apps/procraft-android/src/lib.rs b/apps/procraft-android/src/lib.rs
index 2b416cb..d20f1b3 100644
--- a/apps/procraft-android/src/lib.rs
+++ b/apps/procraft-android/src/lib.rs
@@ -793,7 +793,7 @@ impl CanvasSession {
return;
}
// on a model the stroke is drawn on the screen and carried onto it
- if st.scene.is_some() {
+ if st.scene_shown() {
st.scene_stroke_begin(pointer, &samples);
self.shared.frame(st);
return;
@@ -859,7 +859,7 @@ impl CanvasSession {
pub fn stroke_add(&self, samples: Vec<PenSample>) {
let mut guard = self.shared.lock();
let st = &mut *guard;
- if st.scene.is_some() {
+ if st.scene_shown() {
st.scene_stroke_add(&samples, false);
self.shared.frame(st);
return;
@@ -885,7 +885,7 @@ impl CanvasSession {
pub fn stroke_predict(&self, samples: Vec<PenSample>) {
let mut guard = self.shared.lock();
let st = &mut *guard;
- if st.scene.is_some() {
+ if st.scene_shown() {
return;
}
let canvas_samples = st.to_canvas(&samples);
@@ -898,7 +898,7 @@ impl CanvasSession {
pub fn stroke_end(&self, samples: Vec<PenSample>) {
let mut guard = self.shared.lock();
let st = &mut *guard;
- if st.scene.is_some() {
+ if st.scene_shown() {
st.scene_stroke_add(&samples, true);
self.shared.frame(st);
return;
@@ -924,7 +924,7 @@ impl CanvasSession {
pub fn stroke_cancel(&self) {
let mut guard = self.shared.lock();
let st = &mut *guard;
- if st.scene.is_some() {
+ if st.scene_shown() {
st.scene_stroke_cancel();
self.shared.frame(st);
return;
@@ -948,7 +948,7 @@ impl CanvasSession {
let mut st = self.shared.lock();
// the fingers take the canvas from wherever it was settling to
st.view_motion = None;
- if st.scene.is_some() {
+ if st.scene_shown() {
st.scene_gesture(Vec2::new(pan_x, pan_y), scale);
self.shared.frame(&mut st);
return;
diff --git a/apps/procraft-android/src/project.rs b/apps/procraft-android/src/project.rs
index 39d2e00..73e92ee 100644
--- a/apps/procraft-android/src/project.rs
+++ b/apps/procraft-android/src/project.rs
@@ -310,7 +310,8 @@ impl CanvasSession {
let st = &mut *guard;
st.finish_stroke();
st.settle();
- st.save_snapshot()?
+ // a model saves its colour as the artwork's document
+ st.with_colour(crate::State::save_snapshot)?
};
write_artwork(&mut saved, Path::new(&dir), snapshot)
}
@@ -325,7 +326,8 @@ impl CanvasSession {
if !st.idle() {
return Ok(false);
}
- st.save_snapshot()?
+ // a model saves its colour as the artwork's document
+ st.with_colour(crate::State::save_snapshot)?
};
write_artwork(&mut saved, Path::new(&dir), snapshot).map(|()| true)
}
diff --git a/apps/procraft-android/src/render.rs b/apps/procraft-android/src/render.rs
index 347b021..009f269 100644
--- a/apps/procraft-android/src/render.rs
+++ b/apps/procraft-android/src/render.rs
@@ -95,7 +95,7 @@ pub(crate) fn run(shared: Arc<Shared>, window: NativeWindow) {
}
let to_screen = st.view.map(|v| v.to_screen());
// a model is drawn in place of the flat canvas
- if st.scene.is_some() {
+ if st.scene_shown() {
let convert = colours_to(st.doc.profile, screen);
st.render_scene(&view, format, size, convert);
st.frames += 1;
diff --git a/apps/procraft-android/src/scene3d.rs b/apps/procraft-android/src/scene3d.rs
index 3521c22..50ac235 100644
--- a/apps/procraft-android/src/scene3d.rs
+++ b/apps/procraft-android/src/scene3d.rs
@@ -19,18 +19,46 @@ use procraft_engine::mesh::{MAX_TRIANGLES, Mesh, MeshError, read_obj};
use procraft_engine::stroke::StrokeBuilder;
use procraft_engine::usd::{read_usd, read_usdc, read_usdz};
use procraft_engine::view::View;
-use procraft_gpu::{Canvas, Lighting, MeshView, Orbit, StrokeTool, wgpu};
+use procraft_gpu::{Canvas, Light, Lighting, MOST_LIGHTS, MeshView, Orbit, StrokeTool, wgpu};
use crate::project::{THUMBNAIL_SIDE, file_error, pixels_hash, read_png, write_atomically, write_png};
use crate::{CanvasSession, MobileError, PenSample, PointerKind, State, ToolKind};
/// How far a one-finger drag turns the model: this many radians across the window's shorter
-/// side (a small drag's share of a trackball's turn).
-const TURN_PER_SIDE: f32 = 2.93;
+/// side, wherever the finger is and however fast it goes (measured: 0.0122 rad a point on a 744
+/// point side).
+const TURN_PER_SIDE: f32 = 9.08;
+/// How far a drag across the Lighting Studio takes its camera round: radians across the shorter
+/// side.
+const STUDIO_TURN_PER_SIDE: f32 = 2.93;
/// The nearest and farthest the camera comes, in the framing's units (the model's largest side
/// counted as 4).
const NEAREST: f32 = 0.05;
const FARTHEST: f32 = 30.0;
+/// The brightest the model's light can be taken.
+const MOST_EXPOSURE: f32 = 5.0;
+/// Where a light added in the Lighting Studio stands: just above and behind the model's middle
+/// (framing units).
+const NEW_LIGHT: [f32; 3] = [0.8, 2.6, -0.6];
+/// How far a duplicated light stands from the one it copies (framing units).
+const DUPLICATE_OFFSET: [f32; 3] = [1.0, 0.0, 0.0];
+/// How far round a drag across the studio carries its camera (as the model is turned), and the
+/// lowest and highest it looks from (radians up from level).
+const STUDIO_LOWEST: f32 = 0.05;
+const STUDIO_HIGHEST: f32 = 1.5;
+/// A light is touched within this many pixels of its cube.
+const LIGHT_SLACK: f32 = 24.0;
+/// The hue bar's colours (sRGB), evenly spaced from its start to its end.
+const HUE_STOPS: [[f32; 3]; 8] = [
+ [1.0, 0.0, 0.0],
+ [1.0, 110.0 / 255.0, 0.0],
+ [1.0, 220.0 / 255.0, 0.0],
+ [53.0 / 255.0, 1.0, 0.0],
+ [0.0, 1.0, 236.0 / 255.0],
+ [0.0, 52.0 / 255.0, 1.0],
+ [170.0 / 255.0, 0.0, 1.0],
+ [1.0, 0.0, 0.0],
+];
/// The texture sets' side when none is asked for, and the most there can be.
const DEFAULT_TEXTURE_SIZE: u32 = 2048;
const MOST_TEXTURE_SIZE: u32 = 4096;
@@ -52,10 +80,14 @@ impl TextureSet {
}
}
-/// A new texture set's document: `side` square, nothing painted (the part's base colour shows).
+/// A new texture set's document: `side` square, its one layer the Base Layer, nothing painted (the
+/// part's base colour shows).
fn empty_texture(side: u32) -> Document {
let mut doc = Document::new(side, side);
doc.background_visible = false;
+ if let Some(id) = doc.layers().first().map(|l| l.id) {
+ doc.rename(id, "Base Layer");
+ }
doc
}
@@ -102,10 +134,33 @@ pub(crate) struct Scene {
filtered: Vec<Option<(u64, bool)>>,
/// Strokes reach every surface under them, not only the nearest.
paint_through: bool,
+ /// The current texture set is shown flat, as a canvas, in place of the model.
+ flat: bool,
/// An environment to light the model by, waiting for the next frame to be set up.
environment: Option<procraft_engine::environment::Equirect>,
/// The environment's name, kept with the model (none: the app's default).
environment_name: Option<String>,
+ /// How bright the light is taken (1 as it is; 0 to 5).
+ exposure: f32,
+ /// The environment shows behind the model (else the canvas's surround); and whether one has
+ /// been set up yet.
+ environment_shown: bool,
+ lit: bool,
+ /// The lights it is lit by, as the interface sets them.
+ lights: Vec<ModelLight>,
+ /// The Lighting Studio is showing, seen through its own camera (kept while the model is open).
+ studio_open: bool,
+ studio: Option<Orbit>,
+ /// Which of its submeshes are hidden.
+ hidden: Vec<bool>,
+ /// Each part's roughness and metalness documents, once made; the place of the channel painted
+ /// is empty (it is the session's own).
+ channels: Vec<[Option<TextureSet>; 2]>,
+ /// The channel painted on the current part: 0 its colour, 1 roughness, 2 metalness. While it
+ /// isn't the colour, the part's colour document waits in [`Scene::sets`].
+ channel: usize,
+ /// Each part's channel maps as last given to the view (as [`Scene::filtered`]).
+ channel_shown: Vec<[Option<(u64, bool)>; 2]>,
/// A stroke is being carried onto the model each frame; `ending`: once more, then it ends.
pub(crate) stroking: bool,
ending: bool,
@@ -141,8 +196,19 @@ impl Scene {
bounds: IRect::EMPTY,
filtered: Vec::new(),
paint_through: false,
+ flat: false,
environment: None,
environment_name: None,
+ exposure: 1.0,
+ environment_shown: true,
+ lit: false,
+ lights: Light::DEFAULTS.iter().map(ModelLight::from_gpu).collect(),
+ studio_open: false,
+ studio: None,
+ hidden: Vec::new(),
+ channels: Vec::new(),
+ channel: 0,
+ channel_shown: Vec::new(),
stroking: false,
ending: false,
}
@@ -225,12 +291,358 @@ impl CanvasSession {
self.shared.lock().scene.as_ref().and_then(|s| s.environment_name.clone())
}
+ /// How bright the model's light is taken: 1 as it is, 0 to [`MOST_EXPOSURE`].
+ pub fn model_exposure(&self) -> f32 {
+ self.shared.lock().scene.as_ref().map_or(1.0, |s| s.exposure)
+ }
+
+ pub fn set_model_exposure(&self, exposure: f32) {
+ let mut st = self.shared.lock();
+ let Some(scene) = st.scene.as_mut() else { return };
+ if exposure.is_finite() {
+ scene.exposure = exposure.clamp(0.0, MOST_EXPOSURE);
+ }
+ self.shared.frame(&mut st);
+ }
+
+ /// The lights the model is lit by.
+ pub fn model_lights(&self) -> Vec<ModelLight> {
+ self.shared.lock().scene.as_ref().map_or_else(Vec::new, |s| s.lights.clone())
+ }
+
+ /// Lights the model by `lights` (the first [`MOST_LIGHTS`]).
+ pub fn set_model_lights(&self, lights: Vec<ModelLight>) {
+ let mut st = self.shared.lock();
+ let Some(scene) = st.scene.as_mut() else { return };
+ scene.lights = lights.into_iter().take(MOST_LIGHTS).map(ModelLight::sane).collect();
+ self.shared.frame(&mut st);
+ }
+
+ /// Changes light `index` to `light` (its place, colour and intensity).
+ pub fn set_model_light(&self, index: u32, light: ModelLight) {
+ let mut st = self.shared.lock();
+ let Some(slot) = st.scene.as_mut().and_then(|s| s.lights.get_mut(index as usize)) else { return };
+ *slot = light.sane();
+ self.shared.frame(&mut st);
+ }
+
+ /// Adds a white light above and behind the model; false when it has as many as it can take.
+ pub fn add_model_light(&self) -> bool {
+ let mut st = self.shared.lock();
+ let Some(scene) = st.scene.as_mut() else { return false };
+ if scene.lights.len() >= MOST_LIGHTS {
+ return false;
+ }
+ let [x, y, z] = NEW_LIGHT;
+ scene.lights.push(ModelLight { x, y, z, hue: 0.0, saturation: 0.0, intensity: DEFAULT_INTENSITY });
+ self.shared.frame(&mut st);
+ true
+ }
+
+ /// Adds a copy of light `index` beside it; false when there are as many as can be.
+ pub fn duplicate_model_light(&self, index: u32) -> bool {
+ let mut st = self.shared.lock();
+ let Some(scene) = st.scene.as_mut() else { return false };
+ let Some(light) = scene.lights.get(index as usize).copied() else { return false };
+ if scene.lights.len() >= MOST_LIGHTS {
+ return false;
+ }
+ let [dx, dy, dz] = DUPLICATE_OFFSET;
+ scene.lights.push(ModelLight { x: light.x + dx, y: light.y + dy, z: light.z + dz, ..light });
+ self.shared.frame(&mut st);
+ true
+ }
+
+ pub fn delete_model_light(&self, index: u32) {
+ let mut st = self.shared.lock();
+ let Some(scene) = st.scene.as_mut() else { return };
+ if (index as usize) < scene.lights.len() {
+ scene.lights.remove(index as usize);
+ }
+ self.shared.frame(&mut st);
+ }
+
+ /// The channel painted on: 0 colour, 1 roughness, 2 metalness.
+ pub fn model_channel(&self) -> u32 {
+ self.shared.lock().scene.as_ref().map_or(0, |s| u32::try_from(s.channel).unwrap_or(0))
+ }
+
+ /// Paints on `channel` of the current texture set from now on (0 colour, 1 roughness, 2
+ /// metalness): its layers are the colour's layers, each with its own pixels.
+ pub fn set_model_channel(&self, channel: u32) {
+ let mut st = self.shared.lock();
+ st.choose_channel(channel as usize);
+ self.shared.frame(&mut st);
+ }
+
+ /// Layer `id`'s picture in `channel` of the current texture set (as layer thumbnails are),
+ /// for the channel tiles; none until it is drawn.
+ pub fn channel_layer_thumbnail(
+ &self,
+ channel: u32,
+ id: u64,
+ max_side: u32,
+ ) -> Option<crate::layers::ThumbnailImage> {
+ let mut guard = self.shared.lock();
+ let st = &mut *guard;
+ let channel = channel as usize;
+ let current = st.scene.as_ref().map(|s| s.channel)?;
+ if channel == current {
+ drop(guard);
+ return self.layer_thumbnail(id, max_side);
+ }
+ let gpu = st.gpu.as_ref()?;
+ let scene = st.scene.as_mut()?;
+ let set = scene.slot(scene.current, channel)?.as_mut()?;
+ if set.canvas.is_none() {
+ let mut c = Canvas::new(gpu, &set.doc).ok()?;
+ for (lid, pixels) in std::mem::take(&mut set.pending) {
+ let _ = c.upload_layer(gpu, lid, &pixels);
+ }
+ set.canvas = Some(c);
+ }
+ let canvas = set.canvas.as_mut()?;
+ let t = canvas.layer_thumbnail(gpu, &mut set.doc, LayerId(id), max_side).ok()?;
+ Some(crate::layers::ThumbnailImage { width: t.width, height: t.height, rgba: t.rgba })
+ }
+
+ /// The images the model's file brings for its texture sets (a USDZ's packed images): to be
+ /// decoded and given back with [`CanvasSession::set_model_texture`].
+ pub fn model_texture_files(&self) -> Vec<ModelTextureFile> {
+ let st = self.shared.lock();
+ let Some(scene) = st.scene.as_ref() else { return Vec::new() };
+ if scene.source.extension != "usdz" {
+ return Vec::new();
+ }
+ let Some(zip) = procraft_engine::zip::ZipReader::new(&scene.source.bytes) else { return Vec::new() };
+ let mut out = Vec::new();
+ for (set, part) in scene.mesh.parts.iter().enumerate() {
+ let Some(m) = scene.mesh.materials.iter().find(|m| m.name == part.material) else { continue };
+ let maps = [
+ (0, m.color_map.as_deref(), None),
+ (1, m.roughness_map.as_deref(), m.roughness_component),
+ (2, m.metallic_map.as_deref(), m.metallic_component),
+ ];
+ for (channel, map, component) in maps {
+ let Some(bytes) = map.and_then(|f| packed_file(&zip, f)) else { continue };
+ out.push(ModelTextureFile {
+ texture_set: u32::try_from(set).unwrap_or(0),
+ channel,
+ component: component.and_then(|c| i32::try_from(c).ok()).unwrap_or(-1),
+ bytes,
+ });
+ }
+ }
+ out
+ }
+
+ /// Puts `rgba` (premultiplied, the texture size square) into texture set `texture_set`'s
+ /// Base Layer in `channel` (0 colour, 1 roughness, 2 metalness), with no undo step; false if
+ /// it doesn't fit.
+ pub fn set_model_texture(&self, texture_set: u32, channel: u32, rgba: Vec<u8>) -> bool {
+ let mut guard = self.shared.lock();
+ let st = &mut *guard;
+ let (set, channel) = (texture_set as usize, channel as usize);
+ let Some(scene) = st.scene.as_mut() else { return false };
+ let side = scene.texture_size;
+ if channel > 2 || set >= scene.mesh.parts.len() || rgba.len() != (side as usize) * (side as usize) * 4 {
+ return false;
+ }
+ // the colour's layers say which is the Base Layer
+ let colour_is_session = set == scene.current && scene.channel == 0;
+ let structure = if colour_is_session {
+ st.doc.clone()
+ } else {
+ match scene.sets.get(set).and_then(Option::as_ref) {
+ Some(s) => s.doc.clone(),
+ None => return false,
+ }
+ };
+ let Some(base) = structure.layers().first().map(|l| l.id) else { return false };
+ if set == scene.current && channel == scene.channel {
+ match st.canvas.as_mut() {
+ Some(canvas) => canvas.put_layer(base, rgba),
+ None => st.pending_pixels.push((base, rgba)),
+ }
+ } else {
+ let Some(slot) = scene.slot(set, channel) else { return false };
+ let target = slot.get_or_insert_with(|| {
+ let mut made = TextureSet::empty(side);
+ follow_layers(&mut made, &structure);
+ made
+ });
+ match target.canvas.as_mut() {
+ Some(canvas) => canvas.put_layer(base, rgba),
+ None => target.pending.push((base, rgba)),
+ }
+ }
+ self.shared.frame(st);
+ true
+ }
+
+ /// Which of the model's submeshes are hidden, in order.
+ pub fn submeshes_hidden(&self) -> Vec<bool> {
+ let st = self.shared.lock();
+ st.scene.as_ref().map_or_else(Vec::new, |s| {
+ (0..s.mesh.submeshes.len()).map(|i| s.hidden.get(i).copied().unwrap_or(false)).collect()
+ })
+ }
+
+ /// Hides submesh `index` (not drawn or painted), or shows it again.
+ pub fn set_submesh_hidden(&self, index: u32, hidden: bool) {
+ let mut st = self.shared.lock();
+ let Some(scene) = st.scene.as_mut() else { return };
+ let count = scene.mesh.submeshes.len();
+ scene.hidden.resize(count, false);
+ if let Some(h) = scene.hidden.get_mut(index as usize) {
+ *h = hidden;
+ }
+ self.shared.frame(&mut st);
+ }
+
+ /// Submesh `index` on its own, seen as the model is now, filling a `side` square on nothing:
+ /// its row's picture. None until the model has been drawn.
+ pub fn submesh_thumbnail(&self, index: u32, side: u32) -> Option<crate::layers::ThumbnailImage> {
+ let mut guard = self.shared.lock();
+ let st = &mut *guard;
+ let side = side.clamp(8, 1024);
+ let (Some(gpu), Some(view)) = (st.gpu.as_ref(), st.scene.as_mut().and_then(|s| s.view.as_mut())) else {
+ return None;
+ };
+ match view.submesh_picture(gpu, index as usize, side) {
+ Ok(rgba) => Some(crate::layers::ThumbnailImage { width: side, height: side, rgba }),
+ Err(e) => {
+ log::warn!("submesh picture: {e}");
+ None
+ }
+ }
+ }
+
+ /// Shows the Lighting Studio (the model on its floor among its lights) or the model again.
+ pub fn set_lighting_studio(&self, open: bool) {
+ let mut st = self.shared.lock();
+ let Some(scene) = st.scene.as_mut() else { return };
+ scene.studio_open = open;
+ self.shared.frame(&mut st);
+ }
+
+ /// A drag of (`dx`, `dy`) window pixels across the studio: its camera goes round the model
+ /// (across) and up or down (along), the model upright.
+ pub fn studio_turn(&self, dx: f32, dy: f32) {
+ let mut st = self.shared.lock();
+ let side = st.window_size.0.min(st.window_size.1).max(1) as f32;
+ let Some(scene) = st.scene.as_mut() else { return };
+ let Some(view) = scene.view.as_ref() else { return };
+ let orbit = scene.studio.get_or_insert_with(|| view.studio_orbit());
+ let (yaw, pitch) = yaw_pitch(orbit);
+ let yaw = yaw - dx / side * STUDIO_TURN_PER_SIDE;
+ let pitch = (pitch + dy / side * STUDIO_TURN_PER_SIDE).clamp(STUDIO_LOWEST, STUDIO_HIGHEST);
+ orbit.rotation = Orbit::rotation_of(yaw, pitch);
+ self.shared.frame(&mut st);
+ }
+
+ /// Two fingers pinching the studio by `scale`: its camera nearer or farther.
+ pub fn studio_zoom(&self, scale: f32) {
+ let mut st = self.shared.lock();
+ let Some(scene) = st.scene.as_mut() else { return };
+ let unit = Orbit::unit(&scene.mesh);
+ let Some(view) = scene.view.as_ref() else { return };
+ let orbit = scene.studio.get_or_insert_with(|| view.studio_orbit());
+ if scale.is_finite() && scale > 0.0 {
+ orbit.distance = (orbit.distance / scale).clamp(NEAREST * unit * 20.0, FARTHEST * unit * 2.0);
+ }
+ self.shared.frame(&mut st);
+ }
+
+ /// The light whose cube is under window point (`x`, `y`) in the studio, the nearest if more.
+ pub fn model_light_at(&self, x: f32, y: f32) -> Option<u32> {
+ let st = self.shared.lock();
+ let (w, h) = st.window_size;
+ let scene = st.scene.as_ref()?;
+ let view = scene.view.as_ref()?;
+ let orbit = scene.studio.unwrap_or_else(|| view.studio_orbit());
+ let gpu_view = scene.lights_view(view);
+ (0..gpu_view.lights.len())
+ .filter_map(|i| {
+ let [lx, ly, reach] = gpu_view.light_on_screen(&orbit, i, w, h)?;
+ let d = ((lx - x).powi(2) + (ly - y).powi(2)).sqrt();
+ (d <= reach + LIGHT_SLACK).then_some((i, d))
+ })
+ .min_by(|a, b| a.1.total_cmp(&b.1))
+ .and_then(|(i, _)| u32::try_from(i).ok())
+ }
+
+ /// Light `index` in the studio: its middle on the window and how far its cube reaches (pixels).
+ pub fn model_light_on_screen(&self, index: u32) -> Option<Vec<f32>> {
+ let st = self.shared.lock();
+ let (w, h) = st.window_size;
+ let scene = st.scene.as_ref()?;
+ let view = scene.view.as_ref()?;
+ let orbit = scene.studio.unwrap_or_else(|| view.studio_orbit());
+ scene.lights_view(view).light_on_screen(&orbit, index as usize, w, h).map(|p| p.to_vec())
+ }
+
+ /// Drags light `index` by (`dx`, `dy`) window pixels across the studio, at its depth.
+ pub fn move_model_light(&self, index: u32, dx: f32, dy: f32) {
+ let mut st = self.shared.lock();
+ let (w, h) = st.window_size;
+ let Some(scene) = st.scene.as_mut() else { return };
+ let Some(view) = scene.view.as_ref() else { return };
+ let orbit = scene.studio.unwrap_or_else(|| view.studio_orbit());
+ let mut moved = scene.lights_view(view);
+ moved.move_light(&orbit, index as usize, dx, dy, w, h);
+ if let (Some(light), Some(m)) = (scene.lights.get_mut(index as usize), moved.lights.get(index as usize)) {
+ [light.x, light.y, light.z] = m.place;
+ }
+ self.shared.frame(&mut st);
+ }
+
+ /// Whether the environment shows behind the model (else the canvas's surround).
+ pub fn model_environment_shown(&self) -> bool {
+ self.shared.lock().scene.as_ref().is_none_or(|s| s.environment_shown)
+ }
+
+ pub fn set_model_environment_shown(&self, shown: bool) {
+ let mut st = self.shared.lock();
+ let Some(scene) = st.scene.as_mut() else { return };
+ scene.environment_shown = shown;
+ self.shared.frame(&mut st);
+ }
+
pub fn set_paint_through(&self, on: bool) {
if let Some(scene) = self.shared.lock().scene.as_mut() {
scene.paint_through = on;
}
}
+ /// Whether the texture set painted on is shown flat (its 2D texture) in place of the model.
+ pub fn model_flat(&self) -> bool {
+ self.shared.lock().scene.as_ref().is_some_and(|s| s.flat)
+ }
+
+ /// Shows the texture set painted on flat, a canvas drawn and painted on as any other, or the
+ /// model again.
+ pub fn set_model_flat(&self, on: bool) {
+ let mut st = self.shared.lock();
+ let Some(flat) = st.scene.as_ref().map(|s| s.flat) else { return };
+ if flat == on {
+ return;
+ }
+ // the stroke under way ends where it was drawn
+ if flat {
+ st.finish_stroke();
+ } else {
+ st.scene_stroke_cancel();
+ }
+ if let Some(scene) = st.scene.as_mut() {
+ scene.flat = on;
+ }
+ if on {
+ st.view = Some(st.fitted_view());
+ }
+ self.shared.frame(&mut st);
+ }
+
/// One finger dragged by (`dx`, `dy`) window pixels across a model: it turns.
pub fn turn_model(&self, dx: f32, dy: f32) {
let mut st = self.shared.lock();
@@ -247,26 +659,36 @@ impl CanvasSession {
pub fn export_model_obj(&self, path: String) -> Result<(), MobileError> {
let files = self.model_files()?;
let base = &files.base;
- let images: Vec<(String, String)> =
- files.names.iter().map(|n| (n.clone(), format!("{base}_{n}.png"))).collect();
+ let materials: Vec<(String, [String; 3])> =
+ files.names.iter().map(|n| (n.clone(), MODEL_CHANNEL_FILES.map(|c| format!("{n}-{c}.png")))).collect();
let mut zip = procraft_engine::zip::ZipWriter::new();
zip.add(
&format!("{base}.obj"),
procraft_engine::mesh::write_obj(&files.mesh, &format!("{base}.mtl"), &files.names).as_bytes(),
);
- zip.add(&format!("{base}.mtl"), procraft_engine::mesh::write_mtl(&images).as_bytes());
- for ((_, file), png) in images.iter().zip(&files.pngs) {
- zip.add(file, png);
+ zip.add(&format!("{base}.mtl"), procraft_engine::mesh::write_mtl(&materials).as_bytes());
+ for ((_, images), pngs) in materials.iter().zip(&files.pngs) {
+ for (file, png) in images.iter().zip(pngs) {
+ zip.add(file, png);
+ }
}
let bytes = zip.finish();
write_atomically(Path::new(&path), |p| fs::write(p, &bytes).map_err(|e| file_error("exporting the model", e)))
}
/// Writes the model as a USDZ package at `path`: the mesh with a preview-surface material
- /// for each texture set, reading its picture (as for [`CanvasSession::export_model_obj`]).
+ /// for each texture set, reading its images (as for [`CanvasSession::export_model_obj`]).
pub fn export_model_usdz(&self, path: String) -> Result<(), MobileError> {
let files = self.model_files()?;
- let textures: Vec<(String, Vec<u8>)> = files.names.iter().map(|n| format!("{n}.png")).zip(files.pngs).collect();
+ let textures: Vec<[(String, Vec<u8>); 3]> = files
+ .names
+ .iter()
+ .zip(files.pngs)
+ .map(|(n, [color, roughness, metallic])| {
+ let [c, r, m] = MODEL_CHANNEL_FILES.map(|c| format!("{n}-{c}.png"));
+ [(c, color), (r, roughness), (m, metallic)]
+ })
+ .collect();
let bytes = procraft_engine::usd::write_usdz(&files.mesh, &textures);
write_atomically(Path::new(&path), |p| fs::write(p, &bytes).map_err(|e| file_error("exporting the model", e)))
}
@@ -274,17 +696,19 @@ impl CanvasSession {
#[uniffi::export]
impl CanvasSession {
- /// Each texture set's picture as a PNG in folder `dir` (made if missing; as for
- /// [`CanvasSession::export_model_obj`]); returns the files' paths.
+ /// Each texture set's colour, roughness and metalness as PNGs in folder `dir` (made if
+ /// missing; as for [`CanvasSession::export_model_obj`]); returns the files' paths.
pub fn export_model_textures(&self, dir: String) -> Result<Vec<String>, MobileError> {
let files = self.model_files()?;
let dir = Path::new(&dir);
fs::create_dir_all(dir).map_err(|e| file_error("making the folder", e))?;
let mut out = Vec::new();
- for (name, png) in files.names.iter().zip(&files.pngs) {
- let path = dir.join(format!("{}_{name}.png", files.base));
- write_atomically(&path, |p| fs::write(p, png).map_err(|e| file_error("exporting the textures", e)))?;
- out.push(path.to_string_lossy().into_owned());
+ for (name, pngs) in files.names.iter().zip(&files.pngs) {
+ for (channel, png) in SHARED_CHANNEL_NAMES.iter().zip(pngs) {
+ let path = dir.join(format!("{name}-{channel}.png"));
+ write_atomically(&path, |p| fs::write(p, png).map_err(|e| file_error("exporting the textures", e)))?;
+ out.push(path.to_string_lossy().into_owned());
+ }
}
Ok(out)
}
@@ -292,89 +716,202 @@ impl CanvasSession {
/// The model as the window shows it, at the window's size, as a PNG at `path`: on the
/// canvas's surround, or with `transparent`, on nothing.
pub fn export_model_render(&self, path: String, transparent: bool) -> Result<(), MobileError> {
- let (w, h, rgba) = {
- let mut guard = self.shared.lock();
- let st = &mut *guard;
- st.finish_stroke();
- st.settle();
- let (w, h) = st.window_size;
- let (Some(gpu), Some(view)) = (st.gpu.as_ref(), st.scene.as_mut().and_then(|s| s.view.as_mut())) else {
- return Err(file_error("rendering the model", "the model is not shown yet"));
- };
- let s = crate::SURROUND;
- let clear = if transparent { [0.0; 4] } else { [f64::from(s[0]), f64::from(s[1]), f64::from(s[2]), 1.0] };
- let rgba = view.picture(gpu, w, h, clear).map_err(|e| file_error("rendering the model", e))?;
- (w, h, rgba)
- };
+ let (w, h, rgba) = self.model_picture(transparent)?;
let png = crate::project::rgba_png(w, h, &rgba);
write_atomically(Path::new(&path), |p| fs::write(p, &png).map_err(|e| file_error("rendering the model", e)))
}
+
+ /// The model as the window shows it, on the canvas's surround, as a TIFF at `path`.
+ pub fn export_model_tiff(&self, path: String) -> Result<(), MobileError> {
+ let (w, h, rgba) = self.model_picture(false)?;
+ let bytes = procraft_engine::formats::tiff(w, h, 72.0, &[], &rgba);
+ write_atomically(Path::new(&path), |p| fs::write(p, &bytes).map_err(|e| file_error("rendering the model", e)))
+ }
+}
+
+impl CanvasSession {
+ /// The model as the window shows it, at the window's size: its width, height and pixels
+ /// (premultiplied RGBA8), on the canvas's surround or, `transparent`, on nothing.
+ fn model_picture(&self, transparent: bool) -> Result<(u32, u32, Vec<u8>), MobileError> {
+ let mut guard = self.shared.lock();
+ let st = &mut *guard;
+ st.finish_stroke();
+ st.settle();
+ let (w, h) = st.window_size;
+ let (Some(gpu), Some(view)) = (st.gpu.as_ref(), st.scene.as_mut().and_then(|s| s.view.as_mut())) else {
+ return Err(file_error("rendering the model", "the model is not shown yet"));
+ };
+ let s = crate::SURROUND;
+ let clear = if transparent { [0.0; 4] } else { [f64::from(s[0]), f64::from(s[1]), f64::from(s[2]), 1.0] };
+ let rgba = view.picture(gpu, w, h, clear).map_err(|e| file_error("rendering the model", e))?;
+ Ok((w, h, rgba))
+ }
}
/// How far a turntable's swing goes each way (radians).
const SWING: f32 = std::f32::consts::PI / 6.0;
+/// The turntable's camera: this far above level (radians) and back (framing units), the model
+/// about four fifths of the picture's height; at its farthest it is this much further back for
+/// each share of Zoom distance (the model then about 1.78 times smaller at 100 %).
+const TURNTABLE_PITCH: f32 = 0.1;
+const TURNTABLE_DISTANCE: f32 = 6.5;
+const TURNTABLE_PULL: f32 = 0.65;
+
+/// How a model's turntable goes: once round (or swinging each way and back) every `seconds`,
+/// the camera pulled back by up to `zoom` (0 to 1) half way through, the turn eased in and out at
+/// the loop by `ease` (0 to 1), the environment behind it or not.
+#[derive(Clone, Copy, Debug, PartialEq, uniffi::Record)]
+pub struct Turntable {
+ pub swing: bool,
+ pub seconds: f32,
+ pub zoom: f32,
+ pub ease: f32,
+ pub environment: bool,
+}
+
+impl Turntable {
+ /// The camera at `phase` (0 to 1) through the loop.
+ fn orbit(&self, view: &MeshView, phase: f32) -> Orbit {
+ let tau = std::f32::consts::TAU;
+ let p = phase.rem_euclid(1.0);
+ let ease = self.ease.clamp(0.0, 1.0);
+ // slowed to a stop at the loop's ends by the ease
+ let eased = p - ease * (tau * p).sin() / tau;
+ let yaw = if self.swing { SWING * (tau * eased).sin() } else { tau * eased };
+ let pull = (1.0 - (tau * p).cos()) / 2.0;
+ let distance = TURNTABLE_DISTANCE * (1.0 + TURNTABLE_PULL * self.zoom.clamp(0.0, 1.0) * pull);
+ view.upright_orbit(yaw, TURNTABLE_PITCH, distance)
+ }
+}
#[uniffi::export]
impl CanvasSession {
- /// Frame `index` of `count` of the model on a turntable, as the window shows it and at
- /// `width` × `height`: turning once round, or with `swing`, swinging a twelfth of a turn
- /// each way and back. Opaque, each pixel's bytes blue, green, red, alpha (an ARGB integer
- /// each, little-endian), ready for a video frame; none if the model isn't shown yet.
- pub fn model_turntable_frame(
+ /// Frame `index` of `count` of the model's turntable as `turntable` sets it, at `width` ×
+ /// `height`: RGBA8, or with `bgra` opaque blue, green, red, alpha (an ARGB integer each,
+ /// little-endian, ready for a video frame). None if the model isn't shown yet.
+ pub fn model_turntable_picture(
&self,
index: u32,
count: u32,
- swing: bool,
+ turntable: Turntable,
width: u32,
height: u32,
+ bgra: bool,
) -> Option<crate::layers::ThumbnailImage> {
let mut guard = self.shared.lock();
let st = &mut *guard;
+ let (w, h) = (width.max(1), height.max(1));
let (Some(gpu), Some(view)) = (st.gpu.as_ref(), st.scene.as_mut().and_then(|s| s.view.as_mut())) else {
return None;
};
- let phase = index as f32 / count.max(1) as f32 * std::f32::consts::TAU;
- let angle = if swing { SWING * phase.sin() } else { phase };
- let kept = view.orbit;
- view.orbit = kept.spun(angle);
+ let orbit = turntable.orbit(view, index as f32 / count.max(1) as f32);
let s = crate::SURROUND;
let clear = [f64::from(s[0]), f64::from(s[1]), f64::from(s[2]), 1.0];
- let picture = view.picture(gpu, width.max(1), height.max(1), clear);
- view.orbit = kept;
- match picture {
+ match view.picture_from(gpu, orbit, w, h, clear, turntable.environment) {
Ok(mut rgba) => {
- for p in rgba.as_chunks_mut::<4>().0 {
- *p = [p[2], p[1], p[0], 255];
+ if bgra {
+ for p in rgba.as_chunks_mut::<4>().0 {
+ *p = [p[2], p[1], p[0], 255];
+ }
}
- Some(crate::layers::ThumbnailImage { width: width.max(1), height: height.max(1), rgba })
+ Some(crate::layers::ThumbnailImage { width: w, height: h, rgba })
}
Err(e) => {
- log::warn!("turntable frame: {e}");
+ log::warn!("turntable picture: {e}");
None
}
}
}
+
+ /// The model's turntable as a looping GIF at `path`: `fps` frames a second for its
+ /// `turntable.seconds`, `width` × `height`.
+ pub fn export_model_gif(
+ &self,
+ path: String,
+ turntable: Turntable,
+ width: u32,
+ height: u32,
+ fps: f32,
+ ) -> Result<(), MobileError> {
+ let count = turntable_frames(turntable, fps);
+ let delay = (100.0 / fps.clamp(1.0, 100.0)).round() as u16;
+ let mut gif: Option<procraft_engine::formats::GifWriter> = None;
+ for i in 0..count {
+ let t = self
+ .model_turntable_picture(i, count, turntable, width, height, false)
+ .ok_or_else(|| file_error("saving", "the model is not shown yet"))?;
+ gif.get_or_insert_with(|| procraft_engine::formats::GifWriter::new(t.width, t.height)).frame(
+ &t.rgba,
+ delay.max(1),
+ true,
+ );
+ }
+ let bytes = gif.ok_or_else(|| file_error("saving", "there are no frames"))?.finish();
+ write_atomically(Path::new(&path), |f| fs::write(f, &bytes).map_err(|e| file_error("saving", e)))
+ }
+
+ /// The model's turntable as a looping animated PNG at `path` (as for GIF).
+ pub fn export_model_apng(
+ &self,
+ path: String,
+ turntable: Turntable,
+ width: u32,
+ height: u32,
+ fps: f32,
+ ) -> Result<(), MobileError> {
+ let count = turntable_frames(turntable, fps);
+ let delay = (1000.0 / fps.clamp(1.0, 1000.0)).round().clamp(1.0, 65535.0) as u16;
+ let (w, h) = (width.max(1), height.max(1));
+ write_atomically(Path::new(&path), |f| {
+ let out = fs::File::create(f).map_err(|e| file_error("saving", e))?;
+ let mut encoder = png::Encoder::new(std::io::BufWriter::new(out), w, h);
+ encoder.set_color(png::ColorType::Rgba);
+ encoder.set_depth(png::BitDepth::Eight);
+ encoder.set_compression(png::Compression::Fast);
+ encoder.set_animated(count, 0).map_err(|e| file_error("saving", e))?;
+ encoder.set_frame_delay(delay, 1000).map_err(|e| file_error("saving", e))?;
+ let mut writer = encoder.write_header().map_err(|e| file_error("saving", e))?;
+ for i in 0..count {
+ let t = self
+ .model_turntable_picture(i, count, turntable, w, h, false)
+ .ok_or_else(|| file_error("saving", "the model is not shown yet"))?;
+ writer.write_image_data(&t.rgba).map_err(|e| file_error("saving", e))?;
+ }
+ writer.finish().map_err(|e| file_error("saving", e))
+ })
+ }
+}
+
+/// How many frames a turntable takes at `fps`: its seconds' worth, at least one.
+fn turntable_frames(turntable: Turntable, fps: f32) -> u32 {
+ (turntable.seconds.clamp(0.1, 60.0) * fps.clamp(1.0, 120.0)).round().max(1.0) as u32
}
/// What a model's exports are made of: the model, its texture sets' names (safe as file names,
-/// different from each other), their pictures as PNGs over white, and the artwork's name.
+/// different from each other), each set's colour, roughness and metalness as PNGs (over the
+/// part's own, as the model shows them), and the artwork's name.
struct ModelFiles {
mesh: Mesh,
names: Vec<String>,
- pngs: Vec<Vec<u8>>,
+ pngs: Vec<[Vec<u8>; 3]>,
base: String,
}
+/// The channels as a model's exports name their images (`<set>-<channel>.png`), and as the
+/// shared textures name them.
+const MODEL_CHANNEL_FILES: [&str; 3] = ["color", "roughness", "metallic"];
+const SHARED_CHANNEL_NAMES: [&str; 3] = ["Color", "Roughness", "Metallic"];
+
impl CanvasSession {
fn model_files(&self) -> Result<ModelFiles, MobileError> {
- let (mesh, set_names, pictures, base) = {
+ let (mesh, set_names, pictures, side, base) = {
let mut guard = self.shared.lock();
let st = &mut *guard;
st.finish_stroke();
st.settle();
- let pictures = st.texture_pictures()?;
+ let pictures = st.channel_pictures()?;
let Some(scene) = st.scene.as_ref() else { return Err(file_error("exporting the model", "no model")) };
- (scene.mesh.clone(), scene.set_names(), pictures, file_stem(&st.name, "Model"))
+ (scene.mesh.clone(), scene.set_names(), pictures, scene.texture_size, file_stem(&st.name, "Model"))
};
let mut names: Vec<String> = Vec::new();
for name in set_names {
@@ -386,19 +923,55 @@ impl CanvasSession {
}
let pngs = pictures
.into_iter()
- .map(|(side, mut px)| {
- // over white, opaque
- for p in px.as_chunks_mut::<4>().0 {
- let clear = 255 - p[3];
- *p = [p[0].saturating_add(clear), p[1].saturating_add(clear), p[2].saturating_add(clear), 255];
- }
- crate::project::rgba_png(side, side, &px)
+ .enumerate()
+ .map(|(part, [color, roughness, metallic])| {
+ let (under, r, m) = mesh.surface(part);
+ [
+ flattened(side, color, under, false),
+ flattened(side, roughness, [r; 3], true),
+ flattened(side, metallic, [m; 3], true),
+ ]
})
.collect();
Ok(ModelFiles { mesh, names, pngs, base })
}
}
+/// A channel's picture (premultiplied RGBA8, `side` square; none if never painted) over what the
+/// part shows under it (`under`, 0 to 1), opaque, as a PNG: as it is, or as the grey the model
+/// reads from it.
+fn flattened(side: u32, picture: Option<Vec<u8>>, under: [f32; 3], grey: bool) -> Vec<u8> {
+ let under = under.map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u16);
+ let mut px = picture.unwrap_or_else(|| vec![0; (side as usize) * (side as usize) * 4]);
+ let [ur, ug, ub] = under;
+ for p in px.as_chunks_mut::<4>().0 {
+ let [r, g, b, a] = *p;
+ let clear = 255 - u16::from(a);
+ let over = |c: u8, u: u16| (u16::from(c) + (u * clear + 127) / 255).min(255) as u8;
+ let [r, g, b] = [over(r, ur), over(g, ug), over(b, ub)];
+ *p = if grey {
+ // the model's own weights for a painted grey
+ let v = (0.2126 * f32::from(r) + 0.7152 * f32::from(g) + 0.0722 * f32::from(b)).round() as u8;
+ [v, v, v, 255]
+ } else {
+ [r, g, b, 255]
+ };
+ }
+ crate::project::rgba_png(side, side, &px)
+}
+
+/// A file packed in a USDZ by the path its layer gives (relative to the package; `./` and folders
+/// allowed for), else by its name alone.
+fn packed_file(zip: &procraft_engine::zip::ZipReader<'_>, path: &str) -> Option<Vec<u8>> {
+ let path = path.trim_start_matches("./");
+ if let Some(bytes) = zip.read(path) {
+ return Some(bytes);
+ }
+ let name = path.rsplit('/').next().unwrap_or(path);
+ let found = zip.names().find(|n| n.rsplit('/').next() == Some(name))?.to_owned();
+ zip.read(&found)
+}
+
/// A name made safe as part of a file name: letters, digits, `-` and `_` kept, the rest `_`.
fn file_stem(name: &str, otherwise: &str) -> String {
let kept: String =
@@ -407,37 +980,53 @@ fn file_stem(name: &str, otherwise: &str) -> String {
}
impl State {
- /// Each texture set's picture (premultiplied RGBA8, its side), drawn now.
- fn texture_pictures(&mut self) -> Result<Vec<(u32, Vec<u8>)>, MobileError> {
+ /// Each texture set's colour, roughness and metalness as painted (premultiplied RGBA8, the
+ /// texture size square), drawn now: none for a channel never painted.
+ fn channel_pictures(&mut self) -> Result<Vec<[Option<Vec<u8>>; 3]>, MobileError> {
let failed = |e: String| file_error("reading the model's textures", e);
let gpu = self.gpu.as_ref().ok_or_else(|| failed("the model is not shown yet".to_owned()))?;
let Some(scene) = self.scene.as_mut() else { return Ok(Vec::new()) };
- let side = scene.texture_size;
+ let (current, painted) = (scene.current, scene.channel);
let mut out = Vec::new();
- for (i, set) in scene.sets.iter_mut().enumerate() {
- let px = if i == scene.current {
- let canvas = self.canvas.as_mut().ok_or_else(|| failed("the model is not shown yet".to_owned()))?;
- canvas.render(gpu, &mut self.doc, None);
- canvas.read_composite(gpu).map_err(|e| failed(e.to_string()))?
- } else {
- let Some(set) = set else { continue };
- if set.canvas.is_none() {
- let mut c = Canvas::new(gpu, &set.doc).map_err(|e| failed(e.to_string()))?;
- for (id, pixels) in std::mem::take(&mut set.pending) {
- c.upload_layer(gpu, id, &pixels).map_err(|e| failed(e.to_string()))?;
+ for part in 0..scene.mesh.parts.len() {
+ let mut pictures: [Option<Vec<u8>>; 3] = [None, None, None];
+ for (channel, picture) in pictures.iter_mut().enumerate() {
+ // the channel painted on is the session's; the others wait in the scene
+ *picture = if part == current && channel == painted {
+ let canvas = self.canvas.as_mut().ok_or_else(|| failed("the model is not shown yet".to_owned()))?;
+ canvas.render(gpu, &mut self.doc, None);
+ Some(canvas.read_composite(gpu).map_err(|e| failed(e.to_string()))?)
+ } else {
+ match scene.slot(part, channel).and_then(Option::as_mut) {
+ Some(set) => Some(waiting_picture(gpu, set).map_err(failed)?),
+ None => None,
}
- set.canvas = Some(c);
- }
- let Some(canvas) = set.canvas.as_mut() else { continue };
- canvas.render(gpu, &mut set.doc, None);
- canvas.read_composite(gpu).map_err(|e| failed(e.to_string()))?
- };
- out.push((side, px));
+ };
+ }
+ out.push(pictures);
}
Ok(out)
}
}
+/// A waiting texture set's picture, drawn now (its canvas made first if the model hasn't been
+/// drawn since it was opened).
+fn waiting_picture(gpu: &procraft_gpu::Gpu, set: &mut TextureSet) -> Result<Vec<u8>, String> {
+ let canvas = match set.canvas.take() {
+ Some(c) => c,
+ None => {
+ let mut c = Canvas::new(gpu, &set.doc).map_err(|e| e.to_string())?;
+ for (id, pixels) in std::mem::take(&mut set.pending) {
+ c.upload_layer(gpu, id, &pixels).map_err(|e| e.to_string())?;
+ }
+ c
+ }
+ };
+ let canvas = set.canvas.insert(canvas);
+ canvas.render(gpu, &mut set.doc, None);
+ canvas.read_composite(gpu).map_err(|e| e.to_string())
+}
+
/// Why a model can't be painted on, in words.
fn mesh_error_text(e: &MeshError) -> String {
match e {
@@ -466,6 +1055,18 @@ pub(crate) struct SavedModel {
paint_through: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
environment: Option<String>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ exposure: Option<f32>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ environment_shown: Option<bool>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ lights: Option<Vec<ModelLight>>,
+ /// The submeshes hidden, by place.
+ #[serde(default, skip_serializing_if = "Vec::is_empty")]
+ hidden: Vec<usize>,
+ /// Each part's roughness and metalness documents, by part, if made.
+ #[serde(default, skip_serializing_if = "Vec::is_empty")]
+ channels: Vec<[Option<Document>; 2]>,
}
fn obj() -> String {
@@ -491,14 +1092,32 @@ struct SavedOrbit {
pub(crate) struct ModelSnapshot {
pub(crate) saved: SavedModel,
source: Arc<ModelSource>,
- /// The waiting texture sets' layers, by part.
+ /// The waiting texture sets' layers, by part, and the parts' channels' (part, channel 1 or 2).
layers: Vec<(usize, Pixels)>,
+ channel_layers: Vec<(usize, usize, Pixels)>,
/// The model as last shown (premultiplied RGBA8, its size), for the gallery.
pub(crate) picture: Option<(u32, u32, Vec<u8>)>,
}
-/// What the last saves of a model wrote: a hash of each waiting texture set's layers.
-pub(crate) type SavedSets = HashMap<(usize, LayerId), u64>;
+/// What the last saves of a model wrote: a hash of each waiting texture set's layers, by part,
+/// channel (0 its colour) and layer.
+pub(crate) type SavedSets = HashMap<(usize, usize, LayerId), u64>;
+
+/// A waiting texture set's layers' pixels: read from its canvas, or as opened if not drawn yet.
+fn set_pixels(set: &mut TextureSet, gpu: Option<&procraft_gpu::Gpu>) -> Result<Pixels, MobileError> {
+ match (set.canvas.as_mut(), gpu) {
+ (Some(canvas), Some(gpu)) => {
+ canvas.render(gpu, &mut set.doc, None);
+ let mut pixels = Vec::new();
+ for layer in set.doc.layers() {
+ let rgba = canvas.read_layer(gpu, layer.id).map_err(|e| file_error("reading a layer", e))?;
+ pixels.push((layer.id, rgba));
+ }
+ Ok(pixels)
+ }
+ _ => Ok(set.pending.clone()),
+ }
+}
impl State {
/// The model's part of a save, if this is a model.
@@ -511,22 +1130,24 @@ impl State {
docs.push(None);
continue;
};
- let pixels = match (set.canvas.as_mut(), gpu) {
- (Some(canvas), Some(gpu)) => {
- canvas.render(gpu, &mut set.doc, None);
- let mut pixels = Vec::new();
- for layer in set.doc.layers() {
- let rgba = canvas.read_layer(gpu, layer.id).map_err(|e| file_error("reading a layer", e))?;
- pixels.push((layer.id, rgba));
- }
- pixels
- }
- // not drawn yet: what was opened is still what there is
- _ => set.pending.clone(),
- };
+ let pixels = set_pixels(set, gpu)?;
docs.push(Some(set.doc.clone()));
layers.push((i, pixels));
}
+ // the channels (saved with the colour painted on, so none is the session's own)
+ let mut channels = Vec::new();
+ let mut channel_layers = Vec::new();
+ for (part, pair) in scene.channels.iter_mut().enumerate() {
+ let mut docs_of = [None, None];
+ for (k, set) in pair.iter_mut().enumerate() {
+ let Some(set) = set else { continue };
+ channel_layers.push((part, k + 1, set_pixels(set, gpu)?));
+ if let Some(d) = docs_of.get_mut(k) {
+ *d = Some(set.doc.clone());
+ }
+ }
+ channels.push(docs_of);
+ }
let orbit = scene.orbit().map(|o| SavedOrbit {
target: o.target,
yaw: 0.0,
@@ -552,9 +1173,15 @@ impl State {
extension: scene.source.extension.clone(),
paint_through: scene.paint_through,
environment: scene.environment_name.clone(),
+ exposure: (scene.exposure != 1.0).then_some(scene.exposure),
+ environment_shown: (!scene.environment_shown).then_some(false),
+ lights: Some(scene.lights.clone()),
+ hidden: scene.hidden.iter().enumerate().filter(|(_, h)| **h).map(|(i, _)| i).collect(),
+ channels,
},
source: scene.source.clone(),
layers,
+ channel_layers,
picture,
}))
}
@@ -574,17 +1201,22 @@ pub(crate) fn write_model(saved: &mut SavedSets, dir: &Path, model: &ModelSnapsh
write_atomically(&mtl, |p| fs::write(p, text).map_err(|e| file_error("saving the model", e)))?;
}
let side = model.saved.texture_size;
- for (part, pixels) in &model.layers {
- let folder = dir.join("sets").join(part.to_string());
+ let colour = model.layers.iter().map(|(part, pixels)| (*part, 0, pixels));
+ let channels = model.channel_layers.iter().map(|(part, k, pixels)| (*part, *k, pixels));
+ for (part, channel, pixels) in colour.chain(channels) {
+ let mut folder = dir.join("sets").join(part.to_string());
+ if channel > 0 {
+ folder = folder.join(format!("c{channel}"));
+ }
fs::create_dir_all(&folder).map_err(|e| file_error("making the folder", e))?;
for (id, rgba) in pixels {
let hash = pixels_hash(rgba);
let path = folder.join(format!("{}.png", id.0));
- if saved.get(&(*part, *id)) == Some(&hash) && path.exists() {
+ if saved.get(&(part, channel, *id)) == Some(&hash) && path.exists() {
continue;
}
write_atomically(&path, |p| write_png(p, side, side, rgba, &[]))?;
- saved.insert((*part, *id), hash);
+ saved.insert((part, channel, *id), hash);
}
// layer files the set no longer has
if let Ok(entries) = fs::read_dir(&folder) {
@@ -596,10 +1228,34 @@ pub(crate) fn write_model(saved: &mut SavedSets, dir: &Path, model: &ModelSnapsh
}
}
}
- saved.retain(|(part, id), _| model.layers.iter().any(|(p, px)| p == part && px.iter().any(|(i, _)| i == id)));
+ saved.retain(|(part, channel, id), _| {
+ let has = |px: &Pixels| px.iter().any(|(i, _)| i == id);
+ if *channel == 0 {
+ model.layers.iter().any(|(p, px)| p == part && has(px))
+ } else {
+ model.channel_layers.iter().any(|(p, k, px)| p == part && k == channel && has(px))
+ }
+ });
Ok(())
}
+/// The layers of `doc` saved in `folder` (each `<id>.png`, `side` square); a layer with no file is
+/// empty.
+fn read_set_layers(folder: &Path, doc: &Document, side: u32) -> Pixels {
+ let mut pending = Vec::new();
+ for layer in doc.layers() {
+ let path = folder.join(format!("{}.png", layer.id.0));
+ if !path.exists() {
+ continue;
+ }
+ match read_png(&path, side, side) {
+ Ok(rgba) => pending.push((layer.id, rgba)),
+ Err(e) => log::error!("{} layer {:?}: {e}", folder.display(), layer.id),
+ }
+ }
+ pending
+}
+
/// The model saved in the artwork folder `dir`, as `saved` describes it.
pub(crate) fn open_saved_model(dir: &Path, saved: &SavedModel) -> Result<Scene, MobileError> {
let extension = extension_of(&format!("model.{}", saved.extension));
@@ -617,25 +1273,41 @@ pub(crate) fn open_saved_model(dir: &Path, saved: &SavedModel) -> Result<Scene,
if (doc.width(), doc.height()) != (side, side) {
return None;
}
- let folder = dir.join("sets").join(part.to_string());
- let mut pending = Vec::new();
- for layer in doc.layers() {
- let path = folder.join(format!("{}.png", layer.id.0));
- // a layer with no file is empty
- if !path.exists() {
+ let pending = read_set_layers(&dir.join("sets").join(part.to_string()), &doc, side);
+ Some(TextureSet { doc, canvas: None, pending })
+ })
+ .collect();
+ let channels = saved
+ .channels
+ .iter()
+ .enumerate()
+ .map(|(part, pair)| {
+ let mut out: [Option<TextureSet>; 2] = [None, None];
+ for (k, doc) in pair.iter().enumerate() {
+ let Some(doc) =
+ doc.as_ref().map(|d| d.clone().repaired()).filter(|d| (d.width(), d.height()) == (side, side))
+ else {
continue;
- }
- match read_png(&path, side, side) {
- Ok(rgba) => pending.push((layer.id, rgba)),
- Err(e) => log::error!("texture set {part} layer {:?}: {e}", layer.id),
+ };
+ let folder = dir.join("sets").join(part.to_string()).join(format!("c{}", k + 1));
+ let pending = read_set_layers(&folder, &doc, side);
+ if let Some(slot) = out.get_mut(k) {
+ *slot = Some(TextureSet { doc, canvas: None, pending });
}
}
- Some(TextureSet { doc, canvas: None, pending })
+ out
})
.collect();
let mut scene = Scene::new(mesh, ModelSource { bytes, extension, mtl }, side, saved.current, waiting);
scene.paint_through = saved.paint_through;
scene.environment_name = saved.environment.clone();
+ scene.exposure = saved.exposure.filter(|e| e.is_finite()).map_or(1.0, |e| e.clamp(0.0, MOST_EXPOSURE));
+ scene.environment_shown = saved.environment_shown.unwrap_or(true);
+ if let Some(lights) = &saved.lights {
+ scene.lights = lights.iter().take(MOST_LIGHTS).map(|l| l.sane()).collect();
+ }
+ scene.hidden = (0..scene.mesh.submeshes.len()).map(|i| saved.hidden.contains(&i)).collect();
+ scene.channels = channels;
scene.orbit = saved.orbit.map(|o| Orbit {
target: o.target,
rotation: o.rotation.unwrap_or_else(|| Orbit::rotation_of(o.yaw, o.pitch)),
@@ -645,12 +1317,98 @@ pub(crate) fn open_saved_model(dir: &Path, saved: &SavedModel) -> Result<Scene,
Ok(scene)
}
+impl Scene {
+ /// Where part `part`'s `channel` document waits (0 colour, 1 roughness, 2 metalness).
+ fn slot(&mut self, part: usize, channel: usize) -> Option<&mut Option<TextureSet>> {
+ if channel == 0 {
+ return self.sets.get_mut(part);
+ }
+ let count = self.mesh.parts.len();
+ if self.channels.len() < count {
+ self.channels.resize_with(count, || [None, None]);
+ }
+ self.channels.get_mut(part).and_then(|c| c.get_mut(channel - 1))
+ }
+}
+
+/// `set`'s document made to have `structure`'s layers (their ids, order and settings, with its
+/// own pixels): layers new to it start empty, and its undo history goes if the layers changed.
+fn follow_layers(set: &mut TextureSet, structure: &Document) {
+ let ids = |d: &Document| d.layers().iter().map(|l| (l.id, l.kind, l.parent)).collect::<Vec<_>>();
+ let changed = ids(&set.doc) != ids(structure);
+ let had: Vec<LayerId> = set.doc.layers().iter().map(|l| l.id).collect();
+ let side = set.doc.width();
+ let mut doc = Document::from_layers(side, side, structure.layers().to_vec(), structure.current());
+ doc.background_visible = false;
+ set.doc = doc;
+ if !changed {
+ return;
+ }
+ set.pending.retain(|(id, _)| had.contains(id) && structure.layer(*id).is_some());
+ if let Some(canvas) = set.canvas.as_mut() {
+ for l in structure.layers().iter().filter(|l| !had.contains(&l.id)) {
+ canvas.wipe_layer(l.id);
+ }
+ canvas.forget_history();
+ }
+}
+
impl State {
+ /// Paints on `channel` of the current part from now on: its document and canvas become the
+ /// session's, with the layers of the document painted on until now.
+ pub(crate) fn choose_channel(&mut self, channel: usize) {
+ if channel > 2 || self.scene.as_ref().is_none_or(|s| s.channel == channel) {
+ return;
+ }
+ if self.scene_shown() {
+ self.scene_stroke_cancel();
+ } else {
+ self.finish_stroke();
+ }
+ self.settle();
+ let side = self.doc.width();
+ let Some(scene) = self.scene.as_mut() else { return };
+ let (part, from) = (scene.current, scene.channel);
+ let old = TextureSet {
+ doc: std::mem::replace(&mut self.doc, Document::new(1, 1)),
+ canvas: self.canvas.take(),
+ pending: std::mem::take(&mut self.pending_pixels),
+ };
+ let structure = old.doc.clone();
+ if let Some(slot) = scene.slot(part, from) {
+ *slot = Some(old);
+ }
+ let mut new = scene.slot(part, channel).and_then(Option::take).unwrap_or_else(|| TextureSet::empty(side));
+ follow_layers(&mut new, &structure);
+ self.doc = new.doc;
+ self.canvas = new.canvas;
+ self.pending_pixels = new.pending;
+ scene.channel = channel;
+ self.ensure_canvas();
+ }
+
+ /// Runs `f` with the colour of the current part as the session's document (as saves need),
+ /// the channel painted on put back after.
+ pub(crate) fn with_colour<T>(&mut self, f: impl FnOnce(&mut State) -> T) -> T {
+ let channel = self.scene.as_ref().map_or(0, |s| s.channel);
+ self.choose_channel(0);
+ let out = f(self);
+ self.choose_channel(channel);
+ out
+ }
+
+ /// Whether a model is shown (and strokes and gestures go to it), not a flat canvas: a model
+ /// open and not showing its 2D texture.
+ pub(crate) fn scene_shown(&self) -> bool {
+ self.scene.as_ref().is_some_and(|s| !s.flat)
+ }
+
/// Makes texture set `index` the current one: the session's document and canvas.
pub(crate) fn choose_texture_set(&mut self, index: usize) {
if self.scene.as_ref().is_none_or(|s| index == s.current || index >= s.sets.len()) {
return;
}
+ self.choose_channel(0);
self.scene_stroke_cancel();
self.settle();
let Some(scene) = self.scene.as_mut() else { return };
@@ -837,8 +1595,8 @@ impl State {
view.show_environment(gpu, false);
scene.view = Some(view);
}
- // the waiting texture sets' canvases, with the pixels they opened with
- for set in scene.sets.iter_mut().flatten() {
+ // the waiting texture sets' canvases (and channels), with the pixels they opened with
+ for set in scene.sets.iter_mut().flatten().chain(scene.channels.iter_mut().flatten().flatten()) {
if set.canvas.is_none() {
set.canvas = Canvas::new(gpu, &set.doc).ok();
if let Some(c) = set.canvas.as_mut() {
@@ -854,22 +1612,38 @@ impl State {
}
self.carry_stroke();
let (Some(gpu), Some(scene)) = (self.gpu.as_ref(), self.scene.as_mut()) else { return };
- // a new environment: prefiltered now, shown behind the model
+ // a new environment: prefiltered now, shown behind the model unless that's turned off
if let (Some(view), Some(panorama)) = (scene.view.as_mut(), scene.environment.take()) {
view.set_lighting(gpu, Lighting::new(gpu, &panorama));
- view.show_environment(gpu, true);
+ scene.lit = true;
+ }
+ if let Some(view) = scene.view.as_mut() {
+ view.show_environment(gpu, scene.lit && scene.environment_shown);
+ view.set_exposure(scene.exposure);
}
if let Some(canvas) = self.canvas.as_mut() {
canvas.render(gpu, &mut self.doc, None);
}
let Some(view) = scene.view.as_mut() else { return };
view.set_convert(convert);
+ let d = self.display_scale;
+ view.set_grid(Some(procraft_gpu::SurroundGrid {
+ spacing: crate::GRID_SPACING_PT * d,
+ origin: [0.0, crate::GRID_TOP_PT * d],
+ width: d,
+ color: crate::GRID_LINE,
+ }));
let mut encoder = gpu.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("3D view") });
- // each set's texture, its smaller levels made again when its picture has changed
+ // each set's texture, its smaller levels made again when its picture has changed (the
+ // current part's colour is the session's own unless a channel is painted on)
scene.filtered.resize(scene.sets.len(), None);
+ let painting_colour = scene.channel == 0;
for (i, (set, filtered)) in scene.sets.iter().zip(scene.filtered.iter_mut()).enumerate() {
- let canvas =
- if i == scene.current { self.canvas.as_ref() } else { set.as_ref().and_then(|s| s.canvas.as_ref()) };
+ let canvas = if i == scene.current && painting_colour {
+ self.canvas.as_ref()
+ } else {
+ set.as_ref().and_then(|s| s.canvas.as_ref())
+ };
if let Some(c) = canvas
&& *filtered != Some((c.composites(), i == scene.current))
{
@@ -877,15 +1651,41 @@ impl State {
*filtered = Some((c.composites(), i == scene.current));
}
}
+ // each set's roughness and metalness, as painted
+ scene.channel_shown.resize(scene.mesh.parts.len(), [None, None]);
+ for part in 0..scene.mesh.parts.len() {
+ for k in 1..=2usize {
+ let session_own = part == scene.current && scene.channel == k;
+ let canvas = if session_own {
+ self.canvas.as_ref()
+ } else {
+ scene
+ .channels
+ .get(part)
+ .and_then(|c| c.get(k - 1))
+ .and_then(|s| s.as_ref())
+ .and_then(|s| s.canvas.as_ref())
+ };
+ let key = canvas.map(|c| (c.composites(), session_own));
+ let Some(shown) = scene.channel_shown.get_mut(part).and_then(|c| c.get_mut(k - 1)) else { continue };
+ if *shown != key {
+ view.set_channel_view(gpu, part, k, canvas.map(Canvas::composite_view));
+ *shown = key;
+ }
+ }
+ }
+ view.lights = scene.lights.iter().map(|l| l.to_gpu()).collect();
+ for i in 0..scene.mesh.submeshes.len() {
+ view.set_hidden(i, scene.hidden.get(i).copied().unwrap_or(false));
+ }
let s = crate::SURROUND;
- view.render(
- gpu,
- &mut encoder,
- target,
- size.0,
- size.1,
- [f64::from(s[0]), f64::from(s[1]), f64::from(s[2]), 1.0],
- );
+ let clear = [f64::from(s[0]), f64::from(s[1]), f64::from(s[2]), 1.0];
+ if scene.studio_open {
+ let orbit = *scene.studio.get_or_insert_with(|| view.studio_orbit());
+ view.render_studio(gpu, &mut encoder, target, size.0, size.1, clear, orbit);
+ } else {
+ view.render(gpu, &mut encoder, target, size.0, size.1, clear);
+ }
gpu.queue.submit([encoder.finish()]);
}
}
@@ -909,6 +1709,100 @@ fn bounds_of(stamps: &[procraft_engine::stroke::Stamp]) -> IRect {
stamps.iter().fold(IRect::EMPTY, |r, s| r.union(IRect::around(s.center, s.radius * std::f32::consts::SQRT_2)))
}
+/// A light on the model as the interface sets it: its place (in the framing's units from the
+/// model's middle: x right, y up, z toward the front), its hue (a place along the hue bar, 0 to 1),
+/// how much of that colour it has (0 white, 1 the hue's own), and its intensity (0 to 1).
+#[derive(Clone, Copy, Debug, PartialEq, uniffi::Record, serde::Serialize, serde::Deserialize)]
+pub struct ModelLight {
+ pub x: f32,
+ pub y: f32,
+ pub z: f32,
+ pub hue: f32,
+ pub saturation: f32,
+ pub intensity: f32,
+}
+
+/// A light added in the studio shines this strongly.
+const DEFAULT_INTENSITY: f32 = 0.35;
+
+impl ModelLight {
+ fn from_gpu(l: &Light) -> ModelLight {
+ let [x, y, z] = l.place;
+ ModelLight { x, y, z, hue: 0.0, saturation: 0.0, intensity: l.intensity }
+ }
+
+ /// Kept within its ranges, any value that isn't a number reset.
+ fn sane(self) -> ModelLight {
+ let f = |v: f32, lo: f32, hi: f32, or: f32| if v.is_finite() { v.clamp(lo, hi) } else { or };
+ ModelLight {
+ x: f(self.x, -1e4, 1e4, 0.0),
+ y: f(self.y, -1e4, 1e4, 0.0),
+ z: f(self.z, -1e4, 1e4, 0.0),
+ hue: f(self.hue, 0.0, 1.0, 0.0),
+ saturation: f(self.saturation, 0.0, 1.0, 0.0),
+ intensity: f(self.intensity, 0.0, 1.0, DEFAULT_INTENSITY),
+ }
+ }
+
+ /// The light as the renderer takes it: its colour the hue bar's at its hue, mixed with white
+ /// by its saturation, in linear light.
+ fn to_gpu(self) -> Light {
+ let srgb = hue_color(self.hue).map(|c| 1.0 + (c - 1.0) * self.saturation.clamp(0.0, 1.0));
+ Light { place: [self.x, self.y, self.z], color: srgb.map(srgb_to_linear), intensity: self.intensity }
+ }
+}
+
+/// The hue bar's colour (sRGB) at `t` along it (0 to 1).
+fn hue_color(t: f32) -> [f32; 3] {
+ let at = t.clamp(0.0, 1.0) * (HUE_STOPS.len() - 1) as f32;
+ let i = (at.floor() as usize).min(HUE_STOPS.len() - 2);
+ let f = at - i as f32;
+ let (a, b) = (HUE_STOPS.get(i).copied().unwrap_or([1.0; 3]), HUE_STOPS.get(i + 1).copied().unwrap_or([1.0; 3]));
+ [a[0] + (b[0] - a[0]) * f, a[1] + (b[1] - a[1]) * f, a[2] + (b[2] - a[2]) * f]
+}
+
+fn srgb_to_linear(c: f32) -> f32 {
+ if c <= 0.04045 { c / 12.92 } else { ((c + 0.055) / 1.055).powf(2.4) }
+}
+
+/// An orbit's turn about the upright and its tilt up (radians), its camera held level.
+fn yaw_pitch(orbit: &Orbit) -> (f32, f32) {
+ let back = {
+ let e = orbit.eye();
+ let t = orbit.target;
+ let v = [e[0] - t[0], e[1] - t[1], e[2] - t[2]];
+ let l = (v[0] * v[0] + v[1] * v[1] + v[2] * v[2]).sqrt().max(1e-9);
+ [v[0] / l, v[1] / l, v[2] / l]
+ };
+ (back[0].atan2(back[2]), back[1].clamp(-1.0, 1.0).asin())
+}
+
+impl Scene {
+ /// A copy of the view's lights as the scene's lights set them (to project and move them).
+ fn lights_view<'a>(&self, view: &'a MeshView) -> LightsView<'a> {
+ LightsView { view, lights: self.lights.iter().map(|l| l.to_gpu()).collect() }
+ }
+}
+
+/// The view's way of placing lights on the screen, for lights of the scene's own.
+struct LightsView<'a> {
+ view: &'a MeshView,
+ lights: Vec<Light>,
+}
+
+impl LightsView<'_> {
+ fn light_on_screen(&self, orbit: &Orbit, index: usize, width: u32, height: u32) -> Option<[f32; 3]> {
+ let light = self.lights.get(index)?;
+ self.view.place_on_screen(orbit, light.place, width, height)
+ }
+
+ fn move_light(&mut self, orbit: &Orbit, index: usize, dx: f32, dy: f32, width: u32, height: u32) {
+ if let Some(l) = self.lights.get_mut(index) {
+ l.place = self.view.moved_place(orbit, l.place, dx, dy, width, height);
+ }
+ }
+}
+
/// A model and its texture sets, as the interface sees them.
#[derive(Clone, Debug, PartialEq, uniffi::Record)]
pub struct ModelInfo {
@@ -916,6 +1810,27 @@ pub struct ModelInfo {
pub triangles: u32,
/// How much of each set's texture its triangles cover twice (0..1).
pub overlaps: Vec<f32>,
+ /// The model's meshes, each in its texture set, in order.
+ pub submeshes: Vec<SubmeshInfo>,
+ /// Each texture set's side, pixels.
+ pub texture_size: u32,
+}
+
+/// An image a model's file brings for a texture set's channel (0 colour, 1 roughness, 2
+/// metalness): its file's bytes, and which of its components to take (-1: as it is, or its grey).
+#[derive(Clone, Debug, PartialEq, uniffi::Record)]
+pub struct ModelTextureFile {
+ pub texture_set: u32,
+ pub channel: u32,
+ pub component: i32,
+ pub bytes: Vec<u8>,
+}
+
+/// One of a model's meshes: its name and the texture set it is painted in.
+#[derive(Clone, Debug, PartialEq, uniffi::Record)]
+pub struct SubmeshInfo {
+ pub name: String,
+ pub texture_set: u32,
}
impl ModelInfo {
@@ -924,6 +1839,13 @@ impl ModelInfo {
texture_sets: scene.set_names(),
triangles: u32::try_from(scene.mesh.triangles.len()).unwrap_or(u32::MAX),
overlaps: (0..scene.mesh.parts.len()).map(|i| scene.mesh.uv_overlap(i, 256)).collect(),
+ submeshes: scene
+ .mesh
+ .submeshes
+ .iter()
+ .map(|m| SubmeshInfo { name: m.name.clone(), texture_set: u32::try_from(m.part).unwrap_or(0) })
+ .collect(),
+ texture_size: scene.texture_size,
}
}
}
@@ -1018,6 +1940,210 @@ mod tests {
assert!(painted(&s) > 100);
}
+ #[test]
+ fn the_2d_texture_shows_and_paints_flat() {
+ let s = CanvasSession::open_model(CUBE.into(), "cube.obj".into(), None, "Cube".into(), 128).unwrap();
+ if !shown(&s) {
+ return;
+ }
+ frame(&s, 200);
+ assert!(!s.model_flat());
+ s.set_model_flat(true);
+ assert!(s.model_flat() && !s.shared.lock().scene_shown());
+ // the texture fills the window: a stroke across it paints there as on any canvas
+ s.stroke_begin(PointerKind::Stylus, vec![sample(20.0, 20.0, 0.0)]);
+ s.stroke_add((1..20).map(|i| sample(20.0 + i as f32 * 8.0, 20.0, f64::from(i) * 8.0)).collect());
+ s.stroke_end(vec![sample(180.0, 20.0, 170.0)]);
+ let flat = painted(&s);
+ assert!(flat > 100, "{flat}");
+ // the model again: the turn still works and the paint stays
+ s.set_model_flat(false);
+ assert!(!s.model_flat() && s.shared.lock().scene_shown());
+ s.turn_model(50.0, 0.0);
+ frame(&s, 200);
+ assert_eq!(painted(&s), flat);
+ }
+
+ #[test]
+ fn the_studio_adds_moves_and_keeps_lights() {
+ let s = CanvasSession::open_model(CUBE.into(), "cube.obj".into(), None, "Cube".into(), 64).unwrap();
+ // two white lights to begin with; four at most
+ assert_eq!(s.model_lights().len(), 2);
+ assert!(s.model_lights().iter().all(|l| l.saturation == 0.0));
+ assert!(s.add_model_light());
+ assert!(s.duplicate_model_light(0));
+ assert!(!s.add_model_light() && !s.duplicate_model_light(1));
+ assert_eq!(s.model_lights().len(), 4);
+ let copy = s.model_lights()[3];
+ assert_eq!((copy.y, copy.intensity), (s.model_lights()[0].y, s.model_lights()[0].intensity));
+ s.delete_model_light(3);
+ s.delete_model_light(9);
+ assert_eq!(s.model_lights().len(), 3);
+ let red = super::ModelLight { hue: 0.0, saturation: 1.0, intensity: 2.0, ..s.model_lights()[2] };
+ s.set_model_light(2, red);
+ assert_eq!(s.model_lights()[2].intensity, 1.0);
+ if !shown(&s) {
+ return;
+ }
+ frame(&s, 200);
+ s.set_lighting_studio(true);
+ frame(&s, 200);
+ // the new light, picked where its cube shows, dragged across the screen
+ let at = s.model_light_on_screen(2).unwrap();
+ assert_eq!(s.model_light_at(at[0] + 2.0, at[1] - 2.0), Some(2));
+ assert_eq!(s.model_light_at(at[0] + 150.0, at[1] + 150.0), None);
+ s.move_model_light(2, 20.0, 0.0);
+ let moved = s.model_light_on_screen(2).unwrap();
+ assert!((moved[0] - at[0] - 20.0).abs() < 0.5 && (moved[1] - at[1]).abs() < 0.5, "{at:?} -> {moved:?}");
+ // the camera goes round and nearer; the light goes with the view
+ s.studio_turn(40.0, 0.0);
+ s.studio_zoom(1.5);
+ frame(&s, 200);
+ assert_ne!(s.model_light_on_screen(2).unwrap(), moved);
+ s.set_lighting_studio(false);
+ frame(&s, 200);
+ // its one mesh, hidden and kept so
+ let info = s.model_info().unwrap();
+ assert_eq!(info.submeshes, [super::SubmeshInfo { name: "Mesh 1".into(), texture_set: 0 }]);
+ assert!(s.submesh_thumbnail(0, 32).unwrap().rgba.chunks(4).any(|p| p[3] == 255));
+ s.set_submesh_hidden(0, true);
+ assert_eq!(s.submeshes_hidden(), [true]);
+ frame(&s, 200);
+ // kept with the model
+ let dir = std::env::temp_dir().join(format!("procraft-lights-{}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&dir);
+ s.save(dir.to_string_lossy().into_owned()).unwrap();
+ let again = CanvasSession::open(dir.to_string_lossy().into_owned()).unwrap();
+ assert_eq!(again.model_lights(), s.model_lights());
+ assert_eq!(again.submeshes_hidden(), [true]);
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+
+ #[test]
+ fn the_turntable_pulls_back_half_way_and_exports_as_gif_and_png() {
+ let s = CanvasSession::open_model(CUBE.into(), "cube.obj".into(), None, "Cube".into(), 64).unwrap();
+ if !shown(&s) {
+ return;
+ }
+ frame(&s, 200);
+ let turntable = super::Turntable { swing: false, seconds: 1.0, zoom: 1.0, ease: 1.0, environment: false };
+ let covered = |t: super::Turntable, i: u32| {
+ let p = s.model_turntable_picture(i, 8, t, 100, 100, false).unwrap();
+ let s = crate::SURROUND.map(|c| (c * 255.0).round() as i32);
+ p.rgba.chunks(4).filter(|px| (0..3).any(|k| (i32::from(px[k]) - s[k]).abs() > 12)).count()
+ };
+ let (near, far) = (covered(turntable, 0), covered(turntable, 4));
+ assert!(far * 2 < near, "pulled back: {near} -> {far}");
+ let still = super::Turntable { zoom: 0.0, ..turntable };
+ let (a, b) = (covered(still, 0), covered(still, 4));
+ assert!(a.abs_diff(b) * 10 < a, "no pull: {a} vs {b}");
+ let dir = std::env::temp_dir();
+ let gif = dir.join(format!("procraft-turn-{}.gif", std::process::id()));
+ s.export_model_gif(gif.to_string_lossy().into_owned(), turntable, 40, 30, 8.0).unwrap();
+ assert!(std::fs::read(&gif).unwrap().starts_with(b"GIF89a"));
+ let png = dir.join(format!("procraft-turn-{}.png", std::process::id()));
+ s.export_model_apng(png.to_string_lossy().into_owned(), turntable, 40, 30, 8.0).unwrap();
+ assert!(std::fs::read(&png).unwrap().get(1..4) == Some(b"PNG".as_slice()));
+ let _ = (std::fs::remove_file(&gif), std::fs::remove_file(&png));
+ }
+
+ #[test]
+ fn roughness_and_metalness_are_painted_in_their_own_layers() {
+ let s = CanvasSession::open_model(CUBE.into(), "cube.obj".into(), None, "Cube".into(), 64).unwrap();
+ if !shown(&s) {
+ return;
+ }
+ frame(&s, 200);
+ dab(&s);
+ let colour = painted(&s);
+ assert!(colour > 20, "{colour}");
+ // roughness: the same layers, nothing painted in them yet
+ assert_eq!(s.model_channel(), 0);
+ s.set_model_channel(1);
+ assert_eq!(s.model_channel(), 1);
+ assert_eq!(painted(&s), 0);
+ s.stroke_begin(PointerKind::Stylus, vec![sample(70.0, 100.0, 0.0)]);
+ s.stroke_end(vec![sample(130.0, 100.0, 50.0)]);
+ frame(&s, 200);
+ let rough = painted(&s);
+ assert!(rough > colour, "{rough}");
+ // a layer added here is a layer of the colour too
+ let added = s.add_layer();
+ assert!(added != 0);
+ s.set_model_channel(0);
+ assert_eq!(painted(&s), colour);
+ assert!(s.layers().iter().any(|l| l.id == added));
+ // exports take every channel whichever is painted on: the colour and roughness as
+ // painted, the metalness (nothing painted) the plain one
+ s.set_model_channel(2);
+ assert_eq!(painted(&s), 0);
+ let dir = std::env::temp_dir().join(format!("procraft-channels-{}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&dir);
+ let pngs = s.export_model_textures(dir.join("t").to_string_lossy().into_owned()).unwrap();
+ let names: Vec<String> =
+ pngs.iter().filter_map(|p| std::path::Path::new(p).file_name()?.to_str().map(str::to_owned)).collect();
+ let set = super::file_stem(&s.model_info().unwrap().texture_sets[0], "Material");
+ assert_eq!(names, ["Color", "Roughness", "Metallic"].map(|c| format!("{set}-{c}.png")));
+ let read = |p: &str| {
+ let mut reader =
+ png::Decoder::new(std::io::BufReader::new(std::fs::File::open(p).unwrap())).read_info().unwrap();
+ let mut px = vec![0; reader.output_buffer_size().unwrap()];
+ reader.next_frame(&mut px).unwrap();
+ px.chunks(4).map(|p| (p[0], p[3])).collect::<Vec<_>>()
+ };
+ let plain = (255, 255);
+ assert!(read(&pngs[0]).iter().any(|&p| p != plain));
+ assert!(read(&pngs[1]).iter().any(|&p| p != plain));
+ assert!(read(&pngs[2]).iter().all(|&p| p == (0, 255)));
+ // kept with the model, saved while painting metalness
+ s.set_model_channel(1);
+ s.save(dir.join("a").to_string_lossy().into_owned()).unwrap();
+ assert_eq!(s.model_channel(), 1);
+ assert_eq!(painted(&s), rough);
+ assert!(dir.join("a").join("sets").join("0").join("c1").exists());
+ let again = CanvasSession::open(dir.join("a").to_string_lossy().into_owned()).unwrap();
+ assert!(shown(&again));
+ assert_eq!(again.model_channel(), 0);
+ assert_eq!(painted(&again), colour);
+ again.set_model_channel(1);
+ assert_eq!(painted(&again), rough);
+ frame(&again, 200);
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+
+ #[test]
+ fn a_packaged_models_textures_come_in_as_its_base_layers() {
+ let s = CanvasSession::open_model(CUBE.into(), "cube.obj".into(), None, "Cube".into(), 64).unwrap();
+ assert_eq!(s.layers().first().map(|l| l.name.clone()).as_deref(), Some("Base Layer"));
+ assert!(s.model_texture_files().is_empty());
+ if !shown(&s) {
+ return;
+ }
+ frame(&s, 200);
+ dab(&s);
+ let path = std::env::temp_dir().join(format!("procraft-in-{}.usdz", std::process::id()));
+ s.export_model_usdz(path.to_string_lossy().into_owned()).unwrap();
+ // opened again, its textures come back as their images' files: the colour, and the
+ // roughness and metalness from their red
+ let bytes = std::fs::read(&path).unwrap();
+ let again = CanvasSession::open_model(bytes, "cube.usdz".into(), None, "Again".into(), 64).unwrap();
+ let files = again.model_texture_files();
+ let kinds: Vec<(u32, u32, i32)> = files.iter().map(|f| (f.texture_set, f.channel, f.component)).collect();
+ assert_eq!(kinds, [(0, 0, -1), (0, 1, 0), (0, 2, 0)]);
+ // decoded (as the interface does) into the Base Layer
+ let mut reader = png::Decoder::new(std::io::Cursor::new(files[0].bytes.clone())).read_info().unwrap();
+ let mut px = vec![0; reader.output_buffer_size().unwrap()];
+ reader.next_frame(&mut px).unwrap();
+ assert_eq!(px.len(), 64 * 64 * 4);
+ assert!(!again.set_model_texture(0, 0, vec![0; 8]));
+ assert!(again.set_model_texture(0, 0, px));
+ assert!(shown(&again));
+ frame(&again, 200);
+ // the exported picture is opaque (over white): every texel painted
+ assert_eq!(painted(&again), 64 * 64);
+ let _ = std::fs::remove_file(&path);
+ }
+
#[test]
fn two_fingers_bring_the_model_nearer_and_move_it() {
let s = CanvasSession::open_model(CUBE.into(), "cube.obj".into(), None, "Cube".into(), 64).unwrap();
@@ -1075,14 +2201,28 @@ f 1/1 2/2",
let zip = procraft_engine::zip::ZipReader::new(&bytes).unwrap();
let mut names: Vec<&str> = zip.names().collect();
names.sort_unstable();
- assert_eq!(names, ["Two_cubes.mtl", "Two_cubes.obj", "Two_cubes_front.png", "Two_cubes_side_wall.png"]);
+ assert_eq!(
+ names,
+ [
+ "Two_cubes.mtl",
+ "Two_cubes.obj",
+ "front-color.png",
+ "front-metallic.png",
+ "front-roughness.png",
+ "side_wall-color.png",
+ "side_wall-metallic.png",
+ "side_wall-roughness.png"
+ ]
+ );
let obj = String::from_utf8(zip.read("Two_cubes.obj").unwrap()).unwrap();
let mtl = String::from_utf8(zip.read("Two_cubes.mtl").unwrap()).unwrap();
let mesh = procraft_engine::mesh::read_obj(&obj, Some(&mtl)).unwrap();
assert_eq!(mesh.triangles.len(), 12);
- assert_eq!(mesh.materials[1].color_map.as_deref(), Some("Two_cubes_side_wall.png"));
+ assert_eq!(mesh.materials[1].color_map.as_deref(), Some("side_wall-color.png"));
+ assert_eq!(mesh.materials[1].roughness_map.as_deref(), Some("side_wall-roughness.png"));
+ assert_eq!(mesh.materials[1].metallic_map.as_deref(), Some("side_wall-metallic.png"));
// the painted set's picture has the stroke on white
- let png = zip.read("Two_cubes_front.png").unwrap();
+ let png = zip.read("front-color.png").unwrap();
let mut reader = png::Decoder::new(std::io::Cursor::new(png)).read_info().unwrap();
let mut px = vec![0; reader.output_buffer_size().unwrap()];
reader.next_frame(&mut px).unwrap();
@@ -1092,8 +2232,9 @@ f 1/1 2/2",
// the pictures on their own, and the model as shown
let dir = std::env::temp_dir().join(format!("procraft-textures-{}", std::process::id()));
let pngs = s.export_model_textures(dir.to_string_lossy().into_owned()).unwrap();
- assert_eq!(pngs.len(), 2);
+ assert_eq!(pngs.len(), 6);
assert!(pngs.iter().all(|p| std::path::Path::new(p).exists()));
+ assert!(pngs.iter().any(|p| p.ends_with("side_wall-Roughness.png")));
let _ = std::fs::remove_dir_all(&dir);
let render = std::env::temp_dir().join(format!("procraft-render-{}.png", std::process::id()));
s.export_model_render(render.to_string_lossy().into_owned(), true).unwrap();
@@ -1106,19 +2247,37 @@ f 1/1 2/2",
assert_eq!(px[3], 0);
assert_eq!(px[(100 * 200 + 100) * 4 + 3], 255);
let _ = std::fs::remove_file(&render);
- // a turntable's frames: the model turned, half way round the back
- let first = s.model_turntable_frame(0, 4, false, 100, 100).unwrap();
- let half = s.model_turntable_frame(2, 4, false, 100, 100).unwrap();
+ let tiff = std::env::temp_dir().join(format!("procraft-render-{}.tif", std::process::id()));
+ s.export_model_tiff(tiff.to_string_lossy().into_owned()).unwrap();
+ assert!(
+ std::fs::read(&tiff).unwrap().starts_with(b"II*\0") || std::fs::read(&tiff).unwrap().starts_with(b"MM\0*")
+ );
+ let _ = std::fs::remove_file(&tiff);
+ // a turntable's frames: the model turned, half way round the back, and round again
+ let turntable = super::Turntable { swing: false, seconds: 1.0, zoom: 0.0, ease: 0.0, environment: false };
+ let first = s.model_turntable_picture(0, 4, turntable, 100, 100, true).unwrap();
+ let half = s.model_turntable_picture(2, 4, turntable, 100, 100, true).unwrap();
assert_eq!((first.width, first.height, first.rgba.len()), (100, 100, 100 * 100 * 4));
assert!(first.rgba.chunks(4).all(|p| p[3] == 255));
assert_ne!(first.rgba, half.rgba);
- let again = s.model_turntable_frame(4, 4, false, 100, 100).unwrap();
+ let again = s.model_turntable_picture(4, 4, turntable, 100, 100, true).unwrap();
assert_eq!(first.rgba, again.rgba);
- // as a USDZ: the layer and both pictures
+ // as a USDZ: the layer and both sets' images
s.export_model_usdz(path.to_string_lossy().into_owned()).unwrap();
let bytes = std::fs::read(&path).unwrap();
let zip = procraft_engine::zip::ZipReader::new(&bytes).unwrap();
- assert_eq!(zip.names().collect::<Vec<_>>(), ["model.usda", "textures/front.png", "textures/side_wall.png"]);
+ assert_eq!(
+ zip.names().collect::<Vec<_>>(),
+ [
+ "model.usda",
+ "textures/front-color.png",
+ "textures/front-roughness.png",
+ "textures/front-metallic.png",
+ "textures/side_wall-color.png",
+ "textures/side_wall-roughness.png",
+ "textures/side_wall-metallic.png"
+ ]
+ );
let _ = std::fs::remove_file(&path);
}
@@ -1145,6 +2304,24 @@ f 1/1 2/2",
frame(s, 200);
}
+ #[test]
+ fn a_saved_model_exports_once_opened_again() {
+ let s = CanvasSession::open_model(CUBE.into(), "cube.obj".into(), None, "Cube".into(), 64).unwrap();
+ if !shown(&s) {
+ return;
+ }
+ dab(&s);
+ let dir = std::env::temp_dir().join(format!("procraft-reopen-{}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&dir);
+ s.save(dir.join("a").to_string_lossy().into_owned()).unwrap();
+ let again = CanvasSession::open(dir.join("a").to_string_lossy().into_owned()).unwrap();
+ assert!(shown(&again));
+ frame(&again, 200);
+ let zip = dir.join("cube.zip").to_string_lossy().into_owned();
+ again.export_model_obj(zip).unwrap();
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+
#[test]
fn a_model_saves_with_each_texture_set_and_how_it_was_seen() {
// the cube's front, top and back in one material, its bottom and sides in another
@@ -1174,6 +2351,13 @@ f 1/1 2/2",
s.set_paint_through(true);
let sky = procraft_engine::environment::write_hdr(&procraft_engine::environment::Equirect::uniform([0.5; 3]));
assert!(s.set_model_environment("coast".into(), sky));
+ assert_eq!(s.model_exposure(), 1.0);
+ s.set_model_exposure(9.0);
+ assert_eq!(s.model_exposure(), 5.0);
+ s.set_model_exposure(0.4);
+ assert!(s.model_environment_shown());
+ s.set_model_environment_shown(false);
+ frame(&s, 200);
s.save(dir.to_string_lossy().into_owned()).unwrap();
for file in ["model.obj", "thumbnail.png", "artwork.json", "sets/0"] {
assert!(dir.join(file).exists(), "{file}");
@@ -1185,6 +2369,8 @@ f 1/1 2/2",
assert_eq!(painted(&again), side);
assert!(again.paint_through());
assert_eq!(again.model_environment().as_deref(), Some("coast"));
+ assert_eq!(again.model_exposure(), 0.4);
+ assert!(!again.model_environment_shown());
// seen as it was left: the side still faces the camera
let turn = |s: &CanvasSession| s.shared.lock().scene.as_ref().and_then(|sc| sc.orbit()).unwrap().rotation;
assert!(turn(&again).iter().zip(turn(&s)).all(|(a, b)| (a - b).abs() < 1e-6));
diff --git a/crates/engine/src/mesh.rs b/crates/engine/src/mesh.rs
index 16dad3d..f5334a0 100644
--- a/crates/engine/src/mesh.rs
+++ b/crates/engine/src/mesh.rs
@@ -26,6 +26,17 @@ pub struct Part {
pub count: usize,
}
+/// One of the model's meshes (an object or group in an OBJ, a mesh in USD) within a part: its
+/// name and its run of [`Mesh::triangles`], inside the part's. A part painted on is shared by
+/// every submesh in it.
+#[derive(Clone, Debug, PartialEq)]
+pub struct Submesh {
+ pub name: String,
+ pub part: usize,
+ pub first: usize,
+ pub count: usize,
+}
+
/// What a material file says of one material: its colours and the images it maps.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Material {
@@ -40,6 +51,10 @@ pub struct Material {
pub occlusion_map: Option<String>,
pub roughness: Option<f32>,
pub metallic: Option<f32>,
+ /// Which of a packed image's components the roughness and metalness are (0 red to 3 alpha;
+ /// none, its grey).
+ pub roughness_component: Option<usize>,
+ pub metallic_component: Option<usize>,
}
#[derive(Clone, Debug, Default, PartialEq)]
@@ -47,6 +62,9 @@ pub struct Mesh {
pub vertices: Vec<Vertex>,
pub triangles: Vec<[u32; 3]>,
pub parts: Vec<Part>,
+ /// The meshes in each part, in order: each part's triangles are its submeshes' runs, one after
+ /// another.
+ pub submeshes: Vec<Submesh>,
pub materials: Vec<Material>,
/// The material file the model names, if any.
pub material_file: Option<String>,
@@ -67,7 +85,21 @@ pub enum MeshError {
Unreadable,
}
+/// A part's roughness and metalness where its material gives none.
+pub const PLAIN_ROUGHNESS: f32 = 1.0;
+pub const PLAIN_METALLIC: f32 = 0.0;
+
impl Mesh {
+ /// Part `part`'s own surface, under whatever is painted on it: its material's colour (else
+ /// white), roughness and metalness (else plain), each 0 to 1.
+ pub fn surface(&self, part: usize) -> ([f32; 3], f32, f32) {
+ let m = self.parts.get(part).and_then(|p| self.materials.iter().find(|m| m.name == p.material));
+ let color = m.and_then(|m| m.color).map_or([1.0; 3], |c| c.map(|x| x.clamp(0.0, 1.0)));
+ let roughness = m.and_then(|m| m.roughness).unwrap_or(PLAIN_ROUGHNESS).clamp(0.0, 1.0);
+ let metallic = m.and_then(|m| m.metallic).unwrap_or(PLAIN_METALLIC).clamp(0.0, 1.0);
+ (color, roughness, metallic)
+ }
+
/// The smallest box holding the model: its least and most corners.
pub fn bounds(&self) -> Option<([f32; 3], [f32; 3])> {
let mut it = self.vertices.iter().map(|v| v.position);
@@ -135,6 +167,10 @@ pub fn read_obj(obj: &str, mtl: Option<&str>) -> Result<Mesh, MeshError> {
let mut corners: Vec<[usize; 3]> = Vec::new();
let mut faces: Vec<[usize; 3]> = Vec::new(); // into `corners`
let mut parts: Vec<Part> = Vec::new();
+ // each run's object (or group), and the one faces go in now
+ let mut objects: Vec<String> = Vec::new();
+ let mut object = String::new();
+ let mut material = String::new();
let mut material_file = None;
let mut any_face_without_uv = false;
let mut known: HashMap<[usize; 3], usize> = HashMap::new();
@@ -198,7 +234,8 @@ pub fn read_obj(obj: &str, mtl: Option<&str>) -> Result<Mesh, MeshError> {
continue;
}
if parts.is_empty() {
- parts.push(Part { material: String::new(), first: faces.len(), count: 0 });
+ parts.push(Part { material: material.clone(), first: faces.len(), count: 0 });
+ objects.push(object.clone());
}
for k in 1..poly.len() - 1 {
if let (Some(&a), Some(&b), Some(&c)) = (poly.first(), poly.get(k), poly.get(k + 1)) {
@@ -210,9 +247,15 @@ pub fn read_obj(obj: &str, mtl: Option<&str>) -> Result<Mesh, MeshError> {
}
}
"usemtl" => {
- let name = words.collect::<Vec<_>>().join(" ");
+ material = words.collect::<Vec<_>>().join(" ");
// a new run, or back into a material already used (kept apart: parts are runs)
- parts.push(Part { material: name, first: faces.len(), count: 0 });
+ parts.push(Part { material: material.clone(), first: faces.len(), count: 0 });
+ objects.push(object.clone());
+ }
+ "o" | "g" => {
+ object = words.collect::<Vec<_>>().join(" ");
+ parts.push(Part { material: material.clone(), first: faces.len(), count: 0 });
+ objects.push(object.clone());
}
"mtllib" => material_file = Some(words.collect::<Vec<_>>().join(" ")),
_ => {}
@@ -222,13 +265,13 @@ pub fn read_obj(obj: &str, mtl: Option<&str>) -> Result<Mesh, MeshError> {
return Err(MeshError::NoTextureCoordinates);
}
let materials = mtl.map(read_mtl).unwrap_or_default();
- let gathered = Gathered { positions, uvs, normals, corners, faces, parts };
+ let gathered = Gathered { positions, uvs, normals, corners, faces, parts, objects };
gathered.into_mesh(materials, material_file)
}
/// A model's faces as a file gives them, before they are made a [`Mesh`]: positions, texture
/// coordinates and normals; corners, each an index into those three (`usize::MAX` for none);
-/// triangles of corners; and runs of triangles by material.
+/// triangles of corners; runs of triangles by material, and each run's object (its mesh's name).
pub(crate) struct Gathered {
pub(crate) positions: Vec<[f32; 3]>,
pub(crate) uvs: Vec<[f32; 2]>,
@@ -236,13 +279,15 @@ pub(crate) struct Gathered {
pub(crate) corners: Vec<[usize; 3]>,
pub(crate) faces: Vec<[usize; 3]>,
pub(crate) parts: Vec<Part>,
+ pub(crate) objects: Vec<String>,
}
impl Gathered {
- /// The model: triangles grouped by material in the order materials first appear, normals
- /// the file lacks made smooth from the faces around each position.
+ /// The model: triangles grouped by material in the order materials first appear, and within
+ /// a material by object, normals the file lacks made smooth from the faces around each
+ /// position.
pub(crate) fn into_mesh(self, materials: Vec<Material>, material_file: Option<String>) -> Result<Mesh, MeshError> {
- let Gathered { positions, uvs, normals, corners, faces, parts } = self;
+ let Gathered { positions, uvs, normals, corners, faces, parts, objects } = self;
if faces.is_empty() {
return Err(MeshError::NoFaces);
}
@@ -252,12 +297,14 @@ impl Gathered {
if uvs.is_empty() || corners.iter().any(|c| c[1] == usize::MAX) {
return Err(MeshError::NoTextureCoordinates);
}
- // the runs' sizes, empty ones dropped, runs of one material joined
+ // the runs' sizes and objects, empty ones dropped
let mut counted: Vec<Part> = Vec::new();
+ let mut counted_objects: Vec<&str> = Vec::new();
for (i, p) in parts.iter().enumerate() {
let end = parts.get(i + 1).map_or(faces.len(), |q| q.first);
if end > p.first {
counted.push(Part { material: p.material.clone(), first: p.first, count: end - p.first });
+ counted_objects.push(objects.get(i).map_or("", String::as_str));
}
}
// faces grouped by material, in the order materials first appear
@@ -269,10 +316,31 @@ impl Gathered {
}
let mut triangles_by_corner: Vec<[usize; 3]> = Vec::with_capacity(faces.len());
let mut grouped: Vec<Part> = Vec::new();
- for m in &order {
+ let mut submeshes: Vec<Submesh> = Vec::new();
+ for (index, m) in order.iter().enumerate() {
let first = triangles_by_corner.len();
- for p in counted.iter().filter(|p| &p.material == m) {
- triangles_by_corner.extend(faces.get(p.first..p.first + p.count).unwrap_or(&[]));
+ // the material's objects in the order they first appear in it, each one's runs together
+ let runs: Vec<(&Part, &str)> =
+ counted.iter().zip(counted_objects.iter().copied()).filter(|(p, _)| &p.material == m).collect();
+ let mut names: Vec<&str> = Vec::new();
+ for (_, o) in &runs {
+ if !names.contains(o) {
+ names.push(o);
+ }
+ }
+ for name in names {
+ let start = triangles_by_corner.len();
+ for (p, _) in runs.iter().filter(|(_, o)| *o == name) {
+ triangles_by_corner.extend(faces.get(p.first..p.first + p.count).unwrap_or(&[]));
+ }
+ let name = if !name.is_empty() {
+ name.to_owned()
+ } else if !m.is_empty() {
+ m.clone()
+ } else {
+ format!("Mesh {}", submeshes.len() + 1)
+ };
+ submeshes.push(Submesh { name, part: index, first: start, count: triangles_by_corner.len() - start });
}
grouped.push(Part { material: m.clone(), first, count: triangles_by_corner.len() - first });
}
@@ -302,7 +370,7 @@ impl Gathered {
})
.collect();
let triangles = triangles_by_corner.iter().map(|f| f.map(|c| c as u32)).collect();
- Ok(Mesh { vertices, triangles, parts: grouped, materials, material_file })
+ Ok(Mesh { vertices, triangles, parts: grouped, submeshes, materials, material_file })
}
}
@@ -334,20 +402,29 @@ pub fn write_obj(mesh: &Mesh, mtl: &str, names: &[String]) -> String {
out
}
-/// A material file for [`write_obj`]: each material white, its colour the image beside it.
-pub fn write_mtl(materials: &[(String, String)]) -> String {
+/// A material file for [`write_obj`]: each material (its name, and the images beside it for its
+/// colour, roughness and metalness) white and physically shaded, its properties indented.
+pub fn write_mtl(materials: &[(String, [String; 3])]) -> String {
use std::fmt::Write as _;
let mut out = String::new();
- for (name, image) in materials {
+ for (name, [color, roughness, metallic]) in materials {
let _ = write!(
out,
"newmtl {name}
-Ka 0 0 0
-Kd 1 1 1
-Ks 0 0 0
-d 1
-illum 1
-map_Kd {image}
+\tmap_Kd {color}
+\tKd 1 1 1
+\tKa 0 0 0
+\tKs 0
+\tao 0
+\tsubsurface 0
+\tmap_metallic {metallic}
+\tspecularTint 0
+\tmap_roughness {roughness}
+\tanisotropicRotation 0
+\tsheen 0.05
+\tsheenTint 0
+\tclearCoat 0
+\tclearCoatGloss 0
"
);
@@ -382,9 +459,9 @@ pub fn read_mtl(mtl: &str) -> Vec<Material> {
"Pr" => m.roughness = number(),
"Pm" => m.metallic = number(),
"map_Kd" => m.color_map = file(),
- "map_Pr" => m.roughness_map = file(),
- "map_Pm" => m.metallic_map = file(),
- "map_Bump" | "map_bump" | "bump" | "norm" | "map_Kn" => m.normal_map = file(),
+ "map_Pr" | "map_roughness" => m.roughness_map = file(),
+ "map_Pm" | "map_metallic" => m.metallic_map = file(),
+ "map_Bump" | "map_bump" | "bump" | "norm" | "map_Kn" | "map_tangentSpaceNormal" => m.normal_map = file(),
"map_Ka" | "map_ao" | "map_AO" => m.occlusion_map = file(),
_ => {}
}
@@ -440,7 +517,8 @@ f -8/1 -4/2 -1/3 -5/4
let mesh = read_obj(CUBE, None).unwrap();
let names: Vec<String> = mesh.parts.iter().map(|p| p.material.clone()).collect();
let text = write_obj(&mesh, "cube.mtl", &names);
- let again = read_obj(&text, Some(&write_mtl(&[("body".into(), "body.png".into())]))).unwrap();
+ let images = ["body-color.png".into(), "body-roughness.png".into(), "body-metallic.png".into()];
+ let again = read_obj(&text, Some(&write_mtl(&[("body".into(), images)]))).unwrap();
assert_eq!(again.triangles.len(), mesh.triangles.len());
assert_eq!(again.parts, mesh.parts);
assert_eq!(again.vertices.len(), mesh.vertices.len());
@@ -453,7 +531,22 @@ f -8/1 -4/2 -1/3 -5/4
}
}
assert_eq!(again.material_file.as_deref(), Some("cube.mtl"));
- assert_eq!(again.materials[0].color_map.as_deref(), Some("body.png"));
+ let m = &again.materials[0];
+ assert_eq!(m.color_map.as_deref(), Some("body-color.png"));
+ assert_eq!(m.roughness_map.as_deref(), Some("body-roughness.png"));
+ assert_eq!(m.metallic_map.as_deref(), Some("body-metallic.png"));
+ assert_eq!(m.color, Some([1.0; 3]));
+ }
+
+ #[test]
+ fn a_parts_surface_is_its_materials_else_plain() {
+ let mut mesh = read_obj(CUBE, Some("newmtl body\nKd 1 0.5 2\nPr 0.25\n")).unwrap();
+ assert_eq!(mesh.surface(0), ([1.0, 0.5, 1.0], 0.25, PLAIN_METALLIC));
+ // a part without a material of its own, and one that isn't there
+ assert_eq!(mesh.surface(1), ([1.0; 3], PLAIN_ROUGHNESS, PLAIN_METALLIC));
+ assert_eq!(mesh.surface(9), ([1.0; 3], PLAIN_ROUGHNESS, PLAIN_METALLIC));
+ mesh.materials.clear();
+ assert_eq!(mesh.surface(0).0, [1.0; 3]);
}
#[test]
@@ -488,6 +581,55 @@ f -8/1 -4/2 -1/3 -5/4
}
}
+ #[test]
+ fn objects_are_submeshes_within_their_materials() {
+ // a lid and its tab sharing one material, the can in another; more lid at the end
+ let obj = "v 0 0 0
+v 1 0 0
+v 1 1 0
+vt 0 0
+vt 1 0
+vt 1 1
+o Lid
+usemtl lid
+f 1/1 2/2 3/3
+o Tab
+usemtl lid
+f 1/1 2/2 3/3
+f 1/1 2/2 3/3
+o Can
+usemtl can
+f 1/1 2/2 3/3
+o Lid
+usemtl lid
+f 1/1 2/2 3/3
+";
+ let mesh = read_obj(obj, None).unwrap();
+ let parts: Vec<(&str, usize, usize)> =
+ mesh.parts.iter().map(|p| (p.material.as_str(), p.first, p.count)).collect();
+ assert_eq!(parts, [("lid", 0, 4), ("can", 4, 1)]);
+ let subs: Vec<(&str, usize, usize, usize)> =
+ mesh.submeshes.iter().map(|m| (m.name.as_str(), m.part, m.first, m.count)).collect();
+ assert_eq!(subs, [("Lid", 0, 0, 2), ("Tab", 0, 2, 2), ("Can", 1, 4, 1)]);
+ // without objects, each material is one, named after it
+ let plain = read_obj(
+ "v 0 0 0
+v 1 0 0
+v 1 1 0
+vt 0 0
+vt 1 0
+vt 1 1
+f 1/1 2/2 3/3
+usemtl red
+f 1/1 2/2 3/3
+",
+ None,
+ )
+ .unwrap();
+ let names: Vec<&str> = plain.submeshes.iter().map(|m| m.name.as_str()).collect();
+ assert_eq!(names, ["Mesh 1", "red"]);
+ }
+
#[test]
fn models_without_faces_or_texture_coordinates_are_refused() {
assert_eq!(read_obj("v 0 0 0\nv 1 0 0\nv 0 1 0\n", None), Err(MeshError::NoFaces));
diff --git a/crates/engine/src/usd.rs b/crates/engine/src/usd.rs
index fd77a8b..7b6e5d9 100644
--- a/crates/engine/src/usd.rs
+++ b/crates/engine/src/usd.rs
@@ -12,7 +12,7 @@
use std::collections::HashMap;
use crate::applz4::raw_block;
-use crate::mesh::{Gathered, MAX_TRIANGLES, Mesh, MeshError, Part};
+use crate::mesh::{Gathered, MAX_TRIANGLES, Material, Mesh, MeshError, Part};
use crate::zip::{ZipReader, ZipWriter};
/// Reads a model from a `.usdz` package: its first layer (binary or text).
@@ -38,13 +38,13 @@ pub fn read_usdc(bytes: &[u8]) -> Result<Mesh, MeshError> {
}
/// Writes the model as a USDZ package: a text layer (`model.usda`) with the mesh, a subset of its
-/// faces for each part bound to a preview-surface material, and each part's image beside it
-/// (`textures`, by part: a file name and its PNG).
-pub fn write_usdz(mesh: &Mesh, textures: &[(String, Vec<u8>)]) -> Vec<u8> {
- let layer = write_usda(mesh, textures.iter().map(|(name, _)| name.as_str()));
+/// faces for each part bound to a preview-surface material, and each part's images beside it
+/// (`textures`, by part: its colour, roughness and metalness, each a file name and its PNG).
+pub fn write_usdz(mesh: &Mesh, textures: &[[(String, Vec<u8>); 3]]) -> Vec<u8> {
+ let layer = write_usda(mesh, textures.iter().map(|t| t.each_ref().map(|(name, _)| name.as_str())));
let mut zip = ZipWriter::new();
zip.add_aligned("model.usda", layer.as_bytes(), 64);
- for (name, png) in textures {
+ for (name, png) in textures.iter().flatten() {
zip.add_aligned(&format!("textures/{name}"), png, 64);
}
zip.finish()
@@ -63,8 +63,9 @@ fn identifier(name: &str, otherwise: &str) -> String {
}
/// The text layer of [`write_usdz`]: the mesh's corners as its points (each with its normal and
-/// texture coordinate), its triangles, and per part a subset and a material reading `textures`.
-fn write_usda<'a>(mesh: &Mesh, textures: impl Iterator<Item = &'a str>) -> String {
+/// texture coordinate), its triangles, and per part a subset and a material reading `textures`
+/// (its colour, roughness and metalness images).
+fn write_usda<'a>(mesh: &Mesh, textures: impl Iterator<Item = [&'a str; 3]>) -> String {
let mut names: Vec<String> = Vec::new();
for (i, part) in mesh.parts.iter().enumerate() {
let mut n = identifier(&part.material, "Material");
@@ -123,7 +124,7 @@ fn write_usda<'a>(mesh: &Mesh, textures: impl Iterator<Item = &'a str>) -> Strin
line(0, String::new());
line(1, "def Scope \"Materials\"".into());
line(1, "{".into());
- for (name, file) in names.iter().zip(textures) {
+ for (name, [color, roughness, metallic]) in names.iter().zip(textures) {
let m = format!("/Model/Materials/{name}");
line(2, format!("def Material \"{name}\""));
line(2, "{".into());
@@ -133,17 +134,28 @@ fn write_usda<'a>(mesh: &Mesh, textures: impl Iterator<Item = &'a str>) -> Strin
line(3, "{".into());
line(4, "uniform token info:id = \"UsdPreviewSurface\"".into());
line(4, format!("color3f inputs:diffuseColor.connect = <{m}/Texture.outputs:rgb>"));
- line(4, "float inputs:roughness = 0.5".into());
+ line(4, format!("float inputs:roughness.connect = <{m}/Roughness.outputs:r>"));
+ line(4, format!("float inputs:metallic.connect = <{m}/Metallic.outputs:r>"));
line(4, "token outputs:surface".into());
line(3, "}".into());
- line(0, String::new());
- line(3, "def Shader \"Texture\"".into());
- line(3, "{".into());
- line(4, "uniform token info:id = \"UsdUVTexture\"".into());
- line(4, format!("asset inputs:file = @textures/{file}@"));
- line(4, format!("float2 inputs:st.connect = <{m}/Coordinates.outputs:result>"));
- line(4, "float3 outputs:rgb".into());
- line(3, "}".into());
+ // the colour as it is, the greys as numbers (their red)
+ for (shader, file, output) in [
+ ("Texture", color, "float3 outputs:rgb"),
+ ("Roughness", roughness, "float outputs:r"),
+ ("Metallic", metallic, "float outputs:r"),
+ ] {
+ line(0, String::new());
+ line(3, format!("def Shader \"{shader}\""));
+ line(3, "{".into());
+ line(4, "uniform token info:id = \"UsdUVTexture\"".into());
+ line(4, format!("asset inputs:file = @textures/{file}@"));
+ if shader != "Texture" {
+ line(4, "token inputs:sourceColorSpace = \"raw\"".into());
+ }
+ line(4, format!("float2 inputs:st.connect = <{m}/Coordinates.outputs:result>"));
+ line(4, output.into());
+ line(3, "}".into());
+ }
line(0, String::new());
line(3, "def Shader \"Coordinates\"".into());
line(3, "{".into());
@@ -268,6 +280,7 @@ mod kind {
pub const FLOAT: u8 = 8;
pub const DOUBLE: u8 = 9;
pub const TOKEN: u8 = 11;
+ pub const ASSET: u8 = 12;
pub const MATRIX4D: u8 = 15;
pub const QUATD: u8 = 16;
pub const QUATF: u8 = 17;
@@ -780,6 +793,8 @@ impl Crate<'_> {
fn value(&self, rep: Rep) -> Option<Value> {
match (rep.kind(), rep.is_array()) {
(kind::TOKEN, false) => self.token_value(rep).map(|t| Value::Token(t.to_owned())),
+ // an asset's path is kept as a token
+ (kind::ASSET, false) if rep.is_inlined() => self.token(rep.payload()).map(|t| Value::Token(t.to_owned())),
(kind::TOKEN, true) | (kind::TOKEN_VECTOR, _) => self.token_list(rep).map(Value::Tokens),
(kind::INT | kind::UINT, true) => self.ints(rep).map(Value::Ints),
(kind::VEC2F | kind::VEC2D | kind::VEC2H, true) => self.vectors::<2>(rep).map(Value::Vec2s),
@@ -923,18 +938,63 @@ impl<'a> Stage<'a> {
}
}
- /// The name of the material a prim's `material:binding` points at.
+ /// The path of the material a prim's `material:binding` points at.
fn material(&self, prim: &str) -> Option<String> {
- let target = self.field(&format!("{prim}.material:binding"), "targetPaths").and_then(Value::path)?;
- target.rsplit('/').next().map(str::to_owned)
+ self.field(&format!("{prim}.material:binding"), "targetPaths").and_then(Value::path)
+ }
+
+ /// What an attribute is connected to (a prim's output), if anything.
+ fn connection(&self, attribute: &str) -> Option<String> {
+ self.field(attribute, "connectionPaths").and_then(Value::path)
+ }
+
+ /// The material at `path` as its preview surface describes it: its colour or colour image,
+ /// and its roughness and metalness or their images (and the component of each image read).
+ fn material_info(&self, path: &str) -> Material {
+ let name = path.rsplit('/').next().unwrap_or(path).to_owned();
+ let mut m = Material { name, ..Material::default() };
+ let Some(surface) = self.connection(&format!("{path}.outputs:surface")) else { return m };
+ let shader = prim_of(&surface);
+ let input = |n: &str| format!("{shader}.inputs:{n}");
+ match self.connection(&input("diffuseColor")).and_then(|t| self.texture_file(&t)) {
+ Some((file, _)) => m.color_map = Some(file),
+ None => {
+ m.color =
+ self.value(shader, "inputs:diffuseColor").and_then(|v| v.vector3()).map(|c| c.map(|x| x as f32));
+ }
+ }
+ match self.connection(&input("roughness")).and_then(|t| self.texture_file(&t)) {
+ Some((file, component)) => (m.roughness_map, m.roughness_component) = (Some(file), component),
+ None => m.roughness = self.value(shader, "inputs:roughness").and_then(|v| v.number()).map(|v| v as f32),
+ }
+ match self.connection(&input("metallic")).and_then(|t| self.texture_file(&t)) {
+ Some((file, component)) => (m.metallic_map, m.metallic_component) = (Some(file), component),
+ None => m.metallic = self.value(shader, "inputs:metallic").and_then(|v| v.number()).map(|v| v as f32),
+ }
+ m
+ }
+
+ /// The image a texture's output (`<texture>.outputs:<rgb, r, g, b or a>`) reads, and which
+ /// component of it (none: all three).
+ fn texture_file(&self, output: &str) -> Option<(String, Option<usize>)> {
+ let file = self.value(prim_of(output), "inputs:file").and_then(|v| v.token().map(str::to_owned))?;
+ let component = match output.rsplit(':').next() {
+ Some("r") => Some(0),
+ Some("g") => Some(1),
+ Some("b") => Some(2),
+ Some("a") => Some(3),
+ _ => None,
+ };
+ Some((file, component))
}
- /// The stage's meshes as one model.
+ /// The stage's meshes as one model, with the materials they are bound to.
fn model(&self) -> Result<Mesh, MeshError> {
let mut out = Gather::default();
let z_up = self.token("/", "upAxis").as_deref() == Some("Z");
self.walk("/", IDENTITY, &mut |path, world| self.add_mesh(path, world, z_up, &mut out));
- out.into_gathered().into_mesh(Vec::new(), None)
+ let materials = out.material_paths.iter().map(|p| self.material_info(p)).collect();
+ out.into_gathered().into_mesh(materials, None)
}
/// Adds the mesh at `path` (if it is one) to `out`, placed by `world`.
@@ -946,8 +1006,15 @@ impl<'a> Stage<'a> {
let Some(counts) = self.value(path, "faceVertexCounts").and_then(Value::ints) else { return };
let Some(indices) = self.value(path, "faceVertexIndices").and_then(Value::ints) else { return };
let uv = self.texture_coordinates(path);
- // each face's material: its subset's, else the mesh's own
- let own = self.material(path).unwrap_or_default();
+ // each face's material: its subset's, else the mesh's own (by name; its path kept)
+ let mut name_of = |p: Option<String>| -> String {
+ let Some(p) = p else { return String::new() };
+ if !out.material_paths.contains(&p) {
+ out.material_paths.push(p.clone());
+ }
+ p.rsplit('/').next().unwrap_or(&p).to_owned()
+ };
+ let own = name_of(self.material(path));
let mut face_material: Vec<Option<String>> = vec![None; counts.len()];
for child in self.children(path) {
if self.type_name(&child).as_deref() != Some("GeomSubset") {
@@ -957,13 +1024,15 @@ impl<'a> Stage<'a> {
if family.is_some_and(|fam| fam != "materialBind") {
continue;
}
- let Some(name) = self.material(&child) else { continue };
+ let Some(name) = self.material(&child).map(|p| name_of(Some(p))) else { continue };
for face in self.value(&child, "indices").and_then(Value::ints).unwrap_or_default() {
if let Some(slot) = usize::try_from(face).ok().and_then(|i| face_material.get_mut(i)) {
*slot = Some(name.clone());
}
}
}
+ // its faces are a submesh named as the mesh is
+ out.object = path.rsplit('/').next().unwrap_or(path).to_owned();
let base = out.positions.len();
for p in points {
let mut q = apply(world, p);
@@ -1369,8 +1438,22 @@ impl TextReader {
value = Some(self.raw(0)?);
}
let meta = if self.is('(') { self.metadata()? } else { HashMap::new() };
- // connections and samples over time are not values of their own
- if name.ends_with(".connect") || name.ends_with(".timeSamples") {
+ // a connection is kept as what it connects to; samples over time are not read
+ if let Some(base) = name.strip_suffix(".connect") {
+ let target = match value {
+ Some(Raw::Path(p)) => Some(p),
+ Some(Raw::List(items)) => {
+ items.into_iter().find_map(|r| if let Raw::Path(p) = r { Some(p) } else { None })
+ }
+ _ => None,
+ };
+ if let Some(t) = target {
+ let full = if t.starts_with('/') { t } else { format!("{prim}/{t}") };
+ self.put(&format!("{prim}.{base}"), "connectionPaths", Value::Path(full));
+ }
+ return Some(());
+ }
+ if name.ends_with(".timeSamples") {
return Some(());
}
let path = format!("{prim}.{name}");
@@ -1385,6 +1468,11 @@ impl TextReader {
}
}
+/// The prim a property's path names (before its `.`).
+fn prim_of(property: &str) -> &str {
+ property.split('.').next().unwrap_or(property)
+}
+
/// A written value as its declared type makes it (the types read here).
fn typed(type_name: &str, raw: Raw) -> Option<Value> {
let numbers = |r: &Raw| -> Option<Vec<f64>> {
@@ -1401,7 +1489,7 @@ fn typed(type_name: &str, raw: Raw) -> Option<Value> {
_ => 1,
};
match (base, array) {
- ("token" | "string", false) => match raw {
+ ("token" | "string" | "asset", false) => match raw {
Raw::Text(t) => Some(Value::Token(t)),
_ => None,
},
@@ -1508,6 +1596,11 @@ struct Gather {
known: HashMap<[usize; 2], usize>,
faces: Vec<[usize; 3]>,
parts: Vec<Part>,
+ /// Each run's mesh, and the mesh whose faces come now.
+ objects: Vec<String>,
+ object: String,
+ /// The materials faces are bound to, by path.
+ material_paths: Vec<String>,
}
impl Gather {
@@ -1532,8 +1625,9 @@ impl Gather {
})
})
.collect();
- if self.parts.last().is_none_or(|p| p.material != material) {
+ if self.parts.last().is_none_or(|p| p.material != material) || self.objects.last() != Some(&self.object) {
self.parts.push(Part { material: material.to_owned(), first: self.faces.len(), count: 0 });
+ self.objects.push(self.object.clone());
}
if let Some(&a) = corners.first() {
for pair in corners.get(1..).unwrap_or(&[]).windows(2) {
@@ -1552,6 +1646,7 @@ impl Gather {
corners: self.corners,
faces: self.faces,
parts: self.parts,
+ objects: self.objects,
}
}
}
@@ -1611,12 +1706,16 @@ mod tests {
parts: vec![Part { material: "1 coat".into(), first: 0, count: 1 }],
..Mesh::default()
};
- let bytes = write_usdz(&mesh, &[("coat.png".into(), vec![7; 5])]);
+ let images =
+ [("coat.png".into(), vec![7; 5]), ("rough.png".into(), vec![8; 5]), ("metal.png".into(), vec![9; 5])];
+ let bytes = write_usdz(&mesh, &[images]);
let zip = ZipReader::new(&bytes).unwrap();
- assert_eq!(zip.names().collect::<Vec<_>>(), ["model.usda", "textures/coat.png"]);
+ let names = ["model.usda", "textures/coat.png", "textures/rough.png", "textures/metal.png"];
+ assert_eq!(zip.names().collect::<Vec<_>>(), names);
assert_eq!(zip.read("textures/coat.png").unwrap(), [7; 5]);
+ assert_eq!(zip.read("textures/metal.png").unwrap(), [9; 5]);
// each file's bytes start on a 64-byte boundary
- for name in ["model.usda", "textures/coat.png"] {
+ for name in names {
let data = zip.read(name).unwrap();
let at = bytes.windows(data.len()).position(|w| w == data.as_slice()).unwrap();
assert_eq!(at % 64, 0, "{name}");
@@ -1626,6 +1725,8 @@ mod tests {
assert!(layer.contains("def GeomSubset \"_1_coat\""));
assert!(layer.contains("rel material:binding = </Model/Materials/_1_coat>"));
assert!(layer.contains("asset inputs:file = @textures/coat.png@"));
+ assert!(layer.contains("float inputs:roughness.connect = </Model/Materials/_1_coat/Roughness.outputs:r>"));
+ assert!(layer.contains("asset inputs:file = @textures/metal.png@"));
assert!(layer.contains("int[] faceVertexIndices = [0, 1, 2]"));
}
diff --git a/crates/engine/tests/usd.rs b/crates/engine/tests/usd.rs
index c74fc86..dc3be77 100644
--- a/crates/engine/tests/usd.rs
+++ b/crates/engine/tests/usd.rs
@@ -18,6 +18,10 @@ fn a_packaged_cube_keeps_its_faces_coordinates_and_materials() {
assert_eq!(mesh.triangles.len(), 12);
let parts: Vec<(&str, usize)> = mesh.parts.iter().map(|p| (p.material.as_str(), p.count)).collect();
assert_eq!(parts, [("Body", 8), ("Lid", 4)]);
+ // one mesh, in two materials: a submesh in each, named as the mesh
+ let subs: Vec<(&str, usize)> = mesh.submeshes.iter().map(|m| (m.name.as_str(), m.part)).collect();
+ assert_eq!(subs.iter().map(|s| s.1).collect::<Vec<_>>(), [0, 1]);
+ assert!(subs.iter().all(|s| s.0 == subs[0].0 && !s.0.is_empty()), "{subs:?}");
let (lo, hi) = mesh.bounds().unwrap();
assert_eq!((lo, hi), ([-1.0; 3], [1.0; 3]));
// each face covers the whole texture: its corners are the square's four
@@ -76,12 +80,23 @@ fn damaged_files_are_refused_without_panicking() {
#[test]
fn a_written_package_reads_back() {
let mesh = read_usdz(&model("cube.usdz")).unwrap();
- let textures: Vec<(String, Vec<u8>)> =
- mesh.parts.iter().map(|p| (format!("{}.png", p.material), vec![0; 4])).collect();
+ let textures: Vec<[(String, Vec<u8>); 3]> = mesh
+ .parts
+ .iter()
+ .map(|p| ["color", "roughness", "metallic"].map(|c| (format!("{}-{c}.png", p.material), vec![0; 4])))
+ .collect();
let again = read_usdz(&procraft_engine::usd::write_usdz(&mesh, &textures)).unwrap();
assert_eq!(again.triangles.len(), mesh.triangles.len());
let parts: Vec<(&str, usize)> = again.parts.iter().map(|p| (p.material.as_str(), p.count)).collect();
assert_eq!(parts, [("Body", 8), ("Lid", 4)]);
+ // each material's images, as written: the greys read from their red
+ let maps: Vec<(&str, Option<&str>)> =
+ again.materials.iter().map(|m| (m.name.as_str(), m.color_map.as_deref())).collect();
+ assert_eq!(maps, [("Body", Some("textures/Body-color.png")), ("Lid", Some("textures/Lid-color.png"))]);
+ let lid = &again.materials[1];
+ assert_eq!(lid.roughness_map.as_deref(), Some("textures/Lid-roughness.png"));
+ assert_eq!(lid.metallic_map.as_deref(), Some("textures/Lid-metallic.png"));
+ assert_eq!((lid.roughness_component, lid.metallic_component), (Some(0), Some(0)));
assert_eq!(again.bounds(), mesh.bounds());
}
diff --git a/crates/gpu/src/lib.rs b/crates/gpu/src/lib.rs
index 0f7db8d..bb2aca6 100644
--- a/crates/gpu/src/lib.rs
+++ b/crates/gpu/src/lib.rs
@@ -16,7 +16,7 @@ pub use canvas::{
StrokeTool, SurroundGrid, Target, Thumbnail, read_texture,
};
pub use lighting::Lighting;
-pub use scene::{MeshView, Orbit};
+pub use scene::{Light, MOST_LIGHTS, MeshView, Orbit};
pub use wgpu;
#[derive(Debug, thiserror::Error)]
diff --git a/crates/gpu/src/scene.rs b/crates/gpu/src/scene.rs
index a418814..41e2a5f 100644
--- a/crates/gpu/src/scene.rs
+++ b/crates/gpu/src/scene.rs
@@ -6,7 +6,7 @@ use procraft_engine::mesh::Mesh;
use wgpu::util::DeviceExt;
use crate::lighting::{CUBE_LEVELS, Lighting};
-use crate::{Gpu, GpuError, read_texture};
+use crate::{Gpu, GpuError, SurroundGrid, read_texture};
use procraft_engine::environment::Equirect;
const DEPTH: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
@@ -25,12 +25,35 @@ const DILATE: i32 = 2;
const FRAMED_SIDE: f32 = 4.0;
const FRAMED_DISTANCE: f32 = 8.0;
const FIELD_OF_VIEW: f32 = 0.8727;
-/// The two lights, fixed to the camera: each one's place (in the framing's units, from the
-/// model's middle as the camera sees it) and its intensity (radiance intensity^4 x 50, white).
-const LIGHTS: [([f32; 3], f32); 2] = [([-2.0, 5.0, 2.0], 0.36), ([3.0, 4.0, 0.0], 0.30)];
-/// A part's roughness and metalness when its material gives none.
-const ROUGHNESS: f32 = 1.0;
-const METALLIC: f32 = 0.0;
+/// The most lights a model is lit by.
+pub const MOST_LIGHTS: usize = 4;
+/// The Lighting Studio's camera as it first opens: tilted this far down (radians) and this far
+/// back (in the framing's units, the model's largest side counted as 4).
+const STUDIO_PITCH: f32 = 0.716;
+const STUDIO_DISTANCE: f32 = 22.0;
+/// The studio's floor under the model: its radius and a grid cell's side, and each light's cube
+/// (framing units).
+const FLOOR_RADIUS: f32 = 9.5;
+const FLOOR_CELL: f32 = 1.0;
+const CUBE_SIDE: f32 = 0.8;
+
+/// A light shining on the model: its place (in the framing's units from the model's middle, along
+/// the world's axes), its colour (linear RGB) and its intensity (0 to 1; its radiance is the
+/// intensity^4 x 50 of its colour).
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub struct Light {
+ pub place: [f32; 3],
+ pub color: [f32; 3],
+ pub intensity: f32,
+}
+
+impl Light {
+ /// The lights a model starts with: white, one high on the left in front, one high on the right.
+ pub const DEFAULTS: [Light; 2] = [
+ Light { place: [-2.0, 5.0, 2.0], color: [1.0; 3], intensity: 0.36 },
+ Light { place: [3.0, 4.0, 0.0], color: [1.0; 3], intensity: 0.30 },
+ ];
+}
/// The light of a model before it is given an environment: the same soft grey all round.
const NEUTRAL_LIGHT: f32 = 0.6;
@@ -47,14 +70,24 @@ struct VertexGpu {
struct ViewUniform {
view_proj: [[f32; 4]; 4],
view: [[f32; 4]; 4],
- light0: [f32; 4],
- radiance0: [f32; 4],
- light1: [f32; 4],
- radiance1: [f32; 4],
+ /// Each light's place in the camera's space, and its radiance; how many there are.
+ lights: [[f32; 4]; MOST_LIGHTS],
+ radiances: [[f32; 4]; MOST_LIGHTS],
sh: [[f32; 4]; 9],
params: [f32; 4],
projection: [f32; 4],
convert: [[f32; 4]; 3],
+ counts: [f32; 4],
+}
+
+#[repr(C)]
+#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
+struct StudioUniform {
+ view_proj: [[f32; 4]; 4],
+ floor: [f32; 4],
+ cubes: [[f32; 4]; MOST_LIGHTS],
+ colors: [[f32; 4]; MOST_LIGHTS],
+ target: [f32; 4],
}
#[repr(C)]
@@ -159,6 +192,8 @@ struct PartGpu {
texture: wgpu::Texture,
uniform: wgpu::Buffer,
bind: wgpu::BindGroup,
+ /// What it shows: its albedo, and its roughness and metalness painted over the material's.
+ views: [wgpu::TextureView; 3],
/// The albedo with its smaller levels, once given, so a far or slanting surface shows it
/// filtered rather than speckled.
mips: Option<(wgpu::Texture, Vec<wgpu::TextureView>, wgpu::TextureView)>,
@@ -191,6 +226,21 @@ pub struct MeshView {
/// The environment drawn behind the model, and how bright the light is taken.
backdrop: Option<wgpu::RenderPipeline>,
exposure: f32,
+ /// Without the environment behind it, the canvas's surround grid is, if given; and what draws it.
+ grid: Option<SurroundGrid>,
+ grid_draw: Option<GridDraw>,
+ /// The model's middle and the height of its lowest point (the studio's floor), in the world.
+ middle: [f32; 3],
+ floor: f32,
+ /// What draws the Lighting Studio's floor and lights.
+ studio_draw: Option<StudioDraw>,
+ /// The lights it is lit by (at most [`MOST_LIGHTS`]).
+ pub lights: Vec<Light>,
+ /// A channel with nothing painted in it.
+ blank: wgpu::TextureView,
+ /// Each submesh's part and its run of indices, and whether it is hidden.
+ submeshes: Vec<(usize, u32, u32)>,
+ hidden: Vec<bool>,
/// The framing's unit (the model's largest side over 4): the lights stand that far apart.
unit: f32,
/// What it draws into.
@@ -202,6 +252,172 @@ pub struct MeshView {
pub paint_through: bool,
}
+/// The surround grid's pipeline and its uniform.
+struct GridDraw {
+ pipeline: wgpu::RenderPipeline,
+ buffer: wgpu::Buffer,
+ bind: wgpu::BindGroup,
+}
+
+#[repr(C)]
+#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
+struct GridUniform {
+ v0: [f32; 4],
+ v1: [f32; 4],
+}
+
+impl GridDraw {
+ fn new(gpu: &Gpu, format: wgpu::TextureFormat) -> GridDraw {
+ let device = &gpu.device;
+ let module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
+ label: Some("mesh surround"),
+ source: wgpu::ShaderSource::Wgsl(include_str!("shaders/surround.wgsl").into()),
+ });
+ let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
+ label: Some("mesh surround"),
+ entries: &[wgpu::BindGroupLayoutEntry {
+ binding: 0,
+ visibility: wgpu::ShaderStages::FRAGMENT,
+ ty: wgpu::BindingType::Buffer {
+ ty: wgpu::BufferBindingType::Uniform,
+ has_dynamic_offset: false,
+ min_binding_size: None,
+ },
+ count: None,
+ }],
+ });
+ let buffer = device.create_buffer(&wgpu::BufferDescriptor {
+ label: Some("mesh surround"),
+ size: std::mem::size_of::<GridUniform>() as u64,
+ usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
+ mapped_at_creation: false,
+ });
+ let bind = device.create_bind_group(&wgpu::BindGroupDescriptor {
+ label: Some("mesh surround"),
+ layout: &layout,
+ entries: &[wgpu::BindGroupEntry { binding: 0, resource: buffer.as_entire_binding() }],
+ });
+ let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
+ label: Some("mesh surround"),
+ bind_group_layouts: &[Some(&layout)],
+ immediate_size: 0,
+ });
+ let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
+ label: Some("mesh surround"),
+ layout: Some(&pipeline_layout),
+ vertex: wgpu::VertexState {
+ module: &module,
+ entry_point: Some("vs_surround"),
+ compilation_options: Default::default(),
+ buffers: &[],
+ },
+ primitive: wgpu::PrimitiveState::default(),
+ depth_stencil: Some(wgpu::DepthStencilState {
+ format: DEPTH,
+ depth_write_enabled: Some(false),
+ depth_compare: Some(wgpu::CompareFunction::Always),
+ stencil: wgpu::StencilState::default(),
+ bias: wgpu::DepthBiasState::default(),
+ }),
+ multisample: wgpu::MultisampleState::default(),
+ fragment: Some(wgpu::FragmentState {
+ module: &module,
+ entry_point: Some("fs_surround"),
+ compilation_options: Default::default(),
+ targets: &[Some(wgpu::ColorTargetState { format, blend: None, write_mask: wgpu::ColorWrites::ALL })],
+ }),
+ multiview_mask: None,
+ cache: None,
+ });
+ GridDraw { pipeline, buffer, bind }
+ }
+}
+
+/// The Lighting Studio's floor and lights' pipelines and their uniform.
+struct StudioDraw {
+ floor: wgpu::RenderPipeline,
+ cubes: wgpu::RenderPipeline,
+ buffer: wgpu::Buffer,
+ bind: wgpu::BindGroup,
+}
+
+impl StudioDraw {
+ fn new(gpu: &Gpu, format: wgpu::TextureFormat) -> StudioDraw {
+ let device = &gpu.device;
+ let module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
+ label: Some("studio"),
+ source: wgpu::ShaderSource::Wgsl(include_str!("shaders/studio.wgsl").into()),
+ });
+ let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
+ label: Some("studio"),
+ entries: &[wgpu::BindGroupLayoutEntry {
+ binding: 0,
+ visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
+ ty: wgpu::BindingType::Buffer {
+ ty: wgpu::BufferBindingType::Uniform,
+ has_dynamic_offset: false,
+ min_binding_size: None,
+ },
+ count: None,
+ }],
+ });
+ let buffer = device.create_buffer(&wgpu::BufferDescriptor {
+ label: Some("studio"),
+ size: std::mem::size_of::<StudioUniform>() as u64,
+ usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
+ mapped_at_creation: false,
+ });
+ let bind = device.create_bind_group(&wgpu::BindGroupDescriptor {
+ label: Some("studio"),
+ layout: &layout,
+ entries: &[wgpu::BindGroupEntry { binding: 0, resource: buffer.as_entire_binding() }],
+ });
+ let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
+ label: Some("studio"),
+ bind_group_layouts: &[Some(&layout)],
+ immediate_size: 0,
+ });
+ let pipeline = |label: &str, vs: &str, fs: &str, write: bool, blend: Option<wgpu::BlendState>| {
+ device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
+ label: Some(label),
+ layout: Some(&pipeline_layout),
+ vertex: wgpu::VertexState {
+ module: &module,
+ entry_point: Some(vs),
+ compilation_options: Default::default(),
+ buffers: &[],
+ },
+ primitive: wgpu::PrimitiveState::default(),
+ depth_stencil: Some(wgpu::DepthStencilState {
+ format: DEPTH,
+ depth_write_enabled: Some(write),
+ depth_compare: Some(wgpu::CompareFunction::LessEqual),
+ stencil: wgpu::StencilState::default(),
+ bias: wgpu::DepthBiasState::default(),
+ }),
+ multisample: wgpu::MultisampleState::default(),
+ fragment: Some(wgpu::FragmentState {
+ module: &module,
+ entry_point: Some(fs),
+ compilation_options: Default::default(),
+ targets: &[Some(wgpu::ColorTargetState { format, blend, write_mask: wgpu::ColorWrites::ALL })],
+ }),
+ multiview_mask: None,
+ cache: None,
+ })
+ };
+ let floor = pipeline(
+ "studio floor",
+ "vs_floor",
+ "fs_floor",
+ false,
+ Some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING),
+ );
+ let cubes = pipeline("studio lights", "vs_cube", "fs_cube", true, None);
+ StudioDraw { floor, cubes, buffer, bind }
+ }
+}
+
/// What carrying strokes onto the model takes: its pipelines, and the nearest surface's distance
/// for each screen pixel (with a depth buffer to find it), the screen's size.
struct Projector {
@@ -300,8 +516,28 @@ impl MeshView {
},
count: None,
},
+ map_entry(3),
+ map_entry(4),
],
});
+ // nothing painted in a channel: the material's own value shows
+ let blank = device
+ .create_texture_with_data(
+ &gpu.queue,
+ &wgpu::TextureDescriptor {
+ label: Some("mesh blank map"),
+ size: wgpu::Extent3d { width: 1, height: 1, depth_or_array_layers: 1 },
+ mip_level_count: 1,
+ sample_count: 1,
+ dimension: wgpu::TextureDimension::D2,
+ format: ALBEDO,
+ usage: wgpu::TextureUsages::TEXTURE_BINDING,
+ view_formats: &[],
+ },
+ wgpu::util::TextureDataOrder::LayerMajor,
+ &[0, 0, 0, 0],
+ )
+ .create_view(&wgpu::TextureViewDescriptor::default());
let view_buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("mesh view"),
size: std::mem::size_of::<ViewUniform>() as u64,
@@ -336,7 +572,8 @@ impl MeshView {
let parts = mesh
.parts
.iter()
- .map(|p| {
+ .enumerate()
+ .map(|(i, p)| {
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("mesh albedo"),
size: wgpu::Extent3d { width: side, height: side, depth_or_array_layers: 1 },
@@ -348,28 +585,27 @@ impl MeshView {
view_formats: &[],
});
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
- // the material file's roughness and metalness, else plain
- let m = mesh.materials.iter().find(|m| m.name == p.material);
- let roughness = m.and_then(|m| m.roughness).unwrap_or(ROUGHNESS);
- let metallic = m.and_then(|m| m.metallic).unwrap_or(METALLIC);
+ // the material's own colour, roughness and metalness under what is painted
+ let ([r, g, b], roughness, metallic) = mesh.surface(i);
let uniform = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("mesh part"),
contents: bytemuck::bytes_of(&PartUniform {
- base: [1.0, 1.0, 1.0, 1.0],
- material: [roughness.clamp(0.0, 1.0), metallic.clamp(0.0, 1.0), 0.0, 0.0],
+ base: [r, g, b, 1.0],
+ material: [roughness, metallic, 0.0, 0.0],
}),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});
- let bind = device.create_bind_group(&wgpu::BindGroupDescriptor {
- label: Some("mesh part"),
- layout: &part_layout,
- entries: &[
- wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(&view) },
- wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::Sampler(&sampler) },
- wgpu::BindGroupEntry { binding: 2, resource: uniform.as_entire_binding() },
- ],
- });
- PartGpu { first: (p.first * 3) as u32, count: (p.count * 3) as u32, texture, uniform, bind, mips: None }
+ let views = [view, blank.clone(), blank.clone()];
+ let bind = part_bind(device, &part_layout, &sampler, &views, &uniform);
+ PartGpu {
+ first: (p.first * 3) as u32,
+ count: (p.count * 3) as u32,
+ texture,
+ uniform,
+ bind,
+ views,
+ mips: None,
+ }
})
.collect();
let module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
@@ -436,6 +672,17 @@ impl MeshView {
table_sampler,
backdrop: Some(backdrop),
exposure: 1.0,
+ grid: None,
+ grid_draw: None,
+ middle: mesh
+ .bounds()
+ .map_or([0.0; 3], |(lo, hi)| [(lo[0] + hi[0]) / 2.0, (lo[1] + hi[1]) / 2.0, (lo[2] + hi[2]) / 2.0]),
+ floor: mesh.bounds().map_or(-1.0, |(lo, _)| lo[1]),
+ studio_draw: None,
+ lights: Light::DEFAULTS.to_vec(),
+ blank,
+ submeshes: mesh.submeshes.iter().map(|m| (m.part, (m.first * 3) as u32, (m.count * 3) as u32)).collect(),
+ hidden: vec![false; mesh.submeshes.len()],
unit: Orbit::unit(mesh),
format,
convert: None,
@@ -480,6 +727,35 @@ impl MeshView {
Ok(rgba)
}
+ /// Submesh `index` on its own, seen from where the camera looks at the model now, filling a
+ /// `side` square with a little room round it, on nothing (RGBA8): its row's picture.
+ pub fn submesh_picture(&mut self, gpu: &Gpu, index: usize, side: u32) -> Result<Vec<u8>, GpuError> {
+ let Some(&(_, first, count)) = self.submeshes.get(index) else {
+ return Ok(vec![0; (side * side * 4) as usize]);
+ };
+ // its corners' middle and how far they reach from it
+ let tris = self.triangles.get(first as usize / 3..(first + count) as usize / 3).unwrap_or(&[]);
+ let corners: Vec<[f32; 3]> =
+ tris.iter().flatten().filter_map(|&i| self.positions.get(i as usize).copied()).collect();
+ let (lo, hi) = corners.iter().fold(([f32::MAX; 3], [f32::MIN; 3]), |(lo, hi), p| {
+ ([lo[0].min(p[0]), lo[1].min(p[1]), lo[2].min(p[2])], [hi[0].max(p[0]), hi[1].max(p[1]), hi[2].max(p[2])])
+ });
+ let middle = [(lo[0] + hi[0]) / 2.0, (lo[1] + hi[1]) / 2.0, (lo[2] + hi[2]) / 2.0];
+ let reach = corners
+ .iter()
+ .map(|p| ((p[0] - middle[0]).powi(2) + (p[1] - middle[1]).powi(2) + (p[2] - middle[2]).powi(2)).sqrt())
+ .fold(0.0f32, f32::max)
+ .max(1e-6);
+ let kept = (self.orbit, self.hidden.clone(), self.backdrop.take(), self.grid.take());
+ self.orbit = Orbit { target: middle, distance: reach * 1.15 / (self.orbit.fov / 2.0).sin(), ..self.orbit };
+ for (k, h) in self.hidden.iter_mut().enumerate() {
+ *h = k != index;
+ }
+ let picture = self.picture(gpu, side, side, [0.0; 4]);
+ (self.orbit, self.hidden, self.backdrop, self.grid) = kept;
+ picture
+ }
+
pub fn part_count(&self) -> usize {
self.parts.len()
}
@@ -515,15 +791,18 @@ impl MeshView {
/// as composited, say, so the model shows its layers and the stroke being drawn.
pub fn set_albedo_view(&mut self, gpu: &Gpu, part: usize, albedo: &wgpu::TextureView) {
let Some(p) = self.parts.get_mut(part) else { return };
- p.bind = gpu.device.create_bind_group(&wgpu::BindGroupDescriptor {
- label: Some("mesh part"),
- layout: &self.part_layout,
- entries: &[
- wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(albedo) },
- wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::Sampler(&self.sampler) },
- wgpu::BindGroupEntry { binding: 2, resource: p.uniform.as_entire_binding() },
- ],
- });
+ p.views[0] = albedo.clone();
+ p.bind = part_bind(&gpu.device, &self.part_layout, &self.sampler, &p.views, &p.uniform);
+ }
+
+ /// Part `part`'s roughness (`channel` 1) or metalness (2) painted over its material's from
+ /// now on: `map` premultiplied, its grey taking the place of the material's value as far as
+ /// it covers (any size); none, the material's own everywhere.
+ pub fn set_channel_view(&mut self, gpu: &Gpu, part: usize, channel: usize, map: Option<&wgpu::TextureView>) {
+ let Some(p) = self.parts.get_mut(part) else { return };
+ let Some(slot) = p.views.get_mut(channel).filter(|_| channel > 0) else { return };
+ *slot = map.unwrap_or(&self.blank).clone();
+ p.bind = part_bind(&gpu.device, &self.part_layout, &self.sampler, &p.views, &p.uniform);
}
/// Shows `albedo` (premultiplied RGBA8, the texture size square) on part `part` through
@@ -590,15 +869,8 @@ impl MeshView {
drop(pass);
source = target;
}
- p.bind = gpu.device.create_bind_group(&wgpu::BindGroupDescriptor {
- label: Some("mesh part"),
- layout: &self.part_layout,
- entries: &[
- wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(all) },
- wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::Sampler(&self.sampler) },
- wgpu::BindGroupEntry { binding: 2, resource: p.uniform.as_entire_binding() },
- ],
- });
+ p.views[0] = all.clone();
+ p.bind = part_bind(&gpu.device, &self.part_layout, &self.sampler, &p.views, &p.uniform);
}
/// The colour part `part` shows where nothing is painted.
@@ -636,6 +908,11 @@ impl MeshView {
}
}
+ /// The grid drawn over the clear colour while the environment doesn't show behind the model.
+ pub fn set_grid(&mut self, grid: Option<SurroundGrid>) {
+ self.grid = grid;
+ }
+
/// Whether the environment shows behind the model (else the clear colour does).
pub fn show_environment(&mut self, gpu: &Gpu, shown: bool) {
if shown == self.backdrop.is_some() {
@@ -700,7 +977,162 @@ impl MeshView {
height: u32,
clear: [f64; 4],
) {
+ self.draw(gpu, encoder, target, (width, height), clear, None);
+ }
+
+ /// Draws the Lighting Studio into `target` (`width` × `height`), cleared to `clear` first: the
+ /// model seen through `orbit` on its round floor, each light a cube.
+ #[allow(clippy::too_many_arguments)]
+ pub fn render_studio(
+ &mut self,
+ gpu: &Gpu,
+ encoder: &mut wgpu::CommandEncoder,
+ target: &wgpu::TextureView,
+ width: u32,
+ height: u32,
+ clear: [f64; 4],
+ orbit: Orbit,
+ ) {
+ self.draw(gpu, encoder, target, (width, height), clear, Some(orbit));
+ }
+
+ /// Hides submesh `index` (or shows it again): not drawn, and not painted.
+ pub fn set_hidden(&mut self, index: usize, hidden: bool) {
+ if let Some(h) = self.hidden.get_mut(index) {
+ *h = hidden;
+ }
+ }
+
+ /// The runs of indices of part `part` that are shown: all of it, unless a submesh in it is
+ /// hidden.
+ fn shown_ranges(&self, part: usize) -> Vec<std::ops::Range<u32>> {
+ let Some(p) = self.parts.get(part) else { return Vec::new() };
+ let mine = || self.submeshes.iter().zip(&self.hidden).filter(move |((at, _, _), _)| *at == part);
+ if !mine().any(|(_, &h)| h) {
+ return std::iter::once(p.first..p.first + p.count).collect();
+ }
+ mine().filter(|(_, h)| !**h).map(|((_, first, count), _)| *first..*first + *count).collect()
+ }
+
+ /// The studio's camera as it first opens: the model from in front, high up and far back.
+ pub fn studio_orbit(&self) -> Orbit {
+ Orbit {
+ target: self.middle,
+ rotation: Orbit::rotation_of(0.0, STUDIO_PITCH),
+ distance: STUDIO_DISTANCE * self.unit,
+ fov: FIELD_OF_VIEW,
+ }
+ }
+
+ /// The model upright, turned `yaw` about its upright axis, seen from `pitch` above level and
+ /// `distance` back (framing units), its middle in the middle.
+ pub fn upright_orbit(&self, yaw: f32, pitch: f32, distance: f32) -> Orbit {
+ Orbit {
+ target: self.middle,
+ rotation: Orbit::rotation_of(yaw, pitch),
+ distance: distance * self.unit,
+ fov: FIELD_OF_VIEW,
+ }
+ }
+
+ /// The picture of the model as `orbit` sees it at `width` × `height` (RGBA8), on `clear`,
+ /// with or without the environment behind it (never the surround's grid).
+ pub fn picture_from(
+ &mut self,
+ gpu: &Gpu,
+ orbit: Orbit,
+ width: u32,
+ height: u32,
+ clear: [f64; 4],
+ environment: bool,
+ ) -> Result<Vec<u8>, GpuError> {
+ let kept = (self.orbit, self.grid.take(), self.backdrop.is_some());
+ self.orbit = orbit;
+ if !environment {
+ self.backdrop = None;
+ }
+ let picture = self.picture(gpu, width, height, clear);
+ self.orbit = kept.0;
+ self.grid = kept.1;
+ if kept.2 && self.backdrop.is_none() {
+ self.backdrop = Some(MeshView::backdrop_pipeline(gpu, &self.view_layout, self.format));
+ }
+ picture
+ }
+
+ /// Where `light` stands in the world.
+ fn light_world(&self, light: &Light) -> [f32; 3] {
+ let m = self.middle;
+ let p = light.place;
+ [m[0] + p[0] * self.unit, m[1] + p[1] * self.unit, m[2] + p[2] * self.unit]
+ }
+
+ /// Light `index` on a `width` × `height` screen seen through `orbit`: its middle and about how
+ /// far its cube reaches from it (pixels); none if it is behind the camera.
+ pub fn light_on_screen(&self, orbit: &Orbit, index: usize, width: u32, height: u32) -> Option<[f32; 3]> {
+ let light = self.lights.get(index)?;
+ self.place_on_screen(orbit, light.place, width, height)
+ }
+
+ /// A light's `place` (framing units from the model's middle) on a `width` × `height` screen
+ /// seen through `orbit`, as [`MeshView::light_on_screen`] gives it.
+ pub fn place_on_screen(&self, orbit: &Orbit, place: [f32; 3], width: u32, height: u32) -> Option<[f32; 3]> {
+ let at = self.light_world(&Light { place, color: [1.0; 3], intensity: 0.0 });
+ let (w, h) = (width.max(1) as f32, height.max(1) as f32);
+ let depth = orbit_depth(orbit, at);
+ if depth <= orbit.distance * 0.01 {
+ return None;
+ }
+ let m = orbit.view_proj(w / h);
+ let clip = transform(m, at);
+ if clip[3] <= 0.0 {
+ return None;
+ }
+ let (x, y) = ((clip[0] / clip[3] + 1.0) / 2.0 * w, (1.0 - clip[1] / clip[3]) / 2.0 * h);
+ let per_pixel = 2.0 * depth * (orbit.fov_y(w / h) / 2.0).tan() / h;
+ Some([x, y, CUBE_SIDE * self.unit * 0.8 / per_pixel])
+ }
+
+ /// Moves light `index` by (`dx`, `dy`) pixels (right and down) on a `width` × `height` screen
+ /// seen through `orbit`: across the plane facing the camera at the light's depth.
+ pub fn move_light(&mut self, orbit: &Orbit, index: usize, dx: f32, dy: f32, width: u32, height: u32) {
+ let Some(light) = self.lights.get(index).copied() else { return };
+ let place = self.moved_place(orbit, light.place, dx, dy, width, height);
+ if let Some(l) = self.lights.get_mut(index) {
+ l.place = place;
+ }
+ }
+
+ /// A light's `place` dragged by (`dx`, `dy`) pixels, as [`MeshView::move_light`] moves it.
+ pub fn moved_place(&self, orbit: &Orbit, place: [f32; 3], dx: f32, dy: f32, width: u32, height: u32) -> [f32; 3] {
+ let at = self.light_world(&Light { place, color: [1.0; 3], intensity: 0.0 });
+ let depth = orbit_depth(orbit, at).max(orbit.distance * 0.05);
+ let (w, h) = (width.max(1) as f32, height.max(1) as f32);
+ let per_unit = 2.0 * depth * (orbit.fov_y(w / h) / 2.0).tan() / h / self.unit;
+ let (right, up) = (orbit.right(), orbit.up());
+ let mut out = place;
+ for (k, p) in out.iter_mut().enumerate() {
+ if let (Some(r), Some(u)) = (right.get(k), up.get(k)) {
+ *p += (dx * r - dy * u) * per_unit;
+ }
+ }
+ out
+ }
+
+ fn draw(
+ &mut self,
+ gpu: &Gpu,
+ encoder: &mut wgpu::CommandEncoder,
+ target: &wgpu::TextureView,
+ (width, height): (u32, u32),
+ clear: [f64; 4],
+ studio: Option<Orbit>,
+ ) {
+ let orbit = studio.unwrap_or(self.orbit);
let (width, height) = (width.max(1), height.max(1));
+ let light_worlds: Vec<[f32; 3]> = self.lights.iter().take(MOST_LIGHTS).map(|l| self.light_world(l)).collect();
+ let shown_by_part: Vec<Vec<std::ops::Range<u32>>> =
+ (0..self.parts.len()).map(|i| self.shown_ranges(i)).collect();
let stale = self.depth.as_ref().is_none_or(|(t, _)| t.width() != width || t.height() != height);
if stale {
let texture = gpu.device.create_texture(&wgpu::TextureDescriptor {
@@ -718,31 +1150,72 @@ impl MeshView {
}
let Some((_, depth_view)) = &self.depth else { return };
let aspect = width as f32 / height as f32;
- let view = look_at(self.orbit.eye(), self.orbit.target, self.orbit.up());
- // the lights stand about the model's middle as the camera sees it: they turn with it
- let middle = view[3];
- let light = |k: usize| {
- let (place, intensity) = LIGHTS.get(k).copied().unwrap_or(([0.0; 3], 0.0));
- let [x, y, z] = place;
- let at = [x * self.unit + middle[0], y * self.unit + middle[1], z * self.unit + middle[2]];
- let radiance = intensity.powi(4) * 50.0;
- ([at[0], at[1], at[2], 1.0], [radiance, radiance, radiance, 0.0])
- };
- let ((light0, radiance0), (light1, radiance1)) = (light(0), light(1));
- let f = 1.0 / (self.orbit.fov_y(aspect) / 2.0).tan();
+ let view = look_at(orbit.eye(), orbit.target, orbit.up());
+ // the lights stand about the model in the world, seen from the camera
+ let mut lights = [[0.0; 4]; MOST_LIGHTS];
+ let mut radiances = [[0.0; 4]; MOST_LIGHTS];
+ let shown = self.lights.len().min(MOST_LIGHTS);
+ for (k, (light, w)) in self.lights.iter().zip(&light_worlds).enumerate() {
+ let radiance = light.intensity.clamp(0.0, 1.0).powi(4) * 50.0;
+ if let (Some(l), Some(r)) = (lights.get_mut(k), radiances.get_mut(k)) {
+ *l = transform(view, *w);
+ *r = [light.color[0] * radiance, light.color[1] * radiance, light.color[2] * radiance, 0.0];
+ }
+ }
+ let f = 1.0 / (orbit.fov_y(aspect) / 2.0).tan();
let uniform = ViewUniform {
- view_proj: self.orbit.view_proj(aspect),
+ view_proj: orbit.view_proj(aspect),
view,
- light0,
- radiance0,
- light1,
- radiance1,
+ lights,
+ radiances,
sh: self.lighting.sh,
params: [self.exposure, (CUBE_LEVELS - 1) as f32, width as f32, height as f32],
projection: [f / aspect, f, 0.0, 0.0],
convert: self.convert.map_or([[0.0; 4]; 3], |m| m.map(|r| [r[0], r[1], r[2], 1.0])),
+ counts: [shown as f32, 0.0, 0.0, 0.0],
};
gpu.queue.write_buffer(&self.view_buffer, 0, bytemuck::bytes_of(&uniform));
+ // the surround's grid where no environment shows
+ let grid = match (self.backdrop.is_none(), self.grid) {
+ (true, Some(g)) => {
+ let format = self.format;
+ let draw = self.grid_draw.get_or_insert_with(|| GridDraw::new(gpu, format));
+ let srgb = if format.is_srgb() { 1.0 } else { 0.0 };
+ let u = GridUniform {
+ v0: [g.spacing, g.origin[0], g.origin[1], g.width],
+ v1: [g.color[0], g.color[1], g.color[2], srgb],
+ };
+ gpu.queue.write_buffer(&draw.buffer, 0, bytemuck::bytes_of(&u));
+ Some(&*draw)
+ }
+ _ => None,
+ };
+ // the studio's floor and lights, with the camera it is seen through
+ let studio_draw = match studio {
+ Some(o) => {
+ let format = self.format;
+ let mut cubes = [[0.0; 4]; MOST_LIGHTS];
+ let mut colors = [[0.0; 4]; MOST_LIGHTS];
+ for (k, (light, w)) in self.lights.iter().zip(&light_worlds).enumerate() {
+ if let (Some(c), Some(col)) = (cubes.get_mut(k), colors.get_mut(k)) {
+ *c = [w[0], w[1], w[2], CUBE_SIDE * self.unit];
+ let s = light.color.map(linear_to_srgb);
+ *col = [s[0], s[1], s[2], 1.0];
+ }
+ }
+ let u = StudioUniform {
+ view_proj: o.view_proj(aspect),
+ floor: [self.floor, FLOOR_RADIUS * self.unit, FLOOR_CELL * self.unit, 0.0],
+ cubes,
+ colors,
+ target: [if format.is_srgb() { 1.0 } else { 0.0 }, 0.0, 0.0, 0.0],
+ };
+ let draw = self.studio_draw.get_or_insert_with(|| StudioDraw::new(gpu, format));
+ gpu.queue.write_buffer(&draw.buffer, 0, bytemuck::bytes_of(&u));
+ Some(&*draw)
+ }
+ None => None,
+ };
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("mesh"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
@@ -763,6 +1236,11 @@ impl MeshView {
occlusion_query_set: None,
multiview_mask: None,
});
+ if let Some(draw) = grid {
+ pass.set_pipeline(&draw.pipeline);
+ pass.set_bind_group(0, &draw.bind, &[]);
+ pass.draw(0..3, 0..1);
+ }
pass.set_bind_group(0, &self.view_bind, &[]);
if let Some(backdrop) = &self.backdrop {
pass.set_pipeline(backdrop);
@@ -771,13 +1249,73 @@ impl MeshView {
pass.set_pipeline(&self.pipeline);
pass.set_vertex_buffer(0, self.vertices.slice(..));
pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);
- for p in &self.parts {
+ for (p, shown) in self.parts.iter().zip(&shown_by_part) {
pass.set_bind_group(1, &p.bind, &[]);
- pass.draw_indexed(p.first..p.first + p.count, 0, 0..1);
+ for range in shown {
+ pass.draw_indexed(range.clone(), 0, 0..1);
+ }
}
+ if let Some(draw) = studio_draw {
+ pass.set_bind_group(0, &draw.bind, &[]);
+ pass.set_pipeline(&draw.floor);
+ pass.draw(0..6, 0..1);
+ pass.set_pipeline(&draw.cubes);
+ pass.draw(0..36, 0..shown as u32);
+ }
+ }
+}
+
+/// A part's bindings: its albedo, the sampler, its uniform, and its roughness and metalness maps.
+fn part_bind(
+ device: &wgpu::Device,
+ layout: &wgpu::BindGroupLayout,
+ sampler: &wgpu::Sampler,
+ views: &[wgpu::TextureView; 3],
+ uniform: &wgpu::Buffer,
+) -> wgpu::BindGroup {
+ let [albedo, roughness, metallic] = views;
+ device.create_bind_group(&wgpu::BindGroupDescriptor {
+ label: Some("mesh part"),
+ layout,
+ entries: &[
+ wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(albedo) },
+ wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::Sampler(sampler) },
+ wgpu::BindGroupEntry { binding: 2, resource: uniform.as_entire_binding() },
+ wgpu::BindGroupEntry { binding: 3, resource: wgpu::BindingResource::TextureView(roughness) },
+ wgpu::BindGroupEntry { binding: 4, resource: wgpu::BindingResource::TextureView(metallic) },
+ ],
+ })
+}
+
+/// A part's channel map at `binding`, sampled as its albedo is.
+fn map_entry(binding: u32) -> wgpu::BindGroupLayoutEntry {
+ wgpu::BindGroupLayoutEntry {
+ binding,
+ visibility: wgpu::ShaderStages::FRAGMENT,
+ ty: wgpu::BindingType::Texture {
+ sample_type: wgpu::TextureSampleType::Float { filterable: true },
+ view_dimension: wgpu::TextureViewDimension::D2,
+ multisampled: false,
+ },
+ count: None,
}
}
+/// How far in front of the camera `at` is, along its view.
+fn orbit_depth(orbit: &Orbit, at: [f32; 3]) -> f32 {
+ let eye = orbit.eye();
+ let t = orbit.target;
+ let forward = [t[0] - eye[0], t[1] - eye[1], t[2] - eye[2]];
+ let length = (forward[0] * forward[0] + forward[1] * forward[1] + forward[2] * forward[2]).sqrt().max(1e-9);
+ ((at[0] - eye[0]) * forward[0] + (at[1] - eye[1]) * forward[1] + (at[2] - eye[2]) * forward[2]) / length
+}
+
+/// A linear-light value as sRGB.
+fn linear_to_srgb(c: f32) -> f32 {
+ let c = c.clamp(0.0, 1.0);
+ if c <= 0.003_130_8 { c * 12.92 } else { 1.055 * c.powf(1.0 / 2.4) - 0.055 }
+}
+
impl MeshView {
/// Carries `stroke` (premultiplied, drawn on a `width` × `height` screen, inside `bounds`) onto
/// part `part` as the camera sees it, into `target` (the part's texture, its size square, the
@@ -799,6 +1337,10 @@ impl MeshView {
let Some(p) = self.parts.get(part) else { return IRect::EMPTY };
let (first, count) = (p.first, p.count);
let view_proj = self.orbit.view_proj(width as f32 / height as f32);
+ // what's shown of each part, worked out before the projector is borrowed
+ let shown_by_part: Vec<Vec<std::ops::Range<u32>>> =
+ (0..self.parts.len()).map(|i| self.shown_ranges(i)).collect();
+ let shown = |i: usize| shown_by_part.get(i).cloned().unwrap_or_default();
let projector = self.projector.get_or_insert_with(|| Projector::new(gpu));
let stale = projector.screen.as_ref().is_none_or(|(t, _, _)| t.width() != width || t.height() != height);
if stale {
@@ -919,7 +1461,9 @@ impl MeshView {
pass.set_bind_group(0, &bind, &[]);
pass.set_vertex_buffer(0, self.vertices.slice(..));
pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);
- pass.draw_indexed(0..(self.triangles.len() * 3) as u32, 0, 0..1);
+ for range in (0..self.parts.len()).flat_map(&shown) {
+ pass.draw_indexed(range, 0, 0..1);
+ }
}
// the part in its texture's space, taking the stroke where it is the nearest surface
{
@@ -944,7 +1488,9 @@ impl MeshView {
pass.set_bind_group(0, &bind, &[]);
pass.set_vertex_buffer(0, self.vertices.slice(..));
pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);
- pass.draw_indexed(first..first + count, 0, 0..1);
+ for range in shown(part) {
+ pass.draw_indexed(range, 0, 0..1);
+ }
}
// grown past the islands' edges into the target
{
diff --git a/crates/gpu/src/shaders/mesh.wgsl b/crates/gpu/src/shaders/mesh.wgsl
index af41ab7..8bc4c25 100644
--- a/crates/gpu/src/shaders/mesh.wgsl
+++ b/crates/gpu/src/shaders/mesh.wgsl
@@ -1,5 +1,5 @@
// A 3D model drawn textured and lit: each part's albedo (premultiplied, over the part's base
-// colour where nothing is painted) shaded physically, by two lights fixed to the camera and by an
+// colour where nothing is painted) shaded physically, by up to four lights about it and by an
// environment (its irradiance for diffuse light, a prefiltered radiance cube for reflections,
// with the split-sum table), in the camera's space. Behind it, optionally, the environment's
// irradiance as a backdrop.
@@ -9,10 +9,8 @@ struct View {
view_proj: mat4x4<f32>,
view: mat4x4<f32>,
// each light's place in the camera's space, and its radiance
- light0: vec4<f32>,
- radiance0: vec4<f32>,
- light1: vec4<f32>,
- radiance1: vec4<f32>,
+ lights: array<vec4<f32>, 4>,
+ radiances: array<vec4<f32>, 4>,
// the environment's irradiance as spherical harmonics (bands 0 to 2)
sh: array<vec4<f32>, 9>,
// exposure, the radiance cube's last level, and the target's size
@@ -23,6 +21,8 @@ struct View {
c0: vec4<f32>,
c1: vec4<f32>,
c2: vec4<f32>,
+ // how many lights there are
+ counts: vec4<f32>,
}
struct Part {
@@ -39,6 +39,9 @@ struct Part {
@group(1) @binding(0) var albedo: texture_2d<f32>;
@group(1) @binding(1) var albedo_smp: sampler;
@group(1) @binding(2) var<uniform> part: Part;
+// roughness and metalness painted over the material's (premultiplied greys)
+@group(1) @binding(3) var roughness_map: texture_2d<f32>;
+@group(1) @binding(4) var metallic_map: texture_2d<f32>;
const PI: f32 = 3.14159265;
@@ -120,8 +123,12 @@ fn fs_main(i: Out) -> @location(0) vec4<f32> {
let painted = textureSample(albedo, albedo_smp, i.uv);
let color = painted.rgb + part.base.rgb * (1.0 - painted.a);
let albedo_lin = pow(max(color, vec3<f32>(0.0)), vec3<f32>(2.2));
- let roughness = part.material.x;
- let metallic = part.material.y;
+ // a painted channel's grey over the material's value, as far as it covers
+ let rm = textureSample(roughness_map, albedo_smp, i.uv);
+ let mm = textureSample(metallic_map, albedo_smp, i.uv);
+ let grey = vec3<f32>(0.2126, 0.7152, 0.0722);
+ let roughness = part.material.x * (1.0 - rm.a) + dot(rm.rgb, grey);
+ let metallic = part.material.y * (1.0 - mm.a) + dot(mm.rgb, grey);
let v = normalize(-i.eye);
// a normal turned away from the eye (the far side of an open surface, or a smooth one's
// outline) mirrored across the view plane: facing the eye, its slant kept
@@ -132,8 +139,13 @@ fn fs_main(i: Out) -> @location(0) vec4<f32> {
}
let r = max(roughness, 0.045);
let f0 = mix(vec3<f32>(0.04), albedo_lin, metallic);
- var lo = direct(n, v, normalize(view.light0.xyz - i.eye), view.radiance0.xyz, albedo_lin, f0, r, metallic);
- lo += direct(n, v, normalize(view.light1.xyz - i.eye), view.radiance1.xyz, albedo_lin, f0, r, metallic);
+ var lo = vec3<f32>(0.0);
+ let count = i32(view.counts.x);
+ for (var k = 0; k < 4; k++) {
+ if (k < count) {
+ lo += direct(n, v, normalize(view.lights[k].xyz - i.eye), view.radiances[k].xyz, albedo_lin, f0, r, metallic);
+ }
+ }
// the environment: diffuse light from its irradiance, reflections from the prefiltered cube
let nv = clamp(dot(n, v), 1e-5, 1.0);
let reflected = textureSampleLevel(radiance, radiance_smp, reflect(-v, n), r * view.params.y).rgb;
diff --git a/crates/gpu/src/shaders/studio.wgsl b/crates/gpu/src/shaders/studio.wgsl
new file mode 100644
index 0000000..ac68821
--- /dev/null
+++ b/crates/gpu/src/shaders/studio.wgsl
@@ -0,0 +1,124 @@
+// The Lighting Studio's scene around a model: a round floor of grid lines under it, fading toward
+// its rim, and each light as a small glowing cube in the light's colour.
+
+struct Studio {
+ view_proj: mat4x4<f32>,
+ // the floor's height, its radius, a grid cell's side, and the line width (in cells)
+ floor: vec4<f32>,
+ // each light's place and the cube's side
+ cubes: array<vec4<f32>, 4>,
+ // each light's colour (sRGB 0..1)
+ colors: array<vec4<f32>, 4>,
+ // the target's colours: x 1 when it encodes sRGB itself
+ output: vec4<f32>,
+}
+
+@group(0) @binding(0) var<uniform> studio: Studio;
+
+fn srgb_to_linear(c: vec3<f32>) -> vec3<f32> {
+ let lo = c / 12.92;
+ let hi = pow((c + 0.055) / 1.055, vec3<f32>(2.4));
+ return select(hi, lo, c <= vec3<f32>(0.04045));
+}
+
+fn out_color(rgb: vec3<f32>, a: f32) -> vec4<f32> {
+ var c = rgb;
+ if (studio.output.x > 0.5) {
+ c = srgb_to_linear(c);
+ }
+ return vec4<f32>(c * a, a);
+}
+
+// ---- the floor ------------------------------------------------------------------------------
+
+struct FloorOut {
+ @builtin(position) clip: vec4<f32>,
+ @location(0) at: vec2<f32>,
+}
+
+@vertex
+fn vs_floor(@builtin(vertex_index) vi: u32) -> FloorOut {
+ // two triangles over the floor's square
+ var corners = array<vec2<f32>, 6>(
+ vec2<f32>(-1.0, -1.0), vec2<f32>(1.0, -1.0), vec2<f32>(1.0, 1.0),
+ vec2<f32>(-1.0, -1.0), vec2<f32>(1.0, 1.0), vec2<f32>(-1.0, 1.0),
+ );
+ let r = studio.floor.y;
+ let xz = corners[vi] * r;
+ var o: FloorOut;
+ o.clip = studio.view_proj * vec4<f32>(xz.x, studio.floor.x, xz.y, 1.0);
+ o.at = xz;
+ return o;
+}
+
+@fragment
+fn fs_floor(i: FloorOut) -> @location(0) vec4<f32> {
+ let r = studio.floor.y;
+ let cell = studio.floor.z;
+ let p = i.at / cell;
+ // how far from the nearest line across and along, in pixels (for even lines at any slant)
+ let d = abs(p - round(p)) / max(fwidth(p), vec2<f32>(1e-4));
+ let line = 1.0 - clamp(min(d.x, d.y) - 0.5, 0.0, 1.0);
+ // full in the middle, gone at the rim
+ let rim = length(i.at) / r;
+ let fade = 1.0 - smoothstep(0.45, 1.0, rim);
+ let a = line * fade * 0.85;
+ if (a <= 0.003) {
+ discard;
+ }
+ return out_color(vec3<f32>(0.93, 0.95, 1.0), a);
+}
+
+// ---- the lights' cubes ----------------------------------------------------------------------
+
+struct CubeOut {
+ @builtin(position) clip: vec4<f32>,
+ @location(0) normal: vec3<f32>,
+ @location(1) color: vec3<f32>,
+ @location(2) local: vec3<f32>,
+}
+
+@vertex
+fn vs_cube(@builtin(vertex_index) vi: u32, @builtin(instance_index) light: u32) -> CubeOut {
+ // six faces of two triangles: a face's normal axis and its corners
+ let face = vi / 6u;
+ var corners = array<vec2<f32>, 6>(
+ vec2<f32>(-1.0, -1.0), vec2<f32>(1.0, -1.0), vec2<f32>(1.0, 1.0),
+ vec2<f32>(-1.0, -1.0), vec2<f32>(1.0, 1.0), vec2<f32>(-1.0, 1.0),
+ );
+ let corner = corners[vi % 6u];
+ var n = vec3<f32>(0.0);
+ var p = vec3<f32>(0.0);
+ switch face {
+ case 0u: { n = vec3<f32>(1.0, 0.0, 0.0); p = vec3<f32>(1.0, corner.x, -corner.y); }
+ case 1u: { n = vec3<f32>(-1.0, 0.0, 0.0); p = vec3<f32>(-1.0, corner.x, corner.y); }
+ case 2u: { n = vec3<f32>(0.0, 1.0, 0.0); p = vec3<f32>(corner.x, 1.0, -corner.y); }
+ case 3u: { n = vec3<f32>(0.0, -1.0, 0.0); p = vec3<f32>(corner.x, -1.0, corner.y); }
+ case 4u: { n = vec3<f32>(0.0, 0.0, 1.0); p = vec3<f32>(corner.x, corner.y, 1.0); }
+ default: { n = vec3<f32>(0.0, 0.0, -1.0); p = vec3<f32>(-corner.x, corner.y, -1.0); }
+ }
+ let cube = studio.cubes[light];
+ // turned a little about the upright and leaning, so three faces show
+ let yaw = 0.6;
+ let lean = 0.35;
+ let ry = mat3x3<f32>(cos(yaw), 0.0, -sin(yaw), 0.0, 1.0, 0.0, sin(yaw), 0.0, cos(yaw));
+ let rx = mat3x3<f32>(1.0, 0.0, 0.0, 0.0, cos(lean), sin(lean), 0.0, -sin(lean), cos(lean));
+ let turn = ry * rx;
+ var o: CubeOut;
+ o.clip = studio.view_proj * vec4<f32>(cube.xyz + turn * p * (cube.w * 0.5), 1.0);
+ o.normal = turn * n;
+ o.color = studio.colors[light].rgb;
+ o.local = p;
+ return o;
+}
+
+@fragment
+fn fs_cube(i: CubeOut) -> @location(0) vec4<f32> {
+ // a frosted, glowing block: mostly its own colour, the top brightest, the edges softened
+ let n = normalize(i.normal);
+ let shade = 0.82 + 0.18 * n.y;
+ let edge = max(max(abs(i.local.x) * abs(i.local.y), abs(i.local.y) * abs(i.local.z)), abs(i.local.x) * abs(i.local.z));
+ let soft = 1.0 - 0.12 * smoothstep(0.75, 1.0, edge);
+ let c = mix(vec3<f32>(1.0), i.color, 0.85) * shade * soft;
+ return out_color(clamp(c, vec3<f32>(0.0), vec3<f32>(1.0)), 1.0);
+}
diff --git a/crates/gpu/tests/mesh.rs b/crates/gpu/tests/mesh.rs
index 6e4a6b6..10b385a 100644
--- a/crates/gpu/tests/mesh.rs
+++ b/crates/gpu/tests/mesh.rs
@@ -3,6 +3,7 @@
use procraft_engine::mesh::read_obj;
use procraft_gpu::{Gpu, MeshView, Orbit, read_texture, wgpu};
+use wgpu::util::DeviceExt;
fn gpu() -> Option<Gpu> {
Gpu::headless().ok()
@@ -375,3 +376,132 @@ fn a_mirror_reflects_what_is_behind_the_camera() {
assert!(back[0] > 150 && back[1] < 60, "the environment turns with the camera: {back:?}");
assert_eq!(gpu.errors(), 0);
}
+
+#[test]
+fn without_its_environment_the_model_stands_on_the_surround_grid() {
+ let Some(gpu) = gpu() else { return };
+ let mesh = read_obj(QUADS, None).unwrap();
+ let mut view = MeshView::new(&gpu, &mesh, 4, wgpu::TextureFormat::Rgba8Unorm);
+ view.orbit = Orbit { target: [0.0, 0.0, 0.0], rotation: Orbit::rotation_of(0.0, 0.0), distance: 6.0, fov: 0.9 };
+ // lines 2 pixels wide every 8, from 4 down
+ view.set_grid(Some(procraft_gpu::SurroundGrid {
+ spacing: 8.0,
+ origin: [0.0, 4.0],
+ width: 2.0,
+ color: [0.5, 0.5, 0.5],
+ }));
+ let px = draw(&gpu, &mut view);
+ // at the corner, a line's pixel and the clear colour beside it; nothing over the grid's top
+ assert!(pixel(&px, 0, 12)[0] > 100, "{:?}", pixel(&px, 0, 12));
+ assert_eq!(pixel(&px, 3, 14)[0], 0);
+ assert_eq!(pixel(&px, 0, 2)[0], 0);
+ // the model is drawn over it
+ let middle = pixel(&px, SIDE / 2, SIDE / 2);
+ assert!(middle[0] > 150 && middle[1] > 150, "{middle:?}");
+ assert_eq!(gpu.errors(), 0);
+}
+
+#[test]
+fn the_studio_shows_each_light_as_a_cube_and_moves_it_across_the_screen() {
+ let Some(gpu) = gpu() else { return };
+ let mesh = read_obj(QUADS, None).unwrap();
+ let mut view = MeshView::new(&gpu, &mesh, 4, wgpu::TextureFormat::Rgba8Unorm);
+ view.lights = vec![procraft_gpu::Light { place: [-2.0, 2.0, 1.0], color: [1.0, 0.0, 0.0], intensity: 0.5 }];
+ let orbit = view.studio_orbit();
+ let [x, y, reach] = view.light_on_screen(&orbit, 0, SIDE, SIDE).unwrap();
+ assert!(x > 0.0 && x < SIDE as f32 / 2.0 && y > 0.0 && y < SIDE as f32 / 2.0, "up and left: {x} {y}");
+ assert!(reach > 0.5, "{reach}");
+ let draw_studio = |view: &mut MeshView| {
+ let target = gpu.device.create_texture(&wgpu::TextureDescriptor {
+ label: Some("studio target"),
+ size: wgpu::Extent3d { width: SIDE, height: SIDE, depth_or_array_layers: 1 },
+ mip_level_count: 1,
+ sample_count: 1,
+ dimension: wgpu::TextureDimension::D2,
+ format: wgpu::TextureFormat::Rgba8Unorm,
+ usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC,
+ view_formats: &[],
+ });
+ let target_view = target.create_view(&wgpu::TextureViewDescriptor::default());
+ view.show_environment(&gpu, false);
+ let mut encoder = gpu.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("test") });
+ view.render_studio(&gpu, &mut encoder, &target_view, SIDE, SIDE, [0.0, 0.0, 0.0, 1.0], orbit);
+ gpu.queue.submit([encoder.finish()]);
+ read_texture(&gpu, &target, SIDE, SIDE).unwrap()
+ };
+ let px = draw_studio(&mut view);
+ let cube = pixel(&px, x as u32, y as u32);
+ assert!(cube[0] > 150 && cube[1] < cube[0] / 2, "a red cube: {cube:?}");
+ // the floor's lines show somewhere below the model
+ let floor = (SIDE * 3 / 4..SIDE)
+ .flat_map(|y| (0..SIDE).map(move |x| (x, y)))
+ .filter(|&(x, y)| pixel(&px, x, y)[1] > 60)
+ .count();
+ assert!(floor > 10, "{floor} floor pixels");
+ // dragged 10 pixels right and 6 down, it follows
+ view.move_light(&orbit, 0, 10.0, 6.0, SIDE, SIDE);
+ let [x2, y2, _] = view.light_on_screen(&orbit, 0, SIDE, SIDE).unwrap();
+ assert!((x2 - x - 10.0).abs() < 0.5 && (y2 - y - 6.0).abs() < 0.5, "{x} {y} -> {x2} {y2}");
+ assert_eq!(gpu.errors(), 0);
+}
+
+#[test]
+fn a_hidden_submesh_is_not_drawn_and_each_has_its_own_picture() {
+ let Some(gpu) = gpu() else { return };
+ let mesh = read_obj(&format!("o Front\n{QUADS}").replace("usemtl far", "o Back\nusemtl far"), None).unwrap();
+ assert_eq!(mesh.submeshes.iter().map(|m| m.name.as_str()).collect::<Vec<_>>(), ["Front", "Back"]);
+ let mut view = MeshView::new(&gpu, &mesh, 4, wgpu::TextureFormat::Rgba8Unorm);
+ view.set_base(&gpu, 0, [1.0, 0.0, 0.0]);
+ view.set_base(&gpu, 1, [0.0, 0.0, 1.0]);
+ view.orbit = Orbit { target: [0.0, 0.0, 0.0], rotation: Orbit::rotation_of(0.0, 0.0), distance: 6.0, fov: 0.9 };
+ // the near square hidden, the far one shows through where it was
+ view.set_hidden(0, true);
+ let px = draw(&gpu, &mut view);
+ let middle = pixel(&px, SIDE / 2, SIDE / 2);
+ assert!(middle[2] > 100 && middle[0] < middle[2] / 3, "{middle:?}");
+ view.set_hidden(0, false);
+ // the near square alone fills its picture, on nothing at the corner
+ let picture = view.submesh_picture(&gpu, 0, SIDE).unwrap();
+ let centre = pixel(&picture, SIDE / 2, SIDE / 2);
+ assert!(centre[0] > 100 && centre[3] == 255, "{centre:?}");
+ assert_eq!(pixel(&picture, 0, 0)[3], 0);
+ // and the view is as it was
+ let again = draw(&gpu, &mut view);
+ assert!(pixel(&again, SIDE / 2, SIDE / 2)[0] > 100);
+ assert_eq!(gpu.errors(), 0);
+}
+
+#[test]
+fn a_metalness_painted_over_the_material_changes_how_it_shades() {
+ let Some(gpu) = gpu() else { return };
+ let mesh = read_obj(QUADS, None).unwrap();
+ let mut view = MeshView::new(&gpu, &mesh, 4, wgpu::TextureFormat::Rgba8Unorm);
+ view.set_base(&gpu, 0, [0.8, 0.2, 0.2]);
+ view.orbit = Orbit { target: [0.0, 0.0, 0.0], rotation: Orbit::rotation_of(0.0, 0.0), distance: 6.0, fov: 0.9 };
+ let plain = draw(&gpu, &mut view);
+ // fully metal: the red base reflects instead of scattering
+ let white = gpu.device.create_texture_with_data(
+ &gpu.queue,
+ &wgpu::TextureDescriptor {
+ label: Some("metal"),
+ size: wgpu::Extent3d { width: 1, height: 1, depth_or_array_layers: 1 },
+ mip_level_count: 1,
+ sample_count: 1,
+ dimension: wgpu::TextureDimension::D2,
+ format: wgpu::TextureFormat::Rgba8Unorm,
+ usage: wgpu::TextureUsages::TEXTURE_BINDING,
+ view_formats: &[],
+ },
+ wgpu::util::TextureDataOrder::LayerMajor,
+ &[255, 255, 255, 255],
+ );
+ let map = white.create_view(&wgpu::TextureViewDescriptor::default());
+ view.set_channel_view(&gpu, 0, 2, Some(&map));
+ let metal = draw(&gpu, &mut view);
+ let (a, b) = (pixel(&plain, SIDE / 2, SIDE / 2), pixel(&metal, SIDE / 2, SIDE / 2));
+ assert_ne!(a, b);
+ // and back to the material's own
+ view.set_channel_view(&gpu, 0, 2, None);
+ assert_eq!(pixel(&draw(&gpu, &mut view), SIDE / 2, SIDE / 2), a);
+ assert_eq!(gpu.errors(), 0);
+}