ARKit作画

业务联系  QQ:2118590660 ARKit项目外包  AR项目外包

ARKit入门到精通系列 (视频教程地址)

B站地址: https://space.bilibili.com/103407808/#/

 Youtube地址:https://www.youtube.com/channel/UCmDHmTjKHXA5FGh-OIHUlpw

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Line : MonoBehaviour {

	private LineRenderer lineRenderer;

	private List points;
	// Use this for initialization
	void Start () {
		lineRenderer = this.GetComponent ();
		points = new List ();
	}

	public void AddPoint(Vector3 newPoint)
	{
		points.Add (newPoint);
		UpdateLineRendererPoints ();
	}

	public void UpdateLineRendererPoints()
	{
		lineRenderer.positionCount = points.Count;
		lineRenderer.SetPositions (points.ToArray ());
	}

	public void SetColor(Color color)
	{
		Material mat = new Material (lineRenderer.material);
		mat.SetColor ("_Color", color);
		lineRenderer.material = mat;
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.iOS;


public class DrawingManager : MonoBehaviour {

	private Vector3 previousPos;
	private Line _currentLine;
	private bool isDrawing;
	public GameObject lineObj;

	void OnEnable()
	{
		UnityARSessionNativeInterface.ARFrameUpdatedEvent += MyARFrameUpdate;
	}


	void MyARFrameUpdate(UnityARCamera camera)
	{
		Vector3 currentPos = GetCamPos (camera) + (Camera.main.transform.forward * 0.2f);
		if (Vector3.Distance (currentPos, previousPos) > 0.01f) {
			//刷新
			if (isDrawing) {
				_currentLine.AddPoint (currentPos);
			}

			previousPos = currentPos;
		}
	}


	private Vector3 GetCamPos(UnityARCamera camera)
	{
		Matrix4x4 matrix = new Matrix4x4 ();
		matrix.SetColumn (3, camera.worldTransform.column3);
		return UnityARMatrixOps.GetPosition (matrix);
	}


	void OnGUI()
	{
		if (GUI.Button (new Rect (100, 200, 200, 50), "开始画")) {
			_currentLine = Instantiate (lineObj, this.transform).GetComponent ();
			isDrawing = true;
		}

		if (GUI.Button (new Rect (100, 400, 200, 50), "停止")) {
			isDrawing = false;
		}

		if (GUI.Button (new Rect (100, 600, 200, 50), "改色")) {
			_currentLine.SetColor(new Color(Random.Range(0f,1f),Random.Range(0f,1f),Random.Range(0f,1f)));
		}
	}

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}


	void OnDisable()
	{
		UnityARSessionNativeInterface.ARFrameUpdatedEvent -= MyARFrameUpdate;
	}
}


ARKit作画_第1张图片

 

 

 

 

将Line 这个预制件挂在 LineObj上

 

你可能感兴趣的:(ARKit)