<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Daniel Simamora on Medium]]></title>
        <description><![CDATA[Stories by Daniel Simamora on Medium]]></description>
        <link>https://medium.com/@daniel.simamora2000?source=rss-48b4fb37f2f0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*A6KQ7SGuil9XzUteMgikKw.png</url>
            <title>Stories by Daniel Simamora on Medium</title>
            <link>https://medium.com/@daniel.simamora2000?source=rss-48b4fb37f2f0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 15:24:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@daniel.simamora2000/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Render a SwiftUI Sticky Notes into your Augmented Reality app with RealityKit]]></title>
            <link>https://medium.com/@daniel.simamora2000/render-a-swiftui-sticky-notes-into-your-augmented-reality-app-with-realitykit-8e1184f221bf?source=rss-48b4fb37f2f0------2</link>
            <guid isPermaLink="false">https://medium.com/p/8e1184f221bf</guid>
            <category><![CDATA[realitykit]]></category>
            <category><![CDATA[swiftui]]></category>
            <dc:creator><![CDATA[Daniel Simamora]]></dc:creator>
            <pubDate>Wed, 31 May 2023 18:22:49 GMT</pubDate>
            <atom:updated>2023-05-31T18:22:49.792Z</atom:updated>
            <content:encoded><![CDATA[<p>Sticky Notes are commonly used on variety of things, like expressing thoughts and reminding yourself on today’s important tasks. In this article, we will look on how to creating a 3D entity of sticky note that looks as close as what’s created from SwiftUI.</p><p>For the purpose of learning to develop an Augmented Reality in iOS app, I tried to bring the idea of sticky notes usage into an AR app where user can write a Sticky Note, it will become a paper plane where user can control it with joystick, and “post” it into a board.</p><p>To be more clear on my app flow, I will guide you first for a while from the screenshots below:</p><p><strong>Choosing the paper color</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/351/1*Mcl2NQ5cIIMihAO5QLIQBw.png" /><figcaption>The UI to choose the paper color</figcaption></figure><p><strong>Clicking the paper to start writing</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/351/1*fc4NA3fsrYdzEtnKfsn5LQ.png" /><figcaption>Writing on the paper with the chosen color</figcaption></figure><p><strong>Navigate to the AR View with the written paper plane and control it to collide to the board with joystick</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/351/1*8YL-VpiOpa-5VDww_dgBNQ.png" /><figcaption>Move on to the AR View with the paper plane and joystick</figcaption></figure><p><strong>If the paper plane hit the green board, it will be rendered just like the written sticky note</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/351/1*Qt4L2mwsPQM4odq6LFKEbw.png" /><figcaption>Show the sticky note as an AR Entity when it hit the green board</figcaption></figure><p>With that being said, we will now move on to the main topic,<br>which is “<strong><em>How to convert the SwiftUI into a 3D entity?</em></strong>”. Let’s get started!</p><blockquote>You can find the source code here in my <a href="https://github.com/daniel-bss/cartalada-nc2">GitHub</a>!</blockquote><h4>Preparing the SwiftUI View</h4><p>Before going into the AR development, I often start simple from creating the basic view, which in my case I use SwiftUI. Here is the code for rendering the paper with the chosen color, and ready to be written:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nl1nm7WsiIpVnvWwU3ncAQ.png" /></figure><p>Basically I created a TextEditor with the chosen background color and adjust its size with the .frame(width:) modifier, so it will look like a writable sticky note.</p><p>Below it, I prepared a NavigationLink button that receive the words and the color paperColor to render the ContentView that contains ARViewContainer (which is the main feature that we will discuss more after this).</p><h4><strong>Preparing the ARViewContainer</strong></h4><p>Before putting the AR View into our ContentView , we should create an ARViewContainer and conform to the UIViewRepresentable by creating the makeUIView and updateUIView as follow:</p><pre>import RealityKit<br><br>struct ARViewContainer: UIViewRepresentable {<br>  func makeUIView(context: Context) -&gt; ARView {<br>    // this function is executed first time<br>  }<br><br>  func updateUIView(_ uiView: ARView, context: Context) {<br>    // this function is executed everytime it notices a changes, like @State, @Binding, and @Published variables<br>  }<br>}</pre><p>To make the UI view (hence its name makeUIView), we should do this tasks:</p><ul><li>create an ARView()</li><li>create an Anchor</li><li>append the anchor into the ARView’s scene</li><li>return the ARView</li></ul><pre>func makeUIView(context: Context) -&gt; ARView {<br>  let arView = ARView(frame: .zero)<br>  let anchor = AnchorEntity()<br><br>  arView.scene.anchors.append(anchor)<br>  return arView<br>}</pre><p>To add an entity, we should create an instance of ModelEntity() and then call the 3D model that you have in your project. In my case, I have created some 3D Paper Plane by myself in <a href="https://www.blender.org/download/"><strong>Blender</strong></a>, which I then add to my XCode Project as <strong>.usdz</strong> format<strong> </strong>like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/239/1*6zdrhqY0YMGQOEMUMp2zPA.png" /></figure><pre>func makeUIView(context: Context) -&gt; ARView {<br>  // ... create the arView and anchor<br><br>  var modelEntity = ModelEntity()<br>  modelEntity = try! Entity.loadModel(named: &quot;your-usdz-3d-model-name&quot;)<br>  modelEntity.name = &quot;your-entity-name&quot;<br>  anchor.addChild(modelEntity) // add the entity into the anchor we made<br><br>  // return the arView ...<br><br>}</pre><p>For now, it will now do the work as simple as rendering your 3D asset as a ModelEntity.</p><p>Later we wan’t this entity (which is a paper plane in my case), to collide with a board. So we should define its physics properties with the CollisionComponent() struct, as follow:</p><pre>modelEntity.collision = CollisionComponent(shapes: [.generateBox(size: SIMD3&lt;Float&gt;(0.16, 0.04, 0.21))])<br>modelEntity.physicsBody = .init()<br>modelEntity.physicsBody?.mode = .kinematic</pre><p>The CollisionComponent(shapes:) is used to define the dimension of your asset to collide with another entity. In my code, I created a box for my paper plane with approximately has a width of 16 cm, height of 4 cm, and thickness of 21 cm. We then set the mode of physics to .kinematic to tell the system that we just want the 2 entities detect the collision, but not physically collide.</p><h4><strong>Creating the board</strong></h4><p>When creating the green board, I created a separate class name CustomEntity that must inherit from these classes: Entity, HasModel, HasAnchoring, and HasCollision , like this:</p><pre>class CustomEntity: Entity, HasModel, HasAnchoring, HasCollision {<br><br>}</pre><p>We then have to create the initializer to be called when we create a CustomEntity object</p><pre>class CustomEntity: Entity, HasModel, HasAnchoring, HasCollision {<br><br>  required init(color: UIColor) {<br>    // Initializing its parent classes<br>    super.init()<br>    <br>    // Define a Model Entity of width: 120cm, height: 90cm, and thickness: 3 cm<br>    self.components[ModelComponent] = ModelComponent(<br>        mesh: .generateBox(size: [1.2, 0.9, 0.03]),<br>        materials: [SimpleMaterial(<br>          color: color,<br>          isMetallic: false)<br>        ]<br>    )<br>    <br>    // Define the CollisionComponent witht the exact same dimension<br>    self.components[CollisionComponent] = CollisionComponent(<br>        shapes: [.generateBox(size: [1.2, 0.9, 0.03])],<br>        mode: .trigger<br>    )<br>  }<br>  <br>  // Initializer for adding collor and manipulating my Entity position<br>  // Feel free to modify that suits your project<br>  convenience init(color: UIColor, position: SIMD3&lt;Float&gt;) {<br>    self.init(color: color)<br>    self.position = position<br>  }<br>  <br>  // Initializer that raises an error to tell that this class must implement the initializer<br>  required init() {<br>    fatalError(&quot;init() has not been implemented&quot;)<br>  }<br><br>}</pre><p>Aside of the explanation I put as comments in the code above, one thing to point out is this:</p><pre>self.components[CollisionComponent] = CollisionComponent(<br>    shapes: [.generateBox(size: [1.2, 0.9, 0.03])],<br>    mode: .trigger<br>)</pre><p>We set the <strong>collision mode</strong> as .trigger , similar to the previous reason which we want to “detect” the collision, not physically collide like a ball hitting a board in a real world.</p><h4>Creating the function to detect collision</h4><p>We now have to create a func when two entities collision happened, as follow:</p><pre>class CustomEntity: Entity, HasModel, HasAnchoring, HasCollision {<br>  var collisionSubs: [Cancellable] = []<br><br>  // the initializers...<br><br>  func addCollisions(uiView: ARView, entityName: String) {<br><br>  }<br>}</pre><p>Inside the addCollisions function, we first taking the scene with guard let :</p><pre>func addCollisions(uiView: ARView, entityName: String) {<br>  guard let scene = self.scene else {<br>    return<br>  }<br>}</pre><p>Next we append to our collisionSub something called <strong>subscriber</strong> like this:</p><pre>func addCollisions(uiView: ARView, entityName: String, paperColor: Color, words: String, paperVm: PaperViewModel) {<br>  guard let scene = self.scene else {<br>    return<br>  }<br><br>  collisionSubs.append(scene.subscribe(to: CollisionEvents.Began.self, on: self) { event in<br><br>    // making sure we have our entity in the collision event, which is defined ad entityA<br>    guard let boxA = event.entityA as? CustomEntity else {<br>        return<br>    }<br>    <br>    // looping all of the entities in our 1st anchor the we created in makeUIView<br>    for entity in uiView.scene.anchors[0].children {<br>      if (entity.name == &quot;your-entity-name&quot;)<br>        entity.removeFromParent()<br>        print(&quot;COLLISION HAPPENED!&quot;)<br>      }<br>    }<br>}</pre><p>From the code above, when my paper plane hit the board, it will disappear and print out to the console.</p><h4>Creating the paper and its text</h4><p>When the paper plane of sticky note collide to the board, we want to render it with the exact color and text written before.</p><p>First, we should add more parameters to our addCollision function, which are the paper color, and the written text</p><pre>func addCollisions(uiView: ARView, paperColor: Color, words: String)</pre><p>Next, below the entity.removeFromParent() , we define a 2 kinds of ModelEntity , for <strong>paper </strong>with .generateBox() and <strong>text </strong>with <strong>.generateText()</strong></p><pre>func addCollisions(uiView: ARView, paperColor: Color, words: String) {<br>  guard let scene = self.scene else {<br>    return<br>  }<br><br>  collisionSubs.append(scene.subscribe(to: CollisionEvents.Began.self, on: self) { event in<br><br>    // making sure we have our entity in the collision event, which is defined ad entityA<br>    guard let boxA = event.entityA as? CustomEntity else {<br>        return<br>    }<br>    <br>    // looping all of the entities in our 1st anchor the we created in makeUIView<br>    for entity in uiView.scene.anchors[0].children {<br>      if (entity.name == &quot;your-entity-name&quot;)<br>        entity.removeFromParent()<br>        <br>        // ModelEntity for the paper with .generateBox<br>        let boardEntity = ModelEntity(<br>            mesh: .generateBox(width: 0.2, height: 0.2, depth: 0.001),<br>            materials: [SimpleMaterial(<br>                color: UIColor(paperColor),<br>                isMetallic: false)<br>            ]<br>        )<br>        <br>        // ModelEntity for the written text with .generateText<br>        let textEntity = ModelEntity(<br>            mesh: .generateText(<br>                words,<br>                extrusionDepth: 0.001,<br>                font: .systemFont(ofSize: 0.017, weight: .bold),<br>                containerFrame: CGRect(x: -0.19/2.0, y: -0.12, width: 0.19, height: 0.19),<br>                alignment: .left,<br>                lineBreakMode: .byWordWrapping<br>            ),<br>            materials: [SimpleMaterial(<br>                color: .black,<br>                isMetallic: false)<br>            ]<br>        )<br>      }<br>    }<br>}</pre><p>If we take a look more detail on the textEntity , the main properties we want to tune a lot are:</p><ul><li>extrusionDepth: to define the thickness of our text</li><li>font: setting the font size in unit of meters</li><li>containerFrame: to wrap our text in a shape of rectangle with CGRect(x:y:width:height:)</li><li>lineBreakMode : choose how our text gets wrapped in affect of the container frame</li></ul><p>I personally did a lot of trial-and-error between these parameters to get the closest look as possible to the written text from the previous sticky note view of SwiftUI.</p><p>Moving up, we define our boardEntity and textEntity position before adding it to our anchor:</p><pre>func addCollisions(uiView: ARView, entityName: String, paperColor: Color, words: String, paperVm: PaperViewModel) {<br>  // ... previous code on collision and creating entities of paper &amp; text<br><br>  boardEntity.position = entity.position<br>  textEntity.position = entity.position<br>  textEntity.position.z += 0.005<br><br>  // append them into our anchor<br>  uiView.scene.anchors[0].addChild(boardEntity)<br>  uiView.scene.anchors[0].addChild(textEntity)<br>}</pre><p>We set both of our board &amp; text entities as entity.position , because it’s indeed the last time our entity (paper plane in my case) collide and disappear. We also move the text into the Z axis to give a little pop-up effect.</p><h4>Finally using our CustomEntity and the addCollision function into our ARViewContainer</h4><p>Now we continue coding our makeUIView, by creating the green board with our previously created CustomEntity class!</p><pre>func makeUIView(context: Context) -&gt; ARView {<br>  // our previous code on creating AR View, anchor, model entity and physics<br><br>  // create the board of the color green<br>  let board = CustomEntity(color: .green, position: SIMD3&lt;Float&gt;(0,0,-2.5)) // moved the board 2.5 meters forward (Z+)<br>  anchor.addChild(papan)<br><br>  // call the addCollision function<br>  board.addCollisions(uiView: arView, paperColor: paperColor, words: words)<br>  <br>  arView.scene.anchors.append(anchor)<br>  return arView<br>}</pre><p>That’s it, you should now know how to convert your SwiftUI Sticky Note into an AR Entity by collision! 👍</p><h4>Let’s sum up the whole process</h4><p>We first looked at the SwiftUI View of the sticky note before developing the AR view. We started by creating an ARViewContainer that conforms to UIViewRepresentable by creating 2 functions (makeUIView andupdateUIView). In the makeUIView , we created an ARView , adding an <strong>anchor</strong>, and adding a ModelEntity from our <strong>.usdz</strong> file. We then created a CustomEntity for our green board with the addCollision function.</p><p>Thank you for reading my article. This is a great opportunity for me to share my learning experience as a beginner in iOS Development. I look forward for questions and feedback, so feel free to leave a comment! 👋</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8e1184f221bf" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>