Advertisement
Guest User

ARCoreFeaturePointColors

a guest
Apr 21st, 2018
1,032
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.62 KB | None | 0 0
  1. using System;
  2. using GoogleARCore;
  3. using GoogleARCore.TextureReader;
  4. using UnityEngine;
  5.  
  6. [RequireComponent(typeof(TextureReader))]
  7. public class FeaturePointColors : MonoBehaviour
  8. {
  9.     // Scale output image dimensions for performance
  10.     const int k_DimensionsInverseScale = 2;
  11.  
  12.     public GameObject cubePrefab;
  13.     public int poolSize;
  14.  
  15.     byte[] m_PixelByteBuffer = new byte[0];
  16.     int m_PixelBufferSize;
  17.     Material[] m_PixelMaterials;
  18.     GameObject[] m_PixelObjects;
  19.     Color[] m_PixelColors;
  20.  
  21.     void Awake()
  22.     {
  23.         if (cubePrefab.GetComponent<Renderer>() == null)
  24.         {
  25.             Debug.LogError("No renderer on pixel prefab!");
  26.             enabled = false;
  27.             return;
  28.         }
  29.  
  30.         var textureReader = GetComponent<TextureReader>();
  31.         textureReader.ImageFormat = TextureReaderApi.ImageFormatType.ImageFormatColor;
  32.         textureReader.OnImageAvailableCallback += OnImageAvailable;
  33.  
  34.         var landscape = ScreenIsLandscape();
  35.         var scaledScreenWidth = Screen.width / k_DimensionsInverseScale;
  36.         var scaledScreenHeight = Screen.height / k_DimensionsInverseScale;
  37.  
  38.         // It's arbitrary whether the output image should be portrait or landscape, as long as
  39.         // you know how to interpret it for each potential screen orientation.
  40.         textureReader.ImageWidth = landscape ? scaledScreenWidth : scaledScreenHeight;
  41.         textureReader.ImageHeight = landscape ? scaledScreenHeight : scaledScreenWidth;
  42.  
  43.         m_PixelObjects = new GameObject[poolSize];
  44.         m_PixelMaterials = new Material[poolSize];
  45.         for (var i = 0; i < poolSize; ++i)
  46.         {
  47.             var pixelObj = Instantiate(cubePrefab, transform);
  48.             m_PixelObjects[i] = pixelObj;
  49.             m_PixelMaterials[i] = pixelObj.GetComponent<Renderer>().material;
  50.             pixelObj.SetActive(false);
  51.         }
  52.     }
  53.  
  54.     static bool ScreenIsLandscape()
  55.     {
  56.         return Screen.orientation == ScreenOrientation.LandscapeLeft || Screen.orientation == ScreenOrientation.LandscapeRight;
  57.     }
  58.  
  59.     void OnImageAvailable(TextureReaderApi.ImageFormatType format, int width, int height, IntPtr pixelBuffer, int bufferSize)
  60.     {
  61.         if (format != TextureReaderApi.ImageFormatType.ImageFormatColor)
  62.             return;
  63.  
  64.         // Adjust buffer size if necessary.
  65.         if (bufferSize != m_PixelBufferSize || m_PixelByteBuffer.Length == 0)
  66.         {
  67.             m_PixelBufferSize = bufferSize;
  68.             m_PixelByteBuffer = new byte[bufferSize];
  69.             m_PixelColors = new Color[width * height];
  70.         }
  71.  
  72.         // Move raw data into managed buffer.
  73.         System.Runtime.InteropServices.Marshal.Copy(pixelBuffer, m_PixelByteBuffer, 0, bufferSize);
  74.  
  75.         // Interpret pixel buffer differently depending on which orientation the device is.
  76.         // We need to get pixel colors into a friendly format - an array
  77.         // laid out row by row from bottom to top, and left to right within each row.
  78.         var bufferIndex = 0;
  79.         for (var y = 0; y < height; ++y)
  80.         {
  81.             for (var x = 0; x < width; ++x)
  82.             {
  83.                 int r = m_PixelByteBuffer[bufferIndex++];
  84.                 int g = m_PixelByteBuffer[bufferIndex++];
  85.                 int b = m_PixelByteBuffer[bufferIndex++];
  86.                 int a = m_PixelByteBuffer[bufferIndex++];
  87.                 var color = new Color(r / 255f, g / 255f, b / 255f, a / 255f);
  88.                 int pixelIndex;
  89.                 switch (Screen.orientation)
  90.                 {
  91.                     case ScreenOrientation.LandscapeRight:
  92.                         pixelIndex = y * width + width - 1 - x;
  93.                         break;
  94.                     case ScreenOrientation.Portrait:
  95.                         pixelIndex = (width - 1 - x) * height + height - 1 - y;
  96.                         break;
  97.                     case ScreenOrientation.LandscapeLeft:
  98.                         pixelIndex = (height - 1 - y) * width + x;
  99.                         break;
  100.                     default:
  101.                         pixelIndex = x * height + y;
  102.                         break;
  103.                 }
  104.                 m_PixelColors[pixelIndex] = color;
  105.             }
  106.         }
  107.  
  108.         FeaturePointCubes();
  109.     }
  110.  
  111.     void FeaturePointCubes()
  112.     {
  113.         foreach (var pixelObj in m_PixelObjects)
  114.         {
  115.             pixelObj.SetActive(false);
  116.         }
  117.  
  118.         var index = 0;
  119.         var pointsInViewCount = 0;
  120.         var camera = Camera.main;
  121.         var scaledScreenWidth = Screen.width / k_DimensionsInverseScale;
  122.         while (index < Frame.PointCloud.PointCount && pointsInViewCount < poolSize)
  123.         {
  124.             // If a feature point is visible, use its screen space position to get the correct color for its cube
  125.             // from our friendly-formatted array of pixel colors.
  126.             var point = Frame.PointCloud.GetPoint(index);
  127.             var screenPoint = camera.WorldToScreenPoint(point);
  128.             if (screenPoint.x >= 0 && screenPoint.x < camera.pixelWidth &&
  129.                 screenPoint.y >= 0 && screenPoint.y < camera.pixelHeight)
  130.             {
  131.                 var pixelObj = m_PixelObjects[pointsInViewCount];
  132.                 pixelObj.SetActive(true);
  133.                 pixelObj.transform.position = point;
  134.                 var scaledX = (int)screenPoint.x / k_DimensionsInverseScale;
  135.                 var scaledY = (int)screenPoint.y / k_DimensionsInverseScale;
  136.                 m_PixelMaterials[pointsInViewCount].color = m_PixelColors[scaledY * scaledScreenWidth + scaledX];
  137.                 pointsInViewCount++;
  138.             }
  139.             index++;
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement