using FMOD.Studio; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class Printer3D : MonoBehaviour { public Transform spawnPoint; public int ignorePlayerCollisionLayer = 2; public string shapeScannerTag = "ShapeScannable"; 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); } // 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); 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.isKinematic = true; //spawnedObject.AddComponent(); //spawnedObject.AddComponent(); MeshCollider spawnedObjectCollider = spawnedObject.GetComponent(); spawnedObjectCollider.convex = false; spawnedObject.transform.parent = spawnedObjectParent.transform; spawnedObject.transform.position = spawnedObjectParent.transform.position; spawnedObject.tag = shapeScannerTag; spawnedObject.layer = ignorePlayerCollisionLayer; spawnedObjectParent.AddComponent(); } public bool IsPrinting() { return isPrinting; } }