示例#1
0
    public void OnSegmentationCamRender(AnnotationCamera camera, AnnotationObject annotationObject)
    {
        if (camera != Segmentation.Camera)
        {
            return;
        }

        RenderedObjects.Add(annotationObject);
    }
示例#2
0
    protected override void Start()
    {
        //Check if set
        if (!objectIdentifier)
        {
            Debug.LogError("Please add object identifier shader");
        }

        if (!pixelCountComputeShader)
        {
            Debug.LogError("Please add pixel count Compute shader");
        }

        if (generator.ObjectManager.AnnotatedObjects.Count == 0)
        {
            Debug.LogError("Please select some annotation objects");
        }

        visibilityObjectsCamera = generator.InstantiateNewCamera(generator.OutputCamera.gameObject, autoRender: false);
        GameObject gameObjectVisibilityCamera = visibilityObjectsCamera.gameObject;

        gameObjectVisibilityCamera.name = "VisibilityCamera";
        Camera visibilityCameraComponent = gameObjectVisibilityCamera.GetComponent <Camera>();

        //Different settings
        visibilityCameraComponent.clearFlags      = CameraClearFlags.SolidColor;
        visibilityCameraComponent.backgroundColor = Color.clear;
        visibilityCameraComponent.renderingPath   = RenderingPath.Forward;
        visibilityCameraComponent.allowMSAA       = false;

        //Settings targetTexture
        RenderTextureDescriptor descriptor = generator.OutputCamera.Component.targetTexture.descriptor; //Dimensions

        descriptor.bindMS      = false;
        descriptor.msaaSamples = 1;
        visibilityCameraComponent.targetTexture = new RenderTexture(descriptor); //Create new texture  using dimensions of output
        visibilityCameraComponent.targetTexture.antiAliasing = 1;

        visibilityObjectsCamera.Component.SetReplacementShader(objectIdentifier, "");

        //Calculate the
        float width  = visibilityObjectsCamera.Component.targetTexture.width;
        float height = visibilityObjectsCamera.Component.targetTexture.height;

        threadGroupsX = Mathf.CeilToInt(width / 16);
        threadGroupsY = Mathf.CeilToInt(height / 16);

        //Set compute buffers
        pixelCountsVisibility = new ComputeBuffer(threadGroupsX * threadGroupsY, sizeof(uint)); //number of threads inside the shader

        int kernelHandle = pixelCountComputeShader.FindKernel("CSMain");

        pixelCountComputeShader.SetBuffer(kernelHandle, "countsVisibility", pixelCountsVisibility);
    }
    private void OnWillRenderObject()
    {
        AnnotationCamera annotationCamera = Camera.current.GetComponent <AnnotationCamera>();

        if (annotationCamera)                                          //Potentially not an Annoatation Camera
        {
            foreach (OnWillRenderCallBack callback in renderCallBacks) //Multiple object managers
            {
                callback(annotationCamera, this);
            }
        }
    }
示例#4
0
 private void OnWillRenderAnnotationObject(AnnotationCamera camera, AnnotationObject annotationObject)
 {
     if (!AnnotatedObjects.Contains(annotationObject))
     {
         return; //Object rendered is not part of this object manager
     }
     if (RenderedObjects.ContainsKey(camera))
     {
         RenderedObjects[camera].Add(annotationObject);
     }
     else
     {
         RenderedObjects.Add(camera, new HashSet <AnnotationObject>()
         {
             annotationObject
         });
     }
 }
示例#5
0
    public void Initialize(AnnotationGenerator generator)
    {
        this.generator = generator;

        AnnotationCamera outputCamera = generator.OutputCamera;
        //Instantiate segmentation camera
        GameObject segmentationGameObject = Instantiate(outputCamera.gameObject, gameObject.transform);

        segmentationGameObject.name = "SegmentationCam";
        segmentationGameObject.transform.position = outputCamera.transform.position;
        segmentationGameObject.transform.rotation = outputCamera.transform.rotation;

        Camera = segmentationGameObject.GetComponent <AnnotationCamera>();
        Camera.Component.clearFlags      = CameraClearFlags.SolidColor;
        Camera.Component.backgroundColor = Color.clear;
        Camera.Component.renderingPath   = RenderingPath.Forward;
        Camera.Component.allowMSAA       = false;

        RenderTextureDescriptor descriptor = outputCamera.Component.targetTexture.descriptor; //Dimensions

        descriptor.bindMS              = false;
        descriptor.msaaSamples         = 1;
        Camera.Component.targetTexture = new RenderTexture(descriptor); //Create new texture  using dimensions of output
        Camera.Component.targetTexture.antiAliasing = 1;

        Camera.Component.SetReplacementShader(segmentationShader, "");


        //Instantiate shader settings

        float width  = Camera.Component.targetTexture.width;
        float height = Camera.Component.targetTexture.height;

        threadGroupsX = Mathf.CeilToInt(width / 16);
        threadGroupsY = Mathf.CeilToInt(height / 16);

        pixelCountsVisibility = new ComputeBuffer(threadGroupsX * threadGroupsY, sizeof(uint)); //number of threads inside the shader

        int kernelHandle = pixelCountComputeShader.FindKernel("CSMain");

        pixelCountComputeShader.SetBuffer(kernelHandle, "countsVisibility", pixelCountsVisibility);
    }
示例#6
0
    public void Export(AnnotationCamera annotationCam, AnnotationOutput output)
    {
        // The Render Texture in RenderTexture.active is the one
        // that will be read by ReadPixels.
        RenderTexture target = annotationCam.Component.targetTexture;

        // Store original render texture, and set input as acitve.
        RenderTexture temp = RenderTexture.active;

        RenderTexture.active = target;

        // Make a new texture and read the active Render Texture into it.
        Texture2D image = new Texture2D(target.width, target.height, TextureFormat.RGB24, false);

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

        // Set active render texture back to original
        RenderTexture.active = temp;

        byte[] bytes;
        switch (output.ImageFormatValue)
        {
        case AnnotationOutput.ImageFormat.JPG:
            bytes = image.EncodeToJPG();
            break;

        case AnnotationOutput.ImageFormat.PNG:
            bytes = image.EncodeToPNG();
            break;

        case AnnotationOutput.ImageFormat.EXR:
            bytes = image.EncodeToEXR();
            break;

        case AnnotationOutput.ImageFormat.TGA:
            bytes = image.EncodeToTGA();
            break;

        default:
            bytes = new byte[] { };
            break;
        }


        // destroy texture2D
        UnityEngine.Object.Destroy(image);

        // Write encoded data to a file in the project folder

        if (!Directory.Exists(output.Path))
        {
            Directory.CreateDirectory(output.Path);
        }

        string path    = string.Format("{0}/{1}", output.Path, output.Filename);
        uint   counter = 0;

        while (File.Exists(path))
        {
            path = string.Format("{0}/{1}", output.Path, output.GetFileBaseName() + "_" + counter.ToString() + output.GetFileExtension());
            counter++;
        }
        File.WriteAllBytes(path, bytes);
    }