DEV Community

Cover image for Turing CAD: An Anime Cel Style on a Cutting Mat
alphanine
alphanine

Posted on

Turing CAD: An Anime Cel Style on a Cutting Mat

Turing CAD can now draw a Gundam frame as an anime cel, standing on a cutting mat.

中文版在文末,点开即可阅读。

Cel Shaded style on the cutting mat

Chest Frame 34 drawn as an anime cel, standing on a cutting mat with a soft-edged cel shadow.

The cutting mat as a slab on an endless board

Zoomed out, the mat is a 3 mm slab with inked edges and its own shadow on an endless board.

The shipped cel shadow: flat with a little blur

The flat cel shadow at blur 0, .35 (default) and .6.


Painted Gunpla photos often look like anime frames. Could a CAD viewport draw a model that way while you're still modelling it?

Situation: three CAD looks and a chest frame that wanted to look painted

Turing CAD is a desktop CAD app for robot parts and Gunpla-style kits, with a small Metal viewport written for modelling. It had three looks: Modeling, Clean Model and Studio. I was modelling a Barbatos chest frame (22 parts) and wanted it to read like a painted kit: white panels, blue-grey shadows, black ink.

Two small bugs came up first. A design name with a / in it, Chest Frame 34 · closed thicker sidewalls / shorter inward ledges, turned the default Export USD folder into a path through folders that don't exist, and the export failed. Clean Model also drew metal pistons as chrome.

Clean Model drawing the pistons as chrome
Before · Clean Model drew the metal pistons as chrome

The rods alone, metal reflections on and off
The two hydraulic rods on their own · reflections on (left) · off, as Clean Model now draws them (right)

Task: a cel look in a live modelling viewport

The look had to live in the viewport itself, not in an offline renderer, and stay interactive on a 22-part assembly. Every choice had to be a setting, and the other styles had to stay as they were.

What made it hard:

  • Cel shading needs hard steps and crisp lines, but the viewport's shadows were soft, filtered shadow maps.
  • The outline of a round part is not a B-rep edge, so CAD edges alone can't ink it.
  • A floor that reads as a desk has to hold up at any zoom without showing where it ends.

Action: cel bands, a cutting mat, and shadows that stop moving

What we built

Cel Shaded style. One hard step from the lit colour to a cooler shadow tone, a hard white highlight, and metal as two flat bands. Silhouettes are inked with an inverted hull culled by normal (3 px), and CAD edges at 2 px. A round rod shows why both are needed: its long outline isn't a B-rep edge at all.

The rods with and without the silhouette outline
The same rods in Cel Shaded · only the inverted hull inks the round silhouette

First cel pass on a black kit
First pass · one shadow step works, but black ink vanishes into black plastic

The black housing alone in Cel Shaded
Fixed · housing B11 on its own, charcoal in the light so the ink reads · and with material colours off

Cutting mat. Painted once with QPainter at true millimetre scale (rulers, protractor, hexagons, a title panel) and sized to the model, A5 and up. It's a 3 mm slab with inked edges lying on an endless board.

The painted mat texture
The mat's painted layer · its 10/50 mm grid lines are drawn by the shader instead

The mat as a slab on an endless board
Zoomed out · a 3 mm slab with inked edges and its own shadow on the board

Cel shadows. The filtered shadow value is cut at one half over a single pixel, so a cast shadow becomes a flat shape with a clean edge. The model's own shadow is a separate hard step. On the mat, Shadow softness then blurs that edge back in: after feedback that its maximum wasn't soft enough, it now widens the blur itself, from about 12 to 60 shadow-map texels, sampled in a rotated disc so it grains rather than bands.

Cast shadow before and after the hard cut
Before: a soft filtered blob · After: one cut at the midpoint, a pixel wide

Housing at softness 0 and 1
Housing B11 under a low light · Shadow softness 0 · and 1, after the wider blur

Light, grid and settings. Light angle and height are sliders in every style. The ground grid is endless and drawn per pixel, stepping 1, 10 and 100 mm as you zoom. Settings are remembered per style, with a one-click Reset to Default.

Housing under three light directions
One part, three lights · angle 305° · 215°, with the shadow side towards the camera · a low 20° light

The grid ending in a hard line
Before · the fixed 120 mm grid ended in a hard line

The grid shrinking to a disc when zoomed out
Still before · a fade fixed in world size shrank the grid to a small disc when zoomed out

The endless grid at three zooms
After · the grid fills the view at every zoom, on Cel and on the dark Modeling style

Display panel controls
Display · light angle and height, cel shadow, and the mat's shadow

Inspector and Browser. Each Inspector tab shows only its own content. The Browser selects and highlights whatever part you pick or hover in the viewport, even through a face, edge or vertex.

Info tab with Joints stacked on top
Before · the Info tab opened with the Joints pane on top of it

A selected part with no highlight in the Browser
Before · the part is selected in the viewport, the Browser doesn't show it

The Browser selecting the picked part
After · a face of A11 picked on Geometry selects A11's row

What we tested

  • Every style, rendered for real. A headed check opens a window and renders each style through Metal. It grew to 40 steps covering per-style memory, the light sliders moving the shadow and redrawing its map, Reset to Default leaving other styles alone, and the Inspector tabs not stacking.
  • The Browser, without a GPU. An offscreen selection test hovers and clicks parts and faces, then checks that the right row is highlighted or selected.
  • The real model at each step. I rendered Chest Frame 34 after every change, at normal zoom, zoomed out 6× and close up, and compared variants side by side.
  • Single parts for single features. To show one effect at a time, parts were isolated and rendered under exactly the setting in question: the hydraulic rods for metal and outlines, the black housing B11 for charcoal, softness and light direction.
  • The Info tab, profiled. The first open took 4.1 s, almost all of it in OCCT's volume and surface integrals for 22 parts, while other tabs switched in 10–25 ms.
  • The whole suite before merging. 1330 tests. It surfaced two problems of my own, both fixed before the merge (see below). The two failures left are unrelated: 32 new designs without previews yet, and a downloaded model the worktree didn't have.

What surprised us

Black parts swallowed the ink. A black outline on a black part disappears. The fix was to paint dark parts charcoal in the light, the way kit painters treat black plastic.

The mat's lines went soft up close. The texture only had about 6 pixels per millimetre, so fine lines smeared when magnified. They moved into the shader, which draws them per pixel at any zoom.

A redundant integral. The mass code integrated each part twice to move inertia to the centroid. While testing a one-pass version, the numbers were off by millions, because OCCT already reports inertia about the centroid. One integral was always enough, and the result is now cached next to the cached solids: 1.4 s the first time, then about 14 ms.

The "jagged" shadow ink was real. Inked shadow edges looked rough until the cause turned out to be the shadows of a ring's gear teeth, not stair-steps from the shadow map.

The reference technique lost. I tried a painted anime-background shadow: blue multiply, fade from the contact, a warm glow along the edge. The flat cel shadow with a little blur suited the model better, so that's the default and the rest are sliders.

Painted background shadow against the flat one
Tried · the painted background technique (right) against the flat cel shadow (left)

Three blur levels
Shipped · the flat shadow with a little blur · 0, .35 (default), .6

Shadows kept swinging. With the light turning with the camera, shadows moved on every orbit. Cel now keeps a fixed light. Old saved settings kept bringing the moving light back until startup learned to read each style's own memory.

My own test crashed the suite. A new test created a bare QGuiApplication; a later test found it, tried to build a widget on it, and Qt aborted the whole run. Separately, tutorial steps pointed at sliders that the new Inspector tabs now hid. Both were fixed before the merge.

Result: four styles, a mat to stand on, and a suite that runs to the end

On Chest Frame 34 the viewport now reads like a painted kit photo: material colours, blue-grey shadow sides, black ink, and a crisp, slightly blurred shadow on a cutting mat that scales with the model. Opening the Info tab went from 4.1 s to 1.4 s, then about 14 ms. The work is merged to main, and the same look has since been ported to the offline renderer in the Turing Render series.

Close-up with the defaults
Close-up · crisp printed grid · cel shadow on the mat and on the model

Limits remain:

  • The cel look lives in the modelling viewport; Turing CAD can't export a cel render yet.
  • The mat's printed templates still soften at extreme close-up.
  • The merge also carried another session's uncommitted work, which needs its own review.

Next: model the next Barbatos unit in Cel Shaded, and see whether the mat's templates deserve the same per-pixel treatment as its grid.


中文版 · Chinese version(点击展开)

Turing CAD 现在能把高达骨架画成动画赛璐珞,放在切割垫上。

涂装好的高达照片常常像一帧动画。CAD 视口能不能在建模的同时,就把模型画成这样?

背景:三种 CAD 样式,和一个想看起来像涂装件的胸部骨架

Turing CAD 是一个用来做机器人零件和高达风格模型的桌面 CAD,视口是一个为建模写的小型 Metal 渲染器,原有三种样式:Modeling、Clean Model 和 Studio。我在做 Barbatos 胸部骨架(22 个零件),希望它看起来像涂装好的模型:白色板件、蓝灰阴影、黑色墨线。

先冒出两个小问题。设计名里带 /(Chest Frame 34 · closed thicker sidewalls / shorter inward ledges)时,Export USD 的默认文件夹会变成一串不存在的目录,导出失败;Clean Model 还把金属活塞杆画成了镜面。

Clean Model 把活塞杆画成镜面
之前 · Clean Model 把金属活塞杆画成了镜面

单独的活塞杆:开和关金属反射
单独渲染的两根活塞杆 · 左:开启金属反射 · 右:关闭,也就是 Clean Model 现在的画法

目标与难点:在实时建模视口里做出赛璐珞质感

效果要直接做在视口里,而不是放进离线渲染器,在 22 个零件的装配体上也要保持实时。每种效果都要是设置项,其他样式原样不动。

难点有三个:

  • 赛璐珞需要硬过渡和干净的线条,而视口原来的阴影是柔和的滤波阴影贴图。
  • 圆形零件的外轮廓不是 B-rep 边,只靠 CAD 边线描不出来。
  • 像桌面一样的地面要在任何缩放下都成立,而且看不到尽头。

解决思路与行动:色阶、切割垫,以及不再乱动的阴影

做了什么

  1. Cel Shaded 样式。 亮部到偏冷暗部只有一次硬过渡,加一个硬白高光,金属画成两段平色。外轮廓用按法线剔除的反向外壳描墨线(3 px),CAD 边线 2 px。

单独的黑色外壳 B11
单独渲染的黑色外壳 B11 · 亮部画成炭灰,墨线清晰可见 · 以及关闭材质颜色的样子

  1. 切割垫。 用 QPainter 按真实毫米尺寸画一次(刻度、量角器、六边形、标题框),按模型大小自动选尺寸,A5 起。它是一块 3 mm 厚、边缘描墨线的板,放在无限延伸的板面上。

切割垫是一块放在无限板面上的厚板
缩小视角 · 3 mm 厚、描墨线边缘的垫子,在板面上投下自己的阴影

  1. 赛璐珞阴影。 把滤波后的阴影值在中间以一个像素宽硬切,投影就成了边缘干净的平涂形状;模型自身的投影单独硬切一次。垫子上的 Shadow softness 再把边缘柔化回来:根据“最大值还不够软”的反馈,它现在直接加大模糊半径,从约 12 个阴影贴图纹素到约 60 个,并在旋转的圆盘内采样,出现的是细颗粒而不是色带。

硬切前后的投影
之前:柔和的一团 · 之后:在中点以一个像素宽硬切

柔和度 0 和 1
低角度光源下的外壳 B11 · Shadow softness 0 · 以及加大模糊后的 1

  1. 光源、网格和设置。 所有样式都能用滑杆调光源角度和高度。地面网格改为逐像素绘制、无限延伸,随缩放在 1、10、100 mm 之间切换。设置按样式分别记住,可以一键恢复默认。

三个光源方向下的外壳
同一个零件,三种光源 · 角度 305° · 215°,暗面朝向镜头 · 20° 低角度光

  1. 检查器与 Browser。 检查器每个标签页只显示自己的内容;在视口里点选或悬停零件(包括通过面、边、顶点),Browser 会选中并高亮对应的行。

测了什么

  • 每种样式都真实渲染一遍。 一个有界面的检查会打开窗口,经 Metal 渲染每种样式。它扩展到了 40 步:按样式记忆、光源滑杆会移动阴影并重画阴影贴图、一键恢复不影响其他样式、检查器标签页不再堆叠。
  • 不用 GPU 测 Browser。 一个离屏选择测试会悬停和点击零件与面,再检查对应的行有没有被高亮或选中。
  • 每一步都看真实模型。 每次改动后都渲染 Chest Frame 34,分正常、缩小 6 倍和特写三种视角,并把变体并排比较:蓝灰与紫色阴影、有无阴影墨线、三档模糊。
  • 用单个零件展示单个效果。 为了一次只看一种效果,把零件单独显示出来,只改要说明的那个设置:活塞杆用来看金属和轮廓线,黑色外壳 B11 用来看炭灰、柔和度和光源方向。
  • 给 Info 面板做性能分析。 第一次打开要 4.1 秒,几乎全部花在 OCCT 为 22 个零件做体积和表面积积分上,而其他标签页切换只要 10–25 毫秒。
  • 合并前跑完整测试集。 共 1330 个测试,查出两个我自己造成的问题,都在合并前修好了(见下文)。剩下的两个失败与这次无关:32 个新设计还没有预览图,另一个测试需要的下载模型不在 worktree 里。

意外发现

  • 黑色零件吞掉了墨线。 黑色零件上的黑线看不见。解决办法是让深色零件在亮部变成炭灰,就像涂装师处理黑色塑料那样。
  • 垫子的线拉近就糊。 贴图每毫米只有约 6 个像素,细线放大后会糊成一片。于是把网格线搬进着色器,任何缩放下都逐像素绘制。
  • 一次多余的积分。 质量计算原来对每个零件积分两次,为的是把惯量移到质心。测试只积分一次的版本时,结果差了几百万,原因是 OCCT 给出的惯量本来就以质心为参考点。一次积分一直就够了,结果还缓存在零件实体旁边:第一次 1.4 秒,之后约 14 毫秒。
  • “锯齿”阴影墨线其实是真的。 描了墨线的阴影边缘看起来很粗糙,后来发现那是齿圈齿牙的投影,不是阴影贴图的台阶。
  • 教程里的方法输了。 我试过动画背景式的画法(蓝色正片叠底、离接触点渐淡、边缘暖色光)。略带模糊的平涂阴影更适合这个模型,于是它成了默认值,其余都做成了滑杆。

三档模糊
最终采用 · 略带模糊的平涂阴影 · 0、.35(默认)、.6

  • 阴影一直在甩。 光源跟着相机转时,每次旋转视角阴影都会动。现在 Cel 使用固定光源。旧的保存设置一直把“跟随视角”带回来,直到启动时改为读取每种样式自己的记忆。
  • 我自己的测试让整个测试集崩了。 一个新测试创建了一个单纯的 QGuiApplication,后面的测试拿到它去创建窗口部件,Qt 直接中止了整轮运行。另一个问题是教程步骤指向的滑杆被新的检查器标签页藏了起来。两个都在合并前修好了。

结果与下一步:四种样式、一张可以站的垫子,以及能跑完的测试集

在 Chest Frame 34 上,视口现在像一张涂装好的模型照片:材质颜色、蓝灰暗部、黑色墨线,以及落在切割垫上、略带模糊的清晰阴影,垫子随模型大小变化。打开 Info 面板从 4.1 秒变成 1.4 秒,之后约 14 毫秒。这批改动已合并进 main,同样的风格后来也移植到了离线渲染器,见 Turing Render 系列。

还有这些限制:

  • 卡通效果只在建模视口里,Turing CAD 还不能导出 cel 渲染图。
  • 垫子上的印刷图案在极近距离仍会变糊。
  • 这次合并还带上了另一个会话未提交的工作,需要单独复查。

下一步:用 Cel Shaded 做下一个 Barbatos 部件,再看看垫子上的印刷图案是否也值得像网格一样改成逐像素绘制。

Top comments (0)