Skip to content

Commit

Permalink
Merge pull request #60 from from2001/develop
Browse files Browse the repository at this point in the history
v0.1.4
  • Loading branch information
from2001 authored Aug 5, 2024
2 parents 3df94f1 + a10c480 commit 35ce117
Show file tree
Hide file tree
Showing 6 changed files with 290 additions and 28 deletions.
23 changes: 1 addition & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
- Load glTF/glb models with URL
- File cache

## Install Option A: via OpenUPM command-line interface
## Install via OpenUPM command-line interface

```shell
# Install openupm-cli
Expand All @@ -25,27 +25,6 @@ cd YOUR_UNITY_PROJECT_DIR
openupm add -f com.from2001.gltfast-visualscripting-nodes
```

## Install Option B: via Unity package manager

### 1. Setup scoped registories

Open "Edit - Project Settings - Package Manager" on your Unity project.

Add Scoped Registories and click "Apply".

Name: `OpenUPM`
URL: `https://package.openupm.com`
Scopes:
`com.cysharp.unitask`
`com.from2001.gltfast-visualscripting-nodes`
`com.openupm`

![Project Settings](https://github.com/from2001/glTFast_VisualScriptingNodes/assets/387880/dd5a5f9c-47fc-421c-b262-c27702ce882b)

### 2. Install glTFast Visual Scripting Node Package with Package Manager

![Package Manager](https://github.com/from2001/glTFast_VisualScriptingNodes/assets/387880/73f12fe8-164a-4774-9e76-27771b447186)

## How to Use

Notice: Check "Coroutine" in the "On Start Event" triger node.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using UnityEngine;
using Unity.VisualScripting;
using UnityEngine.UI;
using Cysharp.Threading.Tasks;
using System.Collections;
using GLTFast;
using UnityEngine.Networking;
using System;
using System.IO;
using STYLY.Http;
using STYLY.Http.Service;
using UnityGLTF;
Expand Down Expand Up @@ -37,14 +36,17 @@ public class LoadGltf : Unit
public ValueOutput result;

private GameObject resultValue;
private GameObject loadingIcon;

private ThrobberRotator throbberRotator;
protected override void Definition()
{
inputTrigger = ControlInputCoroutine("inputTrigger", Enter);
outputTrigger = ControlOutput("outputTrigger");

glTF_URL = ValueInput<string>("glTF/glb URL", "");
TargetGameobject = ValueInput<GameObject>("Target Game Object", null);
NormalizeScale = ValueInput<bool>("Normalize Scale", true);
NormalizeScale = ValueInput<bool>("Normalize Scale", false);
result = ValueOutput<GameObject>("Game Object", (flow) => resultValue);
}

Expand All @@ -54,15 +56,105 @@ private IEnumerator Enter(Flow flow)
GameObject target = flow.GetValue<GameObject>(TargetGameobject);
bool adjustScale = flow.GetValue<bool>(NormalizeScale);
GameObject gltfInstance = null;

ShowLoadingIcon(target);

UniTask.Create(async () =>
{
gltfInstance = await LoadGltfWithURL(url, target, adjustScale);
}).Forget();
yield return new WaitUntil(() => gltfInstance);

HideLoadingIcon();

resultValue = gltfInstance;
yield return outputTrigger;
}

/// <summary>
/// Takes care of the loading icon creation and calls the animation accordingly
/// </summary>
/// <param name="target"></param>
private void ShowLoadingIcon(GameObject target)
{
loadingIcon = new GameObject("LoadingIcon");
Canvas canvas = loadingIcon.AddComponent<Canvas>();
canvas.renderMode = RenderMode.WorldSpace;
CanvasScaler canvasScaler = loadingIcon.AddComponent<CanvasScaler>();
canvasScaler.dynamicPixelsPerUnit = 10;

// Add an Image component to serve as the loading icon
GameObject icon = new GameObject("Icon");
icon.transform.SetParent(loadingIcon.transform);
Image image = icon.AddComponent<Image>();

// Load the throbber sprite from Resources, the image file can be changed easily.
Sprite throbberSprite = Resources.Load<Sprite>("throbber");
if (throbberSprite != null)
{
image.sprite = throbberSprite;
}
else
{
Debug.LogWarning("Throbber sprite not found in Resources/throbber");
image.color = Color.white; // Fallback color if the sprite is not found
}

// Set the size of the loading icon
RectTransform rectTransform = image.GetComponent<RectTransform>();
rectTransform.sizeDelta = new Vector2(0.5f, 0.5f); // Set size of the icon in world units. I decided to put it in 0.5 so it is not intrusive

// Position the loading icon based on the target object
if (target != null)
{
// Calculate bounds of the target object
Bounds bounds = GetTargetBounds(target);
loadingIcon.transform.position = bounds.center;
rectTransform.anchoredPosition = Vector2.zero; // Center the icon
}

// Add ThrobberRotator component and start the throbber animation
throbberRotator = loadingIcon.AddComponent<ThrobberRotator>();
throbberRotator.Initialize(icon.transform);
}

/// <summary>
/// Receives the location of the target model to put the loading icon on top of it
/// </summary>
/// <param name="target"></param>
/// <returns></returns>
private Bounds GetTargetBounds(GameObject target)
{
Renderer[] renderers = target.GetComponentsInChildren<Renderer>();
if (renderers.Length == 0)
{
return new Bounds(target.transform.position, Vector3.one);
}

Bounds bounds = renderers[0].bounds;
foreach (Renderer renderer in renderers)
{
bounds.Encapsulate(renderer.bounds);
}
return bounds;
}

/// <summary>
/// Hide the loading icon after the gltf is shown
/// </summary>
private void HideLoadingIcon()
{
if (loadingIcon != null)
{
if (throbberRotator != null)
{
throbberRotator.StopRotation();
}
GameObject.Destroy(loadingIcon);
}
}



/// <summary>
/// Load glTF/glb with URL using UnityGLTF
Expand All @@ -73,6 +165,9 @@ private IEnumerator Enter(Flow flow)
/// <returns></returns>
private async UniTask<GameObject> LoadGltfWithURL(string URL, GameObject target = null, bool normalizescale = true)
{
// Load completed flag
bool loadCompleted = false;

// Create glTF GameObject
GameObject glTF = new("glTF");
var UnityGltfScript = glTF.AddComponent<GLTFComponent>();
Expand All @@ -85,9 +180,10 @@ private async UniTask<GameObject> LoadGltfWithURL(string URL, GameObject target
UnityGltfScript.PlayAnimationOnLoad = true;
UnityGltfScript.HideSceneObjDuringLoad = false;
UnityGltfScript.Factory = null;
UnityGltfScript.onLoadComplete = () => loadCompleted = true;

// Load glTF/glb
await UnityGltfScript.Load();
// Wait until the glTF is loaded
await UniTask.WaitUntil(() => loadCompleted);

// Adjust scale to 1 unit size
if (normalizescale) Utils.FitToUnitSize(glTF);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using UnityEngine;
using System.Collections;

/// <summary>
/// Rotates the throbber to make it seem like a loading animation
/// </summary>
public class ThrobberRotator : MonoBehaviour
{
private Transform iconTransform;
private Coroutine rotationCoroutine;

public void Initialize(Transform iconTransform)
{
this.iconTransform = iconTransform;
rotationCoroutine = StartCoroutine(RotateThrobber());
}

private IEnumerator RotateThrobber()
{
while (true)
{
iconTransform.Rotate(Vector3.forward, -360 * Time.deltaTime);
yield return null;
}
}

public void StopRotation()
{
if (rotationCoroutine != null)
{
StopCoroutine(rotationCoroutine);
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.from2001.gltfast-visualscripting-nodes",
"version": "0.1.3",
"version": "0.1.4",
"displayName": "glTFast Visual Scripting Nodes",
"description": "Unity Visual Scripting node library for glTF",
"unity": "2021.3",
Expand Down

0 comments on commit 35ce117

Please sign in to comment.