Portal belajar membuat aplikasi Augmented Reality dan Virtual Reality

Senin, 04 November 2019

VR- Unity Rotate Camera by Gyroscope and Touch Swipe

22.05 Posted by Dicky Rachmat Iskandar No comments
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GyroscopeCameraRotation : BasicCameraRotation
{
    private float x;
    private float y;

    public bool gyroEnabled = false;
    readonly float sensitivity = 50.0f;

    private Gyroscope gyro;

    void Start()
    {
        gyroEnabled = EnableGyro();
    }

    private bool EnableGyro()
    {
        if (SystemInfo.supportsGyroscope)
        {
            gyro = Input.gyro;
            gyro.enabled = true;
            return true;
        }

        return false;
    }
    void Update()
    {
        if (gyroEnabled)
        {
            GyroRotation();
        }
    }
    void GyroRotation()
    {
        x = Input.gyro.rotationRate.x;
        y = Input.gyro.rotationRate.y;

        float xFiltered = FilterGyroValues(x);
        RotateUpDown(xFiltered * sensitivity);

        float yFiltered = FilterGyroValues(y);
        RotateRightLeft(yFiltered * sensitivity);
    }

    float FilterGyroValues(float axis)
    {
        if (axis < -0.1 || axis > 0.1)
        {
            return axis;
        }
        else
        {
            return 0;
        }
    }
}

public class SpinWheelScript : BasicCameraRotation
{
    Vector3 firstPoint;
    float sensitivity = 2.5f;

    void Update()
    {
        TouchRotation();
    }
    void TouchRotation()
    {
        if (Input.touchCount > 0)
        {
            if (Input.GetTouch(0).phase == TouchPhase.Began)
            {
                firstPoint = Input.GetTouch(0).position;
            }
            if (Input.GetTouch(0).phase == TouchPhase.Moved)
            {
                Vector3 secondPoint = Input.GetTouch(0).position;

                float x = FilterGyroValues(secondPoint.x - firstPoint.x);
                RotateRightLeft(x * sensitivity);

                float y = FilterGyroValues(secondPoint.y - firstPoint.y);
                RotateUpDown(y * -sensitivity);

                firstPoint = secondPoint;
            }
        }
    }
    float FilterGyroValues(float axis)
    {
        float thresshold = 0.5f;
        if (axis < -thresshold || axis > thresshold)
        {
            return axis;
        }
        else
        {
            return 0;
        }
    }
}

public class BasicCameraRotation : MonoBehaviour
{
    //Rotasi ke Atas & Bawah
    public void RotateUpDown(float axis)
    {
        transform.RotateAround(transform.position, transform.right, -axis * Time.deltaTime);
    }

    //Rotasi Ke Kanan & Kiri
    public void RotateRightLeft(float axis)
    {
        transform.RotateAround(transform.position, Vector3.up, -axis * Time.deltaTime);
    }
}

Minggu, 06 Oktober 2019

Back Camera Android - Device Camera

07.27 Posted by Dicky Rachmat Iskandar No comments
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DeviceCamera : MonoBehaviour
{
    bool camAvailable;
    WebCamTexture backCam;
    Texture defaulBackground;

    public RawImage background;
    public AspectRatioFitter fit;

    private void Start()
    {
        defaulBackground = background.texture;
        WebCamDevice[] devices = WebCamTexture.devices;

        if (devices.Length == 0)
        {
            camAvailable = false;
            return;
        }

        for (int   i= 0; i< devices.Length;  i++)
        {
            if (!devices[i].isFrontFacing)
            {
                backCam = new WebCamTexture(devices[i].name, Screen.width, Screen.height);
            }
        }

        if (backCam == null)
        {
            Debug.Log("enabled to find camera");
        }

        backCam.Play();

        background.texture = backCam;

        camAvailable = true;
    }

    private void Update()
    {
        if (!camAvailable)
            return;

        float ratio = (float)backCam.width / (float)backCam.height;
        fit.aspectRatio = ratio;

        float scaleY = backCam.videoVerticallyMirrored ? -1f : 1f;
        background.rectTransform.localScale = new Vector3(1f, scaleY, 1f);

        int orient = -backCam.videoRotationAngle;
        background.rectTransform.localEulerAngles = new Vector3(0, 0, orient);
    }
}

Kamis, 26 September 2019

UNY

00.48 Posted by Dicky Rachmat Iskandar No comments
http://pm.pps.uny.ac.id/id/berita/augmented-reality-kuliah-umum-dari-praktisi-beserta-praktiknya.html

Rabu, 25 September 2019

Spinning Object

02.29 Posted by Dicky Rachmat Iskandar No comments
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpinWheelScript : MonoBehaviour
{
    float rotationSpeed = 10.0f;
    float lerpSpeed = 1.0f;

    private Vector3 speed;
    private Vector3 avgSpeed;
    private bool dragging = false;
    private Vector3 targetSpeedX;


    void OnMouseDown()
    {
        dragging = true;
    }

    void Update()
    {

        if (Input.GetMouseButton(0) && dragging)
        {
            speed = new Vector3(-Input.GetAxis("Mouse X"), 0, 0);
            avgSpeed = Vector3.Lerp(avgSpeed, speed, Time.deltaTime * 5);
        }
        else
        {
            if (dragging)
            {
                speed = avgSpeed;
                dragging = false;
            }
            var i = Time.deltaTime * lerpSpeed;
            speed = Vector3.Lerp(speed, Vector3.zero, i);
        }

        transform.Rotate(Camera.main.transform.up * speed.x * rotationSpeed, Space.World);
        transform.Rotate(Camera.main.transform.right * speed.y * rotationSpeed, Space.World);
        }
    }
}

Senin, 23 September 2019

UnlitShadow (Shader for a shader in AR mode - Vuforia, EasyAR)

10.02 Posted by Dicky Rachmat Iskandar No comments
Shader "UnlitShadows/UnlitShadowReceive" { Properties{ _Color("Main Color", Color) = (1,1,1,1) _MainTex("Base (RGB)", 2D) = "white" {} _Cutoff("Cutout", Range(0,1)) = 0.5 } SubShader{ Pass{ Alphatest Greater[_Cutoff] SetTexture[_MainTex] } Pass{ Blend DstColor Zero Tags{ "LightMode" = "ForwardBase" } CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" #pragma multi_compile_fwdbase #include "AutoLight.cginc" struct v2f { float4 pos : SV_POSITION; LIGHTING_COORDS(0,1) }; v2f vert(appdata_base v) { v2f o; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); TRANSFER_VERTEX_TO_FRAGMENT(o); return o; } fixed4 frag(v2f i) : COLOR{ float attenuation = LIGHT_ATTENUATION(i); return attenuation; } ENDCG } } Fallback "Transparent/Cutout/VertexLit" }

Selasa, 13 Agustus 2019

Simpler Vuforia Event Handling using Unity Events

04.02 Posted by Dicky Rachmat Iskandar No comments
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using Vuforia;
    public class EasyEventHandler : MonoBehaviour, ITrackableEventHandler
    {
    public UnityEvent onMarkerFound;
    public UnityEvent onMarkerLost;
        protected TrackableBehaviour mTrackableBehaviour;
        protected TrackableBehaviour.Status m_PreviousStatus;
        protected TrackableBehaviour.Status m_NewStatus;
        protected virtual void Start()
        {
            mTrackableBehaviour = GetComponent<TrackableBehaviour>();
            if (mTrackableBehaviour)
                mTrackableBehaviour.RegisterTrackableEventHandler(this);
        }
        protected virtual void OnDestroy()
        {
            if (mTrackableBehaviour)
                mTrackableBehaviour.UnregisterTrackableEventHandler(this);
        }
        public void OnTrackableStateChanged(
            TrackableBehaviour.Status previousStatus,
            TrackableBehaviour.Status newStatus)
        {
            m_PreviousStatus = previousStatus;
            m_NewStatus = newStatus;
            if (newStatus == TrackableBehaviour.Status.DETECTED ||
                newStatus == TrackableBehaviour.Status.TRACKED ||
                newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
            {
                onMarkerFound.Invoke();
            }
            else if (previousStatus == TrackableBehaviour.Status.TRACKED &&
                     newStatus == TrackableBehaviour.Status.NO_POSE)
            {

                onMarkerLost.Invoke();
            }
            else
            {
            // For combo of previousStatus=UNKNOWN + newStatus=UNKNOWN|NOT_FOUND
            // Vuforia is starting, but tracking has not been lost or found yet
            // Call OnTrackingLost() to hide the augmentations
            onMarkerLost.Invoke();
            }
        }
    }

Jumat, 14 Juni 2019

Web AR with chromakey video

01.17 Posted by Dicky Rachmat Iskandar No comments
<!doctype HTML>
<html>
 
<script src="https://aframe.io/releases/0.9.0/aframe.min.js"></script>
<script src="https://rawgit.com/jeromeetienne/AR.js/master/aframe/build/aframe-ar.min.js"></script>
<script src="https://rawgit.com/donmccurdy/aframe-extras/master/dist/aframe-extras.loaders.min.js"></script>
<script src="https://unpkg.com/aframe-chromakey-material/dist/aframe-chromakey-material.min.js"></script>

<script>
AFRAME.registerComponent('vidhandler', {
  // ...
  init: function () {
    // Set up initial state and variables.
    this.toggle = false;
    this.vid = document.querySelector("#vid")
    this.vid.pause();
  },
  tick:function(){
 
if(this.el.object3D.visible == true){
  if(!this.toggle){
     this.toggle = true;
     this.vid.play();
    }
  }else{
  this.toggle = false;
    this.vid.pause();
    }
  }
});
</script>


  <body style='margin : 0px; overflow: hidden;'>
    <a-scene embedded arjs="debugUIEnabled: false;" vr-mode-ui="enabled: false">
        <a-assets>
          <video id="vid" src="https://focusar-studio.com/webar/beruang.mp4" loop="true" crossorigin>
          </video>
        </a-assets>
 
      <a-marker id="markerku" vidhandler preset="hiro">
         <a-plane position='0 0 0' scale="1 1 1" width="1.7" rotation="-90 0 0" material='shader: chromakey; src:#vid'></a-plane>
      </a-marker>
      <a-entity camera></a-entity>
    </a-scene>
  </body>
</html>

~shader reference: https://github.com/nikolaiwarner/aframe-chromakey-material/blob/master/README.md

Jumat, 03 Mei 2019

WebAR with AR.js

10.04 Posted by Dicky Rachmat Iskandar No comments
@Bakti19
<script src="https://aframe.io/releases/0.8.2/aframe.min.js"></script>
<script src="https://cdn.rawgit.com/jeromeetienne/AR.js/dev/aframe/build/aframe-ar.js"></script>

<script>
    AFRAME.registerComponent('vidhandler', {
  init: function () {
    this.toggle = false;
    document.querySelector("#vid").pause(); //reference to the video
  },
  tick:function(){
      console.log("Enter Tick");
    if(document.querySelector("a-marker").object3D.visible == true){
        console.log("Enter Valid Mode");
        if(!this.toggle){
            console.log("Enter Play Mode");
            this.toggle = true;
            document.querySelector("#vid").play();
        }
  }else{
      console.log("Enter Pause Mode");
     this.toggle = false;
     document.querySelector("#vid").pause();
  }
  init: function() {
    this.marker = document.querySelector("a-marker")
    this.markerVisible = false
},
tick: function() {
   if (!this.marker) return
   if (this.marker.object3D.visible) {
      if (!this.markerVisible) {
         // marker detected
         this.markerVisible = true
      }
   } else {
      if (this.markerVisbile) {
         // lost sight of the marker
         this.markerVisible = false
      }
   }
}
  }
});
</script>


<body style='margin : 0px; overflow: hidden;'>
<a-scene embedded arjs='sourceType: webcam;'>
      <a-marker preset='hiro' >
          <a-video vidhandler id="vid" src="mayaopening.mp4" autoplay loop="true" width="2" height="1.5" position="0 0 -1.5" rotation="90 180 0"></a-video>
      </a-marker>
</a-scene>
</body>

Minggu, 24 Maret 2019

ios Permission String

20.53 Posted by Dicky Rachmat Iskandar No comments
Photo Permission: App would like to access your photo gallery for your profile photo upload. Your photos wont be shared without your permission.
Contact Permission: App would like to access your contacts to let you choose a friend who you can send a safety sms.
Location Permission: Tootle would like to use your current location to display it on the app, also for directions and estimation of your travel times. It is secure and private.

Rabu, 30 Januari 2019