Skip to content

Commit

Permalink
Fix reference image import
Browse files Browse the repository at this point in the history
  • Loading branch information
andybak authored Aug 14, 2024
1 parent 20489ab commit f8d3c64
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions Assets/Scripts/desktop_app/ReferenceImageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

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

Expand All @@ -20,6 +21,7 @@
using com.google.apps.peltzer.client.model.main;
using com.google.apps.peltzer.client.model.util;
using com.google.apps.peltzer.client.tools;
using UnityEngine.Networking;

namespace com.google.apps.peltzer.client.desktop_app
{
Expand Down Expand Up @@ -69,22 +71,38 @@ public void Update()
/// <param name="previewImagePath"></param>
public void InsertNewReferenceImage(string previewImagePath)
{
WWW www = new WWW("file:///" + System.Uri.EscapeUriString(previewImagePath));
Texture2D texture = new Texture2D(500, 500);
www.LoadImageIntoTexture(texture);
StartCoroutine(LoadTexture(previewImagePath));
}

MoveableReferenceImage.SetupParams setupParams = new MoveableReferenceImage.SetupParams();
setupParams.attachToController = true;
// Have the image start attached to the controller and right ahead of it.
setupParams.positionModelSpace = PeltzerMain.Instance.peltzerController.LastPositionModel +
PeltzerMain.Instance.peltzerController.LastRotationModel * Vector3.forward * MoveableObject.HOVER_DISTANCE;
setupParams.rotationModelSpace = PeltzerMain.Instance.peltzerController.LastRotationModel;
setupParams.scaleModelSpace = Vector3.one * 0.5f;
setupParams.texture = texture;
setupParams.refImageId = nextId++;
setupParams.initialInsertion = true;
private IEnumerator LoadTexture(string previewImagePath)
{
previewImagePath = previewImagePath.Replace('\\', '/');
string uri = "file:///" + System.Uri.EscapeUriString(previewImagePath);
using (UnityWebRequest www = UnityWebRequestTexture.GetTexture(uri))
{
yield return www.SendWebRequest();

pendingReferenceImageCommands.Enqueue(new AddReferenceImageCommand(setupParams));
if (www.isNetworkError || www.isHttpError)
{
Debug.LogError("Failed to load texture: " + www.error);
}
else
{
Texture2D texture = DownloadHandlerTexture.GetContent(www);
MoveableReferenceImage.SetupParams setupParams = new MoveableReferenceImage.SetupParams();
setupParams.attachToController = true;
// Have the image start attached to the controller and right ahead of it.
setupParams.positionModelSpace = PeltzerMain.Instance.peltzerController.LastPositionModel +
PeltzerMain.Instance.peltzerController.LastRotationModel * Vector3.forward * MoveableObject.HOVER_DISTANCE;
setupParams.rotationModelSpace = PeltzerMain.Instance.peltzerController.LastRotationModel;
setupParams.scaleModelSpace = Vector3.one * 0.5f;
setupParams.texture = texture;
setupParams.refImageId = nextId++;
setupParams.initialInsertion = true;

pendingReferenceImageCommands.Enqueue(new AddReferenceImageCommand(setupParams));
}
}
}

/// <summary>
Expand Down

0 comments on commit f8d3c64

Please sign in to comment.