private void updateHWARBackground() { #if UNITY_ANDROID // InsightARState arState = (InsightARState)trackResult.state; // if (arState < InsightARState.Init_OK || arState >= InsightARState.Track_Stop) { // return; // } InsightARTextureHandles handles = InsightARNative.iarGetVideoTextureHandles(); if (handles.textureY == System.IntPtr.Zero) { return; } if (!mARCameraConfiged) { configARCamera(); } Resolution currentResolution = Screen.currentResolution; if (_videoTextureRGBA == null || _videoTextureRGBA.GetNativeTexturePtr().ToInt32() != handles.textureY.ToInt32()) { _videoTextureRGBA = Texture2D.CreateExternalTexture(currentResolution.width, currentResolution.height, TextureFormat.RGBA32, false, false, (System.IntPtr)handles.textureY); _videoTextureRGBA.filterMode = FilterMode.Bilinear; _videoTextureRGBA.wrapMode = TextureWrapMode.Repeat; } _videoTextureRGBA.UpdateExternalTexture(handles.textureY); m_BackgroundMaterial.SetTexture("_MainTex", _videoTextureRGBA); const string topLeft = "_UvTopLeftRight"; const string botLeft = "_UvBottomLeftRight"; InsightARNative.getHWDisplayUVCoords(uvcoords); m_BackgroundMaterial.SetVector(topLeft, new Vector4(uvcoords [0], uvcoords [1], uvcoords [4], uvcoords [5])); m_BackgroundMaterial.SetVector(botLeft, new Vector4(uvcoords [2], uvcoords [3], uvcoords [6], uvcoords [7])); #endif }
private void updateInsightARBackground() { if (CurrentState < InsightARState.Init_OK || CurrentState >= InsightARState.Track_Stop) { return; } #if UNITY_ANDROID || UNITY_IOS #if UNITY_ANDROID GL.IssuePluginEvent(InsightARNative.iarUpdateCameraTexture(), 1); #endif InsightARTextureHandles handles = InsightARNative.iarGetVideoTextureHandles(); // Debug.Log ("-ar- updateInsightARBackground:handles :" + handles.textureY); if (handles.textureY == System.IntPtr.Zero) { return; } #if UNITY_IOS if (handles.textureCbCr == System.IntPtr.Zero) { return; } #endif if (!mARCameraConfiged) { configARCamera(); } Resolution currentResolution = Screen.currentResolution; #if UNITY_IOS // Texture Y _videoTextureY = Texture2D.CreateExternalTexture(currentResolution.width, currentResolution.height, TextureFormat.R8, false, false, (System.IntPtr)handles.textureY); _videoTextureY.filterMode = FilterMode.Bilinear; _videoTextureY.wrapMode = TextureWrapMode.Repeat; _videoTextureY.UpdateExternalTexture(handles.textureY); // Texture CbCr _videoTextureCbCr = Texture2D.CreateExternalTexture(currentResolution.width, currentResolution.height, TextureFormat.RG16, false, false, (System.IntPtr)handles.textureCbCr); _videoTextureCbCr.filterMode = FilterMode.Bilinear; _videoTextureCbCr.wrapMode = TextureWrapMode.Repeat; _videoTextureCbCr.UpdateExternalTexture(handles.textureCbCr); m_BackgroundMaterial.SetTexture("_textureY", _videoTextureY); m_BackgroundMaterial.SetTexture("_textureCbCr", _videoTextureCbCr); #elif UNITY_ANDROID if (_videoTextureRGBA == null || _videoTextureRGBA.GetNativeTexturePtr().ToInt32() != handles.textureY.ToInt32()) { _videoTextureRGBA = Texture2D.CreateExternalTexture(currentResolution.width, currentResolution.height, TextureFormat.RGBA32, false, false, (System.IntPtr)handles.textureY); _videoTextureRGBA.filterMode = FilterMode.Bilinear; _videoTextureRGBA.wrapMode = TextureWrapMode.Repeat; m_BackgroundMaterial.SetTexture("_MainTex", _videoTextureRGBA); } _videoTextureRGBA.UpdateExternalTexture(handles.textureY); #endif int isPortrait = 0; float rotation = 0; if (Screen.orientation == ScreenOrientation.Portrait) { rotation = -90; isPortrait = 1; } else if (Screen.orientation == ScreenOrientation.PortraitUpsideDown) { rotation = 90; isPortrait = 1; } else if (Screen.orientation == ScreenOrientation.LandscapeRight || Screen.orientation == ScreenOrientation.LandscapeLeft) { isPortrait = 0; } Matrix4x4 m = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0.0f, 0.0f, rotation), Vector3.one); m_BackgroundMaterial.SetMatrix("_TextureRotation", m); float imageAspect = (float)trackResult.param.width / (float)trackResult.param.height; float screenAspect = (float)currentResolution.width / (float)currentResolution.height; float ratio = screenAspect > 1 ? imageAspect / screenAspect : imageAspect * screenAspect; float s_ShaderScaleX = 1.0f; float s_ShaderScaleY = 1.0f; if (isPortrait == 1) { if (ratio < 1) { s_ShaderScaleX = ratio; } else if (ratio > 1) { s_ShaderScaleY = 1f / ratio; } } else if (isPortrait == 0) { if (ratio < 1f) { s_ShaderScaleY = ratio; } else if (ratio > 1f) { s_ShaderScaleX = 1f / ratio; } } m_BackgroundMaterial.SetFloat("_texCoordScaleX", s_ShaderScaleX); m_BackgroundMaterial.SetFloat("_texCoordScaleY", s_ShaderScaleY); m_BackgroundMaterial.SetInt("_isPortrait", isPortrait); #endif }