Runtime Chromakey in Unity

PunSystem

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using UnityEngine.UI;
public class Punsystem : MonoBehaviour
{
    public PhotonView M_Photonview; //That object we want to use this script, must be have photon view component
    public GameViewDecoder GameViewDecoder; //The component of video decoder for convert the bytes to screen
    public string YourName; //The name of you !!!
    public GameObject Track;
    public Toggle WebCamToggle;
    public string ChromakeyMode;
    public Toggle CaptureToggle;
    public GameObject VRAvatar;
    public GameObject VRGround;
    public string MultiuserVRMode;
    public GameObject LiveStream;
    private void Start()
    {
        YourName = PlayerPrefs.GetString("UserName");
        ChromakeyMode = PlayerPrefs.GetString("ChromakeyMode");
        if (ChromakeyMode == "False")
        {
            CaptureToggle.isOn = false;
            CaptureToggle.gameObject.SetActive(false);
        }
        MultiuserVRMode = PlayerPrefs.GetString("MultiuserVRMode");
        if (MultiuserVRMode == "True")
        {
            VRAvatar.SetActive(true);
            VRGround.SetActive(true);
        }
    }
    public void WebCam()
    {
        if (WebCamToggle.isOn == true)
        {
            Track.SetActive(false);
        }
        if (WebCamToggle.isOn == false)
        {
            Track.SetActive(true);
        }
    }
    public void LiveStreamShower()
    {
        if (CaptureToggle.isOn == true)
        {
            LiveStream.SetActive(true);
        }
        if (CaptureToggle.isOn == false)
        {
            LiveStream.SetActive(false);
        }
    }
    public void SendMessage(byte[] _byteData, string message, string username)
    {
        M_Photonview.RPC("RPC_SendMessage",RpcTarget.All, _byteData, message,username);
    }
    [PunRPC]
    private void RPC_SendMessage(byte[] _byteData, string message, string username)
    {
        if (ChromakeyMode == "False")
        {
            if (YourName != username)
            {
                if (message.Contains("VideoShare"))
                {
                    GameViewDecoder.Action_ProcessImageData(_byteData);
                }
            }
        }
    }
}


ARClicktoPlace

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
public class ARClicktoPlace : MonoBehaviour
{
    public GameObject Model;
    public GameObject objectToPlace;
    public GameObject placementIndicator;
    private ARSessionOrigin arOrigin;
    private ARRaycastManager arRaycast;
    private Pose placementPose;
    private bool placementPoseIsValid = false;
    private bool onetime = true;
    public Camera ARCam;
    void Start()
    {
        arOrigin = FindObjectOfType();
        arRaycast = FindObjectOfType();
    }
    void Update()
    {
        if( onetime )
{
            UpdatePlacementPose();
            UpdatePlacementIndicator();
            if (placementPoseIsValid && Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
            {
                PlaceObject();
            }
        }
    }
    private void PlaceObject()
    {
        if (onetime == true)
        {
            GameObject obj = Instantiate(objectToPlace, placementPose.position, placementPose.rotation);
            Model.transform.localPosition = obj.transform.localPosition;
            Model.transform.localRotation = obj.transform.localRotation;
            onetime = false;
            placementIndicator.SetActive(false);
            GetComponent().enabled = false;
        }
    }
    private void UpdatePlacementIndicator()
    {
        if( onetime )
{
            if (placementPoseIsValid)
            {
                placementIndicator.SetActive(true);
                placementIndicator.transform.SetPositionAndRotation(placementPose.position, placementPose.rotation);
            }
            else
            {
                placementIndicator.SetActive(false);
            }
        }
    }
    private void UpdatePlacementPose()
    {       
        var screenCenter = ARCam.ViewportToScreenPoint(new Vector3(0.5f, 0.5f));
        var hits = new List();
       
        arRaycast.Raycast(screenCenter, hits, TrackableType.Planes);
        placementPoseIsValid = hits.Count > 0;
        if (placementPoseIsValid)
        {
            placementPose = hits[0].pose;
            var cameraForward = Camera.main.transform.forward;
            var cameraBearing = new Vector3(cameraForward.x, 0, cameraForward.z).normalized;
            placementPose.rotation = Quaternion.LookRotation(cameraBearing);           
        }
    }
}


Chromakey Shader

https://drive.google.com/file/d/14yzZTmPvo9qTKZXHo7tCFoJ4Rbmys8lu/view?usp=sharing



Content ©2022 Abolfazl Tanha All Rights Reserved​​​​​​​