{"id":731,"date":"2017-04-25T10:38:21","date_gmt":"2017-04-25T08:38:21","guid":{"rendered":"http:\/\/davikingcode.com\/blog\/?p=731"},"modified":"2017-12-14T13:34:17","modified_gmt":"2017-12-14T12:34:17","slug":"building-a-3d-furniture-visualiser-with-three-js","status":"publish","type":"post","link":"https:\/\/davikingcode.com\/blog\/building-a-3d-furniture-visualiser-with-three-js\/","title":{"rendered":"Building a 3D furniture visualiser with Three.js"},"content":{"rendered":"<p style=\"text-align: center;\"><a href=\"http:\/\/davikingcode.com\/blog\/wp-content\/uploads\/2017\/04\/blanCambouisScreen2_.png\"><img loading=\"lazy\" class=\" wp-image-851 aligncenter\" src=\"http:\/\/davikingcode.com\/blog\/wp-content\/uploads\/2017\/04\/blanCambouisScreen2_-300x152.png\" alt=\"\" width=\"1018\" height=\"516\" srcset=\"https:\/\/davikingcode.com\/blog\/wp-content\/uploads\/2017\/04\/blanCambouisScreen2_-300x152.png 300w, https:\/\/davikingcode.com\/blog\/wp-content\/uploads\/2017\/04\/blanCambouisScreen2_-768x388.png 768w, https:\/\/davikingcode.com\/blog\/wp-content\/uploads\/2017\/04\/blanCambouisScreen2_-1024x518.png 1024w, https:\/\/davikingcode.com\/blog\/wp-content\/uploads\/2017\/04\/blanCambouisScreen2_.png 1914w\" sizes=\"(max-width: 1018px) 100vw, 1018px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/www.blanccambouis.fr\" target=\"_blank\" rel=\"noopener\">Blanc Cambouis<\/a> is a company selling high end furniture to their customer. In order to let the customer visualize the product they are buying we were asked to create a custom real-time 3D object viewer, able to manage different kinds of models and to customize them on the fly.<\/p>\n<p>While <a href=\"https:\/\/www.integral-service.fr\/\">Integral Service <\/a> worked on the web side of the product, us at Da Viking Code worked on the object viewer.<\/p>\n<p>In order to meet their requirement we built a <a href=\"https:\/\/threejs.org\/\">Three.js<\/a> (<a href=\"https:\/\/en.wikipedia.org\/wiki\/WebGL\">WebGL<\/a> Based) app using the <a href=\"https:\/\/haxe.org\/\">Haxe<\/a> language and <a href=\"https:\/\/bitbucket.org\/yar3333\/haxe-threejs\">yar3333\u2019s Haxe extern for Three.js<\/a>, we needed a modular solution to import the furniture into the engine, but also to enable the furniture&#8217;s multiple customization option without implementing new logic for each model.<br \/>\n<!--more--><\/p>\n<h4>Exporting with Unity<\/h4>\n<p>The first task for this project was to actually be able to create and customize the 3D scene to be exported into threeJS, as we were delivered fbx model and we wanted a Json scene.<br \/>\nFor this purpose we used <a href=\"https:\/\/unity3d.com\/fr\">Unity<\/a> for setting up the object&#8217;s scale, different object parts and the scene&#8217;s lights, then exported them with <a href=\"https:\/\/github.com\/nickjanssen\/UnityToThreeExporter\">Nick Janssen\u2019s Three.js JSON Exporter<\/a>.<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/davikingcode.com\/blog\/wp-content\/uploads\/2017\/04\/01_Unity.png\"><img loading=\"lazy\" class=\"wp-image-842 aligncenter\" src=\"http:\/\/davikingcode.com\/blog\/wp-content\/uploads\/2017\/04\/01_Unity-300x202.png\" alt=\"\" width=\"1015\" height=\"683\" srcset=\"https:\/\/davikingcode.com\/blog\/wp-content\/uploads\/2017\/04\/01_Unity-300x202.png 300w, https:\/\/davikingcode.com\/blog\/wp-content\/uploads\/2017\/04\/01_Unity-768x518.png 768w\" sizes=\"(max-width: 1015px) 100vw, 1015px\" \/><\/a><br \/>\n<em>Unity exporting scene<\/em><\/p>\n<h4>Naming convention and customization<\/h4>\n<p>As the object was meant to have different customization option switchable in the engine (opened, closed, with or without a backplate, different wood materials, different reflectiveness and different colors on certain parts, sometimes with different patterns) we needed a way to express all thoses different states of the object so the same engine could manage each models with their particular options seamlessly.<\/p>\n<p>A naming convention was chosen so the engine can identify the dynamic objects, understand their behavior and apply changes on the model accordingly.<br \/>\nThe website just has to call certain functions with certain parameters and they can change color, texture, models, brightness on the fly\u00a0!<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/davikingcode.com\/blog\/wp-content\/uploads\/2017\/04\/02_NamingConvention.png\"><img loading=\"lazy\" class=\"wp-image-843 aligncenter\" src=\"http:\/\/davikingcode.com\/blog\/wp-content\/uploads\/2017\/04\/02_NamingConvention-300x225.png\" alt=\"\" width=\"1020\" height=\"765\" srcset=\"https:\/\/davikingcode.com\/blog\/wp-content\/uploads\/2017\/04\/02_NamingConvention-300x225.png 300w, https:\/\/davikingcode.com\/blog\/wp-content\/uploads\/2017\/04\/02_NamingConvention-768x576.png 768w, https:\/\/davikingcode.com\/blog\/wp-content\/uploads\/2017\/04\/02_NamingConvention-1024x768.png 1024w, https:\/\/davikingcode.com\/blog\/wp-content\/uploads\/2017\/04\/02_NamingConvention.png 1271w\" sizes=\"(max-width: 1020px) 100vw, 1020px\" \/><\/a><br \/>\n<em>example of conventions and the way they behave<\/em><\/p>\n<p>&nbsp;<\/p>\n<h4>Customizing furniture patterns with a Three.js custom phong material<\/h4>\n<p>The furniture as we&#8217;ve seen, can be customizable in material (wood,plastic) , color (flat color) , or patterns (repeated printed patterns) for certain parts of each furniture models (panels, door handles&#8230;) .<\/p>\n<p>We chose to have a unified shader to display either a color, a texture, or a black and white pattern, where the black and white colors would get replaced by user chosen colors, giving patterns a lot more customizability. That way, a single material can be used for every case, and every sub objects of the viewed model.<\/p>\n<p>We started off writing a custom glsl shader for webgl, using three.js&#8217;s\u00a0 ShaderMaterial . However this would not take into account lighting info, shininess, or metalness, or environmental maps, or reflective maps, everything three.js has done and is available by default which would have to be rewritten from scratch assuming we could connect it properly to three.js default uniforms.<\/p>\n<p>So we decided to do it the three.js way : using its prewritten shader code snippets that are called &#8220;shader chunks&#8221;. Those are glsl code snippets &#8216;assembled&#8217; at runtime by Three.js internally to build up its available default materials.<\/p>\n<p>A great idea to avoid redundancy of code and better maintainance of its source code I guess.<br \/>\nIt was hard to find a tutorial or good info on this topic, most &#8220;customize your own three.js shader&#8221; articles will tell you how to start from nothing&#8230; and end up maybe coloring something with a solid color, with no lights.<\/p>\n<p>Exploring how the basic phong material was defined and the three.js\/src\/renderers\/shaders\/ folder on Three.js&#8217;s github, we worked things out.<\/p>\n<p>Thankfully, all shader chunks, uniforms, are intuitively named so that you can figure out which chunk\/lib corresponds to what basic material &#8211; on top of that, once you get into the mind of people writing an open source project and you start understanding why things seem to be all over the place, it gets easier.<\/p>\n<p>It&#8217;s possible to crossreference which&#8217; snippet&#8217; needs to be &#8216;included&#8217; where , going through the ShaderLib files for example. We&#8217;ll keep Phong as an example, and <a href=\"https:\/\/github.com\/mrdoob\/three.js\/blob\/7871fbed080cae463aee0ab1ba55ace2062af979\/src\/renderers\/shaders\/ShaderLib.js#L48\">here <\/a>are its &#8220;specs&#8221; for which UniformLibs (snippet of code to defined uniforms in the shader program) to use.<\/p>\n<p>And from this you can also deduce which vertex \/ fragment shader snippet is used. Here&#8217;s the <a href=\"https:\/\/github.com\/mrdoob\/three.js\/blob\/e7dc951e829bad80c244001e6c63023e58ad8260\/src\/renderers\/shaders\/ShaderLib\/meshphong_vert.glsl\">link<\/a> to the vertex one.<\/p>\n<p>And again from this, you can deduce which other dependencies you need to slowly reconstruct, recompile, a full phong shader which will allow us to use the shininess properties, all the lighting options and reflective maps (and so on) three.js offers, without actually rewriting everything&#8230; or decompiling stuff at runtime.<\/p>\n<p>So it is a painful process.<br \/>\nBut once that is done, you get to understand three.js more, and can customize any other shader without starting from scratch and allow yourself to expand on the contributors amazing work on lighting shaders for example.<\/p>\n<p>When coding in Haxe, there will be the problem of whether or not you have the necessary externs, however translating from .js to haxe , and adding to your externs so you get the necessary access is easy. The code might be long but things make sense you&#8217;ll see, it&#8217;s all about accessing &#8220;snippets&#8221; or &#8220;chunks&#8221; via statics.<\/p>\n<p>So here&#8217;s a full .js example which reconstitutes a Three.js Phong Material and customizes it by &#8216;injecting&#8217; glsl code to modify the fragment colors before lighting is processed, or the vertex positions before<\/p>\n<p><script async src=\"\/\/jsfiddle.net\/gsynuh\/bc1q9prL\/embed\/result,js,html\/\"><\/script><\/p>\n<p>You will find that the javascript part of this is creating the objects, lights, the shader itself, custom uniforms such as time, colors and other values required for this example.<br \/>\nThe html part contains the &#8220;injected&#8221; fragment and vertex glsl code, some of which like the perlin noise algorithm (by Stefan Gustavson) is not ours so be sure to check out the comments.<\/p>\n<p>This helps with not getting the js part cluttered with extra code so we have a nice example, the html part is really only for this example, the .js part can be adapted for everyone and hopefully well understood as the purpose is really to customize a base three.js material by adding chunks or snippets of the available three.js code one by one in an array, then joining it with &#8220;\\n&#8221; so it makes for a valid shader program and that&#8217;s it.<\/p>\n<p>Essentially this is what this project uses so the actual custom code , the non three.js shader code, is simply using a black and white pattern, two input colors, and lerping between the input colors based on the texture sample&#8217;s value so you&#8217;ve got a two color pattern with any two color you need.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Blanc Cambouis is a company selling high end furniture to their customer. In order to let the customer visualize the product they are buying we were asked to create a custom real-time 3D object viewer, able to manage different kinds of models and to customize them on the fly. While Integral Service worked on the &hellip; <a href=\"https:\/\/davikingcode.com\/blog\/building-a-3d-furniture-visualiser-with-three-js\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Building a 3D furniture visualiser with Three.js<\/span><\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_discordance_state":"","_discordance_checked":true},"categories":[4],"tags":[27,6,29,5,28],"_links":{"self":[{"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/posts\/731"}],"collection":[{"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/comments?post=731"}],"version-history":[{"count":37,"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/posts\/731\/revisions"}],"predecessor-version":[{"id":1063,"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/posts\/731\/revisions\/1063"}],"wp:attachment":[{"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/media?parent=731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/categories?post=731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/tags?post=731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}