<?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 Oleksii Shliama on Medium]]></title>
        <description><![CDATA[Stories by Oleksii Shliama on Medium]]></description>
        <link>https://medium.com/@shliama?source=rss-68f91534b3ca------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*hMNrDC9_R0WnZozZCA-FfQ.jpeg</url>
            <title>Stories by Oleksii Shliama on Medium</title>
            <link>https://medium.com/@shliama?source=rss-68f91534b3ca------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 13:31:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shliama/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[Beetlens]]></title>
            <link>https://medium.com/krootl/beetlens-42eac15503d0?source=rss-68f91534b3ca------2</link>
            <guid isPermaLink="false">https://medium.com/p/42eac15503d0</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[motion-design]]></category>
            <category><![CDATA[opengl]]></category>
            <category><![CDATA[open-source]]></category>
            <dc:creator><![CDATA[Oleksii Shliama]]></dc:creator>
            <pubDate>Tue, 12 May 2020 11:18:03 GMT</pubDate>
            <atom:updated>2020-05-12T11:29:18.615Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rIVftZdMmAKo6q_iUOmRiw.png" /></figure><h4>Beetles &amp; Lenses, or How I spent the quarantine building custom Android UI with OpenGL</h4><p>Once in a while, I have an itch for building <strong>custom Android UI</strong>. No wonder, I turned the quarantine home hibernation into an opportunity to play around with <strong>OpenGL</strong> <a href="https://en.wikipedia.org/wiki/Metaballs">metaballs</a>, <a href="https://en.wikipedia.org/wiki/Refraction">refraction</a>, and other stuff:</p><ul><li>View-to-OpenGL rendering using samplerExternalOES</li><li>TextureView + GLSurfaceView = GLTextureView</li><li>SpringAnimation &amp; ViewPropertyAnimator</li></ul><p>TL;DR — [<a href="https://github.com/Krootl/Beetlens">Source code</a>]</p><p>For this article to be not just a lame coding demo, I needed to come up with an application idea — a trivial task, especially when you don’t have to think about users, monetization strategy, or common sense.</p><p>Naturally, I ended up with the app that has a sole purpose — observing beetles with a magnifying glass. But with one catch — the only interactive element on the screen is the FAB a.k.a. Floating Action Button.</p><p>Satisfied with the idea I started coding ❤</p><h3>Metaballs</h3><p>One can render metaballs on Android in multiple ways, for example, using <a href="https://github.com/yarolegovich/RSMetaballs">RenderScript</a> or <a href="https://github.com/danielzeller/MetaBalls-LIB-Android">ColorMatrixColorFilter</a>. Intredasting… but let’s reinvent the wheel with OpenGL.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*w64_LBm_12q5LJ04j80yjg.gif" /><figcaption>Mixing different colors looks eye candy 🤩</figcaption></figure><p>I wrote a <a href="https://github.com/Krootl/Beetlens/blob/master/app/src/main/res/raw/metaballs_fragment_shader.glsl">shader</a> that takes position, size, and color of two metaballs as input arguments and blends them together in a beautiful render. Basically, you have two blobs of alpha channel radial-gradient — opaque at the center, and transparent at the radius. The total alpha channel value is then compared to the threshold and multiplied by color.</p><h3>Navigation</h3><p>The next thing I wanted to do is to use the FAB to switch between the different images.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*2nDKRKIifFiW6HOkvmRaSA.gif" /></figure><p>I used <a href="https://developer.android.com/jetpack/androidx/releases/viewpager2">ViewPager2</a> to display the beetles. Hence, at first, I tried to convert the FAB position offset into the argument for the ViewPager2.fakeDragBy() method but it didn’t play out well — too many edge cases to handle and the animation wasn’t pleasing.</p><p>Well, no big deal, there is another option — ViewPager2.setCurrentItem(page, smoothScroll = true). I call this method as soon as the FAB is dragged over the horizontal distance limit, left or right. There is also a minimum delay between page-turning so it feels natural.</p><h3>Zoom</h3><p>Yeah yeah, I know — sane people already got used to pinch-to-zoom gesture. But I believe it’s <em>not much fun</em> so let me get you out of the UX comfort zone.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*Q-mpsnXlLuOTEvy--9b4SQ.gif" /></figure><p>I found <a href="https://www.shadertoy.com/view/wt2GDW">this</a> lens shader some time ago knowing that I’ll use it sooner or later.</p><p>The original artistic lens style was quite nice but didn’t fit my idea. So I modified the code to increase the lens curvature and make it look like magnifying glass.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*yIW0IBb-97DWyR8qxoDlrQ.gif" /></figure><p>As for the reveal animation, the FAB can be dragged upwards simultaneously scaling up the magnifying glass.</p><p>As soon as the FAB position offset is beyond the upward distance limit it detaches from the other metaball and turns into the magnifying glass handle.</p><p>But why stop? Let’s use more APIs and add some finishing touches here and there.</p><h4>Physics-based motion</h4><p>As soon as the FAB is let go, the <a href="https://developer.android.com/reference/android/support/animation/SpringAnimation">SpringAnimation</a> class is used to mimic a physically natural rebound effect.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*uH2RlUdCL-FqkppGnzCMqg.gif" /><figcaption>Larger offset leads to more resistance followed by a physically natural rebound.</figcaption></figure><h4>Slow-down drag</h4><p>I also added custom drag “slow-down” code so it feels like the FAB is attached with a rubber band.</p><h4>Haptic feedback</h4><p>The FAB state change triggers haptic feedback via View.performHapticFeedback() method that pulses the vibration hardware on the device.</p><h4>Onboarding</h4><p>I get it — this design is outstanding and maybe a tiny bit confusing. Let’s help users figure out what’s going on using the icons and fake drag.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*2rX8x4gSYVd-_1Y-dn19iQ.gif" /><figcaption>The drag is simulated to show that the FAB is not static and can be interacted with.</figcaption></figure><p>The icons fade out as soon as the corresponding functions are triggered.</p><h3>Rendering a view to GL texture in real-time</h3><p>The magnifying glass shader takes the target image (the one you want to magnify) as the input. Typically you use sampler2D type of texture, but it shows bad performance if the texture gets invalidated too often or every single frame. Instead, I created SurfaceTexture with external GL texture and access it in the shader code as the samplerExternalOES input.</p><p>🎩 <em>That’s the magic </em>🐇</p><p>One the one hand, it is easy to draw any Android view directly into the SurfaceTexture canvas. In my case, it is just ViewPager2 with images, but this method can render any view hierarchy. On the other hand, samplerExternalOES GL texture is shared with the Android view rendering thread, improving the app performance at times.</p><h3>GLSurfaceView + TextureView = GLTextureView</h3><p>The composition and hierarchy of the views on the screen ruled out the possibility of using GLSurfaceView. But you simply cannot replace GLSurfaceView with TextureView and use the same Renderer object as nothing happened. Or can you?</p><p>What’s the first thing you do before thinking on your own? That’s right, you google it — <a href="https://stackoverflow.com/questions/12061419/converting-from-glsurfaceview-to-textureview-via-gltextureview/12083413">fantastic Stack Overflow thread on the topic.</a></p><blockquote>…I wanted to just replace GLSurfaceView with TextureView, and keep the rest of my code the same, and just receive the advantages of the TextureView.</blockquote><p>He asked — Android community delivered. Now both metaballs and magnifying glass can be treated the same as the general views.</p><p>Rest was the history — it’s a tiny app after all and modern Android development with Kotlin and AndroidX is a breeze. For anyone interested, please dive into the full <a href="https://github.com/Krootl/Beetlens">source code</a> or read my other article 😉</p><p><a href="https://medium.com/krootl/arcore-sceneform-simple-video-playback-3fe2f909bfbc">Bring images to life with ARCore and Sceneform — Part 1: Simple video playback</a></p><p>Farewell 👋</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=42eac15503d0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/krootl/beetlens-42eac15503d0">Beetlens</a> was originally published in <a href="https://medium.com/krootl">Krootl.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Bring images to life with ARCore and Sceneform — Part 3: Video crop and fade-in]]></title>
            <link>https://medium.com/krootl/arcore-sceneform-video-crop-n-fade-in-1c70d72efaf?source=rss-68f91534b3ca------2</link>
            <guid isPermaLink="false">https://medium.com/p/1c70d72efaf</guid>
            <category><![CDATA[augmented-reality]]></category>
            <category><![CDATA[sceneform]]></category>
            <category><![CDATA[arcore]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Oleksii Shliama]]></dc:creator>
            <pubDate>Mon, 29 Apr 2019 11:35:31 GMT</pubDate>
            <atom:updated>2019-10-29T15:55:54.223Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P1B7iD413cb3hwptAP_IdQ.png" /><figcaption>Illustration made by myself in Adobe Draw</figcaption></figure><h3>Bring images to life with ARCore and Sceneform — Part 3: Video crop and fade in</h3><blockquote>The tutorial has been updated to work with ARCore version 1.12.0 so now we’re able to determine whether the image is currently being tracked by the camera to pause/resume the video accordingly.</blockquote><p>This is the third and final part of the series where I cover the combination of ARCore and Sceneform. More specifically, I’m showing you how to play a video, using the Sceneform, on top of an image that can be detected with the help of ARCore <a href="https://developers.google.com/ar/develop/java/augmented-images/"><em>Augmented Images</em></a> capabilities.</p><ul><li><a href="https://medium.com/@shliama/arcore-sceneform-simple-video-playback-3fe2f909bfbc">Bring images to life with ARCore and Sceneform — Part 1: Simple video playback</a></li><li><a href="https://medium.com/@shliama/arcore-sceneform-video-scale-n-rotation-362a3b5de4dc">Bring images to life with ARCore and Sceneform — Part 2: Video scale and rotation</a></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uvTAaZ2KEy8_zigzTly5DA.gif" /></figure><p>At the end of the <a href="https://medium.com/@shliama/arcore-sceneform-video-scale-n-rotation-362a3b5de4dc">previous post</a>, we have ended up with the working demo of the above-described concept. Still, there is room for improvement.</p><p>One thing we can add is the ability to remove unwanted outer areas of a video which exceed the detected image. Please refer to the illustration below so we are on the same page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CupODiZRf25zpLfMuet-1g.png" /><figcaption>CenterCrop scale type &amp; hide video excess areas</figcaption></figure><p>Another thing that can actually improve the user experience is a fade-in effect when a video appears on top of the image.</p><p><strong><em>TL;DR </em></strong><em>Complete source code is available at this </em><a href="https://github.com/shliama/AugmentedVideoTutorial/tree/step_3/video_crop_and_fade_in"><em>repository</em></a><em>.</em></p><h3>Material</h3><p>Both improvements can be done mostly inside the custom video material (i.e. augmented_video_material.mat file). But before we can modify the shader code, we want to add several new material parameters and change the material blending mode.</p><p>Let’s start with the new <a href="https://developers.google.com/ar/develop/java/sceneform/custom-material#parameters">parameters</a>, there are four of them, namely:</p><ul><li><strong>imageSize</strong> and <strong>videoSize</strong> of type float2 — to calculate a difference between image and video aspect ratios, so it’s possible to define an excess outer area</li><li><strong>videoCropEnabled</strong> of type bool — to enable/disable the crop logic</li><li><strong>videoAlpha</strong> of type float — to control video overall transparency</li></ul><p>As for the <a href="https://developers.google.com/ar/develop/java/sceneform/custom-material#blending">blending</a> mode, let’s change it from opaque to transparent, otherwise, the video texture will not look transparent when the alpha channel is modified.</p><p>See the code below with all the changes applied:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4c4b9d79baf6cf513129ceeeffe8f92c/href">https://medium.com/media/4c4b9d79baf6cf513129ceeeffe8f92c/href</a></iframe><h3>Crop</h3><blockquote>Oh boy, it looks like, developing crop features on Android is my thing. Back in the days, when I was working at <a href="https://yalantis.com/">Yalantis</a>, I’ve created <a href="https://github.com/Yalantis/uCrop">uCrop</a> library — it was one <strong>special kind of fun</strong> adventure into bitmaps decoding, matrix calculations, and native libraries. And look at me now, cropping videos in Augmented Reality<em> 🤷‍♂️</em></blockquote><p>I assume there are multiple ways to “crop” (i.e. hide outer areas) a video that is rendered on the ExternalTexture. However, one that I demonstrate below is not just perfectly working but also the only one I came up with and managed to implement on my own.</p><p>My idea of cropping video via shader code is kinda trivial — we just have to make the area we want to crop fully transparent. Piece of cake, right?</p><p>Let’s modify the shader code to execute the cropVideo() function if the <strong>videoCropEnabled</strong> parameter equals true.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f4eb259cafb45ef8ca147b46760dc2e2/href">https://medium.com/media/f4eb259cafb45ef8ca147b46760dc2e2/href</a></iframe><p>Notice that when cropVideo() function returns true there is a return statement. That’s because we don’t need to execute the rest of the material() function for this particular UV coordinates (i.e. set the material color) — it’s going to be absolutely transparent anyway.</p><p>Next, let’s define the body of cropVideo() function. The logic goes like this:</p><ul><li>get the image and video size values from the material parameters</li><li>depending on aspect ratio difference, work with either horizontal or vertical excess areas</li><li>calculate a difference between image and video aspect ratios — that is the float2 excessArea variable</li><li>for all UV coordinates that belong to the excess outer area multiply the material color channels by 0.0 and return true; otherwise, return false.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f3041a7e80786ce732fbe1ef04b6357b/href">https://medium.com/media/f3041a7e80786ce732fbe1ef04b6357b/href</a></iframe><p>Finally, we need to modify the playbackArVideo() function to set above-defined material parameters right after setup of the video Node.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4798e0e389f759319bd2bc5eca6bc018/href">https://medium.com/media/4798e0e389f759319bd2bc5eca6bc018/href</a></iframe><p>As you can see, <a href="https://developers.google.com/ar/reference/java/sceneform/reference/com/google/ar/sceneform/rendering/Material">Material</a> class has several methods (e.g. setFloat2(), setBoolean(), etc.) to modify material parameters in the runtime, which in turn can be used within the shader code.</p><h3>Fade in</h3><p>In order to fade in the video texture, we’ll use the same trick as we used for crop — modifying alpha.</p><p>Shader code modification is trivial, all we need to do is to add one line at the end of the material() function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5dc67d684ad9553c0eb4c67a556b3579/href">https://medium.com/media/5dc67d684ad9553c0eb4c67a556b3579/href</a></iframe><p>When videoAlpha value is equal to 0.0 — video is fully transparent; when the value is equal to 1.0 — video is fully opaque. It’s easy to figure out the rest of fade in logic. We’ll just use <a href="https://developer.android.com/reference/android/animation/ValueAnimator">ValueAnimator</a> to interpolate videoAlpha value from zero to hero. The start of the animation is the perfect place to set the video renderable.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/180a2fb7d6017cf8723fed2691babcaa/href">https://medium.com/media/180a2fb7d6017cf8723fed2691babcaa/href</a></iframe><p>That should do it. Now let’s build the sources and give it a try!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F332818746%3Fapp_id%3D122963&amp;dntp=1&amp;url=https%3A%2F%2Fvimeo.com%2F332818746&amp;image=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F778632289_640.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=vimeo" width="720" height="1280" frameborder="0" scrolling="no"><a href="https://medium.com/media/de7afa4c0fea92d3e1ec7d08a15990a0/href">https://medium.com/media/de7afa4c0fea92d3e1ec7d08a15990a0/href</a></iframe><p>All right, finally we’ve got exactly the same look and feel as on the GIF at the start of the post.</p><p>Let’s recap — during the series we have learned how to draw a video stream to a texture in augmented reality. We’ve also added some code to support video rotation metadata and several different scale types. Finally, we modified the shader code to use the alpha channel in order to crop and fade the video. I think you will agree that it wasn’t that hard from the technical point of view. Most of the non-trivial code is all those equations for different aspect ratios and scale types — not the actual ARCore or Sceneform APIs.</p><p>Nevertheless, I hope someone will find it helpful. And I’d love to read your comments, especially if you know a better way to accomplish this task.</p><p>P.S. The quality and stability of the <a href="https://developers.google.com/ar/develop/java/augmented-images/"><em>Augmented Images</em></a> feature was definitely a PITA. One can casually read through the <a href="https://github.com/google-ar/arcore-android-sdk/issues">ARCore GitHub issues</a> to feel developers&#39; pain. On the other hand, working with <a href="https://github.com/google-ar/sceneform-android-sdk">Sceneform</a> (which has <a href="https://github.com/google/filament">Filament</a> under the hood) was extremely satisfying — for that I tip my hat to <a href="https://medium.com/u/c967b7e51f8b">Romain Guy</a>.</p><p>I’d guess that most of the developers who had a chance to work with the ARCore, including me, would be extremely happy to see this framework evolve and improve. It’s so awesome that we have this kind of technology in our hands nowadays and I do appreciate the ARCore team for the work they have done. Hopefully, Google’s AR ambitions won’t stop on Childish Gambino AR stickers and that sort of stuff ;)</p><p>P.P.S. Since the introduction of the Augmented Images feature, it has been greatly improved. At the moment of writing, it is version 1.12.0 and some of the major issues have been resolved. I am sure it will be much better in the future. Go team ARCore 💪</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1c70d72efaf" width="1" height="1" alt=""><hr><p><a href="https://medium.com/krootl/arcore-sceneform-video-crop-n-fade-in-1c70d72efaf">Bring images to life with ARCore and Sceneform — Part 3: Video crop and fade-in</a> was originally published in <a href="https://medium.com/krootl">Krootl.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Bring images to life with ARCore and Sceneform — Part 2: Video scale and rotation]]></title>
            <link>https://medium.com/krootl/arcore-sceneform-video-scale-n-rotation-362a3b5de4dc?source=rss-68f91534b3ca------2</link>
            <guid isPermaLink="false">https://medium.com/p/362a3b5de4dc</guid>
            <category><![CDATA[augmented-reality]]></category>
            <category><![CDATA[arcore]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[sceneform]]></category>
            <dc:creator><![CDATA[Oleksii Shliama]]></dc:creator>
            <pubDate>Mon, 22 Apr 2019 01:15:59 GMT</pubDate>
            <atom:updated>2019-10-29T15:56:47.693Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pK_subI73dVUrEb_OtrmCQ.png" /><figcaption>Illustration made by myself in Adobe Draw</figcaption></figure><h3>Bring images to life with ARCore and Sceneform — Part 2: Video scale and rotation</h3><blockquote>The tutorial has been updated to work with ARCore version 1.12.0 so now we’re able to determine whether the image is currently being tracked by the camera to pause/resume the video accordingly.</blockquote><p>This is the second part of the series where I cover the combination of ARCore and Sceneform. More specifically, I’m showing you how to play a video, using the Sceneform, on top of an image that can be detected with the help of ARCore <a href="https://developers.google.com/ar/develop/java/augmented-images/"><em>Augmented Images</em></a> capabilities.</p><p><a href="https://medium.com/@shliama/arcore-sceneform-simple-video-playback-3fe2f909bfbc">Bring images to life with ARCore and Sceneform — Part 1: Simple video playback</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uvTAaZ2KEy8_zigzTly5DA.gif" /></figure><p>At the end of the <a href="https://medium.com/@shliama/arcore-sceneform-simple-video-playback-3fe2f909bfbc">previous post</a>, we have ended up with a working demo of the above-described concept. However, there was a couple of issues:</p><ol><li>Video aspect ratio is not maintained</li><li>Some videos are displayed at the wrong angle</li></ol><p>Well, it’s about time to clean this mess.</p><p><strong><em>TL;DR </em></strong><em>Complete source code is available at this </em><a href="https://github.com/shliama/AugmentedVideoTutorial/tree/step_2/video_rotation_and_scale_support"><em>repository</em></a><em>.</em></p><p>Let’s first figure out what could possibly go wrong?</p><p>As for the <strong>aspect ratio</strong>, that’s an easy one — we didn’t write any code to maintain it. The size of the video Node (i.e. the texture on which a video is rendered) is scaled exactly to match detected augmented image. But we don’t live in a perfect world and oftentimes, images and videos do not match. Hence my goal was to gracefully support all possible scenarios, where both image and respective video can have either the same or different size and aspect ratio.</p><p>As for the <strong>rotation</strong> — videos recorded using mobile devices often contain rotation metadata. This data is used by video players to display the video the way it was shot: portrait or landscape. For example, a video shot on a Google Pixel or iPhone in portrait orientation may have <em>1920×1080</em> px resolution and one may think <em>“Hey, the width is greater than height, hence the video is landscape”</em> and one will be wrong because that video file contains rotation metadata with integer value <em>90</em> (or <em>270</em> if device was held upside down). It’s really important to take video rotation metadata into consideration.</p><h3>Metadata</h3><p>To solve both issues, we must know about video resolution and rotation. These properties can be extracted from a video file with the help of <a href="https://developer.android.com/reference/android/media/MediaMetadataRetriever">MediaMetadataRetriever</a> class.</p><p>Previously, we used <a href="https://developer.android.com/reference/java/io/FileDescriptor">FileDescriptor</a> only to set <a href="https://developer.android.com/reference/android/media/MediaPlayer">MediaPlayer</a> data source:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f80e555ef80d20c15ff0d1c9ca08c6e1/href">https://medium.com/media/f80e555ef80d20c15ff0d1c9ca08c6e1/href</a></iframe><p>Now, we will extend this code to also obtain video metadata:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/df526675c629545d88b488c4623bd967/href">https://medium.com/media/df526675c629545d88b488c4623bd967/href</a></iframe><h3>Scale</h3><p>The goal is to support the two most usable scale types — CenterCrop and CenterInside — all equivalent to <a href="https://developer.android.com/reference/android/widget/ImageView.ScaleType">ImageView.ScaleType</a>. Coincidentally, we already have FitXY scale type supported, that’s the original logic of “squashing” a video to match the image size and aspect ratio.</p><p>Let’s define all the options with the enum.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2d2f4cb8e5384e4635076513df8aaf27/href">https://medium.com/media/2d2f4cb8e5384e4635076513df8aaf27/href</a></iframe><p>Next, let’s create a VideoAnchorNode class that extends AnchorNode. It will encapsulate all the code responsible for the video Node scale and rotation.</p><p>Child node instance, namely videoNode, is used for the sake of better composition — in the future it may be easier, for example, to add a frame 3D object around the video, as a child of the VideoAnchorNode class.</p><p>Define a method that receives both image and video dimensions, video rotation and also a scale type.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c71ce837225a3012df78de013a31543/href">https://medium.com/media/5c71ce837225a3012df78de013a31543/href</a></iframe><p>Now, depending on the VideoScaleType parameter, we’ll have different logic.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d5994d5c90e5258e25f40faf8a28c4d6/href">https://medium.com/media/d5994d5c90e5258e25f40faf8a28c4d6/href</a></iframe><h4>FitXY</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jOkEZkNIeHwDT6YB23xlXg.png" /><figcaption>FitXY scale type example</figcaption></figure><p>Scale video width and height independently, so that video dimensions <strong>match</strong> target image exactly. This may change the aspect ratio of the video.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/95be0796ba629e76e8b39df68704e739/href">https://medium.com/media/95be0796ba629e76e8b39df68704e739/href</a></iframe><h4>CenterCrop</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Me6kJEczdW82D8cCzB7Vag.png" /><figcaption>CenterCrop scale type example</figcaption></figure><p>Scale the video uniformly (maintain aspect ratio) so that both dimensions (width and height) of the video will be <strong>equal to or larger</strong> than the corresponding dimension of the target image.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a29382ce31063271ece64f0007a097b/href">https://medium.com/media/7a29382ce31063271ece64f0007a097b/href</a></iframe><h4>CenterInside</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rGgCPwZhPsY-GvXQgG_K5A.png" /><figcaption>CenterInside scaleType example</figcaption></figure><p>Scale the video uniformly (maintain aspect ratio) so that both dimensions (width and height) of the video will be <strong>equal to or less</strong> than the corresponding dimension of the target image.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d30f2614e3897578625f4acd2ae0708d/href">https://medium.com/media/d30f2614e3897578625f4acd2ae0708d/href</a></iframe><p>One can notice that both CenterCrop and CenterInside methods are almost identical except for two lines of code, namely this comparison:</p><p>(videoAspectRatio &lt;&gt; imageAspectRatio)</p><p>Indeed, that’s where the difference between these two scale types lies — either fit the <em>source</em> inside the <em>target</em> or cover the <em>target</em> with the <em>source</em>. Nevertheless, I haven’t merged them into a single method for clarity.</p><h3>Rotation</h3><p>Now, let’s rotate the Node to match video metadata. Luckily, that’s a one-liner:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/055c89a0ba123cab59e787ad39db30f7/href">https://medium.com/media/055c89a0ba123cab59e787ad39db30f7/href</a></iframe><p>Basically, we compensate for the video rotation with the Node rotation in the opposite direction. You may want to change the affected axis if using your own 3D model, where axes aligned differently.</p><h3>Wrapping up</h3><p>Now that we have all the logic in place, it’s time to replace the basic AnchorNode with the VideoAnchorNode and pass the video parameters. Once again, we shall modify ArVideoFragment#playbackArVideo() method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/03a631ca95782f890b5bc053296ba4da/href">https://medium.com/media/03a631ca95782f890b5bc053296ba4da/href</a></iframe><p>Apart from simply passing information about video and image to the Node, we also have to add one <strong>important</strong> step — applying video rotation to the image dimensions. Simply put, we have to swap width and height in case video rotation is equal to <em>90</em> or <em>270</em> degrees so that the aspect ratio equations stay true.</p><p>That’s pretty much it! Now let’s build and install the app on a device and see what will happen.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F331253419%3Fapp_id%3D122963&amp;dntp=1&amp;url=https%3A%2F%2Fvimeo.com%2F331253419&amp;image=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F776570522_640.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=vimeo" width="720" height="960" frameborder="0" scrolling="no"><a href="https://medium.com/media/45788746b179bf36365eb52b4ce7afcb/href">https://medium.com/media/45788746b179bf36365eb52b4ce7afcb/href</a></iframe><p>During the first half of the demo, you can see that the aspect ratio of the videos is maintained and they all fit right inside the corresponding images.</p><p>The second half shows a similar result — aspect ratio of the videos is maintained, however, this time the images are completely covered with the corresponding videos.</p><p>Moreover, the last sample video (one with rotation metadata) does not only maintain it’s aspect ratio but also properly rotated. Sweet!</p><p>But what if you want to use CenterCrop scale type and at the same time not exceed the augmented image area (e.g. crop the video)? No worries folks, I’ve got you covered.</p><p>In the <a href="https://medium.com/@shliama/arcore-sceneform-video-crop-n-fade-in-1c70d72efaf">next post</a>, we’ll modify the shader code to support video crop and add a finishing touch with the fade-in effect. Stay tuned!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=362a3b5de4dc" width="1" height="1" alt=""><hr><p><a href="https://medium.com/krootl/arcore-sceneform-video-scale-n-rotation-362a3b5de4dc">Bring images to life with ARCore and Sceneform — Part 2: Video scale and rotation</a> was originally published in <a href="https://medium.com/krootl">Krootl.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Bring images to life with ARCore and Sceneform — Part 1: Simple video playback]]></title>
            <link>https://medium.com/krootl/arcore-sceneform-simple-video-playback-3fe2f909bfbc?source=rss-68f91534b3ca------2</link>
            <guid isPermaLink="false">https://medium.com/p/3fe2f909bfbc</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[augmented-reality]]></category>
            <category><![CDATA[sceneform]]></category>
            <category><![CDATA[arcore]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Oleksii Shliama]]></dc:creator>
            <pubDate>Mon, 15 Apr 2019 06:30:35 GMT</pubDate>
            <atom:updated>2020-04-17T10:17:46.865Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ix6AX1pPwzexnbm7poXxYA.png" /><figcaption>Illustration made by myself in Adobe Draw</figcaption></figure><h3>Bring images to life with ARCore and Sceneform — Part 1: Simple video playback</h3><blockquote>The tutorial has been updated to work with ARCore version 1.12.0 so now we’re able to determine whether the image is currently being tracked by the camera to pause/resume the video accordingly.</blockquote><p>I’ve been fascinated by augmented reality for quite some time. With both Google and Apple investing in ARCore and ARKit respectively, it’s never been a better moment to jump onto mobile AR wagon and start tinkering your very own augmented experience.</p><p>In the following series, I will cover the combination of ARCore and Sceneform. More specifically, I’ll show you how to play a video, using the Sceneform, on top of an image that can be detected with the help of ARCore <a href="https://developers.google.com/ar/develop/java/augmented-images/"><em>Augmented Images</em></a> capabilities.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uvTAaZ2KEy8_zigzTly5DA.gif" /></figure><ul><li>In this post, we will go through the general implementation of the above idea.</li><li>In part two, we will support video rotation and three different scale types, such as fitXY, centerInside and centerCrop (without hiding excess image area)— all equivalent to <a href="https://developer.android.com/reference/android/widget/ImageView.ScaleType">ImageView.ScaleType</a>.</li></ul><p><a href="https://medium.com/krootl/arcore-sceneform-video-scale-n-rotation-362a3b5de4dc">Bring images to life with ARCore and Sceneform — Part 2: Video scale and rotation</a></p><ul><li>In the third and final part, we’ll add a finishing touch with the video fade-in effect and support hiding excess image area when using centerCrop scale type.</li></ul><p><a href="https://medium.com/@shliama/arcore-sceneform-video-crop-n-fade-in-1c70d72efaf">Bring images to life with ARCore and Sceneform — Part 3: Video crop and fade-in</a></p><p>Without further ado, let’s get started.</p><p>First of all, I assume that you have a proper Android development knowledge; hence I mostly cover AR specific things. Moreover, you are welcome to pull the code from <a href="https://github.com/shliama/AugmentedVideoTutorial">this Github repository</a>. All further steps can be found in according branches (i.e. everything described below belongs to <a href="https://github.com/shliama/AugmentedVideoTutorial/tree/step_1/simple_video_playback"><strong><em>step_1/simple_video_playback</em></strong></a> branch).</p><p>Our <a href="https://github.com/Krootl/AugmentedVideoTutorial/commit/1aa5c0d64c9db7b695587a37552c7c55de308240">starting point</a> resembles <a href="https://github.com/google-ar/sceneform-android-sdk/tree/master/samples/augmentedimage">Sceneform Augmented Images sample</a> but with several notable changes:</p><ul><li>It’s written in Kotlin. Nuff said.</li><li>ArVideoFragment contains all AR-related code for simplicity&#39;s sake. Decomposition is up to you and your business needs. It also extends Sceneform ArFragment thus inheriting most of the boilerplate code that handles Camera permission, ARCore availability checks, etc.</li><li>AugmentedImageDatabase instance is created and the sample images are added in the runtime so it’s easy to swap the sample image with your own; however, you can generate a database with <a href="https://developers.google.com/ar/develop/c/augmented-images/arcoreimg">the arcoreimg tool</a> to save on performance.</li><li>Each sample image has a corresponding sample video. Overall I prepared three image-video pairs to demonstrate all common edge-cases.</li></ul><p>If you launch the <a href="https://github.com/Krootl/AugmentedVideoTutorial/commit/1aa5c0d64c9db7b695587a37552c7c55de308240">initial commit<strong><em> </em></strong>code</a> on a device that supports ARCore (or AVD with the Virtual Scene camera… if you’re brave enough) and point the device camera to a sample image, you should see a bunch of logs in logcat indicating augmented image tracking state. <a href="https://developers.google.com/ar/develop/java/augmented-images/guide">Official documentation</a> might give you more details on how <em>Augmented Images</em> works if you’re not familiar with the topic.</p><p>Now that the app is able to detect sample images, it’s time to d̶o̶ ̶a̶ ̶b̶a̶r̶r̶e̶l̶ ̶r̶o̶l̶l̶ attach a video surface on top of that image. One needs multiple things to be able to render a video with the Sceneform:</p><ul><li>3D model source asset file (*.obj) — represents a surface (in our case, it’s just a simple plane)</li><li>Material definitions file (*.mat) — defines the visual appearance of a surface</li><li>Sceneform Asset Definition file (*.sfa) and Sceneform Binary asset (*.sfb) — references the models and textures in your source asset, and also defines materials by providing material parameters for Sceneform’s physically-based materials.</li><li>Some Kotlin code — to rule them all</li></ul><h4>3D model</h4><p>For our task, we want to render a video on a simple plane. One can look at the <a href="https://github.com/google-ar/sceneform-android-sdk/tree/master/samples/chromakeyvideo"><strong><em>Sceneform Chromakey video sample repository</em></strong></a> to find <a href="https://github.com/google-ar/sceneform-android-sdk/blob/master/samples/chromakeyvideo/app/sampledata/models/chroma_key_video.obj"><strong><em>chromakey_video.obj</em></strong></a> file ready for use. Well, almost ready. In that example, a video is attached perpendicular to the plane, so it looks like the character stands straight on the floor. However, in our case, a video should lay on top of the plane (i.e. augmented image).</p><p>There are two ways to solve this issue, either modify the 3D model asset so its positioned the way we want <em>by default</em> or add some code that rotates the plane whenever we attach it to the augmented image anchor. I prefer the former one — less code means fewer bugs.</p><p>I’ve used <a href="https://www.blender.org/">Blender</a> to modify the 3D model — the difference is easy to notice in the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*foC6G2mgxzZ2_ZfhYdxDOw.png" /><figcaption>Aligned with XY-axes, perpendicular to Z-axis</figcaption></figure><blockquote>A little segue into the folder structure — most of the Sceneform related files will be stored inside the sampledata folder.</blockquote><blockquote>To create the folder, right-click on the app folder in the <strong>Project</strong> window, then select <strong>New &gt; Sample Data Directory</strong>. Note that you need <a href="https://plugins.jetbrains.com/plugin/10698-google-sceneform-tools-beta-">Sceneform plugin</a> installed to see this option.</blockquote><blockquote>The sampledata folder is part of your Android Studio project, but its contents will not be included in your APK.</blockquote><blockquote><strong>— </strong><a href="https://developers.google.com/ar/develop/java/sceneform/import-assets"><strong>ARCore doc</strong></a><strong>.</strong></blockquote><p>Now, either grab the ready-to-use 3D model file from <a href="https://gist.github.com/shliama/6dd562975be9f3033df076e3c20408fa"><strong>here</strong></a> or produce your own. Give this file the name augmented_video_model.obj and put it inside the sampledata folder.</p><h4>Material definition</h4><blockquote>A material definition is a text file that describes all the information required by a material. A minimum viable material definition must contain a material block and a fragment block.</blockquote><blockquote><strong><em>— </em></strong><a href="https://developers.google.com/ar/develop/java/sceneform/custom-material#material_definitions"><strong><em>ARCore doc</em></strong></a><strong><em>.</em></strong></blockquote><p>The first step is to create a new file with the (*.mat) extension inside the sampledata folder (e.g. augmented_video_material.mat).</p><p>Let’s define the material block that contains a list of property pairs that describes all non-shader data.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c00d8afa3695cda976dfbda72f8e5894/href">https://medium.com/media/c00d8afa3695cda976dfbda72f8e5894/href</a></iframe><p><a href="https://developers.google.com/ar/develop/java/sceneform/custom-material#parameters">Parameters</a> can be set at runtime using Sceneform’s material API and then accessed from the shaders. For now, we need just one parameter with the type samplerExternal — it will be used to pass <a href="https://developers.google.com/ar/reference/java/sceneform/reference/com/google/ar/sceneform/rendering/ExternalTexture">ExternalTexture</a> data (i.e. video stream) to the shader.</p><p>Next, we need to specify the <a href="https://developers.google.com/ar/develop/java/sceneform/custom-material#requires">vertex attributes</a> required by the material. In our case, only uv0 should be specified, so it is possible to do <a href="https://en.wikipedia.org/wiki/UV_mapping">texture mapping</a>.</p><p><a href="https://developers.google.com/ar/develop/java/sceneform/custom-material#shadingmodel">Shading model</a> is set to unlit — it’s a perfect choice for rendering a video.</p><blockquote>The unlit material model can be used to turn off all lighting computations. Its primary purpose is to render pre-lit elements such as a cubemap, external content (such as a video or camera stream), user interfaces, visualization/debugging etc.</blockquote><blockquote><strong><em>— </em></strong><a href="https://developers.google.com/ar/develop/java/sceneform/custom-material#unlit_model"><strong><em>ARCore doc</em></strong></a><strong><em>.</em></strong></blockquote><p><a href="https://developers.google.com/ar/develop/java/sceneform/custom-material#blending">Blending</a> is set to opaque — for now, an augmented video will have neither chromakey logic nor general transparency.</p><p>Next step is to define the fragment block that must contain valid <a href="https://www.khronos.org/registry/OpenGL/specs/es/3.0/GLSL_ES_Specification_3.00.pdf">ESSL 3.0</a> code.</p><blockquote>The fragment block must be used to control the fragment shading stage of the material. You are free to create multiple functions inside the vertex block but you <strong>must</strong> declare the <em>material()</em> function. This function will be invoked automatically at runtime by the shading system and gives you the ability to read and modify material properties. The goal of the <em>material()</em> function is to compute the material properties specific to the selected shading model.</blockquote><blockquote><strong><em>— </em></strong><a href="https://developers.google.com/ar/develop/java/sceneform/custom-material#fragment_block"><strong><em>ARCore doc</em></strong></a><strong><em>.</em></strong></blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d56e80c035e716f867de39b8e2699295/href">https://medium.com/media/d56e80c035e716f867de39b8e2699295/href</a></iframe><p>In our case, the code inside the fragment block gets color information from the video texture using UV coordinates and then sets it as the color of the material — basically, lays out the video texture on a plane, pixel by pixel.</p><h4>Sceneform assets</h4><p>Now that we have both the 3D model and its material definition, it’s time to generate (*.sfa) and (*.sfb) files, with the help of the <a href="https://plugins.jetbrains.com/plugin/10698-google-sceneform-tools-beta-">Sceneform plugin</a>.</p><p>To begin the <a href="https://developers.google.com/ar/develop/java/sceneform/import-assets">import process</a>, right-click on the 3D model source asset file (i.e. augmented_video_model.obj) and select <strong>Import Sceneform Asset</strong> option.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tVKoOtJTGuiXPLsXP-7sPg.png" /></figure><p>Both <strong>Source Asset Path</strong> and <strong>.sfa Output Path </strong>are already set for us.</p><p><strong>Material Path</strong> is set to default which tells Sceneform to use the built-in default material but we want to set the path to our own augmented_video_material.mat file.</p><p><strong>.sfb Output Path</strong> is set to thesrc/main/assets/ folder by default. However, if the filename (without file extension) is a valid resource identifier (e.g. R.raw.filename), then you can instead use the src/main/res/raw/ folder. I chose the second option, just because it’s a bit easier to refer to the (*.sfb) file later in the code.</p><p>After you click the <strong>Finish</strong> button, the Sceneform plugin will grind some bytes and produce both augmented_video_model.sfa and augmented_video_model.sfb files.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UqlD2H6iWxrZ8yS_yze2WQ.png" /><figcaption>*.sfa and *.sfb files are the result of Sceneform Plugin gradle tasks execution</figcaption></figure><p>Moreover, it will automatically add these lines of code to the app level grade file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7debe6d67569f7fffb1a1f71404b1a9c/href">https://medium.com/media/7debe6d67569f7fffb1a1f71404b1a9c/href</a></iframe><p>With all the above things set up, we’re ready to use Sceneform API. Splendid!</p><p>While ARCore gives us information about the position, rotation, and size of objects in the real world, Sceneform lets us easily render 3D scenes. For the current use-case, ARCore will tell us where the image is and what are the dimensions; Sceneform will place and render texture, overlapping the image, that will consume a video stream provided by MediaPlayer. The implementation does not take many lines of code and is quite straightforward — let me walk you through the main steps.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c80e25c5f4994ff91a28fd1c6570ed69/href">https://medium.com/media/c80e25c5f4994ff91a28fd1c6570ed69/href</a></iframe><p>The first thing we need is an instance of <a href="https://developers.google.com/ar/reference/java/sceneform/reference/com/google/ar/sceneform/rendering/ExternalTexture">ExternalTexture</a>, which contains <a href="https://developer.android.com/reference/android/graphics/SurfaceTexture">SurfaceTexture</a> and <a href="https://developer.android.com/reference/android/view/Surface">Surface</a> that can be displayed by Sceneform. Next, we call mediaPlayer.setSurface(externalTexture.surface) to instruct the MediaPlayer instance to use above ExternalTexture surface as the sink for the video portion of the media.</p><blockquote>I’ve deliberately used the simplest possible MediaPlayer setup to focus more on the AR aspect of the app. However, one can and should handle things like playback lifecycle, error handling, and <a href="https://developer.android.com/guide/topics/media-apps/audio-focus">audio focus</a>. It is also possible to replace MediaPlayer with <a href="https://developer.android.com/guide/topics/media/exoplayer.html">ExoPlayer</a> if it better meets your business needs.</blockquote><p>The second thing we need is to create a <a href="https://developers.google.com/ar/reference/java/sceneform/reference/com/google/ar/sceneform/rendering/ModelRenderable">ModelRenderable</a> instance. This is actually the thing that renders 3D model (video surface in our case) when attached to a Node instance.</p><p>The source is the augmented_video_model.sfb<em> </em>Sceneform binary asset, which was generated during the previous steps. Inside the thenAccept { renderable -&gt; ... } block, the resulting instance is assigned to the class property, so it can be used later. Both isShadowCaster and isShadowReceiver fields are set to false in order to disable redundant (in our case) costly shadow computation. Another crucial thing is to assign the ExternalTexture instance to the renderable material — the key is videoTexture the same as the name of the material parameter of type samplerExternal. ExternalTexture acts as a bridge between the MediaPlayer video output and renderable material input.</p><p>Lastly, <a href="https://developers.google.com/ar/reference/java/sceneform/reference/com/google/ar/sceneform/AnchorNode">AnchorNode</a> instance is created and it’s parent set to AR scene (the root of 3D scene hierarchy).</p><p>All right, it’s time to connect all the dots! First of all, let’s see how we consume AR scene data.</p><p>On every frame onUpdate method is called.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/809f4e56b60e214bf97ba1c13eac61f0/href">https://medium.com/media/809f4e56b60e214bf97ba1c13eac61f0/href</a></iframe><p>After we make sure that current arFrame is not null it’s safe to retrieve all the tracked augmented images. We want to support the playback of one video at a time. Therefore, if ARCore loses the current active image FULL_TRACKING state we will pause the video. If the same image gets FULL_TRACKING state back, the video will resume. If a new image will become active, then the corresponding video playback will start from scratch.</p><p>Talking about video playback — here is the juicy part.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7d906c615269e402252b9487179e94c3/href">https://medium.com/media/7d906c615269e402252b9487179e94c3/href</a></iframe><p>It turned out handy that every augmented image that was saved to ARCore augmentedImageDatabase has the name that is equal to a corresponding video file stored inside the assets folder. The first block of code opens proper AssetFileDescriptor and feeds it to the MediaPlayer instance. Right after that, we tell the MediaPlayer instance to prepare and start the video playback.</p><p>The second block of code attaches the video Node to the augmented image centerPose making sure, that the Node will stay aligned with the augmented image no matter how users move their mobile devices. Remember that little modification to the 3D model of a plane? Thanks to it we don’t need to either move or rotate the Node — it’s perfectly aligned by default. In order to fully overlap the image — we set the Node instance localScale property equal to the size of the augmented image. The scale trick works only because our 3D plane model is 1-by-1 meter in size.</p><p>Lastly, the third block of code sets OnFrameAvailableListener to the ExternalTexture instance. What happens next is that after the first video frame becomes available (i.e. MediaPlayer manages to deliver a first glance at the video) we assign the Node instance renderable property — hence it appears inside the AR scene for the first time. If you immediately assign Node renderable property, you will see a solid black texture during the preparation of the MediaPlayer which isn’t a good user experience.</p><p>Finally, let’s build and install the app on a compatible device and see what will happen. <em>Don’t forget — the full source code is also available on </em><a href="https://github.com/shliama/AugmentedVideoTutorial/tree/step_1/simple_video_playback"><em>GitHub</em></a><em>.</em></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F329962240%3Fapp_id%3D122963&amp;dntp=1&amp;url=https%3A%2F%2Fvimeo.com%2F329962240&amp;image=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F774903109_640.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=vimeo" width="720" height="1280" frameborder="0" scrolling="no"><a href="https://medium.com/media/9276a1a5b26eb6ba54f949d3caa81c63/href">https://medium.com/media/9276a1a5b26eb6ba54f949d3caa81c63/href</a></iframe><p>Oh well, I can definitely see the videos, and they indeed overlap the images.</p><p>However, one can notice that video proportions are all wrong. This is because the size of an image is not equal to the size of the corresponding video. And we didn’t write any code to compensate for that, yet.</p><p>Also, what’s with the last video rotation? Well, some video files contain metadata, such as rotation and unfortunately, MediaPlayer doesn’t apply it automatically — that’s another thing for us to support.</p><p>Not a big deal folks, trust me, we’ve just finished the boring part. In the <a href="https://medium.com/@shliama/arcore-sceneform-video-scale-n-rotation-362a3b5de4dc">next post</a> (more laconic, fingers crossed), we’ll introduce multiple different scale types along with video rotation support. Stay tuned!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3fe2f909bfbc" width="1" height="1" alt=""><hr><p><a href="https://medium.com/krootl/arcore-sceneform-simple-video-playback-3fe2f909bfbc">Bring images to life with ARCore and Sceneform — Part 1: Simple video playback</a> was originally published in <a href="https://medium.com/krootl">Krootl.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>