| View previous topic :: View next topic |
shawnpigott

Joined: 28 Sep 2005 Posts: 213 Location: Vancouver Island Canada
|
Posted: Fri Oct 31, 2008 3:07 am Post subject: Old Unity Examples |
|
|
|
A while back there was an example project on the Unity website that showed how to paint directly onto objects. I think it may have used vertex colouring. In any event, I can't find it on the site and was wondering if anyone knew where to find a copy.
I'm hoping to modify it so that I can create the effects of scrubbing paint off of a wall. My idea is to have an alpha texture map that is slowly painted away to reveal an image map underneath. I'm not sure if I'm on the right track, but I thought that it would be a good place to start. |
|
| Back to top |
|
|
StarManta
Joined: 23 Oct 2006 Posts: 1522 Location: Columbus, OH
|
Posted: Fri Oct 31, 2008 4:29 am Post subject: |
|
|
|
I don't know where the example is, but it used RaycastHit.textureCoords to modify the texture itself (not vertex coloring). I think the RaycastHit documentation has some info that should get you started. _________________ Last Bastion Games
.... will be relaunching under a new name soon, with iPhone components! Watch this space. |
|
| Back to top |
|
|
drJones
Joined: 19 Oct 2005 Posts: 1342 Location: New Hampshire
|
Posted: Fri Oct 31, 2008 1:02 pm Post subject: |
|
|
|
its in the *old* procedural package, haven't checked if it works with 2.1 but this should be the script:
| Code: | /*
Allows you to paint on textures with arbitrary brushes.
* You have to use MeshColliders, otherwise the
* texture coordinates cant be determined.
* You have to map your textures uniquely (no repeating textures) and
* your textures may not use texture compression.
*/
// The decal texture we use to paint. Alpha of the texture can be used to fade in the decal.
var decalMap : Texture2D;
//
var opacity = 1.0;
// Tint's the color we paint into the texture
var tintColor = Color.white;
// Enable this when painting normal maps
var paintNormalMap = false;
var instantiateModifiedTextures = true;
private var instantiatedTextures = ArrayList ();
function ApplyDecal (uv : Vector2, target : Texture2D)
{
var pixelX : int = uv.x * target.width;
var pixelY : int = uv.y * target.height;
var unmodifiedMinX : int = pixelX - decalMap.width / 2;
var unmodifiedMinY : int = pixelY - decalMap.height / 2;
var minx : int = Mathf.Clamp(pixelX - decalMap.width / 2, 0, target.width);
var miny : int = Mathf.Clamp(pixelY - decalMap.height / 2, 0, target.width);
var maxX : int = Mathf.Clamp(pixelX + decalMap.width / 2, 0, target.width);
var maxY : int = Mathf.Clamp(pixelY + decalMap.height / 2, 0, target.height);
// Painting into empty an empty area? Don't do that
if (minx == maxX || miny == maxY)
{
target.Apply();
return;
}
// Read the pixels from the target texture
var srcPixels = target.GetPixels(minx, miny, maxX - minx, maxY - miny);
// Read the pixels from the decal texture
var decalPixels = decalMap.GetPixels(minx - unmodifiedMinX, miny - unmodifiedMinY, maxX - minx, maxY - miny);
// Paint the pixels
// We use the alpha of the decal to fade in the decal smoothly
for (var i=0;i<srcPixels.Length;i++)
{
var blendFactor = decalPixels[i].a * opacity;
var targetColor = tintColor * decalPixels[i];
srcPixels[i] = Color.Lerp(srcPixels[i], targetColor, blendFactor);
}
// Apply the pixels
target.SetPixels(minx, miny, maxX - minx, maxY - miny, srcPixels);
target.Apply();
}
function GrabTexture (renderer : Renderer)
{
// Get the bump map or diffuse texture
var paintTex : Texture2D;
if (paintNormalMap)
paintTex = renderer.sharedMaterial.GetTexture("_BumpMap") as Texture2D;
else
paintTex = renderer.sharedMaterial.mainTexture as Texture2D;
// Should we duplicate the texture before modifying it?
if (instantiateModifiedTextures)
{
if (instantiatedTextures.IndexOf(renderer) == -1)
{
var cloned : Texture2D;
if (paintTex != null)
{
cloned = new Texture2D (paintTex.width, paintTex.height);
cloned.SetPixels(paintTex.GetPixels());
paintTex = cloned;
}
else
{
cloned = new Texture2D (256, 256);
var colors = new Color[cloned.width * cloned.height];
var clearColor = Color.white;
if (paintNormalMap)
clearColor = Color (0.5,0.5,1,1);
for (var i=0;i<colors.length;i++)
colors[i] = clearColor;
cloned.SetPixels(colors);
paintTex = cloned;
}
if (paintNormalMap)
renderer.material.SetTexture("_BumpMap", paintTex);
else
renderer.material.mainTexture = paintTex;
}
}
return paintTex;
}
// Attach this script to a camera and it will paint black pixels in 3D
// on whatever the user clicks. Make sure the mesh you want to paint
// on has a mesh collider attached.
function Update () {
// Only when we press the mouse
if (!Input.GetMouseButton (0))
return;
// Only if we hit something, do we continue
var hit : RaycastHit;
if (!Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), hit))
return;
// Just in case, also make sure the collider has a renderer,
// material and texture. Also we should ignore primitive colliders.
var renderer : Renderer = hit.collider.renderer;
var meshCollider = hit.collider as MeshCollider;
if (renderer == null || renderer.sharedMaterial == null || meshCollider == null)
return;
var paintTex : Texture2D = GrabTexture(renderer);
// And just apply those pixels
if (paintTex != null)
ApplyDecal(hit.textureCoord, paintTex);
} |
_________________ -O |
|
| Back to top |
|
|
shawnpigott

Joined: 28 Sep 2005 Posts: 213 Location: Vancouver Island Canada
|
Posted: Fri Oct 31, 2008 10:58 pm Post subject: |
|
|
|
| This is great. I haven't tested it with 2.1 yet but (technically) it's exactly what I was looking for. Thanks for the help. |
|
| Back to top |
|
|
shawnpigott

Joined: 28 Sep 2005 Posts: 213 Location: Vancouver Island Canada
|
Posted: Sat Nov 01, 2008 8:06 am Post subject: |
|
|
|
I would appreciate some help with modifying the above script. I'm trying to get it to do the opposite of what it is currently doing. Instead of painting a new texture onto an object, I want to paint an alpha map onto an object so that you can see through it. Think scraping frost off of a window.
I think the code that needs modified is in this section.
| Code: | // Paint the pixels
// We use the alpha of the decal to fade in the decal smoothly
for (var i=0;i<srcPixels.Length;i++)
{
var blendFactor = decalPixels[i].a * opacity;
var targetColor = tintColor * decalPixels[i];
srcPixels[i] = Color.Lerp(srcPixels[i], targetColor, blendFactor);
}
// Apply the pixels
target.SetPixels(minx, miny, maxX - minx, maxY - miny, srcPixels);
target.Apply(); |
I've been playing with it but have had with no success so far. |
|
| Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|