forked from cgvr/DeltaVR
80 lines
2.8 KiB
C#
80 lines
2.8 KiB
C#
using FMOD.Studio;
|
|
using System;
|
|
using UnityEngine;
|
|
|
|
public class Printer3D : MonoBehaviour
|
|
{
|
|
public Transform spawnPoint;
|
|
public int ignorePlayerCollisionLayer = 2;
|
|
public string shapeScannerTag = "ShapeScannable";
|
|
public float printedObjectSize = 0.5f;
|
|
|
|
private bool isPrinting = false;
|
|
private EventInstance printingSound;
|
|
private GameObject GeneratedModel;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
printingSound = AudioManager.Instance.CreateInstance(FMODEvents.Instance.Printing);
|
|
printingSound.setParameterByName("3DPrinterPrintingJob", 1);
|
|
printingSound.setParameterByName("Occlusion", 0);
|
|
printingSound.set3DAttributes(FMODUnity.RuntimeUtils.To3DAttributes(spawnPoint));
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
|
|
public async void PrintObject(Texture2D texture)
|
|
{
|
|
isPrinting = true;
|
|
printingSound.start();
|
|
printingSound.setParameterByName("3DPrinterPrintingJob", 0);
|
|
|
|
string encodedTexture = Convert.ToBase64String(texture.EncodeToJPG());
|
|
byte[] encodedModel = await TrellisClient.Instance.GenerateModel(encodedTexture);
|
|
|
|
GameObject spawnedObject = await ModelGenerationUtils.Instance.SpawnModel(encodedModel, printedObjectSize);
|
|
InitializeSpawnedObject(spawnedObject);
|
|
if (GeneratedModel != null)
|
|
{
|
|
// Destroy previous generated object (first move out of ShapeScanner to trigger OnTriggerExit
|
|
GeneratedModel.transform.position = Vector3.zero;
|
|
}
|
|
GeneratedModel = spawnedObject;
|
|
|
|
printingSound.setParameterByName("3DPrinterPrintingJob", 1);
|
|
isPrinting = false;
|
|
}
|
|
|
|
private void InitializeSpawnedObject(GameObject spawnedObject)
|
|
{
|
|
GameObject spawnedObjectParent = new GameObject("SpawnedModelParent");
|
|
spawnedObjectParent.transform.parent = spawnPoint;
|
|
spawnedObjectParent.transform.position = spawnPoint.transform.position;
|
|
spawnedObjectParent.layer = ignorePlayerCollisionLayer;
|
|
Rigidbody rigidbody = spawnedObjectParent.AddComponent<Rigidbody>();
|
|
rigidbody.isKinematic = true;
|
|
|
|
//spawnedObject.AddComponent<NetworkObject>();
|
|
//spawnedObject.AddComponent<NetworkTransform>();
|
|
|
|
MeshCollider spawnedObjectCollider = spawnedObject.GetComponent<MeshCollider>();
|
|
spawnedObjectCollider.convex = false;
|
|
spawnedObject.transform.parent = spawnedObjectParent.transform;
|
|
spawnedObject.transform.position = spawnedObjectParent.transform.position;
|
|
spawnedObject.tag = shapeScannerTag;
|
|
spawnedObject.layer = ignorePlayerCollisionLayer;
|
|
|
|
spawnedObjectParent.AddComponent<TwoHandScaleGrabInteractable>();
|
|
}
|
|
|
|
public bool IsPrinting()
|
|
{
|
|
return isPrinting;
|
|
}
|
|
}
|