Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using UnityEngine;
- using UnityEngine.Rendering;
- public class CaptureDevice : MonoBehaviour
- {
- [SerializeField] WebCamDevice[] devices;
- WebCamTexture webcamTexture;
- new Renderer renderer;
- [SerializeField] Light screenLight;
- RenderTexture _rendTex;
- void Start()
- {
- devices = WebCamTexture.devices;
- for (int i = 0; i < devices.Length; i++)
- Debug.Log(devices[i].name);
- InitializeCamera();
- }
- private void InitializeCamera()
- {
- webcamTexture = new WebCamTexture();
- renderer = GetComponent<Renderer>();
- if (devices.Length > 0)
- {
- webcamTexture.deviceName = devices[0].name;
- renderer.material.mainTexture = webcamTexture;
- webcamTexture.Play();
- _rendTex = new RenderTexture(webcamTexture.width, webcamTexture.height, 0);
- _rendTex.useMipMap = true;
- _rendTex.autoGenerateMips = true;
- }
- }
- private void FixedUpdate()
- {
- GetAveragebyMipmap();
- }
- private void GetAveragebyMipmap()
- {
- Graphics.Blit(webcamTexture, _rendTex);
- var asyncAction = AsyncGPUReadback.Request(_rendTex, _rendTex.mipmapCount - 1);
- asyncAction.WaitForCompletion();
- // Extract average color
- Color32 Average = asyncAction.GetData<Color32>()[0];
- screenLight.color = Average;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement