Object seems to be scaled twice by my custom vertex shader

Hi everyone,

I am trying to tackle shaders and I want to create a simple “swing in the wind” shader for my foliage. Basically I want to transform the vertices with a sin() depending on their height in the object, to give them the swinging motion. This works fine already, but during the matrix calculations I seem to be scaling the object twice, and I have no idea why.

This is my vertex shader function.

void vert (inout appdata_full v) {
	float4 wpos = mul( _Object2World, v.vertex);
	float4 opos = mul( _Object2World, float4(0,0,0,1) );
	float phase = _Time * 25.0 + opos.x * 0.6 + opos.z * 0.05 ;

	wpos.x = wpos.x + sin(phase) * wpos.y * _Bend;

	v.vertex = mul(_World2Object, wpos);
}

I use the object position to determine the phase so that the whole objects swings in unison. I also make the calculations in world space so that the rotation of the object does not affect the swing direction.

The problem I have is this:

5212-shaderscaling.png

The mushroom is scaled inside the editor to 0.3, 0.3, 0.3. The desired position is shown by the wireframe which appears when I select the object. But the shader seems to apply the scaling again, making the object to small. Is there a way to fix this?

Thanks for your help.

Alright, I found the solution to this problem.

The correct shader just adds one line at the end of the vert() function

void vert (inout appdata_full v) {
    float4 wpos = mul( _Object2World, v.vertex);
    float4 opos = mul( _Object2World, float4(0,0,0,1) );
    float phase = _Time * 25.0 + opos.x * 0.6 + opos.z * 0.05 ;

    wpos.x = wpos.x + sin(phase) * wpos.y * _Bend;

    v.vertex = mul(_World2Object, wpos );
    v.vertex.xyz *= unity_Scale.w;
}

Sorry that I’ve used this system to basically brainstorm with myself. But I hope the answer helps somebody in the future.

It helped me, exactly same problem with shaders, thank you!