1
0
forked from cgvr/DeltaVR

can insert printable into 3d printer, 3d printer starts printing

This commit is contained in:
2026-02-14 16:08:13 +02:00
parent 884459842e
commit 17c9122a14
11 changed files with 192 additions and 45 deletions

View File

@@ -1,7 +1,6 @@
using DG.Tweening;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class ComputerPrinter : MonoBehaviour
{
@@ -9,8 +8,9 @@ public class ComputerPrinter : MonoBehaviour
public PushableButton enterKey;
public Transform ejectionOrigin;
public Transform ejectionDestination;
public GameObject printablePrefab;
public Printable printablePrefab;
public float ejectionDuration = 1.0f;
public int ignorePlayerCollisionLayer = 2;
// Start is called before the first frame update
void Start()
@@ -29,11 +29,14 @@ public class ComputerPrinter : MonoBehaviour
string inputPrompt = textDisplay.text;
byte[] imageBytes = await InvokeAiClient.Instance.GenerateImage(inputPrompt);
Texture2D generatedTexture = ModelGenerationUtils.CreateTexture(imageBytes);
Sprite sprite = ModelGenerationUtils.CreateSprite(generatedTexture);
GameObject printable = Instantiate(printablePrefab, ejectionOrigin.position, Quaternion.identity);
Image printableDisplay = printable.GetComponentInChildren<Image>();
printableDisplay.sprite = sprite;
Printable printable = Instantiate(printablePrefab, ejectionOrigin.position, Quaternion.identity);
printable.AttachTexture(generatedTexture);
printable.gameObject.layer = ignorePlayerCollisionLayer;
foreach (Transform childTrans in printable.transform.GetComponentInChildren<Transform>())
{
childTrans.gameObject.layer = ignorePlayerCollisionLayer;
}
printable.transform.DOMove(ejectionDestination.position, ejectionDuration).OnComplete(() =>
{

View File

@@ -0,0 +1,34 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Printable : MonoBehaviour
{
private Texture2D attachedTexture;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public Texture2D GetTexture()
{
return attachedTexture;
}
public void AttachTexture(Texture2D texture)
{
Sprite sprite = ModelGenerationUtils.CreateSprite(texture);
Image imageDisplay = GetComponentInChildren<Image>();
imageDisplay.sprite = sprite;
attachedTexture = texture;
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bb8ba6b5820955f4a95511b55c3a8db9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,62 @@
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 GameObject GeneratedModel;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public async void PrintObject(Texture2D texture)
{
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;
}
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>();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 165a0eb9b371428468a9f9253668ef2b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,35 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Printer3DInputHole : MonoBehaviour
{
public Printer3D printer;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter(Collider other)
{
Debug.Log(other.gameObject.name);
Transform parent = other.transform.parent;
if (parent != null) {
Printable printable = parent.GetComponent<Printable>();
if (printable != null)
{
printer.PrintObject(printable.GetTexture());
Destroy(printable.gameObject);
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2970ff5f70d74904ea849da1b6364e0e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: