https://github.com/google-ar/arcore-unity-sdk/releases/tag/v1.8.0 ③Unity2017.4.15f1 or later https://unity3d.com/jp/unity/qa/lts-releases?version=2017.4 ④Android SDK https://developer.android.com/studio ※Please finish setting up Android build on Unity before hand.
be set just by assigning texture file if a material to use texture was applied to3D model. 【Idea】 Make a image which has alpha channel. Change color of opaque pixels by using script. Generate plane which is attached this image.
GameObject canvas; //Viewer of image. public RawImage preview; //Region of screen shot. UnityEngine.Rect capRect; //Texture of screen shot image. Texture2D capTexture; void Start () { int w = Screen.width; int h = Screen.height; //Definition of capture region as (0,0) to (w,h). capRect = new UnityEngine.Rect(0, 0, w, h); //Creating texture image of the size of capRect. capTexture = new Texture2D(w, h, TextureFormat.RGBA32, false); //Applying capTexture as texture of preview area. preview.material.mainTexture = capTexture; } width height (0,0)
0, 0); capTexture.Apply(); canvas.SetActive(true); } void CreateImages() { /* Cut & Paste Code of Image Creation */ } Image Creation Make CreateImages function since amount of source code for image creation will be increased later.
//画像の生成 canvas.SetActive(true); } void CreateImages() { capTexture.ReadPixels(capRect, 0, 0); capTexture.Apply(); } Do not forget to call CreateImages() from ImageProcessing()
store captured image. Texture2D capTexture; //Mat:Format of image for OpenCV //bgraMat is for color image with alpha channel. //binMat is for binarized image. Mat bgraMat, binMat; void Start () { int w = Screen.width; int h = Screen.height; //Definition of capture region as (0,0) to (w,h). capRect = new UnityEngine.Rect(0, 0, w, h); //Creating texture image of the size of capRect. capTexture = new Texture2D(w, h, TextureFormat.RGBA32, false); //Applying capTexture as texture of preview area. preview.material.mainTexture = capTexture; }
to Mat bgraMat = OpenCvSharp.Unity.TextureToMat(capTexture); //Conversion Color Image to Gray Scale Image binMat = bgraMat.CvtColor(ColorConversionCodes.BGRA2GRAY); //Binarization of image with Otsu’s method. binMat = binMat.Threshold(100, 255, ThresholdTypes.Otsu); //Conversion Gray Scale to BGRA to change its color later. bgraMat = binMat.CvtColor(ColorConversionCodes.GRAY2BGRA); } bgraMat binMat(GrayScale) binMat (Binarized) bgraMat (B=G=R)
yield return new WaitForEndOfFrame(); CreateImages();//Creating Image SetColor(capTexture);//Setting color to capTexture canvas.SetActive(true); } //Setting color information of bgraMat to texture. void SetColor(Texture2D texture) { OpenCvSharp.Unity.MatToTexture(bgraMat, texture); }
//Do nothing if Mats are null if (bgraMat == null || binMat == null) { return; } unsafe { //Get pointer of pixel array of 2 Mats. byte* bgraPtr = bgraMat.DataPointer; byte* binPtr = binMat.DataPointer; //Calculate number of pixels of the image. int pixelCount = binMat.Width * binMat.Height; //Make white pixels to transparent for (int i = 0; i < pixelCount; i++) { } } OpenCvSharp.Unity.MatToTexture(bgraMat, texture); } 後ほど処理を記述
byte* binPtr = binMat.DataPointer; int pixelCount = binMat.Width * binMat.Height; for (int i = 0; i < pixelCount; i++) { //Address of blue color of i-th pixel int bgraPos = i * 4; //If i-th pixel of binPtr is 255(white). if (binPtr[i] == 255) { bgraPtr[bgraPos + 3] = 0; } //If i-th pixel of binPtr is 0(black). else { bgraPtr[bgraPos + 3] = 255; } }
//Do nothing if Mats are null if (bgraMat == null || binMat == null) { return; } unsafe { //Get pointer of pixel array of 2 Mats. byte* bgraPtr = bgraMat.DataPointer; byte* binPtr = binMat.DataPointer; //Calculate number of pixels of the image. int pixelCount = binMat.Width * binMat.Height; //Setting transparency and changing color. for (int i = 0; i < pixelCount; i++) { } } OpenCvSharp.Unity.MatToTexture(bgraMat, texture); } Changing color of each pixel. (See next slide.)
public GameObject original; void Start() { int w = Screen.width; int h = Screen.height; capRect = new UnityEngine.Rect(0, 0, w, h); capTexture = new Texture2D(w, h, TextureFormat.RGBA32, false); preview.material.mainTexture = capTexture; } //Function to putting stamp object. public void PutObject() { } Source is shown in the next page.
public GameObject original; //List for holding generated stamps List<GameObject> stampList = new List<GameObject>(); void Start() { int w = Screen.width; int h = Screen.height; capRect = new UnityEngine.Rect(0, 0, w, h); capTexture = new Texture2D(w, h, TextureFormat.RGBA32, false); preview.material.mainTexture = capTexture; }