/***************************************************************** * オブジェクトをカメラから近い順にゆっくり表示するコルーチン *****************************************************************/ IEnumerator AppearSlowly() { finish_coroutine = false; for (int i = 0; i < 5; i++) { yield return(null); } Dictionary <int, float> goods_distance_dictionary = new Dictionary <int, float>(); foreach (KeyValuePair <int, GoodsData> goods in goods_data_dictionary) { goods_distance_dictionary.Add(goods.Key, CalcDistance(ar_camera, goods.Value.obj)); } var sorted = goods_distance_dictionary.OrderBy((x) => x.Value); foreach (KeyValuePair <int, float> goods in sorted) { if (goods_data_dictionary[goods.Key].state_bool) { ShaderChange goods_shaderchange = goods_data_dictionary[goods.Key].obj.GetComponent <ShaderChange>(); goods_shaderchange.alpha = 0.4f; goods_shaderchange.ChangeColors(); goods_data_dictionary[goods.Key].text3d.SetActive(true); for (int i = 0; i < 5; i++) { yield return(null); } } } finish_coroutine = true; }
// Update is called once per frame //ずっと繰り返し呼び出されるよ~ void Update() { if (!mainSystem.finish_read_config) { return; } switch (calibration_state) { case 0: mainSystem.UpdateMainCanvasInfoText("Fail to Start"); break; case 1: mainSystem.UpdateMainCanvasInfoText("Can NOT Connect [" + mainSystem.GetConfig().ros_ip + "]"); break; case 2: mainSystem.UpdateMainCanvasInfoText("Access to Database"); break; case 3: mainSystem.UpdateMainCanvasInfoText("Please Look [IRVS Marker]"); break; case 4: mainSystem.UpdateMainCanvasInfoText("Ready to AR B-sen"); break; default: mainSystem.UpdateMainCanvasInfoText("Error : " + calibration_state.ToString()); break; } //phase 0 //毎回すること //AugmentedImageの更新 if (!Application.isEditor) { Session.GetTrackables <AugmentedImage>(m_AugmentedImages, TrackableQueryFilter.Updated); } //CameraとB-senのポジション表示 mainSystem.UpdateCalibrationInfoCamera(Camera.main.transform.position, Camera.main.transform.eulerAngles); if (mainSystem.GetConfig().old_calibration) { mainSystem.UpdateCalibrationInfoBsen(bsen_model.transform.position, bsen_model.transform.eulerAngles); } else { mainSystem.UpdateCalibrationInfoDevice(arcore_device.transform.position, arcore_device.transform.eulerAngles); } //どれだけ手動キャリブしてるか表示 if (mainSystem.GetConfig().old_calibration) { Vector3 offset_pos = bsen_model.transform.position - not_offset_pos; Vector3 offset_rot = bsen_model.transform.eulerAngles - not_offset_rot; mainSystem.UpdateCalibrationInfoOffset(offset_pos, offset_rot); } else { Vector3 offset_pos = arcore_device.transform.position - not_offset_pos; Vector3 offset_rot = arcore_device.transform.eulerAngles - not_offset_rot; mainSystem.UpdateCalibrationInfoOffset(offset_pos, offset_rot); } //自動キャリブ終了前 if (!CheckFinishCalibration()) { switch (calibration_state) { //DBにアクセス開始 case 1: if (DBAdapter.IsConnected() && !DBAdapter.CheckWaitAnything()) { IEnumerator coroutine = DBAdapter.ReadMarkerPos(); StartCoroutine(coroutine); calibration_state = 2; } break; //DBのデータをもとにモデルの位置&回転を変更 case 2: if (DBAdapter.CheckSuccess()) { ServiceResponseDB responce = DBAdapter.GetResponce(); DBAdapter.FinishReadData(); //位置を取得&変換 Vector3 marker_position = new Vector3((float)responce.values.tmsdb[0].x, (float)responce.values.tmsdb[0].y, (float)responce.values.tmsdb[0].z); marker_position = Ros2UnityPosition(marker_position); marker_position += mainSystem.GetConfig().vicon_offset_pos; marker_position += mainSystem.GetConfig().calibration_offset_pos; Debug.Log("Marker Pos: " + marker_position); mainSystem.MyConsole_Add("Marker Pos: " + marker_position); //回転を取得&変換 Vector3 marker_euler = new Vector3(Rad2Euler((float)responce.values.tmsdb[0].rr), Rad2Euler((float)responce.values.tmsdb[0].rp), Rad2Euler((float)responce.values.tmsdb[0].ry)); marker_euler = Ros2UnityRotation(marker_euler); if (mainSystem.GetConfig().old_calibration) { marker_euler *= -1.0f; } marker_euler.x = 0.0f; marker_euler.z = 0.0f; marker_euler.y += mainSystem.GetConfig().calibration_offset_yaw; Debug.Log("Marker Rot: " + marker_euler); mainSystem.MyConsole_Add("Marker Rot: " + marker_euler); mainSystem.UpdateDatabaseInfoViconIRVSMarker(marker_position, marker_euler); //回転をモデルに適用 if (mainSystem.GetConfig().old_calibration) { bsen_model.transform.eulerAngles = marker_euler; } //位置と回転をモデル上のマーカーに適用 irvs_marker = Instantiate(new GameObject()); irvs_marker.name = "IRVS Marker"; irvs_marker.transform.SetParent(GameObject.Find("rostms/world_link").transform, false); irvs_marker.transform.localPosition = marker_position; irvs_marker.transform.localEulerAngles = marker_euler; //回転軸をマーカーの位置に合わせる if (mainSystem.GetConfig().old_calibration) { GameObject world_link = GameObject.Find("rostms/world_link"); world_link.transform.localPosition = marker_position * -1; } calibration_state = 3; } break; //画像認識したらキャリブレーションしてモデルを表示 //UnityEditor上ではここはスキップ case 3: if (Application.isEditor) { rostms_shader.alpha = 0.6f; rostms_shader.ChangeColors(); calibration_state = 4; finish_calibration = true; return; } if (!detected_marker) { foreach (var image in m_AugmentedImages) { if (image.TrackingState == TrackingState.Tracking) { detected_marker = true; marker_image = image; autoPositioning(); rostms_shader.alpha = 0.6f; rostms_shader.ChangeColors(); calibration_state = 4; finish_calibration = true; } } } //自動キャリブ終了時の位置と回転を保存 if (mainSystem.GetConfig().old_calibration) { not_offset_pos = bsen_model.transform.position; not_offset_rot = bsen_model.transform.eulerAngles; } else { not_offset_pos = arcore_device.transform.position; not_offset_rot = arcore_device.transform.eulerAngles; } break; } } else //手動キャリブ { manualCalibration(); } }
// Update is called once per frame void Update() { if (!mainSystem.finish_read_config) { return; } //CoordinatesAdapterの位置を調整してカメラとの距離を計算 coordinates_adapter.transform.localPosition = new Vector3(-0.23f, 0.0f, -0.3f); distance_old = distance; distance = CalcDistance(coordinates_adapter, ar_camera); //最初の1回Shaderを変更する if (!change_goods_shader) { foreach (GoodsData goods in goods_data_dictionary.Values) { ShaderChange shaderchange = goods.obj.GetComponent <ShaderChange>(); shaderchange.ChangeShader(Shader.Find("Custom/Transparent")); } change_goods_shader = true; } //距離が閾値以下でデータベースのstateが1だったら表示,違ったら非表示 if (calib_system.CheckFinishCalibration() && finish_coroutine) { //近づいたとき if (distance < mainSystem.GetConfig().refrigerator_distance&& distance_old >= mainSystem.GetConfig().refrigerator_distance) { refrigerator_shaderchange.ChangeShader(Shader.Find("Custom/Transparent")); refrigerator_shaderchange.alpha = 0.4f; refrigerator_shaderchange.ChangeColors(); IEnumerator coroutine = AppearSlowly(); StartCoroutine(coroutine); } //遠くにいるとき else if (distance >= mainSystem.GetConfig().refrigerator_distance) { foreach (GoodsData goods in goods_data_dictionary.Values) { ShaderChange goods_shaderchange = goods.obj.GetComponent <ShaderChange>(); goods_shaderchange.alpha = 0.0f; goods_shaderchange.ChangeColors(); goods.text3d.SetActive(false); } if (refrigerator_shaderchange.shader_now != Shader.Find("Custom/ARTransparent")) { refrigerator_shaderchange.ChangeShader(Shader.Find("Custom/ARTransparent")); refrigerator_shaderchange.alpha = rostms_shaderchange.alpha; refrigerator_shaderchange.ChangeColors(); } } //ずっと近くにいるとき else if (distance < mainSystem.GetConfig().refrigerator_distance&& distance_old < mainSystem.GetConfig().refrigerator_distance) { foreach (GoodsData goods in goods_data_dictionary.Values) { ShaderChange goods_shaderchange = goods.obj.GetComponent <ShaderChange>(); if (goods.state_bool) { goods_shaderchange.alpha = 0.4f; goods.text3d.SetActive(true); } else { goods_shaderchange.alpha = 0.0f; goods.text3d.SetActive(false); } goods_shaderchange.ChangeColors(); } } } //画像認識による自動キャリブレーションが終わった後に実行 if (calib_system.CheckFinishCalibration()) { //冷蔵庫に入っている物品のデータを取得 time_1 += Time.deltaTime; if (!DBAdapter.CheckWaitAnything() && time_1 > 1.0f) { time_1 = 0.0f; IEnumerator coroutine = DBAdapter.GetRefrigeratorItem(); StartCoroutine(coroutine); } if (DBAdapter.CheckGetRefrigeratorItem()) { if (DBAdapter.CheckAbort()) { DBAdapter.ConfirmAbort(); } if (DBAdapter.CheckSuccess()) { id_list = new List <int>(); ServiceResponseDB responce = DBAdapter.GetResponce(); DBAdapter.FinishReadData(); foreach (tmsdb data in responce.values.tmsdb) { //Debug.Log(data.name); //Debug.Log(data.x + ", " + data.y + ", " + data.z); if (data.sensor == 3018 && goods_data_dictionary.ContainsKey(data.id)) { GoodsData goods = goods_data_dictionary[data.id]; Vector3 place = new Vector3((float)data.x, (float)data.y, (float)data.z); place = Ros2UnityPosition(place); if (data.state == 1) { goods.state_bool = true; Debug.Log(data.name + " pos: " + place.ToString("f2")); mainSystem.MyConsole_Add(data.name + " pos: " + place.ToString("f2")); goods.obj.transform.localPosition = place; id_list.Add(data.id); } else { goods.state_bool = false; } if (goods.name == null) { goods.name = data.name; } if (goods.expiration == null) { goods.expiration = "don't know"; } goods.state = data.state; goods.pos = place; } } } } //冷蔵庫に入っている物品の消費期限を取得 time_2 += Time.deltaTime; if (!DBAdapter.CheckWaitAnything() && time_2 > 5.0f) { time_2 = 0.0f; DBAdapter.GiveItemIDList(id_list); IEnumerator coroutine = DBAdapter.ReadExpiration(); StartCoroutine(coroutine); } if (DBAdapter.CheckReadExpiration()) { if (DBAdapter.CheckAbort()) { DBAdapter.ConfirmAbort(); } if (DBAdapter.CheckSuccess()) { Dictionary <int, string> expiration_dictionary = DBAdapter.ReadExpirationData(); DBAdapter.FinishReadData(); foreach (KeyValuePair <int, string> goods in expiration_dictionary) { ExpirationData expiration_data = JsonUtility.FromJson <ExpirationData>(goods.Value); string expiration = expiration_data.expiration; Debug.Log("id: " + goods.Key + ", name: " + goods_data_dictionary[goods.Key].name + ", expiration: " + expiration); mainSystem.MyConsole_Add("id: " + goods.Key + ", name: " + goods_data_dictionary[goods.Key].name + ", expiration: " + expiration); goods_data_dictionary[goods.Key].text3d.GetComponent <TextMeshPro>().text = expiration; goods_data_dictionary[goods.Key].expiration = expiration; } } } //UIを更新する Dictionary <int, string> goods_info_string_dictionary = new Dictionary <int, string>(); foreach (KeyValuePair <int, GoodsData> goods in goods_data_dictionary) { string info = goods.Value.name + ", " + goods.Key.ToString() + ", " + goods.Value.state.ToString() + ", " + goods.Value.pos.ToString("f3") + ", " + goods.Value.expiration; goods_info_string_dictionary.Add(goods.Key, info); } mainSystem.UpdateDatabaseInfoRefrigerator(goods_info_string_dictionary); } }