Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to view Slices in Image Component #252

Open
alykhaled opened this issue Jun 4, 2024 · 0 comments
Open

How to view Slices in Image Component #252

alykhaled opened this issue Jun 4, 2024 · 0 comments

Comments

@alykhaled
Copy link

I used this code to try to view the slice in Image Component
I tried to use the same method from SliceRenderingEditorWindow but it didn't work

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace UnityVolumeRendering
{
    public class SliceImage : MonoBehaviour
    {
        private SlicingPlane[] spawnedPlanes;
        private Image sliceImage;
        // Start is called before the first frame update
        void Start()
        {
            
        }

        // Update is called once per frame
        void Update()
        {
            spawnedPlanes = FindObjectsOfType<SlicingPlane>();
            var sliceImageParent = this.gameObject.transform.GetChild(0).gameObject.transform.GetChild(0).gameObject.transform.GetChild(2).gameObject;
            Debug.Log("Parent name: " + sliceImageParent.name);
            sliceImage = sliceImageParent.GetComponent<Image>();
            if (sliceImage == null)
            {
                Debug.LogError("No Image component found in children of this object");
            }
            else{
                Debug.Log("Image component found");
                Debug.Log("Image component name: " + sliceImage.name);
            }
            if (spawnedPlanes.Length > 0)
            {
                SlicingPlane planeObj = spawnedPlanes[0];
                Material mat = planeObj.GetComponent<MeshRenderer>().sharedMaterial;
                Texture3D tex3D = mat.GetTexture("_DataTex") as Texture3D;

                // Create a new Texture2D with the same format as the Texture3D
                Texture2D tex2D = new Texture2D(tex3D.width, ex3D.height, tex3D.format, tex3D.mipmapCount > 1);

                // Copy the slice from the 3D texture to the 2D texture
                Graphics.CopyTexture(tex3D, 0, 0, tex2D, 0, 0);

                sliceImage.sprite = Sprite.Create(tex2D, new Rect(0, 0, tex2D.width, tex2D.height), new Vector2(0.5f, 0.5f));

            }
        }
    }

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant