<?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 Pulkit Juneja on Medium]]></title>
        <description><![CDATA[Stories by Pulkit Juneja on Medium]]></description>
        <link>https://medium.com/@pulkit.16296?source=rss-87fd741b9180------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*92hNW8KPYYWac2zU.</url>
            <title>Stories by Pulkit Juneja on Medium</title>
            <link>https://medium.com/@pulkit.16296?source=rss-87fd741b9180------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 00:14:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pulkit.16296/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[The OpenGL rendering pipeline in a nutshell]]></title>
            <link>https://medium.com/@pulkit.16296/the-opengl-rendering-pipeline-in-a-nutshell-b14874ce8aa2?source=rss-87fd741b9180------2</link>
            <guid isPermaLink="false">https://medium.com/p/b14874ce8aa2</guid>
            <category><![CDATA[rendering]]></category>
            <category><![CDATA[computer-graphics]]></category>
            <category><![CDATA[opengl]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Pulkit Juneja]]></dc:creator>
            <pubDate>Sun, 25 Nov 2018 21:04:04 GMT</pubDate>
            <atom:updated>2018-11-25T21:04:04.950Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kung9DGiGywRMNkaY4jXFg.jpeg" /></figure><p>Did you ever wonder, while playing a video games or watching a animation movie, how do these complex scenes with hundreds of models and textures get transformed into a single image that can be displayed on a monitor ?. Well, that’s exactly what a rendering pipeline such as the one in OpenGL does. OpenGL is a cross-platform Rendering API for interfacing with programmable GPUs for the purpose of rendering real-time 3d graphics. Its use is common in games, CAD, and data visualization application. The rendering pipeline refers to the steps used to convert 3D scene data into a 2D image that can be rendered onto your screen.</p><p>OpenGL provides a set of functions that are used to issue commands to the GPU; but since the actual rendering takes place on the GPU, there is no way to directly configure the rendering pipeline from the application code. Fortunately, we can do that using special programs called shaders. These programs are uploaded to the GPU and can be plugged into a specific stage of the pipeline.</p><p>Before much ado lets look into each stage of the rendering pipeline in more detail.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/271/0*6oES-C10xkU_37UH.png" /><figcaption>OpenGl Rendering Pipeline (Khronos.org)</figcaption></figure><blockquote>The preceding image from the <a href="https://www.khronos.org/opengl/wiki/">OpenGL wiki </a>lists the stages in the current version of OpenGL. the blue stages are configurable shader stages while the yellow ones are fixed function stages. stages with a dash borders are optional</blockquote><h3>Vertex Specification</h3><p>Any 3D model is specified using a collection of vertices and indices along with other data such as textures and animations. A rendering job begins by uploading this vertex data also known as vertex attributes into a set of one or more vertex buffers on the GPU. Common vertex attributes include the location of the vertex in model space, the texture coordinates that map the vertex to a point on the texture and the vertex normals. we also upload the models indices to the element buffer array. these indices control how the vertices get combined into triangles later on during the vertex assembly step. The following image shows the three vertices of a triangle</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/0*9PIF87n1Frd6Awa9.png" /></figure><h3>Vertex Shader</h3><p>Once the vertices are uploaded onto the GPU, they are passed onto the vertex shader. The vertex shader is a small user defined program that takes a of vertex attributes as inputs and outputs a new set of attributes, referred to as <strong>varying</strong> values. These values are fed to the rasterizer which then interpolates and passes them onto the <strong>fragment Shader</strong>. The vertex shader should always return an output vertex, i.e it cannot return a null or empty value. At the minimum a vertex shader calculates the <strong>projected position</strong> of the vertex from the Object space position. The vertex shader can also generate other varying outputs, such as a color, normals or texture coordinates. the following image shows the triangle from the previous example rotated by translations applied in the vertex shader.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*zubc5b1VW9tZTmOKF2jFrg.png" /></figure><h3>Tessellation</h3><p>The tessellation stage is an optional and exciting feature in the new OpenGL 4.x. Tessellation in a nutshell allows you to subdivide your primitives on the fly in order to achieve varying <strong>levels of detail (LOD)</strong> for your models. This proves to be useful when we want to display a model at a much higher LOD when it is closer to the camera than when it is further away from the camera. for example when rendering a complex model such as a human face, we would prefer to use a highly detailed model that will bring out the tiny details (e.g. skin bumps, etc). A highly detailed model automatically translates to more triangles and more compute power required for processing. When we render the same model at a greater distance we would prefer to use a lower detailed model and allow more compute resources to the objects that are closer to the camera. The tessellation stage is further divided into 2 programmable shader stages with a fixed function stage in between. The first shader stage is called <em>Tessellation Control Shader</em> (TCS), the fixed function stage is called the <em>Primitive Generator</em> (PG), and the second shader stage is called <em>Tessellation Evaluation Shader</em> (TES). you can read about the tessellation stage in much more detail <a href="http://ogldev.atspace.co.uk/www/tutorial30/tutorial30.html">here</a>.</p><h3>Geometry shader</h3><p>After the vertex shader, a geometry shader is run. The geometry shader governs the processing of primitives; it takes a single primitive as input and outputs one or more primitives i.e they are used to extrapolate geometry from some input geometry. This means that a geometry shader requires that some basic primitive assembly happen before it is run. Geometry shaders have been used for a variety of different purposes such as for making grass, volumetric shadows, they are also used to tessellate geometry to achieve varying <strong>LOD</strong> levels. geometry shaders just like tessellation is an optional stage. if GS is enabled, a basic form of primitive assembly happens before it. In the following image the geometry shader just added an additional vertex to our triangle.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/0*N3v8c9-I0YMkTWdw.png" /></figure><h3>Vertex Post Processing</h3><p>After the vertex processing stages are done, the output from the stages undergo a variety of operations to set them up for the rasterisation phase.</p><ul><li>Perspective Division — After the vertex pipeline, our vertex positions are in a homogeneous clip space with -<em>w≤ x≤ w, </em>-<em>w≤ y≤ w, 0 &lt; z &lt; w </em>and 0&lt;<em>w </em>. The homogeneous coordinate system is used in projective geometry because much of the math ends up being simpler in homogeneous coordinate space. prior to the <em>Clipping</em> operation, OpenGL performs a <em>perspective division</em>, which involves dividing all four components of the position by the last, <em>w</em> component. This has the effect of projecting the vertex from the homogeneous space to the Cartesian space, leaving <em>w</em> as 1.0.</li><li>Clipping — After the projective division, the resulting position is in <em>normalized device space</em> that extends from −1.0 to 1.0 in the <em>x</em> and <em>y</em> dimensions and from 0.0 to 1.0 in the <em>z</em> dimension. Any geometry that is contained in this region may become visible to the user and anything outside of it should be discarded. Different primitives are handled differently for this step. For example a vertex that lie outside the clip region is discarded completely whereas a triangle, that is partially outside the clip region, is split into smaller triangles by generating appropriate triangles who’s vertices are on the boundary of the clipping volume.</li><li>Viewport Transformation — After clipping, all of the vertices of the geometry have coordinates that lie between −1.0 and 1.0 in the <em>x</em> and <em>y</em> dimensions. Along with a <em>z</em> coordinate that lies between 0.0 and 1.0. However, the window that you’re drawing to has coordinates that usually1 start from (0, 0) at the bottom left and range to (<em>w</em> − 1, <em>h</em> − 1), where <em>w</em> and <em>h </em>are the width and height of the window in pixels, respectively. To place your geometry into the window, OpenGL applies the <em>viewport transform</em> using the following mathematical operation.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/394/0*crcHsXfvPj7hXQSD.png" /></figure><ul><li>where <em>farVal</em> and <em>nearVal</em> are the far and near plane distances in the <em>z</em> coordinate, respectively.</li></ul><h3>Primitive Assembly</h3><p>The primitive assembly steps convert a vertex stream into a sequence of base primitives. For example, a primitive which is a line list of 12 vertices needs to generate 11 line base primitives. Now from what I understand about the primitive assembly step, its not sequentially after the vertex processing phase. Even though a full primitive assembly always happen at this stage, Some sort of early primitive assembly must happen in case the <em>Tesselation</em> or <em>Geometry Shader</em> stages are active as both stages require base primitives as their inputs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/0*N3v8c9-I0YMkTWdw.png" /></figure><ul><li>Face Culling — One thing that definitely happens after primitive assembly is face culling. Triangles after all processing have a facing or winding order determined by the order of their vertices. the triangles can either be back facing or front facing which is then used to decide whether to keep or discard these triangles. It is very common to discard back facing triangles because when an object is closed, any back-facing triangle will be hidden by another front-facing triangle. face culling is an optional step and can be enabled by calling <em>glEnable()</em> with cap set to GL_CULL_FACE.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*yfRV8UQ0sb_08xdG.png" /></figure><h3>Rasterization</h3><p>After the vertex processing and the primitive assembly steps, a fixed function step called rasterization is run. The <strong>rasterizer</strong> takes each triangle, clips it and discards parts that are outside of the screen, and breaks the remaining visible parts into pixel-sized <strong>fragments</strong>. A vertex shader has varying outputs that are interpolated across the rasterized surface of each primitive, assigning a smooth gradient of values to each fragment. For example if the vertex shader outputs a different color for each vertex belonging to the primitive the rasterizer would blend those colors across the pixelated surface of the primitive. In the following image you can see that we have rasterized our triangle albeit in a very low resolution.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/0*cIUcZ20LvRRIDdgO.png" /></figure><h3>Fragment Shader</h3><p>The generated fragments then pass through another stage called the fragment shader. A fragment shader is a small program that receives the varying values outputted by the vertex shader and interpolated by the rasterizer. It outputs the final color and depth of the fragment that then get drawn to the framebuffer. Common use cases for fragment shader include texture mapping and per-pixel-lighting. A fragment shader is run for every pixel drawn can can be used to simulate some very complex effects however, this also makes it the most performance sensitive part of the entire pipeline. An unoptimized fragment shader can really hit the performance of your application. Even though you would almost always use a fragment shader, they are technically optional. If no fragment shader is used, then the color values of the output Fragment have undefined values. This can be useful when only the fragment’s depth value is required as an output.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*41UsW3RERsCNHsXoRctY9g.png" /></figure><h3>Per-Sample Operations</h3><p>A frame buffer is the final output for the rendering pipeline. In addition to the default frame buffer that is sent to the monitor, most graphic APIs also allow you to make frame buffer objects that can then be drawn to off screen render buffers or textures. . A frame buffer is much more than a 2D image; in addition to the color buffer, it can have a <strong>depth buffer</strong> and/or <strong>stencil buffer</strong>. These are used to perform some final tests which decide whether the fragment will be drawn or discarded.</p><ul><li>Stencil Test — Once the fragment shader has processed the fragment a so called stencil test is executed that is based on the content of yet another buffer called the stencil buffer that we’re allowed to update during rendering to achieve interesting effects. A stencil buffer (usually) contains 8 bits per stencil value that amounts to a total of 256 different stencil values per pixel/fragment. We can then set these stencil values to values of our liking and then we can discard or keep fragments whenever a particular fragment has a certain stencil value. One of the most common uses of stencil buffer is object outlining</li><li>depth Test — The depth buffer stores, as the name implies, the depth of the current fragment in 16, 24, or 32 bit values. When depth testing is enabled OpenGL tests the depth value of a fragment against the content of the depth buffer. OpenGL performs a depth test and if this test passes, the depth buffer is updated with the new depth value. If the depth test fails, the fragment is discarded. Depth testing is enabled by default but can be disabled. Today most GPUs support a hardware feature called early depth testing. Early depth testing allows the depth test to run before the fragment shader runs. Wherever it is clear a fragment is never going to be visible (it is behind other objects) we can prematurely discard the fragment. This is useful as fragment shaders tend to be very expensive and the less fragments we have to process the better.</li></ul><p>After this, <strong>color blending happens</strong>. During which OpenGL takes the color output for the fragment from the fragment shader and combines them with the colors already present in the color buffers that these outputs map to. Blending parameters can allow the source and destination colors for each output to be combined in various ways. you can read about blending in a lot more detail <a href="https://www.khronos.org/opengl/wiki/Blending">here</a>. After color blending we get the final output frame buffer that is ready to be sent onto the display, which on a very low resolution display will look something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*FqO4CGDEtXuXEOtDWyNSCA.png" /></figure><p>And that’s all!. That is what happens whenever your GPU wants to render something on the monitor. Hope this was useful. Cheers!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b14874ce8aa2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building Markerless AR For Web using Three.ar.js (Part 1)]]></title>
            <link>https://medium.com/@pulkit.16296/building-markerless-ar-for-web-using-three-ar-js-part-1-5eca95d545ec?source=rss-87fd741b9180------2</link>
            <guid isPermaLink="false">https://medium.com/p/5eca95d545ec</guid>
            <category><![CDATA[threejs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[augmented-reality]]></category>
            <dc:creator><![CDATA[Pulkit Juneja]]></dc:creator>
            <pubDate>Mon, 02 Jul 2018 08:30:03 GMT</pubDate>
            <atom:updated>2018-07-02T10:24:05.860Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L3_JGnrC1Koiz9NWcdsu7A.jpeg" /></figure><p><a href="https://threejs.org/">Three.js</a> is a wonderful library for developing 3D applications that run on WebGL. One of the best things about it is that it runs on the browser and is platform agnostic. That’s why I was excited about <a href="https://github.com/google-ar/three.ar.js">Three.ar.js</a>, a plugin for Three.js that allows developers to create seamless, cross platform Augmented reality experiences. In this series, I will cover the basics of Three.ar.js and walk you through developing a small AR application using it.</p><h3>Prerequisites</h3><p>This post assumes that you have</p><ul><li>experience with Javascript programming</li><li>a basic understanding of Three.js.</li></ul><p>For those looking to learn Three.js there is an amazing blog post by <a href="https://medium.com/u/4f03e3dbc8fa">@necsoft</a> <a href="https://medium.com/@necsoft/three-js-101-hello-world-part-1-443207b1ebe1">here</a>. Apart from that the <a href="https://threejs.org/docs/">official docs</a> are also pretty amazing for getting started.</p><h3>What is Markerless AR</h3><p>Traditional Marker-based AR systems use a marker to keep track of virtual objects position and rotation with respect to the real world. Markerless AR systems take a different approach. They use data from different sensors of the device (GPS, Camera etc). to accomplish feats required for an AR experience such as world tracking and feature Point detection.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/967/0*j8Z1rovp7dURz3zK.png" /><figcaption>Remember Them ?</figcaption></figure><p>So how does it work ?. Any Platform seeking to provide a markerless AR experience must integrate three key capabilities.</p><ul><li><strong>Motion Tracking</strong> — which allows the device to keep track of its location in the real world</li><li><strong>Environmental understanding </strong>— which allows the device to detect the size and location of all type of surfaces: horizontal, vertical and angled surfaces like the ground, a coffee table or walls</li><li><strong>Light Estimation</strong> — which allows the phone to estimate the environment’s current lighting conditions.</li></ul><p>Using these capabilities allows the system to define “Anchor” points which are then used to track the position of the virtual objects.</p><blockquote><strong>You can think of any AR application in this way. The world around you represents the scene, while your phone acts as the camera through which the scene is rendered. As you move your phone, The camera moves in the scene allowing you to see the world from different angles and positions</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*XBjGX0bA3XlaicX4.gif" /></figure><h3>Setting things Up</h3><p>Before we get started, let me give you a glimpse of what our final output will look like. we would be creating a small app which will let you shoot balls through virtual hoops anchored in space.</p><p>I compiled a gif of the result that you can view <a href="https://i.imgur.com/ZuyvhBO.gifv">here</a> .</p><h4>Getting your hands on a compatible device</h4><ul><li>The first thing you need to do is get your hands on a device that’s compatible ArKit (iPhone 6s and above, Ipad pro and above) or ArCore (<a href="https://developers.google.com/ar/discover/supported-devices">Supported ArCore devices</a>) .</li><li>Once you have that you need to set up <a href="https://github.com/google-ar/WebARonARKit">WebARonARKit</a> (if the device is an iPhone or <a href="https://github.com/google-ar/WebARonARCore">WebARonARCore</a> if the device is an android. The instructions for that are very well given on their GitHub pages. These are minimal experimental web browsers that expose ArKit/ARCore functionality to web content.</li></ul><p>Once you have setup your device, you can check out some of the examples at <a href="https://developers.google.com/ar/develop/web/quickstart">https://developers.google.com/ar/develop/web/quickstart</a> to see if everything is running smoothly.</p><h4>Setting up Three.js for Development</h4><p>Even though you can just import the Three.js library from CDN and write your entire code in a single HTML file, I prefer a node, Webpack based environment for development. This is because it allows me to organize my code in a better way which becomes really necessary as the project starts to grow in size.</p><p>Now if you don’t want to go through the hassle of writing the package scripts for yourself, I created a boilerplate code for this tutorial that you can find <a href="https://github.com/pulkitjuneja/Three-Ar-Boilerplate">here</a>. That being said I will still go through the process of creating a new project from scratch.</p><ul><li>Create a new Directory (or navigate to an empty one) and copy this <a href="https://github.com/pulkitjuneja/AR-hoops/blob/boilerplate/package.json">Package.json </a>into it.</li><li>Run npm install and it should install all the dependencies including <a href="https://webpack.js.org/">Webpack</a> which we will use to bundle and server the project.</li><li>To configure Webpack we will copy this <a href="https://github.com/pulkitjuneja/AR-hoops/blob/boilerplate/webpack.config.js">Webpack config</a> into our root Directory.</li></ul><p>A Webpack configuration is used to provide information such as which file to use as the entry point for the project, the location of the final minified bundle file and the loaders to use to transpile the javascript files. you can find out more about Webpack configuration files <a href="https://webpack.js.org/configuration/">here</a>. This config tells Webpack to use src/Main.js as the entry point for the project, bundle.js as the location for the minified bundle file as well as the loaders to transpile the project.</p><ul><li>Next we will need to create the index.html page that will be served when our web servers run. Copy this <a href="https://github.com/pulkitjuneja/AR-hoops/blob/boilerplate/index.html">index.html</a> in the root directory of the project.</li><li>At this point, we are done with the basic setup for the project. If you want the eslint goodness (which you would) you can also copy the <a href="https://github.com/pulkitjuneja/AR-hoops/blob/boilerplate/.eslintrc">.eslint.rc</a> into the root directory.</li></ul><h3>Creating the Renderer</h3><p>Once we have the basic setup in place we can start writing the code for our app .</p><p>The fundamental thing that any Three.js program needs is a renderer, So let’s start by writing a renderer. Create a new file in src/Managers/Renderer.js and add the following code into it</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/96353a5e39bf37e7e37c679a999e45d5/href">https://medium.com/media/96353a5e39bf37e7e37c679a999e45d5/href</a></iframe><p>As you can see, this class does more than just initialize a <a href="https://threejs.org/docs/#api/renderers/WebGLRenderer">WebGlRenderer</a>. This is because Rendering an AR program in Three.js requires two other things in addition to a renderer.</p><ul><li>A <a href="https://developer.mozilla.org/en-US/docs/Web/API/VRDisplay">VRDisplay</a> to represent the VR device and invoke the requestAnimationFrame call in place of the renderer.</li><li>An ArView that handles the pass-through camera differences between ARCore and ARKit platforms, and renders the camera behind your scene</li></ul><h3>Adding the scene and the camera</h3><p>The next thing that we need to add for any Three.js program is the scene and the camera. Let’s start By adding the Camera</p><h4><strong>The Camera</strong></h4><p>Let’s define a new class that will hold these components.</p><p>Create a new class App in src/Managers/App.js and add the following code to it</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c3ece68b9470578bc1c6fa0c9148b9cd/href">https://medium.com/media/c3ece68b9470578bc1c6fa0c9148b9cd/href</a></iframe><p>This defines a new camera of type <a href="https://github.com/google-ar/three.ar.js/blob/master/API.md#new-threearperspectivecameravrdisplay-fov-aspect-near-far">ARPerspectiveCamera</a> in the class. The ARPerspectiveCamerais a subclass of the <a href="https://threejs.org/docs/#api/cameras/PerspectiveCamera">THREE.PerspectiveCamera</a> and is used when we want to render three.js objects on top of an AR see through camera.</p><p>An AR application also requires that the camera move and rotate along with the device.</p><p>For this, we will Use the Three.js VR controls. These were earlier available as a part of the library but were later on moved to the examples section so we will need to include them in our project separately.</p><ul><li>Copy the <a href="https://github.com/google-ar/three.ar.js/blob/master/third_party/three.js/VRControls.js">VRControls</a> in Three.ar.js github repository to a new file in src/Utils/VRControls.js</li><li>Then make the following changes in the App class</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e3dd0f20ea0bfd8ab499465815f5772a/href">https://medium.com/media/e3dd0f20ea0bfd8ab499465815f5772a/href</a></iframe><h4><strong>The Scene</strong></h4><p>Setting up the scene is simple, same as we would do for a normal THREE.js program. All we need to do (For now) is create a new THREE.Scene() and add our camera to it</p><ul><li>Add the following function to our App class</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/80d523bd8e0e2019919691b71865a2a8/href">https://medium.com/media/80d523bd8e0e2019919691b71865a2a8/href</a></iframe><h4><strong>Writing the update Loop</strong></h4><p>One last thing we need to do to finish up the basic wiring for our Three.Js scene is write the update loop that will be called every frame through the <a href="https://developer.mozilla.org/en-US/docs/Web/API/VRDisplay/requestAnimationFrame">requestAnimationFrame</a> method and kick it off.</p><ul><li>Add the following functions to the App class</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/022b1f779415404dde934d3846a122ec/href">https://medium.com/media/022b1f779415404dde934d3846a122ec/href</a></iframe><p>You might notice we have written the actual update function &#39;curried&#39; under a startUpdate function and not directly. This is because the update function will lose its current context when it is invoked from the requestAnimationFrame callback and the value of &#39;this&#39; would be null. To prevent that we assign the value of the &#39;this&#39; pointer to a variable which we then use within the function.</p><ul><li>Now we just need to call this start function from the src/Main.js</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cb1902fd72aeebc9cae74a35644710b1/href">https://medium.com/media/cb1902fd72aeebc9cae74a35644710b1/href</a></iframe><p>If you run the app now, you will see the live feed from your camera in the browser window. not much exciting isn’t it ?.</p><h3>Adding Some 3D Objects</h3><p>What’s AR without Virtual content. Lets quickly add a 3D Mesh to the screen and test our code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/520224f3eb61196ec8f497b2cce54b16/href">https://medium.com/media/520224f3eb61196ec8f497b2cce54b16/href</a></iframe><p>and in the update method add this line.</p><p>currentInstance.torus.rotateY(0.1);</p><p>This will add a rotating Torus and a light to the scene. If you now try and run the code you should a red torus rotating over your camera feed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*HoJNY4nzooXQMATQ.gif" /></figure><h3>Entities and Entity Manager</h3><p>In the last section, we added the code for Torus mesh generation and rotation directly into the App class. While we can keep on doing this, this will make the App class really cluttered as we add more objects to the scene. For this reason, I like to separate out the code for each object into its own entity class handling the object&#39;s generation and update loops.</p><p>Let’s start by creating a base class for our entity. The base class will act as an interface between the entity and anything trying to use the entity. This allows callers to know which functions they can expect an entity to have.</p><p>In a new file src/Entities/BaseEntity add the following code</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d65d12e46692dedc4ac199a657c60cba/href">https://medium.com/media/d65d12e46692dedc4ac199a657c60cba/href</a></iframe><p>The class assigns each object a unique ID and name. This will be useful when we need to search for an entity. It also defines two methods update() and dispose(). Any entity can override these two methods with their own specific definition.</p><p>Let’s define an entity class for the torus we created above.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c04c32ab1f2ab2f6580ce4d03931e2c/href">https://medium.com/media/5c04c32ab1f2ab2f6580ce4d03931e2c/href</a></iframe><p>Then we can modify our App class to use it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c3b8d27ccfb7888968b05a81251be537/href">https://medium.com/media/c3b8d27ccfb7888968b05a81251be537/href</a></iframe><p>This already makes the code so much cleaner, but wouldn’t it be even nicer if we have some kind of manager class that handles all our entities and our App class has to just interact with that manager?</p><h4>The Entity Manager</h4><p>The Entity manager will be a simple class (No Three.js code) which will contain a collection of all the entities in the scene and methods to</p><ul><li>Add/Remove an entity</li><li>Update entities</li><li>find an entity</li></ul><p>Create a new file src\Managers\EntityManager and add the following code to it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/566274e2824cf35c024e6233d3ffc487/href">https://medium.com/media/566274e2824cf35c024e6233d3ffc487/href</a></iframe><p>Then we can use this in our App class. we just need to modify the code for adding the Torus</p><pre>entityManager.addEntity(new MoveableTorus(&#39;torus&#39;, this.scene, new THREE.Vector3(0, 0, -1.5)));</pre><p>and in our update function</p><p>So how does this all help ?. To add another torus to the scene all we have to do is add</p><pre>entityManager.addEntity(new MoveableTorus(&#39;torus1&#39;, this.scene, new THREE.Vector3(-4, 0, -1.5)));</pre><p>and we will have another spinning Torus in our scene.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/479/1*gWUQ2AoIqG8REqJTPvcrJA.gif" /></figure><p>That’s it for the first part of the series, The full code for this demo can be found <a href="https://github.com/pulkitjuneja/AR-hoops/tree/Part1">here</a>.</p><p>In the next part, we will discuss how we can move the objects and spawn new objects based on user’s touch input.</p><p>Cheers :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5eca95d545ec" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using multiple player prefabs with UNET in Unity]]></title>
            <link>https://medium.com/@pulkit.16296/using-multiple-player-prefabs-with-unet-in-unity-b458b7a8e80b?source=rss-87fd741b9180------2</link>
            <guid isPermaLink="false">https://medium.com/p/b458b7a8e80b</guid>
            <category><![CDATA[unity3d]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Pulkit Juneja]]></dc:creator>
            <pubDate>Tue, 01 May 2018 14:35:33 GMT</pubDate>
            <atom:updated>2018-05-03T13:53:14.686Z</atom:updated>
            <content:encoded><![CDATA[<p>Unity provides an inbuilt <a href="https://docs.unity3d.com/ScriptReference/Networking.NetworkManager.html">network manager</a> that is really good for small games and quick prototyping.It comes as an out if the box pluggable component that can be added to any game object in the scene and configured to an extent using the Unity UI itself. However, using this script “as is” does take away some of the flexibility that the Unity’s <a href="https://docs.unity3d.com/Manual/UNetUsingHLAPI.html">HLAPI</a> is actually capable of. One such functionality is the ability to use different player prefabs for different players.</p><p>In this post, I will show you how you can use a custom network manager to control player spawning for different connections.</p><h3>What are Player Objects ?</h3><p>So, what are player Objects in UNET and how do they differ from other networked objects ?. Why can’t you spawn them just like other networked Objects using <a href="https://docs.unity3d.com/ScriptReference/Networking.NetworkServer.Spawn.html">NetworkServer.Spawn</a>?.</p><p>Player objects are special in a sense because they are local to a single client connection. The <a href="https://docs.unity3d.com/ScriptReference/Networking.NetworkBehaviour.html">NetworkBehavior</a> class for player objects provides a variable isLocalPlayer that can be used to identify whether a player is local to the current game or not. This is important in a networked game because out of the many player instances only the local instance should accept inputs from the user. This is what makes the spawning process of a player object different. Here is a list of steps that take place when a player object is to be spawned for a connection</p><ul><li>A client connects to the host</li><li>client calls the <a href="https://docs.unity3d.com/ScriptReference/Networking.ClientScene.AddPlayer.html">ClientScene.AddPlayer()</a> or sends a network message of type <a href="https://docs.unity3d.com/ScriptReference/Networking.MsgType.AddPlayer.html">MsgType.AddPlayer</a> to the server</li><li>Server receives the message and invokes <a href="https://docs.unity3d.com/ScriptReference/Networking.NetworkManager.OnServerAddPlayer.html">OnServerAddPlayer()</a> on the NetworkManager with the requesting client information</li><li>The player game object is instantiated and is passed to <a href="https://docs.unity3d.com/ScriptReference/Networking.NetworkServer.html">NetworkServer.AddPlayerForConnection()</a> along with the client information.</li><li>A network message of type MsgType.Owner is sent to the client that added the player (only that client!)</li><li>The above message when received by the client causes the isLocalPlayer to be set to true for that client’s game instance.</li></ul><p>So that’s the theory behind the process. ready to experiment with it?</p><h3>What will we be making</h3><p>In this post, I will walk you through making a basic character selection scene. After connecting to the game the user will see three buttons letting him select from one of the three player classes. upon clicking a button a message would be sent to the server and the player will be spawned local to that connection</p><h3>Preparing the scene</h3><ul><li>Create a new project in unity and</li><li>Add a new 3D plane to the scene at position (0,0,0) and with scale (10,1,10). This will be the plane our players will walk on.</li><li>Add a new button to the screen by right-clicking on the hierarchy pane -&gt; UI -&gt; Button. This will automatically create a canvas game object and add your button to it.</li><li>Duplicate the button 2 times and arrange all three buttons on the top right corner of the screen.</li><li>change the text displayed as well as the name of the buttons to “SpawnRedButton”, “SpawnBlueButton” and “SpawnRedButton” respectively.</li></ul><p>your scene should look something like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*3_L7wmZmeGHaRsbg.png" /></figure><h3>Creating the players and a basic player controller</h3><ul><li>create a new Capsule Game Object. This will be the base for our very basic, just for the sake of this tutorial players.</li><li>Create a cube game object inside the player game object positioned at (0,0.56,0.13) with scale (0.9,0.2,0.7). This will act as a visor which will tell us the direction in which the player is facing.</li><li>Next, Add a RigidBody Component and restrict the rotation in the x and y axes. This is to prevent our players from toppling over.</li><li>Finally, and a red colored material and your player should look something like this</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*AUoUx_QR2ciNic_Y.png" /></figure><ul><li>Create a new script, name it as PlayerController and add the following code into it</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/27263764b5640a6fde5af89648a59597/href">https://medium.com/media/27263764b5640a6fde5af89648a59597/href</a></iframe><p>pay attention to how we are making use of the isLocalPlayer variable to make sure only the local player would respond to keyboard input. If you run the game now you should be able to move your player on the plane using the WASD keys.</p><ul><li>Since our players are networked objects and need to transmit their movements. we will also need to add the <a href="https://docs.unity3d.com/Manual/class-NetworkIdentity.html">NetworkIdentity</a> and the <a href="https://docs.unity3d.com/Manual/class-NetworkTransform.html">NetworkTransform</a> components to them.</li><li>Finally, Create the other two player types with green and blue materials respectively and save all the three Game Objects as prefabs</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*D6l04Fs7NEnZRu6N.png" /><figcaption>our badass players</figcaption></figure><h3>Writing the network logic</h3><p>Now we can start writing the network logic.</p><ul><li>To inform the server which player class to spawn, the client will need to send an extra message to the server along with the MsgType.AddPlayer message. Lest start by defining this message.</li><li>create a new script PlayerInfoMessage.cs and add the following code to it</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/60a61bb995f576e3c1face570e51b04e/href">https://medium.com/media/60a61bb995f576e3c1face570e51b04e/href</a></iframe><blockquote>Any message that needs to be sent over the network (Just like our message) needs to inherit from the <a href="https://docs.unity3d.com/ScriptReference/Networking.MessageBase.html">MessageBase</a> class.</blockquote><p>After this, we can start writing the custom network Manager. The custom network manager will inherit from the base NetworkManager and override the OnServerAddPlayer allowing us to control player instantiation.</p><ul><li>Create a new script customNetworkManager and add the following code to it</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/944f686400823bdc202dc597dd170362/href">https://medium.com/media/944f686400823bdc202dc597dd170362/href</a></iframe><blockquote>The OnServerAddPlayer instantiates a player object and calls the AddPlayerForConnection to make the player local to that connection</blockquote><blockquote>It uses a <a href="https://docs.unity3d.com/ScriptReference/Networking.NetworkReader.html">NetworkReader</a> to read the playerInfoMessage passed from the client and uses it to choose the Prefab to spawn.</blockquote><ul><li>To make this custom network manager active, attach the script to a game object. and assign the player prefabs to it from the Editor UI.</li><li>Also attach the NetworkHUD component to the same game object.</li></ul><p>now that we have a network manager ready and capable to process spawn requests from the clients, let us write the code to send these requests.</p><ul><li>Create a new script ButtonHandlers.cs and add the following code to it</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/859ab87c192bc78e03cb86653725a046/href">https://medium.com/media/859ab87c192bc78e03cb86653725a046/href</a></iframe><blockquote><strong>A little explanation of the code</strong> — In the start method we add listeners for the three buttons created in the initial step. Each of these listeners define a PlayerInfoMessage with the required PlayerClass and pass it to the ClientScene.AddPlayer() call. Notice that the second argument to the call is always 0. this is to make sure that only one player per client connection can be called. To spawn more players, this value should be incremented</blockquote><ul><li>After this, create a parent game object to the three spawn buttons and attach this script to it. Name this game object “SpawnButtons”.</li><li>Since the Listeners reference to NetworkClient.connection. we need to make sure that the client is indeed connected to a host before any of these buttons are clicked. else the connection object simply does not exist</li><li>To do this add the following code to the customNetworkManager class</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/23e5cddc5424e15f2b01d164a2bbbb1d/href">https://medium.com/media/23e5cddc5424e15f2b01d164a2bbbb1d/href</a></iframe><p>The OnStartClient and OnStopClient methods are called on a client when the client is started and stopped respectively. we leverage these callbacks to show the UI when a connection is active and hide it when its now.</p><p>So that’s about it !. If you build the game now you can see it spawning players across the network.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*sV-GXrjtMrz_SIdd.gif" /></figure><h3>Source Code</h3><p>You can find the sample project for this post at <a href="https://github.com/pulkitjuneja/UNET-Multiple-Players">https://github.com/pulkitjuneja/UNET-Multiple-Players</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b458b7a8e80b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>