Example #1
0
    /// <summary>
    /// Apply Region mask to a Scene Object
    /// </summary>
    private void ApplyQuadRegionMask(SceneQuad quad, GameObject gameobject, Color color)
    {
        if (quad == null || gameobject == null)
        {
            Debug.LogWarning("SceneUnderstandingManager.ApplyQuadRegionMask: One or more arguments are null.");
            return;
        }

        // Resolution of the mask.
        ushort width  = 256;
        ushort height = 256;

        byte[] mask = new byte[width * height];
        quad.GetSurfaceMask(width, height, mask);

        MeshRenderer meshRenderer = gameobject.GetComponent <MeshRenderer>();

        if (meshRenderer == null || meshRenderer.sharedMaterial == null || meshRenderer.sharedMaterial.HasProperty("_MainTex") == false)
        {
            Debug.LogWarning("SceneUnderstandingManager.ApplyQuadRegionMask: Mesh renderer component is null or does not have a valid material.");
            return;
        }

        // Create a new texture.
        Texture2D texture = new Texture2D(width, height);

        texture.filterMode = FilterMode.Bilinear;
        texture.wrapMode   = TextureWrapMode.Clamp;

        // Transfer the invalidation mask onto the texture.
        Color[] pixels = texture.GetPixels();
        for (int i = 0; i < pixels.Length; ++i)
        {
            byte value = mask[i];

            if (value == (byte)SceneRegionSurfaceKind.NotSurface)
            {
                pixels[i] = Color.clear;
            }
            else
            {
                pixels[i] = color;
            }
        }

        texture.SetPixels(pixels);
        texture.Apply(true);

        // Set the texture on the material.
        meshRenderer.sharedMaterial.mainTexture = texture;
    }
Example #2
0
        public bool TryGetOcclusionMask(Guid quadId, ushort textureWidth, ushort textureHeight, out byte[] mask)
        {
            Tuple <SceneQuad, SceneObject> result;

            if (!cachedSceneQuads.TryGetValue(quadId, out result))
            {
                mask = null;
                return(false);
            }

            SceneQuad   quad        = result.Item1;
            SceneObject sceneObject = result.Item2;

            var maskResult = new byte[textureWidth * textureHeight];

            quad.GetSurfaceMask(textureWidth, textureHeight, maskResult);

            mask = maskResult;

            return(true);
        }
Example #3
0
        private SpatialAwarenessSceneObject ConvertSceneObject(SceneObject sceneObject)
        {
            int quadCount = sceneObject.Quads.Count;
            int meshCount = sceneObject.Meshes.Count;

            List <SpatialAwarenessSceneObject.Quad>     quads  = new List <SpatialAwarenessSceneObject.Quad>(quadCount);
            List <SpatialAwarenessSceneObject.MeshData> meshes = new List <SpatialAwarenessSceneObject.MeshData>(meshCount);

            if (RequestPlaneData)
            {
                SceneQuad sceneQuad = null;

                for (int i = 0; i < quadCount; ++i)
                {
                    sceneQuad = sceneObject.Quads[i];

                    var quadIdKey = sceneQuad.Id;

                    byte[] occlusionMaskBytes = null;

                    if (RequestOcclusionMask)
                    {
                        occlusionMaskBytes = new byte[OcclusionMaskResolution.x * OcclusionMaskResolution.y];
                        sceneQuad.GetSurfaceMask((ushort)OcclusionMaskResolution.x, (ushort)OcclusionMaskResolution.y, occlusionMaskBytes);
                    }

                    var extents = new UnityEngine.Vector2(sceneQuad.Extents.X, sceneQuad.Extents.Y);

                    var quad = new SpatialAwarenessSceneObject.Quad(quadIdKey, extents, occlusionMaskBytes);

                    quads.Add(quad);

                    // Store a cache so we can retrieve best position on plane later.

                    if (!cachedSceneQuads.ContainsKey(quadIdKey))
                    {
                        cachedSceneQuads.Add(quadIdKey, new Tuple <SceneQuad, SceneObject>(sceneQuad, sceneObject));
                    }
                }
            }

            if (RequestMeshData)
            {
                SceneMesh sceneMesh = null;

                for (int i = 0; i < meshCount; ++i)
                {
                    sceneMesh = sceneObject.Meshes[i];
                    var meshData = MeshData(sceneMesh);
                    meshes.Add(meshData);
                }
            }

            // World space conversion

            System.Numerics.Matrix4x4 worldXformSystem = sceneObject.GetLocationAsMatrix() * sceneToWorldXformSystem * correctOrientation;

            System.Numerics.Vector3    worldTranslationSystem;
            System.Numerics.Quaternion worldRotationSytem;
            System.Numerics.Vector3    localScale;

            System.Numerics.Matrix4x4.Decompose(worldXformSystem, out localScale, out worldRotationSytem, out worldTranslationSystem);

            var result = new SpatialAwarenessSceneObject(
                sceneObject.Id,
                SpatialAwarenessSurfaceType(sceneObject.Kind),
                worldTranslationSystem.ToUnityVector3(),
                worldRotationSytem.ToUnityQuaternion(),
                quads,
                meshes);

            return(result);
        }