<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kastomiko</title>
    <description>The latest articles on DEV Community by Kastomiko (@kastomiko).</description>
    <link>https://dev.to/kastomiko</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4246503%2F167ce0e5-efdd-49af-bc72-e2f542322e76.png</url>
      <title>DEV Community: Kastomiko</title>
      <link>https://dev.to/kastomiko</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kastomiko"/>
    <language>en</language>
    <item>
      <title>Why a soft shadow prints as white dots: checking customer images before UV printing</title>
      <dc:creator>Kastomiko</dc:creator>
      <pubDate>Sun, 15 Nov 2026 20:18:42 +0000</pubDate>
      <link>https://dev.to/kastomiko/why-a-soft-shadow-prints-as-white-dots-checking-customer-images-before-uv-printing-2p23</link>
      <guid>https://dev.to/kastomiko/why-a-soft-shadow-prints-as-white-dots-checking-customer-images-before-uv-printing-2p23</guid>
      <description>&lt;p&gt;At &lt;a href="https://kloniko.store/en" rel="noopener noreferrer"&gt;Kloniko&lt;/a&gt; customers upload their own images to print on a figure: a face, a logo on a torso, a pattern on the legs. In the &lt;a href="https://kloniko.store/en/pages/bundle-figures" rel="noopener noreferrer"&gt;configurator&lt;/a&gt; the image looks right. Sometimes the printed part did not: stubble, soft shadows and blush came out as whitish specks.&lt;/p&gt;

&lt;p&gt;The image was fine. The preview was wrong.&lt;/p&gt;

&lt;h2&gt;
  
  
  What a UV printer does with transparency
&lt;/h2&gt;

&lt;p&gt;A UV printer does not blend ink with the surface. To make colours show on a coloured part, it first lays down a &lt;strong&gt;white underbase&lt;/strong&gt; wherever the image has any pixel, and then prints the colour on top.&lt;/p&gt;

&lt;p&gt;The underbase has no "30% opacity". A pixel with alpha 60 out of 255 gets 100% white under it and only a little colour over it.&lt;/p&gt;

&lt;p&gt;On screen, the browser blends that pixel with the skin-coloured part, and you see a soft shadow. On the part, you get an almost white dot.&lt;/p&gt;

&lt;h2&gt;
  
  
  Simulating the print
&lt;/h2&gt;

&lt;p&gt;So we render two versions of every uploaded image in the browser, on a canvas:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// what the screen shows: normal alpha blending with the part colour&lt;/span&gt;
&lt;span class="n"&gt;screen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;src&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;part&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// what the printer makes: colour over a white base, wherever a &amp;gt; 0&lt;/span&gt;
&lt;span class="n"&gt;uv&lt;/span&gt;     &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;src&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;white&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the two differ a lot over a noticeable area, the customer has a problem they cannot see. Our test for it is simple: a grid of semi-transparent brown dots on a skin-coloured part must come out clearly lighter in the UV version than on screen.&lt;/p&gt;

&lt;p&gt;A solid shape with an anti-aliased edge must &lt;strong&gt;not&lt;/strong&gt; trigger the warning. One row of soft pixels around a logo is normal, and warning about it would train people to ignore the dialog.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fixing it for the customer
&lt;/h2&gt;

&lt;p&gt;Showing a warning is not enough, most customers cannot fix an image themselves. So the dialog offers a corrected version next to the simulation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// alpha below the threshold: drop the pixel&lt;/span&gt;
&lt;span class="c1"&gt;// alpha above it: make it solid, pre-blended with the part colour&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;transparent&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;rgb&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;src&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;part&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;255&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The pixel keeps the colour the customer saw on screen, but it is now opaque, so the white base is fully covered. The shadow looks like a shadow again.&lt;/p&gt;

&lt;p&gt;After any scaling we harden the alpha once more, so it is only 0 or 255. Resizing brings semi-transparent edges back.&lt;/p&gt;

&lt;h2&gt;
  
  
  The second problem: tiny dots
&lt;/h2&gt;

&lt;p&gt;Very small separate dots (a few pixels, freckles, noise from a bad cut-out) either do not print or print as specks. We find connected groups of pixels below a minimum size and offer two choices: remove them or enlarge them to a printable size. A large shape next to them stays untouched.&lt;/p&gt;

&lt;h2&gt;
  
  
  What we learned
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;If production differs from the screen, simulate production in the preview. The customer should approve what will be made, not what was uploaded.&lt;/li&gt;
&lt;li&gt;Offer the fix, not just the warning.&lt;/li&gt;
&lt;li&gt;Put the check on every upload path. We wrap each one in the same &lt;code&gt;checkUvPrint&lt;/code&gt; call, a new path without it is a bug.&lt;/li&gt;
&lt;li&gt;Cover it with tests on tiny synthetic images: a 2×1 pixel image is enough to pin the alpha rules.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We build print-ready configurators and web-to-print tools at &lt;a href="https://kastomiko.com/" rel="noopener noreferrer"&gt;Kastomiko&lt;/a&gt;: &lt;a href="https://kastomiko.com/product-configurator-development" rel="noopener noreferrer"&gt;product configurator development&lt;/a&gt;, &lt;a href="https://kastomiko.com/web-to-print-software" rel="noopener noreferrer"&gt;web-to-print software&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>canvas</category>
      <category>imageprocessing</category>
      <category>ecommerce</category>
    </item>
    <item>
      <title>Our 3D configurator kept crashing on iPad. The bug was innerWidth &lt; 768</title>
      <dc:creator>Kastomiko</dc:creator>
      <pubDate>Sun, 15 Nov 2026 20:18:13 +0000</pubDate>
      <link>https://dev.to/kastomiko/our-3d-configurator-kept-crashing-on-ipad-the-bug-was-innerwidth-768-3jje</link>
      <guid>https://dev.to/kastomiko/our-3d-configurator-kept-crashing-on-ipad-the-bug-was-innerwidth-768-3jje</guid>
      <description>&lt;p&gt;We make a &lt;a href="https://kastomiko.com/minifigure-configurator-development" rel="noopener noreferrer"&gt;minifigure configurator&lt;/a&gt;: the customer puts together a figure in 3D, sees the price, and orders. It runs in production on &lt;a href="https://kloniko.store/en/pages/bundle-figures" rel="noopener noreferrer"&gt;kloniko.store&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For a while iPad users saw "A problem repeatedly occurred" in Safari and "Can't open this page" in Chrome. No error in Sentry, no error boundary triggered. The tab just died.&lt;/p&gt;

&lt;h2&gt;
  
  
  The cause: iPad got the desktop budget
&lt;/h2&gt;

&lt;p&gt;Every "light" branch in our code was guarded by screen width:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;isPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;innerWidth&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;768&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On a phone we lowered the pixel ratio, turned off ambient occlusion and bloom, and kept one 3D snapshot slot. An iPad is 820–1366 px wide, so it got the &lt;strong&gt;desktop&lt;/strong&gt; path: on the frame layout step it had up to two live 3D figures plus three parallel snapshot renderers, each with post-processing.&lt;/p&gt;

&lt;p&gt;But the memory available to a tab on an iPad is close to an iPhone, not to a laptop. The OS killed the tab.&lt;/p&gt;

&lt;p&gt;So we separated two questions that we had mixed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Layout&lt;/strong&gt; (bottom sheet or sidebar) still depends on width.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Memory and render quality&lt;/strong&gt; depend on the device.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;isTightGpuBudget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;ua&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;userAgent&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;isIOS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nf"&gt;iP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hone&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;od&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;ad&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ua&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;// iPadOS reports itself as a Mac&lt;/span&gt;
    &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nc"&gt;Macintosh&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ua&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;maxTouchPoints&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;isIOS&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nc"&gt;Android&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ua&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;isPerfConstrained&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;innerWidth&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;768&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nf"&gt;isTightGpuBudget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Android is in the list on purpose. In-app browsers of Instagram, Facebook and TikTok share memory with the app itself, and three WebGL contexts at once kill them the same way.&lt;/p&gt;

&lt;p&gt;One detail that matters: call this &lt;strong&gt;synchronously&lt;/strong&gt;, in the &lt;code&gt;useState&lt;/code&gt; initialiser, not in &lt;code&gt;useEffect&lt;/code&gt;. Otherwise the heavy component mounts for one frame and the WebGL context already exists.&lt;/p&gt;

&lt;h2&gt;
  
  
  The crash loop
&lt;/h2&gt;

&lt;p&gt;There was a second problem. After the crash the browser offers to reload, the page restores the same heavy state, and crashes again.&lt;/p&gt;

&lt;p&gt;You cannot catch an out-of-memory kill: the process is gone. But you can notice it on the next start. We keep an "alive and on screen" mark in &lt;code&gt;localStorage&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;set it on start and every time the tab becomes visible;&lt;/li&gt;
&lt;li&gt;remove it on &lt;code&gt;pagehide&lt;/code&gt; and when the tab is hidden (close, reload, navigation, switching apps);&lt;/li&gt;
&lt;li&gt;if the mark is still there on start, the previous session died while on screen. That is a crash.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then we turn on a safe mode for 30 minutes, with levels by the number of crashes in a row:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;tight budget: one live 3D context, low pixel ratio, no AO or bloom;&lt;/li&gt;
&lt;li&gt;no post-processing anywhere;&lt;/li&gt;
&lt;li&gt;the main preview is 2D, no WebGL at all.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Two things we got wrong at first:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A second tab is not a crash.&lt;/strong&gt; The mark carries a tab id, and a live tab answers through &lt;code&gt;BroadcastChannel&lt;/code&gt;. If the owner of the mark answers, we roll the crash record back.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Desktop does not need this.&lt;/strong&gt; Desktop tabs are not killed for memory, but the mark stayed from normal cases, like two windows side by side. Our managers ended up with a 2D preview after three such "crashes". Now it is phones and tablets only.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How we test without the device
&lt;/h2&gt;

&lt;p&gt;A URL switch: &lt;code&gt;?gpu=tight&lt;/code&gt; forces the phone budget on a desktop, and &lt;code&gt;window.__glStats&lt;/code&gt; counts live WebGL contexts. A narrow window plus &lt;code&gt;tight&lt;/code&gt; reproduces the iPad path well enough to catch regressions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Screen width tells you about layout. It tells you nothing about memory.&lt;/li&gt;
&lt;li&gt;Count WebGL contexts. Hidden ones (snapshots, thumbnails) count too.&lt;/li&gt;
&lt;li&gt;A tab killed by the OS is invisible to error tracking. Detect it on the next start.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We build configurators like this for other brands at &lt;a href="https://kastomiko.com/" rel="noopener noreferrer"&gt;Kastomiko&lt;/a&gt;: &lt;a href="https://kastomiko.com/3d-product-configurator" rel="noopener noreferrer"&gt;3D product configurator&lt;/a&gt;, &lt;a href="https://kastomiko.com/minifigure-configurator-development" rel="noopener noreferrer"&gt;minifigure configurator&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webgl</category>
      <category>threejs</category>
      <category>ios</category>
      <category>performance</category>
    </item>
    <item>
      <title>5,100 draw calls to 270: 3D building instructions for a 1,689-part brick model on a phone</title>
      <dc:creator>Kastomiko</dc:creator>
      <pubDate>Sun, 15 Nov 2026 20:17:59 +0000</pubDate>
      <link>https://dev.to/kastomiko/5100-draw-calls-to-270-3d-building-instructions-for-a-1689-part-brick-model-on-a-phone-4bne</link>
      <guid>https://dev.to/kastomiko/5100-draw-calls-to-270-3d-building-instructions-for-a-1689-part-brick-model-on-a-phone-4bne</guid>
      <description>&lt;p&gt;We build &lt;a href="https://kastomiko.com/3d-building-instructions-software" rel="noopener noreferrer"&gt;BrickPlay&lt;/a&gt;: step-by-step 3D building instructions that open in the browser from a QR code on the box. No app, the builder just scans and builds on a phone.&lt;/p&gt;

&lt;p&gt;Small models were fine. Then we loaded a 1,689-part model, and on phones the step animation stuttered badly. Here is what was wrong and how we fixed it with three.js.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why it was slow
&lt;/h2&gt;

&lt;p&gt;We load models with &lt;code&gt;LDrawLoader&lt;/code&gt;. Every LDraw part becomes three objects: a mesh, its edges, and the "conditional" lines that draw the outline of curved surfaces. That is three draw calls per part, plus the shadow pass.&lt;/p&gt;

&lt;p&gt;For 1,689 parts this gave about &lt;strong&gt;5,100 draw calls and 28–31 ms of CPU per frame on a strong desktop&lt;/strong&gt;. On a phone it was several times worse.&lt;/p&gt;

&lt;p&gt;On desktop we already avoided this by baking: everything that is already built is merged into one buffer per colour, in world coordinates. The parts are sorted by step, so "show steps 1…N" is just a draw range. No scene rebuild, about 100 draw calls, 3.5 ms per frame.&lt;/p&gt;

&lt;p&gt;The catch is memory. Baking copies the geometry of every part. For this model it is 146 MB. Our phone budget is far below that, so on phones the model was simply not merged and fell back to drawing part by part. That was the stutter.&lt;/p&gt;

&lt;h2&gt;
  
  
  The memory budget
&lt;/h2&gt;

&lt;p&gt;We decide by device, not by screen width:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// bytes we allow for merged geometry&lt;/span&gt;
&lt;span class="n"&gt;phone&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;   &lt;span class="mi"&gt;16&lt;/span&gt; &lt;span class="no"&gt;MB&lt;/span&gt; × &lt;span class="n"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deviceMemory&lt;/span&gt;
&lt;span class="n"&gt;iPhone&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="mi"&gt;64&lt;/span&gt; &lt;span class="no"&gt;MB&lt;/span&gt;            &lt;span class="c1"&gt;// deviceMemory is not available in Safari&lt;/span&gt;
&lt;span class="n"&gt;desktop&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt; &lt;span class="no"&gt;MB&lt;/span&gt; × &lt;span class="no"&gt;GB&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;up&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="mi"&gt;256&lt;/span&gt; &lt;span class="no"&gt;MB&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The fix: a hybrid of instancing and baking
&lt;/h2&gt;

&lt;p&gt;When a model does not fit the budget, we split the parts in two groups:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Parts that repeat many times&lt;/strong&gt; are drawn with instancing: one geometry plus a 64-byte matrix per copy. No geometry copies at all.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rare parts&lt;/strong&gt; (8 copies or fewer) are baked, as long as they fit into half of the budget.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The copies are ordered by step, so "steps 1…N" is just &lt;code&gt;mesh.count = n&lt;/code&gt; — the same trick as the draw range for baked buffers.&lt;/p&gt;

&lt;p&gt;One thing three.js does not give you: it instances meshes only. For the edges we use &lt;code&gt;LineSegments&lt;/code&gt; with the &lt;code&gt;isInstancedMesh&lt;/code&gt; flag, so the renderer supplies &lt;code&gt;instanceMatrix&lt;/code&gt; itself. The conditional lines of LDraw need their own copy of the shader with instancing support. If the loader's shader ever changes and our copy fails to compile, the model falls back to part-by-part drawing instead of rendering wrong.&lt;/p&gt;

&lt;h2&gt;
  
  
  Results
&lt;/h2&gt;

&lt;p&gt;Same model, same camera, whole frame:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Mode&lt;/th&gt;
&lt;th&gt;Draw calls&lt;/th&gt;
&lt;th&gt;CPU per frame&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Part by part (phones before)&lt;/td&gt;
&lt;td&gt;~5,100&lt;/td&gt;
&lt;td&gt;28–31 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hybrid, iPhone budget (phones now)&lt;/td&gt;
&lt;td&gt;250–290&lt;/td&gt;
&lt;td&gt;4.5–5.4 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Everything instanced&lt;/td&gt;
&lt;td&gt;480–660&lt;/td&gt;
&lt;td&gt;~8.5 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Everything baked (desktop)&lt;/td&gt;
&lt;td&gt;~100&lt;/td&gt;
&lt;td&gt;3.5 ms, +146 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The picture matches the baked version: at most 0.4% of pixels differ, and only in transparent glass, because of blending order. Building the buffers takes about 110 ms.&lt;/p&gt;

&lt;p&gt;A side effect we liked: big models on phones now have buffers, so the intro where the model assembles itself works there too.&lt;/p&gt;

&lt;h2&gt;
  
  
  What we would tell ourselves earlier
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Count draw calls before anything else. Ours were hidden: one part looked like one object, but it was three.&lt;/li&gt;
&lt;li&gt;"Does not fit, so skip the optimisation" is the worst fallback. Have a middle path.&lt;/li&gt;
&lt;li&gt;Add a URL switch for each mode. We test with &lt;code&gt;?bpmerge=phone|inst|bake|off&lt;/code&gt; on production.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;BrickPlay reads LDraw (&lt;code&gt;.ldr&lt;/code&gt;, &lt;code&gt;.mpd&lt;/code&gt;) and BrickLink Studio (&lt;code&gt;.io&lt;/code&gt;) files. If you sell brick sets and want instructions like this under your brand, this is what we do at &lt;a href="https://kastomiko.com/" rel="noopener noreferrer"&gt;Kastomiko&lt;/a&gt;: &lt;a href="https://kastomiko.com/3d-building-instructions-software" rel="noopener noreferrer"&gt;3D building instructions software&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>threejs</category>
      <category>webgl</category>
      <category>performance</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
