Hello Guest

Author Topic: Blend2TexVertexColor on instances of objects  (Read 7540 times)

coshea

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 33
    • View Profile
Blend2TexVertexColor on instances of objects
« on: June 02, 2017, 09:50:17 am »

Hi

So I have a bunch of sprites in one atlas that uses BlendVertexColor.

Via script I would like to take some of those objects and change it to Blend2TexVertexColor, but still leaving the other objects having their single texture.

If I change any object material to 2tex shader, they all change of course.

I was wondering if you know any way I can do it with instancing, either setting the shader/material per object, or maybe all sprites using Blend2TexVertexColor but then setting the gradient texture per object instance?

I've just been reading about MaterialProperyBlocks, do you think that could work if I modify the shader?
http://thomasmountainborn.com/2016/05/25/materialpropertyblocks/

Thanks

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Blend2TexVertexColor on instances of objects
« Reply #1 on: June 05, 2017, 10:10:46 pm »
If you the material changes are per sprite type, you can easily set up material overrides on the sprite.
http://2dtoolkit.com/docs/latest/tutorial/multiple_materials_in_a_sprite_collection.html

This will only work if you want to group sprites in a collection with materials / or use unique ones on each sprite type.

If you want instance specific data, material parameter blocks will work, but you won't be able to change shaders with material property blocks.

coshea

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 33
    • View Profile
Re: Blend2TexVertexColor on instances of objects
« Reply #2 on: June 13, 2017, 05:30:55 pm »

Thanks for the reply.  I want to change it per sprite, not over a whole sheet (or group of sprites).

So I figure then that I could use the Blend2TexVertexColor shader for everything, but then use MaterialPropertyBlocks to set the overlay texture only on certain sprites, leaving the other ones empty?

Thing is with Blend2TexVertexColor is that the alpha from the gradient texture also effects the base texture alpha, so the base texture becomes transparent in those places. What I actually want to do is overlay my sprite (keeping the texture underneath the same alpha) with a texture on top that has some alpha in it. Does that make sense? Not the right shader to use?

Thanks

coshea

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 33
    • View Profile
Re: Blend2TexVertexColor on instances of objects
« Reply #3 on: June 22, 2017, 11:52:08 am »


Will any of your shaders allow me to achieve this shown above? Or could you recommend how to edit Blend2TexVertexColor so that alpha of the base sprite isn't effected by the gradient?

Thanks!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Blend2TexVertexColor on instances of objects
« Reply #4 on: July 21, 2017, 09:37:35 pm »
If you change the fragment shader to this you'll get the result you're after. You should create a copy of the shader, rename the title and change the contents as needed.

Code: [Select]
fixed4 frag(v2f_vctt i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.texcoord01.xy);
fixed4 grad = tex2D(_GradientTex, i.texcoord01.zw);
col.rgb *= grad.rgb;

return col * i.color;
}


coshea

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 33
    • View Profile
Re: Blend2TexVertexColor on instances of objects
« Reply #5 on: March 06, 2018, 02:11:16 pm »
Thanks.

I *think* it is working, although the gradient texture needs to be the same size as the atlas? If I have lots of small sprites in an Atlas, lets say 1024x1024, and a gradient texture that is only 100x100, when using this modification to the shader, it only works for sprites on the atlas in the first 100x100 of the atlas image. Is this because of the vertex position used for the gradient?

ideally you want to use the vertex pos from the atlas for the base sprite, but on the gradient texture starting at 0,0 rather than the atlas position.

Any idea how to fix that? Many thanks