x


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?

more ▼

asked Aug 10 '12 at 05:40 AM

Overlord gravatar image

Overlord
484 66 78 88

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

Aug 10 '12 at 07:11 AM tomekkie2

Yeah that was what I was thinking, but I can't seem to find how to find the positions of individual pixels.

Aug 10 '12 at 05:43 PM Overlord

T think it might be easier and more efficient to do with shaders.

Aug 10 '12 at 05:49 PM tomekkie2
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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 metal

shader 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 opaque

sol 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 reason

more ▼

answered Aug 10 '12 at 06:21 PM

ScroodgeM gravatar image

ScroodgeM
7.6k 2 6

I 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.

Aug 10 '12 at 06:54 PM Overlord

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

Aug 10 '12 at 07:13 PM ScroodgeM

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 AOE.

Aug 10 '12 at 07:25 PM Overlord

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

Aug 10 '12 at 08:14 PM ScroodgeM

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/Multiply 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?

Aug 10 '12 at 08:32 PM Overlord
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2194
x982
x374
x354
x83

asked: Aug 10 '12 at 05:40 AM

Seen: 870 times

Last Updated: Aug 12 '12 at 12:39 AM