- Home /
Changing Part Of Texture/Material
I'm wondering if I can change the texture of part of an object if it enters a trigger? For example, I have a flat plane, and I put a trigger behind it. The plane has a metal texture on it. If I push part of the plane into the trigger, I want only that part of the texture that has entered the trigger to change to a water texture. Just that part only, the rest of the plane still has its metal texture. Is this even possible? If so, is it too intensive on the computer to work en masse?
I've looked into using:
Texture2D.GetPixels
Texture2D.SetPixels
Texture2D.ReadPixels
Texture2D.LoadImage
But I don't know how to use them to perform what I need. I was thinking if I can find whether or not the individual pixels have entered the bounds of the triggers, change them using LoadImage on those specific pixels. I'm not sure how that would translate into code. Can anyone put me on the right track?
I would try to get the positions of individual pixels, which depend on the mesh faces vertex positions, then, uv mapping, textureoffset and texturescale and loop through those pixels if they are contained within the trigger collider bounds. Possibly you could also play with create a special shader for that. Or you could setup two overlapping objects with the cutout shaders on them with inverse transparency maps - one with base metal texture and other with base water texture and then manipulate with textureoffset following the trigger position
Yeah that was what I was thinking, but I can't seem to find how to find the positions of individual pixels.
T think it might be easier and more efficient to do with shaders.
Answer by ScroodgeM · Aug 10, 2012 at 06:21 PM
sol 1
use paint mask to cover the object with textures. similar to terrain in unity. mask can be stored in other texture or vertex colors. need simple shader for it.sol 2
use projectors to draw another texture on top of your metalshader for sol 2
Shader "Projector/Unlit" { Properties { _Color ("Main Color", Color) = (1,1,1,1) _ShadowTex ("Cookie", 2D) = "" { TexGen ObjectLinear } _FalloffTex ("FallOff", 2D) = "" { TexGen ObjectLinear } } Subshader { Pass { ZWrite off Fog { Color (0, 0, 0) } Color [_Color] ColorMask RGB Blend One OneMinusSrcAlpha Offset -1, -1 SetTexture [_ShadowTex] { combine texture * primary, texture Matrix [_Projector] } SetTexture [_FalloffTex] { constantColor (0,0,0,1) combine previous lerp (texture) constant Matrix [_ProjectorClip] } } } }
sol 3
use two materials on one mesh to draw opaque metal first, and transparent other texture on top. changing transparency will show other texture on top of opaquesol 4
redraw textures. get a raycasthit for point of object, this gives you object's coords and texture's coords of hit. then get-set pixels. not recommended caused by performance reasonI tried using Solution 2 which makes perfect sense and actually gives me a lot of extra functionality, but what happens is that when part of it moves into the effect of the projector, the whole plane turns to that texture.
move plane with projectors to make projected image 'sticked' to plane. when whole plane turns to texture - you can replace plane's material's texture to this texture and remove all projectors
But that still doesn't solve the problem of making only part of the plane turn to that texture. As long as any part of the plane moves into the area of the projector, the whole plane is projected on, even parts that aren't in the projector's AO$$anonymous$$
but you can project only a small area on whole plane. just project a new texture on small part and you'll see only small part is changed
Its probably the shader I'm using. If I used a normal diffuse material on the projector it projects the whole plane, and the only one that works is Projector/$$anonymous$$ultiply but what I apply my own textures it doesn't even project since it deals with light/shadows and only takes black and white. What should I use?