Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Placement of a 3d texture

Discussion in 'Shaders' started by djnike00x, May 5, 2022.

  1. djnike00x

    djnike00x

    Joined:
    Nov 30, 2021
    Posts:
    1
    Hi everyone, I am new here so I hope to write a question in the right place.
    I am programming an ecography simulator and I have an interface that allow the user to place a 3D object of possible internal organs inside a human body.
    The rendering of the object is done using a shader over the object and a 3D texture.
    This is the shader code (if can be useful)
    Code (CSharp):
    1. Shader "Custom/Texture3D"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Albedo (RGB)", 3D) = "" {}
    6.     }
    7.     SubShader
    8.     {
    9.         Tags { "RenderType"="Opaque" }
    10.         LOD 200
    11.  
    12.         Pass{
    13.             CGPROGRAM
    14.  
    15.             #pragma vertex vert
    16.             #pragma fragment frag
    17.             #include "UnityCG.cginc"
    18.             #pragma target 3.0
    19.  
    20.             sampler3D _MainTex;
    21.  
    22.             struct vertInput
    23.             {
    24.                 float4 pos : POSITION;
    25.                 float3 uv : TEXCOORD0;
    26.             };
    27.  
    28.             struct v2f
    29.             {
    30.                 float4 pos : SV_POSITION;
    31.                 float4 pos_w : TEXCOORD1;
    32.                 float3 uv : TEXCOORD0;
    33.             };
    34.  
    35.             v2f vert (vertInput input)
    36.             {
    37.                 v2f o;
    38.                 o.pos_w = mul(unity_ObjectToWorld, input.pos) ;
    39.                 o.pos = UnityObjectToClipPos(input.pos);
    40.                 o.uv = input.uv;
    41.                 return o;
    42.             }
    43.  
    44.             half4 frag(v2f i) : SV_Target
    45.             {
    46.                 half4 mainColor = tex3D(_MainTex, float3(i.pos_w.x, i.pos_w.y, i.pos_w.z));
    47.                 return mainColor;
    48.             }
    49.            
    50.             ENDCG
    51.         }
    52.     }
    53.     FallBack "Diffuse"
    54. }
    It seems to work well, the problem is that using a shader the 3D texture is always placed in the origin (0,0,0).
    Is there a method to assign new world coordinates to the texture position?
    Any kind of alternative solutions are wisely accepted, thanks