Search Unity

Painting a texture ingame

Discussion in 'General Graphics' started by wabuilderman, May 2, 2015.

  1. wabuilderman

    wabuilderman

    Joined:
    Jul 30, 2014
    Posts:
    46
    Ok, so what I want is to paint a texture over the skin of a mesh. This texture would override the previous coloring of the mesh, but only in areas around a ray-casted point.

    I.e. ray-cast his point blah blah blah, you create a circle of effect around it, (fading off around the edges preferably, though not necessary), and then paint a assigned texture.

    This would be used for an effect like this:


    (For those who recognize this, this is Kirito from SAO II, after getting slapped REALLY hard)

    All I want is to be able to place a texture over the currently existing one, in game, and dynamically.

    How would I go about doing this? (Also, is this under the wrong forum?)
     
  2. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
  3. wabuilderman

    wabuilderman

    Joined:
    Jul 30, 2014
    Posts:
    46
    Sorry, but I don't have 95 dollars to spend. I was just wondering how to paint a texture onto an object dynamically. Does it really take spending money to achieve just that?
     
    NeverTrustShadows likes this.
  4. wabuilderman

    wabuilderman

    Joined:
    Jul 30, 2014
    Posts:
    46
    *bump* still not solved.
     
  5. timochu

    timochu

    Joined:
    May 3, 2015
    Posts:
    1
    Hey wabuilderman, I think I might have the solution for you.

    Here's an article about command buffers: http://blogs.unity3d.com/2015/02/06/extending-unity-5-rendering-pipeline-command-buffers/

    What you want to pay close attention to is deferred decals.

    At the end there, there's a sample project, download it and look into the deferred decals scene. It's like a couple of scripts that enable you to project textures onto surfaces.

    Hope that helps. :)
     
  6. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    It's can't be solution for skinned meshes.
     
  7. Zicandar

    Zicandar

    Joined:
    Feb 10, 2014
    Posts:
    388
    The standard shader has it's detail texture slot?
    Either use that or create a variation of the standard shader (or whatever shader your using).
    However your likely to run into problems that are related to uv seams, and uv's not being evenly sized ect.
    If you know Where this texture will be placed in UV space and can prepare your texture to fit, then it's generally quite simple. If you can't, well what we do is use a secondary uv set, but one that is generated (in maya) using cylinder mapping + some manual work to make it fill out to the edges, make arms placed somewhat ok ect...
     
  8. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    Version 4.0 or 3.5 includes a demo project "Boot camp". You can get free decals in there. Rather, they was not work in new versions(it's can be fixed), and for sure you need to add support for skinned your own.
     
  9. ThomasCreate

    ThomasCreate

    Joined:
    Feb 23, 2015
    Posts:
    81
    It's also possible to paint onto a texture using Graphics.Blit(). Basically, you raycast to your model, get the uv coordinate from the RaycastHit, and .Blit() a circle of the texture you want to apply onto the texture of the model (or onto a separate texture that gets overlayed onto the model, if you need the functionality that that would give). The texture has to be a RenderTexture, but if you do some reading on the subject I'm positive you can make it work.
     
    SAAARGE and theANMATOR2b like this.
  10. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    ThomasCreate, it's ok for single point, but OP want decal like thing, how to calculate uv.
     
  11. wabuilderman

    wabuilderman

    Joined:
    Jul 30, 2014
    Posts:
    46
    Umm... I was looking at the Blit, but I can't figure out how to use it in this context... can someone tell me how I would use uv coordinates with Blit? also, I am fine with drawing a million points, so long as it isn't lag-endusing.
     
  12. ThomasCreate

    ThomasCreate

    Joined:
    Feb 23, 2015
    Posts:
    81
    Oh, it's Graphics.DrawTexture(), my bad. In short, it goes like this:

    Code (CSharp):
    1.  
    2. //Activate the render texture (the one that's on your model)
    3. RenderTexture.active = _renderTexture;
    4. //Work in the pixel matrix of the texture resolution.
    5. GL.PushMatrix();
    6. GL.LoadPixelMatrix(0, _textureResolution, _textureResolution, 0);
    7.  
    8. //Paint the desired texture onto the desired coordinates with the desired size.
    9. //the "hit" variable is the RaycastHit
    10. Vector2 coord = new Vector2(hit.textureCoord.x * _textureResolution, _textureResolution - hit.textureCoord.y * _textureResolution);
    11. Graphics.DrawTexture(new Rect(coord.x - brushSize / 2, (coord.y - brushSize / 2), brushSize, brushSize), brush);
    12.  
    13. //Revert the matrix and active render texture.
    14. GL.PopMatrix();
    15. RenderTexture.active = null;
    16.  
    Drawing "a million" points will be lag inducing though - I noticed that myself when drawing lines using this method. I have an idea brewing for painting lines with the help of a shader, but I've yet to have the time to implement it. If I do, I'll get back to you with that. ;)

    Also, you have to be aware of the fact that because you're drawing directly onto the UV map of the model, you can accidentally colour in an area that's next to another on the unwrap, but somewhere completely different on the model. I accounted for that by creating a separate colour map that has the same colour for areas on the UV map that are physically connected, and checking that in the script that paints lines.
     
    Last edited: May 7, 2015
    Propagant likes this.
  13. wabuilderman

    wabuilderman

    Joined:
    Jul 30, 2014
    Posts:
    46
    That "Bleeding into another UV coordinate" problem should be pretty minimal, since I plan on having fairly narrow lines. But seams will pretty much always be a bit of a problem. It shouldn't be to bad though. Also, thanks. =)
     
  14. ThomasCreate

    ThomasCreate

    Joined:
    Feb 23, 2015
    Posts:
    81
    If you intend on drawing a pattern like the one in your original post, I would strongly advise you not to do that by drawing each line separately using this method. Instead, draw with a big, feathered brush onto a texture map that just serves as an "alpha layer" of sorts for a shader that applies that texture on the painted areas. Drawing narrow lines point by point on the CPU is very, very slow.
     
  15. wabuilderman

    wabuilderman

    Joined:
    Jul 30, 2014
    Posts:
    46
    I wasn't planning on making lines like that. I was mostly just planning on using a pre-made grid texture. Aligning it to the camera is just not necessary, especially for what it would cost in performance. (o_O) I know enough about the 0.1 frames per second moments to know how deadly some rendering techniques can be (whether it be 3d rendering or 2d rendering).
     
  16. ThomasCreate

    ThomasCreate

    Joined:
    Feb 23, 2015
    Posts:
    81
    If you apply the grid texture in a shader, there would be no performance cost to camera alignment. Check out the "detail texture in screen space" segment here for a sample.