Vertices Stay In Box

Hi, I’m trying to make a shader that contains vertices within specified bounds. I’ve gotten it to work with local positions, but I can’t figure out how to make the bounds world positions. I’m using _Min and _Max to define the bounds. You can see my attempt to change _Min and _Max to world positions where the commented lines are, but when I used them in the clamp function, the results looked the same. I’m trying to make it so that when I move the position of the object, the bounds don’t move with it.

Shader "Custom/Clamp"
{
	Properties
	{
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_Min ("Min", Vector) = (-1, -1, -1, 0)
		_Max ("Max", Vector) = (1, 1, 1, 0)
	}
	
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		CGPROGRAM
		#pragma surface surf Lambert vertex:vert

		sampler2D _MainTex;

		struct Input
		{
			float2 uv_MainTex;
		};
		
		float4 _Min;
		float4 _Max;
		void vert(inout appdata_full v)
		{
			//float3 localMin = mul(_World2Object, _Min).xyz;
			//float3 localMax = mul(_World2Object, _Max).xyz;
			v.vertex.xyz = clamp(v.vertex.xyz, _Min.xyz, _Max.xyz);
		}
		
		void surf (Input IN, inout SurfaceOutput o) {
			half4 c = tex2D (_MainTex, IN.uv_MainTex);
			o.Albedo = c.rgb;
			o.Alpha = c.a;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}

your approach seems correct to me.
Why did you comment it out?

float3 localMin = mul(_World2Object, _Min).xyz;
float3 localMax = mul(_World2Object, _Max).xyz;
v.vertex.xyz = clamp(v.vertex.xyz, localMin, localMax);

I tried it both ways, and they both yielded the same results. I can’t figure out why though. Here’s some images attached with localMin and localMax applied. I also added #include UnityCg.cginc. The expected results of moving it would be for example, in picture 01 the box would be visible, then flattened against the x wall in 02. By the way, I just image manipulated the Inspector and Hierarchy to be in more convenient places.


maybe you just forgot to put localMin and localMax in the clamp function and left it as _Min.xyz, _Max.xyz?
Not sure what else it could be. I will try it when I get home.

Yeah, I can’t figure out why this is wrong. I checked to make sure I’m not editing the wrong script and that I’m using localMin and localMax with clamp. Here’s my copy past just to make sure.

float4 _Min;
float4 _Max;
void vert(inout appdata_full v)
{
	float3 localMin = mul(_World2Object, _Min).xyz;
	float3 localMax = mul(_World2Object, _Max).xyz;
	v.vertex.xyz = clamp(v.vertex.xyz, localMin, localMax);
}

Uh, well hell. I changed the w coordinates from 0 to 1 and I think it’s working how it’s supposed to. Ug.