I am importing my own 3D mesh (a ply file converted to a .obj and imported to Unity) and using it to render an image in Unity. I have added the object to prefab and I’m accessing it during rendering (Code is attached at the end)
While rendering I’m noticing that there are two objects - Clone and default (which have different orientations, positions, and scale). The Clone seems to have the positions, orientations, scale, etc I’m setting from my script, but the one that is getting rendered is the default one. I can see this when I select the objects and observing the co-ordinate frames. Adding pictures for reference.
Why are there two objects, and how do I get the correct one to render?
As an experiment I tried changing the properties of the default to match the ones of the clone, (so I thought if I match them, the default one which is rendering would come to the pos and orientation that I want), but when I did that the default went completely out of the frame to a different pos altogether, so I am suspecting they are on different coordinate frames altogether)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SceneController : MonoBehaviour
{
public ImageSynthesis synth;
public GameObject[] prefabs;
public int num_Objects = 2;
public int trainingImages;
public int valImages;
public bool grayscale = false;
public bool save = false;
private ShapePool pool;
private int frameCount = 0;
// Start is called before the first frame update
void Start()
{
pool = ShapePool.Create(prefabs);
}
// Update is called once per frame
void Update()
{
if (frameCount < trainingImages + valImages) {
GenerateRandom();
Debug.Log($"FrameCount: {frameCount}");
frameCount++;
if (save)
{
if (frameCount < trainingImages)
{
string filename = $"image_{frameCount.ToString().PadLeft(5, '0')}";
synth.Save(filename, 1920, 1080, "captures/train", 2);
}
else if (frameCount < trainingImages + valImages)
{
int valFrameCount = frameCount - trainingImages;
string filename = $"image_{valFrameCount.ToString().PadLeft(5, '0')}";
synth.Save(filename, 1920, 1080, "captures/val", 2);
}
}
}
}
void GenerateRandom()
{
pool.ReclaimAll();
//pick out prefab
int prefabIndx = Random.Range(0, prefabs.Length);
GameObject prefab = prefabs[prefabIndx];
//OBJECT 1
/*
DATA from Python script
PSM_1 Orientation in quaternions WXYZ [ 0.06844 0.458484 -0.60438 0.647945]
PSM_1 Position in mm [14.620537 3.197186 64.227186
*/
//Position
float newX, newY, newZ;
newX = 14.620537f;
newY = 3.197186f;
newZ = 64.227186f;
var newPos = new Vector3(newX, newY, newZ);
var shape = pool.Get((ShapeLabel)prefabIndx);
var newObj = shape.obj;
newObj.transform.position = newPos;
//Rotation
float quatX = 0.458484f;
float quatY = -0.60438f;
float quatZ = 0.647945f;
float quatW = 0.06844f;
newObj.transform.rotation = new Quaternion(quatX, quatY, quatZ, quatW);
//Scale
float sx = 1.0f;
Vector3 newScale = new Vector3(sx, sx, sx);
newObj.transform.localScale = newScale;
// Color
/*
float newR, newG, newB;
newR = Random.Range(0.0f, 1.0f);
newG = Random.Range(0.0f, 1.0f);
newB = Random.Range(0.0f, 1.0f);
var newColor = new Color(newR, newG, newB);
newObj.GetComponent<Renderer>().material.color = newColor;
*/
}
}
synth.OnSceneChange(grayscale);
}
}