{"id":424,"date":"2012-02-05T23:22:01","date_gmt":"2012-02-05T20:22:01","guid":{"rendered":"http:\/\/unitycoder.com\/blog\/?p=424"},"modified":"2013-08-15T22:08:39","modified_gmt":"2013-08-15T19:08:39","slug":"convert-bitmap-to-3d-extruded-mesh-unity3d","status":"publish","type":"post","link":"https:\/\/unitycoder.com\/blog\/2012\/02\/05\/convert-bitmap-to-3d-extruded-mesh-unity3d\/","title":{"rendered":"Bitmap2Mesh (release v3.0)"},"content":{"rendered":"<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"425\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2012\/02\/05\/convert-bitmap-to-3d-extruded-mesh-unity3d\/bitmap2mesh\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/bitmap2mesh.jpg?fit=680%2C457&amp;ssl=1\" data-orig-size=\"680,457\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"bitmap2mesh\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/bitmap2mesh.jpg?fit=680%2C457&amp;ssl=1\" class=\"alignnone size-full wp-image-425\" title=\"bitmap2mesh\" alt=\"\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/bitmap2mesh.jpg?resize=680%2C457\" width=\"680\" height=\"457\" srcset=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/bitmap2mesh.jpg?w=680&amp;ssl=1 680w, https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/bitmap2mesh.jpg?resize=300%2C201&amp;ssl=1 300w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><\/p>\n<p>Bitmap to 3D Extruded Mesh-script. <em>Related to this forum topic (<a href=\"http:\/\/forum.unity3d.com\/threads\/118588-Texture-to-3D-weapon\" target=\"_blank\">unity forum link<\/a>)<\/em><\/p>\n<p><strong>What it is?<\/strong><br \/>\nIt reads bitmap pixel by pixel, checks if pixel alpha value is &gt; 0, then add vertice there (but skip adding, if we are going the same direction) (it follows the shape border, currently just 1 shape is scanned..)<\/p>\n<p>Then it uses<a href=\"http:\/\/www.unifycommunity.com\/wiki\/index.php?title=Triangulator\" target=\"_blank\"> Triangulator<\/a> from unifywiki and <del><a href=\"http:\/\/forum.unity3d.com\/threads\/102629-Trying-extrude-a-2d-polygon-to-create-a-mesh\" target=\"_blank\">This extrude script<\/a> from unity forums to make it 3D.<\/del>*<br \/>\n*Re-wrote the extrusion connection, now its using Unity Examples Procedural-extrusion. (which can be also used to extrude in multiple sections)<\/p>\n<p><strong>TODO:<\/strong><br \/>\n<span style=\"color: #000000;\"> &#8211; <del>Optimization, clean up (or dont add) too many vertices, if its a straight line<\/del><\/span> (done, using kind of &#8220;RLE&#8221;)<br \/>\n&#8211; <del>UV mapping.. problem if you try to add textures to this version<\/del> (Planar UV map works, not sure how to make others, * Testing Custom UV mapping, works bit better)<br \/>\n&#8211; <del>Some error checking, havent tried yet what happens if you make multiple objects in the bitmap..or holes..?<\/del> (It only detects the first object that it hits, when scanning pixels)<br \/>\n&#8211; <del>Pivot location<\/del> (see update note #2)<br \/>\n&#8211; <del>Scaling (currently its 1pixel = 1unit..)<\/del> (see update note #2)<br \/>\n&#8211;<del> Last vertice \/ triangle \/uv is sometimes broken?<\/del> (hopefully fixed, 1.2.2012, see update note #1)<br \/>\n<del>&#8211; Model is mirrored \/ upside down?<\/del><br \/>\n&#8211; Proper UV mapping \/ texturing (bake vertex colors? special shader: 2 materials, use vertex color as mask?)<br \/>\n&#8211; Support multiple shapes (submeshes, combine meshes?)<br \/>\n&#8211; Use bitmap image as texture directly (possible after proper UV&#8217;s are done?) (works, just need to remove extra variable..)<br \/>\n&#8211; <del>Fix scanning: breaks if there is not atleast 1 pixel empty margin around the image..<\/del> (done)<br \/>\n&#8211; <del>Cannot scan diagonal pixels or too thin lines..<\/del> (Now works)<br \/>\n&#8211; Single pixels coming out from the shape are not correctly square (need to add extra vertex function there later..)<br \/>\n&#8211; Need to do scaling for gameObject, instead of pixels..(?)<br \/>\n&#8211; Make the whole thing as a function\/class.. so you can call it and pass the bitmap image, settings\u00a0 as a variables<\/p>\n<p><strong>FUTURE IDEAS<\/strong>:<br \/>\n&#8211; <del>Make pixel texture painter script, so you can paint your weapon and click &#8220;extrude to 3D&#8221;<\/del> (test version done, not so useful yet..)<br \/>\n&#8211; Take extrude depth from alpha or color value? So you could make it a bit less blocky?<br \/>\n&#8211; Bake texture to vertex colors? (would need more vertices..?)<br \/>\n&#8211; Make it as unity editor script?<br \/>\n&#8211; Make it as online page (you draw \/ upload bitmap, it creates you the mesh to download, as .obj or &#8230;)<\/p>\n<p><strong>*Update#1 (1.2.2012)<\/strong><br \/>\nThis fixed the uv\/triangle problem in some cases,<br \/>\nFind these 2 lines:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">triangles&#x5B;count_tris+4] = n + poly.Length;\r\ntriangles&#x5B;count_tris+5] = i + poly.Length;<\/pre>\n<p>replace with these 2 lines: (swaps 4 and 5)<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">triangles&#x5B;count_tris+5] = n + poly.Length;\r\ntriangles&#x5B;count_tris+4] = i + poly.Length;<\/pre>\n<p><strong>*Update#2 (2.2.2012)<\/strong><br \/>\nIf you want to set pivot point to first encountered pixel(vertex)<br \/>\nand to set the scale (currently 1 pixel = 1 unit), do it here:<\/p>\n<p>Find line:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">polys.Push(scanpoint);<\/pre>\n<p>Replace with: (you could make the &#8220;0.1&#8221; (=pixelscale) as public variable, so you can easily adjust it later<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">polys.Push((scanpoint-startpoint)*0.1);<\/pre>\n<p><strong>*Update#2 (4.2.2012)<\/strong><br \/>\nCustom UV mapping &amp; texturing.. <em>(see image#3)<\/em><br \/>\nneed to clean it up a bit and do testing before upload..<\/p>\n<p><strong>*Update#3 (5.2.2012)<\/strong><br \/>\nUpdated the pixel scanning system, now its more robust and takes the outer edge of pixel as vertex location (not the pixel midpoint..)<br \/>\nSee image#4, original minecraft sword extruded &amp; textured.<br \/>\nAdded v2 download for testing..<\/p>\n<p><strong>*Update#4 (9.2.2012)<\/strong><br \/>\nNow UV mapping works on most cases<br \/>\nAdded v3 download link to the toolbar<\/p>\n<h4><strong>Download source: (js + c#)<\/strong><\/h4>\n<p><a title=\"Download\" href=\"http:\/\/unitycoder.com\/download\/index2.php?link=70&amp;uid=un1t3c0d5r\" target=\"_blank\">mBitmapToMesh,unitypackage<\/a>\u00a0 <strong>v1.0<\/strong><br \/>\n<a title=\"Download\" href=\"http:\/\/unitycoder.com\/download\/index2.php?link=137&amp;uid=un1t3c0d5r\" target=\"_blank\">mBitmap2Meshv20,unitypackage<\/a>\u00a0 <strong>v2.0<\/strong><br \/>\n<a title=\"Download Source\" href=\"http:\/\/unitycoder.com\/download\/index2.php?link=144&amp;uid=un1t3c0d5r\" target=\"_blank\">mBitmap2Meshv30.unityPackage<\/a> <strong>v3.0<\/strong><br \/>\n<strong><\/strong><\/p>\n<p><strong>Additional images:<\/strong><br \/>\n<strong>Image#2:<\/strong> Optimized vertices (compare to the main image)<br \/>\n<img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"525\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2012\/02\/05\/convert-bitmap-to-3d-extruded-mesh-unity3d\/bitmap2mesh_v102\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/bitmap2mesh_v102.jpg?fit=680%2C546&amp;ssl=1\" data-orig-size=\"680,546\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"bitmap2mesh_v102\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/bitmap2mesh_v102.jpg?fit=680%2C546&amp;ssl=1\" class=\"alignnone size-full wp-image-525\" title=\"bitmap2mesh_v102\" alt=\"\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/bitmap2mesh_v102.jpg?resize=680%2C546\" width=\"680\" height=\"546\" srcset=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/bitmap2mesh_v102.jpg?w=680&amp;ssl=1 680w, https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/bitmap2mesh_v102.jpg?resize=300%2C240&amp;ssl=1 300w, https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/bitmap2mesh_v102.jpg?resize=200%2C160&amp;ssl=1 200w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><\/p>\n<p><strong>Image#3:<\/strong> Automatic UV mapping and texturing (using the original bitmap).. bit off, but close enough for now..<br \/>\n<img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"538\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2012\/02\/05\/convert-bitmap-to-3d-extruded-mesh-unity3d\/mbitmap2meshuvmap\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/mbitmap2meshUVmap.jpg?fit=680%2C619&amp;ssl=1\" data-orig-size=\"680,619\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"mbitmap2meshUVmap\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/mbitmap2meshUVmap.jpg?fit=680%2C619&amp;ssl=1\" class=\"alignnone size-full wp-image-538\" title=\"mbitmap2meshUVmap\" alt=\"\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/mbitmap2meshUVmap.jpg?resize=680%2C619\" width=\"680\" height=\"619\" srcset=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/mbitmap2meshUVmap.jpg?w=680&amp;ssl=1 680w, https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/mbitmap2meshUVmap.jpg?resize=300%2C273&amp;ssl=1 300w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><br \/>\n<strong>&#8212;<br \/>\nImage#4<\/strong>: Original minecraft sword .png, extruded to 3D (same image as texture) <em>*Sword image\u00a0 \u00a9 Mojang\/<a href=\"http:\/\/www.minecraft.net\/\" target=\"_blank\">Minecraft<\/a><\/em><br \/>\n(Sides still have UV mapping problems..)<br \/>\n<img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"544\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2012\/02\/05\/convert-bitmap-to-3d-extruded-mesh-unity3d\/mbitmap2mesh_pixels\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/mbitmap2mesh_pixels.jpg?fit=680%2C403&amp;ssl=1\" data-orig-size=\"680,403\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"mbitmap2mesh_pixels\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/mbitmap2mesh_pixels.jpg?fit=680%2C403&amp;ssl=1\" class=\"alignnone size-full wp-image-544\" title=\"mbitmap2mesh_pixels\" alt=\"\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/mbitmap2mesh_pixels.jpg?resize=680%2C403\" width=\"680\" height=\"403\" srcset=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/mbitmap2mesh_pixels.jpg?w=680&amp;ssl=1 680w, https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/mbitmap2mesh_pixels.jpg?resize=300%2C177&amp;ssl=1 300w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bitmap to 3D Extruded Mesh-script. Related to this forum topic (unity forum link) What it is? It reads bitmap pixel by pixel, checks if pixel alpha value is &gt; 0, then add vertice there (but skip adding, if we are going the same direction) (it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":425,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[4,3],"tags":[65,62,112,110,51,5,144,143,111,142,33],"class_list":["post-424","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-demos","category-unity3d","tag-2d","tag-3d","tag-bitmap","tag-extrude","tag-image","tag-mesh","tag-minecraft","tag-model","tag-polygon","tag-texture","tag-vertex"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/bitmap2mesh.jpg?fit=680%2C457&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/p1KTaT-6Q","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/424","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/comments?post=424"}],"version-history":[{"count":32,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/424\/revisions"}],"predecessor-version":[{"id":2303,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/424\/revisions\/2303"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media\/425"}],"wp:attachment":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media?parent=424"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/categories?post=424"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/tags?post=424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}