// Update is called once per frame
    void Update()
    {
        if (frameCount < HowManyImages)
        {
            moveCamera();
            rotateLight();
            string filename = $"{frameCount.ToString().PadLeft(10, '0')}";
            synth.Save(filename, ImgSizeX, ImgSizeY, "syntheticData");

            synth.OnSceneChange();
            frameCount++;
            slider.value = (float)frameCount / (float)HowManyImages;
        }
        else
        {
            EditorApplication.isPlaying = false;
        }
    }
    void Update()
    {
        if (frameCount % 5 == 0)
        {
            GenerateRandom();
            // synth.OnSceneChange();
            // GetComponent<ImageSynthesis>().Save(frameCount);

            cycleCount = cycleCount + 1;

            string filename = $"image_{cycleCount.ToString().PadLeft(5, '0')}";

            _imageSynthesis.Save(filename, 256, 256, "captures2");
            cycleCount = cycleCount + 1;
        }

        if (cycleCount == 203)
        {
            Debug.Log("end!");
            Debug.Log("end!");
        }

        frameCount = frameCount + 1;
    }
    // Update is called once per frame
    void Update()
    {
        if (generate)
        {
            if (method == Methods.manual)
            {
                if (frameCount == 0)
                {
                    synth.Save(new Vector3[0], cadObj.GetComponent <Collider>().bounds, "image_groundtruth", width, height, "captures/GroundTruth", true);
                }

                if (Input.GetMouseButtonDown(0))
                {
                    Vector3 mFar  = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.farClipPlane);
                    Vector3 mNear = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane);

                    Vector3 mousePosF = Camera.main.ScreenToWorldPoint(mFar);
                    Vector3 mousePosN = Camera.main.ScreenToWorldPoint(mNear);

                    Debug.DrawRay(mousePosN, mousePosF - mousePosN, Color.green);

                    RaycastHit hit;

                    if (Physics.Raycast(mousePosN, mousePosF - mousePosN, out hit))
                    {
                        //Debug.Log($"hit.point {hit.point}  WorldToScreenPoint(hit.point) {Camera.main.WorldToScreenPoint(hit.point)} hit.transform.position {hit.transform.position} object.position {hit.point - hit.transform.position}");
                        Debug.Log($"Point3D {hit.point}  :  Point2D {Input.mousePosition} ");
                        tw.WriteLine("(" + Input.mousePosition.x + ", " + Input.mousePosition.y + ") : " + (hit.point - hit.transform.position));
                    }
                    tw.Flush();
                }
            }
            else if (method == Methods.orb)
            {
                if (frameCount == 0)
                {
                    synth.Save(new Vector3[0], cadObj.GetComponent <Collider>().bounds, "image_groundtruth", width, height, "captures/GroundTruth", true, generate);
                }
            }
            else if (method == Methods.random)
            {
                if (frameCount == 0)
                {
                    synth.Save(randomPoints, cadObj.GetComponent <Collider>().bounds, "image_groundtruth", width, height, "captures/GroundTruth", true, generate);
                }
            }
        }
        else
        {
            string folder   = "captures/";
            string filename = $"image_{frameCount.ToString().PadLeft(5, '0')}";

            if (control)
            {
                if (frameCount < trainingImages)
                {
                    folder = folder + "Train";
                    createAndSave(folder, filename, true);
                }
            }
            else if (convert)
            {
                if (frameCount < testImages)
                {
                    folder = folder + "SynthTest";
                    createAndSave(folder, filename, false);
                }
            }
        }
        frameCount++;
    }
    public void CreateImage()
    {
        Camera camera = view.GetGameObject().GetComponent <Camera>();
        int    width  = ApplicationManager.instance.GlobalSettingsModel.GeneralParameterModel.ResolutionX;
        int    height = ApplicationManager.instance.GlobalSettingsModel.GeneralParameterModel.ResolutionY;

        RenderTexture previousActive = RenderTexture.active;
        RenderTexture previousCamera = camera.targetTexture;

        RenderTextureDescriptor descriptor = new RenderTextureDescriptor(width, height);

        camera.targetTexture = new RenderTexture(descriptor);
        string targetPath = ApplicationManager.instance.GlobalSettingsModel.GeneralParameterModel.TargetFolder;

        RenderTexture.active = camera.targetTexture;

        Texture2D imageTexture = new Texture2D(width, height);

        //Create color image
        if (ApplicationManager.instance.GlobalSettingsModel.GeneralParameterModel.ColorPictures)
        {
            Directory.CreateDirectory(targetPath + "\\Color");

            camera.Render();
            imageTexture.ReadPixels(new Rect(0, 0, width, height), 0, 0);
            imageTexture.Apply();

            byte[] bytes = imageTexture.EncodeToPNG();
            File.WriteAllBytes(targetPath + "\\Color\\" + ApplicationManager.instance.generatedImages + ".png", bytes);
        }

        //Create depth image with greyScale
        if (ApplicationManager.instance.GlobalSettingsModel.GeneralParameterModel.DepthPictures)
        {
            Directory.CreateDirectory(targetPath + "\\Depth");

            camera.GetComponent <RenderDepth>().enabled = true;
            camera.Render();

            imageTexture.ReadPixels(new Rect(0, 0, width, height), 0, 0);
            imageTexture.Apply();

            byte[] depthBytes = imageTexture.EncodeToPNG();

            File.WriteAllBytes(targetPath + "\\Depth\\" + ApplicationManager.instance.generatedImages + ".png", depthBytes);
            camera.GetComponent <RenderDepth>().enabled = false;
        }

        if (ApplicationManager.instance.GlobalSettingsModel.GeneralParameterModel.SegmentationPicture)
        {
            Directory.CreateDirectory(targetPath + "\\Segmentation");
            ImageSynthesis imageSynthesis = camera.GetComponent <ImageSynthesis>();
            imageSynthesis.enabled               = false;
            imageSynthesis.enabled               = true;
            imageSynthesis.saveDepth             = false;
            imageSynthesis.saveImage             = false;
            imageSynthesis.saveOpticalFlow       = false;
            imageSynthesis.saveNormals           = false;
            imageSynthesis.saveLayerSegmentation = false;
            imageSynthesis.saveIdSegmentation    = true;

            imageSynthesis.Save(ApplicationManager.instance.generatedImages + ".png", width, height, targetPath + "\\Segmentation");
        }
        RenderTexture.active = previousActive;

        Texture2D.Destroy(imageTexture);
    }