MaxstARSDK  3.5.0
AbstractImageTrackableBehaviour.cs
1 /*==============================================================================
2 Copyright 2017 Maxst, Inc. All Rights Reserved.
3 ==============================================================================*/
4 
5 using UnityEngine;
6 using System.IO;
7 using System;
8 using System.Runtime.InteropServices;
9 using JsonFx.Json;
10 using System.Collections;
11 using System.Collections.Generic;
12 using System.Collections.Specialized;
13 using System.Text;
14 using UnityEngine.Rendering;
15 
16 namespace maxstAR
17 {
22  {
23  [SerializeField]
24  private string textureDir = null;
25 
26  [SerializeField]
27  private float targetWidth = 0;
28 
29  [SerializeField]
30  private float targetHeight = 0;
31 
35  public float TargetWidth
36  {
37  get { return targetWidth; }
38  }
39 
43  public float TargetHeight
44  {
45  get { return targetHeight; }
46  }
47 
48  void Start()
49  {
50  Renderer myRenderer = GetComponent<Renderer>();
51  myRenderer.enabled = false;
52  Destroy(myRenderer);
53  }
54 
59  protected override void OnTrackerDataFileChanged(string trackerFileName)
60  {
61  TrackableId = null;
62  TrackableName = Path.GetFileNameWithoutExtension(trackerFileName);
63  int startIdx = trackerFileName.LastIndexOf("/") + 1;
64  int endIdx = trackerFileName.LastIndexOf(".");
65  string fileName = trackerFileName.Substring(startIdx, endIdx - startIdx);
66 
67  textureDir = Path.GetFileName(Path.GetDirectoryName(trackerFileName));
68 
69  string textureFileName = MaxstARUtils.ImageTargetTexturePath + "/" +
70  textureDir + "/" + fileName + "_resized.jpg";
71 
72  if (Application.platform == RuntimePlatform.WindowsEditor ||
73  Application.platform == RuntimePlatform.OSXEditor)
74  {
75  Stream fs = File.OpenRead(Application.streamingAssetsPath + "/" + trackerFileName);
76  if (!fs.CanSeek) throw new ArgumentException("Stream must be seekable");
77 
78  byte[] buf = new byte[4];
79  fs.Position = 100;
80  fs.Read(buf, 0, 4);
81  ChangeObjectProperty(textureFileName, BitConverter.ToSingle(buf, 0));
82  fs.Close();
83  }
84  }
85 
86  private void ChangeObjectProperty(string textureName, float scale)
87  {
88  Debug.Log("scale : " + scale);
89 
90  if (File.Exists(textureName))
91  {
92  StartCoroutine(MaxstUtil.loadImageFromFileWithSizeAndTexture(Application.streamingAssetsPath + "/../../" + textureName, (width, height, texture) => {
93  Texture2D localTexture = texture;
94  if (texture)
95  {
96  MeshFilter imagePlaneMeshFilter = gameObject.GetComponent<MeshFilter>();
97  if (imagePlaneMeshFilter.sharedMesh == null)
98  {
99  imagePlaneMeshFilter.sharedMesh = new Mesh();
100  imagePlaneMeshFilter.sharedMesh.name = "ImagePlane";
101  }
102 
103  float imageW = 1.0f;
104  float imageH = (float)height / (float)width;
105 
106  float vertexWidth = imageW * 0.5f * scale;
107  float vertexHeight = imageH * 0.5f * scale;
108  imagePlaneMeshFilter.sharedMesh.vertices = new Vector3[]
109  {
110  new Vector3(-vertexWidth, 0.0f, -vertexHeight),
111  new Vector3(-vertexWidth, 0.0f, vertexHeight),
112  new Vector3(vertexWidth, 0.0f, -vertexHeight),
113  new Vector3(vertexWidth, 0.0f, vertexHeight)
114  };
115 
116  targetWidth = imageW * scale;
117  targetHeight = imageH * scale;
118 
119  imagePlaneMeshFilter.sharedMesh.triangles = new int[] { 0, 1, 2, 2, 1, 3 };
120  imagePlaneMeshFilter.sharedMesh.uv = new Vector2[]
121  {
122  new Vector2(0, 0),
123  new Vector2(0, 1),
124  new Vector2(1, 0),
125  new Vector2(1, 1),
126  };
127 
128  if (gameObject.GetComponent<MeshRenderer>().sharedMaterial == null)
129  {
130  gameObject.GetComponent<MeshRenderer>().sharedMaterial = new Material(Shader.Find("Unlit/Texture"));
131  }
132 
133  gameObject.GetComponent<MeshRenderer>().sharedMaterial.SetTexture("_MainTex", texture);
134  }
135  }));
136 
137  }
138  }
139  }
140 }
string TrackableName
Tracking file name only without extention.
string TrackableId
Tracking file uuid. This value is addressed in tracking file.
Handle tracking file definition in editor and runtime target image visibility
Define constant values
Definition: MaxstARUtils.cs:18
Parent class of all TrackableBehaviour. Save tracking file&#39;s id (uuid), name, path etc...
override void OnTrackerDataFileChanged(string trackerFileName)
Notify tracking file changed and change target image texture and scale, etc.