当前位置:   article > 正文

Unity EasyAR3.0 案例解析(一) ------ HelloAR_Coloring3D(涂涂乐)

unity ar涂涂乐

EasyAR3.0发布有一段时间了,但是官方群里面出现了各种问题,因此我也没有第一时间去体验,不过非常期待4.0的到来,不需要ARCore和ARKit就能实现平面识别,还有各种炫酷的功能,太感兴趣了,哈哈,期待他能将我使用的Vuforia替换下来,下载官方案例后感觉很惊喜,以前下载要下载好久,几百兆,一个个的unity工程案例,标识很不喜欢这样子的示例,现在下载是一个unitypackage包,导入新建的工程里面,结构也很清晰明了,我不记得之前案例是否能在编辑器里面运行观看效果,现在可以感觉还是很好的
说了一大堆废话哈哈,好啦,开始正题

一.环境

案例下载网址 : https://www.easyar.cn/view/download.html
官方文档网址 : https://www.easyar.cn/doc/EasyAR%20SDK/EasyAR%20SDK.htmlhttps://docs.unity3d.com/Packages/com.unity.xr.arfoundation@2.2/api/UnityEngine.XR.ARFoundation.html
Unity版本 : 2018.4

顺便提一下,填写key的位置如下图所示 :


17787668-4d75785e38dafc14.png

二.解析

1.运行效果

没有改变bear图片颜色之前 :


17787668-7f94116160b64320.png

改变bear图片颜色之后 :


17787668-3053e57277ac45e7.png

2.项目结构

17787668-ce7341bc88465b53.png

这里只指出比较重要的脚本
Main Camera上挂载CameraImageRenderer脚本 :
渲染摄像机画面
ImageTarget上挂载ImageTargetController脚本 :
根据配置加载识别图生成target
bear上挂载Coloring3D脚本 :
设置摄像机渲染到RenderTexture,实时贴renderTexture

3.代码分析

原理 : 将摄像机的TargetTexture设置成RenderTexture,再将此RenderTexture设置模型行材质的贴图,位置设置我就有点看不太懂了~~~
主要代码Coloring3D如下,做了简单的注释 :

  1. //================================================================================================================================
  2. //
  3. // Copyright (c) 2015-2019 VisionStar Information Technology (Shanghai) Co., Ltd. All Rights Reserved.
  4. // EasyAR is the registered trademark or trademark of VisionStar Information Technology (Shanghai) Co., Ltd in China
  5. // and other countries for the augmented reality technology developed by VisionStar Information Technology (Shanghai) Co., Ltd.
  6. //
  7. //================================================================================================================================
  8. using UnityEngine;
  9. using easyar;
  10. public class Coloring3D : MonoBehaviour
  11. {
  12. public Camera Camera = null;
  13. public ImageTargetController ImageTarget = null;
  14. public RenderTexture renderTexture = null;
  15. private Texture2D staticTexture = null;
  16. private Material material = null;
  17. private bool useStaticTex = false;
  18. private void Start()
  19. {
  20. //设置Camera的TargetTexture为renderTexture
  21. if (Camera != null)
  22. {
  23. var cameraRender = Camera.GetComponent<CameraImageRenderer>();
  24. if (cameraRender != null)
  25. {
  26. renderTexture = cameraRender.TargetTexture;
  27. }
  28. }
  29. //获取模型上的材质
  30. var renderer = GetComponent<MeshRenderer>();
  31. if (renderer != null)
  32. {
  33. material = renderer.material;
  34. }
  35. }
  36. private void Update()
  37. {
  38. //如果使用静态的,就不用实时获取修改renderTexture
  39. if (useStaticTex)
  40. {
  41. return;
  42. }
  43. //修改位置及_MainTex
  44. var halfWidth = ImageTarget.TargetWidth * 0.5f;
  45. var halfHeight = ImageTarget.TargetHeight * 0.5f;
  46. Vector3 targetAnglePoint1 = ImageTarget.transform.TransformPoint(new Vector3(-halfWidth, halfHeight, 0));
  47. Vector3 targetAnglePoint2 = ImageTarget.transform.TransformPoint(new Vector3(-halfWidth, -halfHeight, 0));
  48. Vector3 targetAnglePoint3 = ImageTarget.transform.TransformPoint(new Vector3(halfWidth, halfHeight, 0));
  49. Vector3 targetAnglePoint4 = ImageTarget.transform.TransformPoint(new Vector3(halfWidth, -halfHeight, 0));
  50. material.SetVector("_Uvpoint1", new Vector4(targetAnglePoint1.x, targetAnglePoint1.y, targetAnglePoint1.z, 1f));
  51. material.SetVector("_Uvpoint2", new Vector4(targetAnglePoint2.x, targetAnglePoint2.y, targetAnglePoint2.z, 1f));
  52. material.SetVector("_Uvpoint3", new Vector4(targetAnglePoint3.x, targetAnglePoint3.y, targetAnglePoint3.z, 1f));
  53. material.SetVector("_Uvpoint4", new Vector4(targetAnglePoint4.x, targetAnglePoint4.y, targetAnglePoint4.z, 1f));
  54. material.SetTexture("_MainTex", renderTexture);
  55. }
  56. //RenderTexture转Texture2D
  57. public Texture2D getBackgroundPic()
  58. {
  59. RenderTexture.active = renderTexture;
  60. var tempTexture = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.RGB24, false);
  61. tempTexture.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
  62. RenderTexture.active = null;
  63. tempTexture.Apply();
  64. return tempTexture;
  65. }
  66. //静态贴图
  67. public void SetStaticPic()
  68. {
  69. Destroy(staticTexture);
  70. useStaticTex = true;
  71. RenderTexture.active = renderTexture;
  72. staticTexture = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.RGB24, false);
  73. staticTexture.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
  74. RenderTexture.active = null;
  75. staticTexture.Apply();
  76. material.SetTexture("_MainTex", staticTexture);
  77. }
  78. //动态贴图
  79. public void SetDynamicPic()
  80. {
  81. Destroy(staticTexture);
  82. useStaticTex = false;
  83. }
  84. }
17787668-4eca4da34eb71a37.png
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/118075
推荐阅读
相关标签
  

闽ICP备14008679号