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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Accessing object bounds in cg shader

Discussion in 'Shaders' started by ronronmx, Aug 28, 2012.

  1. ronronmx

    ronronmx

    Joined:
    Aug 16, 2010
    Posts:
    201
    Basically I need to do something like:

    half2 localUV = v.vertex * (1.0 / objectWidth);

    I need to access the object's width/height, but i can't figure out how to get those values within a cg shader.
    Thanks in advance!

    SR
     
  2. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,864
    you can pass variables from a script to the shader using shader.Setfloat, int, vector...etc commands.
    Look at Shader class and Material class in the documents.
     
  3. ronronmx

    ronronmx

    Joined:
    Aug 16, 2010
    Posts:
    201
    Thanks for the help aubergine!
    I was trying to skip having to pass variables from a script, and was hopping there was a way - or built-in value - to do it within the shader without needing external data. Looks like I was wrong :)
    I'm basically trying to project a gradient texture onto my object, in ObjectSpace, regardless of the object's uv's, so that the gradient repeats only once across the object's surface. I've manage to 'sort of' do it using _ScreenParams.x, but this projects the gradient across the whole scene size instead of the object's size, and I would need to use a different multiplier for each object...getting closer every day haha
     
  4. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,864
    as far as i know, the distance between center and the farthest vertex is 1.0 so you can use the distance function to get what you are trying to do.
    i might be wrong though never tried it.

    By the way, passing variables doesnt cause a speed loss in your case if you wonder. Just pass the object size value at the awake or start and thats it.
     
  5. ronronmx

    ronronmx

    Joined:
    Aug 16, 2010
    Posts:
    201
    For the distance function (in shader correct?):

    float dist = distance( ?, ? ); // In shader, what do I use to calculate the distance as you mentioned

    Thanks for the clarification on speed loss when passing variables. Although the only reason I'm trying to avoid it is just because I'd like to keep everything shader related to shaders only, but if it can't then it can't :)
     
  6. antislash

    antislash

    Joined:
    Apr 23, 2015
    Posts:
    646
    hi guys.. i am interested in that question too ....
    i made this simple shader that actually gives a kind of "proportion" and tiles the texture according to it,
    and it works..
    hope that helps
    Code (CSharp):
    1. Shader "Custom/detections/extends" {
    2.  
    3.    Properties {
    4.         _Color ("Diffuse Color", Color) = (1,1,1,1)
    5.         _MainTex ("Base layer (RGB)", 2D) = "white" {}
    6.      
    7.  
    8.     }
    9.       SubShader {
    10.    
    11.         Tags{"Queue"="Transparent" "LightMode" = "ForwardBase" }
    12.  
    13.         Blend SrcAlpha OneMinusSrcAlpha
    14.         Lighting On
    15.         ZWrite On
    16.      
    17.     Pass { //pass1
    18.         CGPROGRAM
    19.         #pragma vertex vert
    20.         #pragma fragment frag
    21.         #include "UnityCG.cginc"
    22.            
    23.          float4 _MainTex_ST;
    24.         float4 _Color;
    25.         sampler2D _MainTex;
    26.      
    27.          struct v2f {
    28.               float4 pos : SV_POSITION;
    29.               fixed4 color : COLOR;
    30.               float3 norm : NORMAL;
    31.               float2 uv : TEXCOORD0;
    32.               float3 extend : TEXCOORD1;
    33.         };
    34.    
    35.           v2f vert (appdata_full v)
    36.           {
    37.               v2f o;
    38.        
    39.             o.norm = v.normal.xyz;
    40.                o.pos = mul (UNITY_MATRIX_MVP, v.vertex );
    41.             o.uv = TRANSFORM_TEX(v.texcoord.xy,_MainTex);
    42.  
    43.             float4 centerPos = mul ( _Object2World, float4(0,0,0,1) );
    44.             float4 cornerPos = mul ( _Object2World, float4(1,1,1,1) );
    45.             o.extend = (cornerPos - centerPos);
    46.  
    47.             //assign color and alpha
    48.             o.color.xyz = _Color.xyz;
    49.             o.color.w = 1;
    50.             return o;
    51.         }
    52.      
    53.         fixed4 frag (v2f i) : COLOR
    54.         {
    55.             fixed4 o;
    56.             fixed4 tex = _Color * tex2D (_MainTex, i.uv*i.extend);
    57.  
    58.             o = tex;
    59.             return o;
    60.           }
    61.              
    62.         ENDCG
    63.     }//pass 1
    64.   } //subshader
    65. }//shader
     
  7. Henry_Sun

    Henry_Sun

    Joined:
    Jun 1, 2019
    Posts:
    23
    No it's not right . Coordinates in local space are infinite in theory. and sometimes people combined different pieces of meshes into one giant mesh and its local coordinates are wild.