Exemplo n.º 1
0
            /// <summary>
            /// Sets the current descriptor, and creates/updates the associated texture as appropriate.
            /// </summary>
            /// <param name="textureInfo">The texture info to update.</param>
            /// <param name="descriptor">The texture descriptor wrapping a native texture object.</param>
            public static TextureInfo GetUpdatedTextureInfo(TextureInfo textureInfo, XRTextureDescriptor descriptor)
            {
                // If the current and given descriptors are equal, exit early from this method.
                if (textureInfo.m_Descriptor.Equals(descriptor))
                {
                    return(textureInfo);
                }

                // If there is a texture already and if the descriptors have identical texture metadata, we only need
                // to update the existing texture with the given native texture object.
                if ((textureInfo.m_Texture != null) && textureInfo.m_Descriptor.hasIdenticalTextureMetadata(descriptor))
                {
                    // Update the current descriptor with the given descriptor.
                    textureInfo.m_Descriptor = descriptor;

                    // Update the current texture with the native texture object.
                    textureInfo.m_Texture.UpdateExternalTexture(textureInfo.m_Descriptor.nativeTexture);
                }
                // Else, we need to create a new texture object.
                else
                {
                    // Update the current descriptor with the given descriptor.
                    textureInfo.m_Descriptor = descriptor;

                    // Replace the current texture with a newly created texture, and update the material.
                    textureInfo.DestroyTexture();
                    textureInfo.m_Texture = CreateTexture(textureInfo.m_Descriptor);
                }

                return(textureInfo);
            }
Exemplo n.º 2
0
            /// <summary>
            /// Create the texture object for the native texture wrapped by the valid descriptor.
            /// </summary>
            /// <param name="descriptor">The texture descriptor wrapping a native texture object.</param>
            /// <returns>
            /// If the descriptor is valid, the <c>Texture2D</c> object created from the texture descriptor. Otherwise,
            /// <c>null</c>.
            /// </returns>
            static Texture2D CreateTexture(XRTextureDescriptor descriptor)
            {
                if (!descriptor.valid)
                {
                    return(null);
                }

                Texture2D texture = Texture2D.CreateExternalTexture(descriptor.width, descriptor.height,
                                                                    descriptor.format, (descriptor.mipmapCount != 0),
                                                                    k_TextureHasLinearColorSpace,
                                                                    descriptor.nativeTexture);

                // NB: SetWrapMode needs to be the first call here, and the value passed
                //     needs to be kTexWrapClamp - this is due to limitations of what
                //     wrap modes are allowed for external textures in OpenGL (which are
                //     used for ARCore), as Texture::ApplySettings will eventually hit
                //     an assert about an invalid enum (see calls to glTexParameteri
                //     towards the top of ApiGLES::TextureSampler)
                // reference: "3.7.14 External Textures" section of
                // https://www.khronos.org/registry/OpenGL/extensions/OES/OES_EGL_image_external.txt
                // (it shouldn't ever matter what the wrap mode is set to normally, since
                // this is for a pass-through video texture, so we shouldn't ever need to
                // worry about the wrap mode as textures should never "wrap")
                texture.wrapMode   = TextureWrapMode.Clamp;
                texture.filterMode = FilterMode.Bilinear;
                texture.hideFlags  = HideFlags.HideAndDontSave;

                return(texture);
            }
            public override NativeArray <XRTextureDescriptor> GetTextureDescriptors(XRTextureDescriptor defaultDescriptor, Allocator allocator)
            {
                var remote = ARKitStreamReceiver.Instance;

                if (remote == null)
                {
                    return(new NativeArray <XRTextureDescriptor>(0, allocator));
                }

                var yTex    = remote.YTextrue;
                var cbcrTex = remote.CbCrTexture;

                if (yTex == null || cbcrTex == null)
                {
                    return(new NativeArray <XRTextureDescriptor>(0, allocator));
                }


                var arr = new NativeArray <XRTextureDescriptor>(2, allocator);

                arr[0] = new TextureDescriptor(yTex, _TEXTURE_Y);
                arr[1] = new TextureDescriptor(cbcrTex, _TEXTURE_CB_CR);

                return(arr);
            }
Exemplo n.º 4
0
            public unsafe XRTextureDescriptor Convert()
            {
                var result = new XRTextureDescriptor();

                Convert(out result);
                return(result);
            }
        /// <summary>
        /// Sets the current descriptor, and creates/updates the associated texture as appropriate.
        /// </summary>
        /// <param name="textureInfo">The texture info to update.</param>
        /// <param name="descriptor">The texture descriptor wrapping a native texture object.</param>
        /// <returns>
        /// The updated texture information.
        /// </returns>
        public static ARTextureInfo GetUpdatedTextureInfo(ARTextureInfo textureInfo, XRTextureDescriptor descriptor)
        {
            // If the current and given descriptors are equal, exit early from this method.
            if (textureInfo.m_Descriptor.Equals(descriptor))
            {
                return(textureInfo);
            }

            // If the given descriptor is invalid, destroy any existing texture, and return the default texture
            // info.
            if (!descriptor.valid)
            {
                textureInfo.DestroyTexture();
                return(default(ARTextureInfo));
            }

            DebugWarn.WhenFalse(textureInfo.m_Descriptor.dimension == TextureDimension.None || textureInfo.m_Descriptor.dimension == descriptor.dimension)?.
            WithMessage($"Texture descriptor dimension should not change from {textureInfo.m_Descriptor.dimension} to {descriptor.dimension}.");

            // If there is a texture already and if the descriptors have identical texture metadata, we only need
            // to update the existing texture with the given native texture object.
            if ((textureInfo.m_Texture != null) && textureInfo.m_Descriptor.hasIdenticalTextureMetadata(descriptor))
            {
                // Update the current descriptor with the given descriptor.
                textureInfo.m_Descriptor = descriptor;

                // Update the current texture with the native texture object.
                switch (descriptor.dimension)
                {
#if UNITY_2020_2_OR_NEWER
                case TextureDimension.Tex3D:
                    ((Texture3D)textureInfo.m_Texture).UpdateExternalTexture(textureInfo.m_Descriptor.nativeTexture);
                    break;
#endif
                case TextureDimension.Tex2D:
                    ((Texture2D)textureInfo.m_Texture).UpdateExternalTexture(textureInfo.m_Descriptor.nativeTexture);
                    break;

                case TextureDimension.Cube:
                    ((Cubemap)textureInfo.m_Texture).UpdateExternalTexture(textureInfo.m_Descriptor.nativeTexture);
                    break;

                default:
                    throw new NotSupportedException($"'{descriptor.dimension.ToString()}' is not a supported texture type.");
                }
            }
            // Else, we need to destroy the existing texture object and create a new texture object.
            else
            {
                // Update the current descriptor with the given descriptor.
                textureInfo.m_Descriptor = descriptor;

                // Replace the current texture with a newly created texture, and update the material.
                textureInfo.DestroyTexture();
                textureInfo.m_Texture = CreateTexture(textureInfo.m_Descriptor);
            }

            return(textureInfo);
        }
Exemplo n.º 6
0
 public unsafe void Convert(out XRTextureDescriptor target)
 {
     fixed(XRTextureDescriptor *targetPtr = &target)
     fixed(XRTextureDescriptorMock * selfPtr = &this)
     {
         UnsafeUtility.MemCpy(targetPtr, selfPtr, sizeof(XRTextureDescriptor));
     }
 }
Exemplo n.º 7
0
            public override bool TryGetHumanStencil(out XRTextureDescriptor humanStencilDescriptor)
            {
                if (OcclusionApi.TryGetHumanStencil(out var t))
                {
                    GetTextureDescriptor(t).Convert(out humanStencilDescriptor);
                    return(true);
                }

                humanStencilDescriptor = new XRTextureDescriptor();
                return(false);
            }
Exemplo n.º 8
0
            public override bool TryGetEnvironmentDepthConfidence(out XRTextureDescriptor environmentDepthConfidenceDescriptor)
            {
                if (OcclusionApi.TryGetEnvironmentDepthConfidence(out var t))
                {
                    GetTextureDescriptor(t).Convert(out environmentDepthConfidenceDescriptor);
                    return(true);
                }

                environmentDepthConfidenceDescriptor = new XRTextureDescriptor();
                return(false);
            }
Exemplo n.º 9
0
 public override NativeArray <XRTextureDescriptor> GetTextureDescriptors(XRTextureDescriptor defaultDescriptor, Allocator allocator)
 {
     //var currentDescriptors = OcclusionApi.GetTextures();
     //if (previousDescriptors != currentDescriptors)
     //{
     //    descriptors = currentDescriptors.Select(m => GetTextureDescriptor(m).Convert()).ToArray();
     //    previousDescriptors = currentDescriptors;
     //}
     //
     //return new NativeArray<XRTextureDescriptor>(descriptors, allocator);
     return(base.GetTextureDescriptors(defaultDescriptor, allocator));
 }
Exemplo n.º 10
0
        /// <summary>
        /// Create a new <c>Cubemap</c> texture object with the given native texture object.
        /// </summary>
        /// <param name="textureDescriptor">The <c>XRTextureDescriptor</c> wrapping a native texture object.
        /// </param>
        /// <returns>
        /// The <c>Cubemap</c> object created from the given native texture object.
        /// </returns>
        Cubemap CreateEnvironmentTexture(XRTextureDescriptor textureDescriptor)
        {
            Debug.Assert(textureDescriptor.valid,
                         "cannot create a cubemap with an invalid native texture object");

            Cubemap cubemap = Cubemap.CreateExternalTexture(textureDescriptor.width,
                                                            textureDescriptor.format,
                                                            (textureDescriptor.mipmapCount != 0),
                                                            textureDescriptor.nativeTexture);

            cubemap.filterMode = m_EnvironmentTextureFilterMode;

            return(cubemap);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Applies the texture data in the <c>XRTextureDescriptor</c> to the reflection probe settings.
        /// </summary>
        /// <param name="textureDescriptor">The environment texture data to apply to the reflection probe baked
        /// texture.</param>
        void UpdateEnvironmentTexture(XRTextureDescriptor textureDescriptor)
        {
            // If the current environment texture equals the given environment texture, the texture does not need to be
            // updated.
            if (m_CurrentTextureDescriptor.Equals(textureDescriptor))
            {
                return;
            }

            // Get the current baked texture as a cubemap, if any.
            Cubemap cubemapTexture = m_ReflectionProbe.customBakedTexture as Cubemap;

#if UNITY_2019_1_OR_NEWER
            const bool k_NoCubemapUpdate = false;
#else
            const bool k_NoCubemapUpdate = true;
#endif

            // If there is no current reflection probe texture or if the current environment texture data is not
            // identical to the given environment texture metadata, then we need to create a new environment texture
            // object.
            if (k_NoCubemapUpdate || (cubemapTexture == null) ||
                !m_CurrentTextureDescriptor.hasIdenticalTextureMetadata(textureDescriptor))
            {
                // Destroy any previous texture object.
                if (m_ReflectionProbe.customBakedTexture != null)
                {
                    Object.Destroy(m_ReflectionProbe.customBakedTexture);
                }

                // Create a new environment texture object.
                m_ReflectionProbe.customBakedTexture = CreateEnvironmentTexture(textureDescriptor);
            }
#if UNITY_2019_1_OR_NEWER
            else
            {
                // Else, we have a current texture object with identical metadata, we simply update the external
                // texture with the native texture.
                cubemapTexture.UpdateExternalTexture(textureDescriptor.nativeTexture);
            }
#endif

            // Update the current environment texture metadata.
            m_CurrentTextureDescriptor = textureDescriptor;
        }
 public static bool IsSupported(XRTextureDescriptor descriptor)
 {
     if (descriptor.dimension == TextureDimension.Tex3D)
     {
         return(true);
     }
     else if (descriptor.dimension == TextureDimension.Tex2D)
     {
         return(true);
     }
     else if (descriptor.dimension == TextureDimension.Cube)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
            public override bool TryGetHumanDepth(out XRTextureDescriptor humanDepthDescriptor)
            {
                if (ARKitStreamReceiver.Instance == null)
                {
                    humanDepthDescriptor = default(XRTextureDescriptor);
                    return(false);
                }

                var tex = ARKitStreamReceiver.Instance.DepthTexture;

                if (tex == null)
                {
                    humanDepthDescriptor = default(XRTextureDescriptor);
                    return(false);
                }

                humanDepthDescriptor = new TextureDescriptor(tex, 0);
                return(true);
            }
Exemplo n.º 14
0
            /// <summary>
            /// Gets the texture descriptors associated with th current camera
            /// frame.
            /// </summary>
            /// <returns>The texture descriptors.</returns>
            /// <param name="defaultDescriptor">Default descriptor.</param>
            /// <param name="allocator">Allocator.</param>
            public unsafe override NativeArray <XRTextureDescriptor> GetTextureDescriptors(
                XRTextureDescriptor defaultDescriptor,
                Allocator allocator)
            {
                int length, elementSize;
                var textureDescriptors = NativeApi.UnityARKit_Camera_AcquireTextureDescriptors(
                    out length, out elementSize);

                try
                {
                    return(NativeCopyUtility.PtrToNativeArrayWithDefault(
                               defaultDescriptor,
                               textureDescriptors, elementSize, length, allocator));
                }
                finally
                {
                    NativeApi.UnityARKit_Camera_ReleaseTextureDescriptors(textureDescriptors);
                }
            }
Exemplo n.º 15
0
        public static void ImplicitTest()
        {
            TextureDescriptor d1 = new TextureDescriptor()
            {
                nativeTexture  = new IntPtr(99),
                width          = 10,
                height         = 20,
                mipmapCount    = 1,
                format         = TextureFormat.R8,
                propertyNameId = 30
            };

            XRTextureDescriptor xr = d1;

            Debug.Log(xr);

            TextureDescriptor d2 = xr;

            Assert.AreEqual(d1, d2);
        }
            public override bool TryGetHumanStencil(out XRTextureDescriptor humanStencilDescriptor)
            {
                var recevier = ARKitReceiver.Instance;

                if (recevier == null)
                {
                    humanStencilDescriptor = default(XRTextureDescriptor);
                    return(false);
                }

                var tex = recevier.StencilTexture;

                if (tex == null)
                {
                    humanStencilDescriptor = default(XRTextureDescriptor);
                    return(false);
                }

                humanStencilDescriptor = new TextureDescriptor(tex, 0);
                return(true);
            }
Exemplo n.º 17
0
        public static bool IsSupported(XRTextureDescriptor descriptor)
        {
            if (descriptor.dimension == TextureDimension.Tex3D)
            {
#if UNITY_2020_2_OR_NEWER
                return(true);
#else
                return(false);
#endif
            }
            else if (descriptor.dimension == TextureDimension.Tex2D)
            {
                return(true);
            }
            else if (descriptor.dimension == TextureDimension.Cube)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 18
0
 /// <summary>
 /// Get the human depth texture descriptor.
 /// </summary>
 /// <param name="humanDepthDescriptor">The human depth texture descriptor to be populated, if available
 /// </param>
 /// <returns>
 /// <c>true</c> if the human depth texture descriptor is available and is returned. Otherwise,
 /// <c>false</c>.
 /// </returns>
 public override bool TryGetHumanDepth(out XRTextureDescriptor humanDepthDescriptor)
 {
     return(NativeApi.UnityARKit_HumanBodyProvider_TryGetHumanDepth(out humanDepthDescriptor));
 }
Exemplo n.º 19
0
 public static unsafe extern bool UnityARKit_HumanBodyProvider_TryGetHumanDepth(out XRTextureDescriptor humanDepthDescriptor);
Exemplo n.º 20
0
 /// <summary>
 /// Constructs the texture info with the given descriptor and material.
 /// </summary>
 /// <param name="descriptor">The texture descriptor wrapping a native texture object.</param>
 public TextureInfo(XRTextureDescriptor descriptor)
 {
     m_Descriptor = descriptor;
     m_Texture    = CreateTexture(m_Descriptor);
 }