I’m in the process of creating a 3D Scanning function, and while my function works if I do not add rotation to the mix (2d scan), I cannot seem to create anything but a spherical scan when I rotate the object.
The function works as follows, or at least is intended to do so. I have a ‘resolution’ variable per say titled Scanner_Pixel_Size. So when the scan starts we start at the top of the screen, move down 1 resolution unit, move over 1 resolution unit, raycast the object, set a new gameobject to the hit position and child it to the scanned object. Then the object spins 1 degree, raycast again etc and this all repeats until the entire screen is covered. In theory as the scanned object rotates, the child gameobjects should stay put with the rotating scanned object, but for some reason I have something else going on.
I have two pictures below to help capture the issue I am facing. The gameobjects located at the hitpoints are shown as the yellow dots. I have also posted the majority of the script below -
for (float zhigh = 12; zhigh >= 0; zhigh = zhigh - Scanner_Pixel_Size) {
ScanCam.transform.position = new Vector3(ScanCam.transform.position.x, zhigh, ScanCam.transform.position.z);
for (float xhigh = 12; xhigh >= -12; xhigh = xhigh - Scanner_Pixel_Size) {
ScanCam.transform.position = new Vector3(xhigh, ScanCam.transform.position.y, ScanCam.transform.position.z);
RotatingPlatform.transform.rotation = Quaternion.identity;
for (float r = 0; r < 360; r = r + Scanner_Pixel_Size) {
int layer_mask = LayerMask.GetMask("Imported");
RaycastHit hit;
Vector3 noPos = Vector3.forward;
Ray ray = ScanCam.ScreenPointToRay(noPos);
if (Physics.Raycast(ray, out hit, 100.0f, layer_mask)) {
if (hit.transform.gameObject.layer == 6) {
Renderer rend = hit.transform.GetComponent<Renderer>();
MeshCollider meshCollider = hit.collider as MeshCollider;
Texture2D tex = rend.material.mainTexture as Texture2D;
Vector2 pixelUV = hit.textureCoord;
pixelUV.x *= tex.width;
pixelUV.y *= tex.height;
GameObject g = Instantiate(Blank,hit.point,RotatingPlatform.transform.rotation, RotatingPlatform.transform);
Color ScanColor = tex.GetPixel((int)pixelUV.x, (int)pixelUV.y);
AllHits.Add(g);
AllColors.Add(ScanColor);
}
}
RotatingPlatform.transform.rotation = Quaternion.Euler(RotatingPlatform.transform.eulerAngles.x,r,RotatingPlatform.transform.eulerAngles.z);
}
RotatingPlatform.transform.rotation = Quaternion.identity;
}
}
