Search Unity

Script access to vertex colors in a mesh

Discussion in 'Editor & General Support' started by RockHound, Sep 20, 2006.

  1. RockHound

    RockHound

    Joined:
    Apr 14, 2006
    Posts:
    132
    I am trying to attenuate my character's shading based on its location in a room.

    More specifically: The room in question has "baked lighting" stored in the mesh vertex colors, and we modulate the room's texture color with the baked lighting via "BindChannels". I would like to also modulate my character's color using the room mesh's vertex colors.

    My intention is to render the floor under the character's feet in a separate render-to-texture camera (ortho, 1x1 pixel). That render would shade the floor using only vertex colors. I could then modify the character's texture using the rendered texture via "combine previous * texture". I haven't done this yet, but it seems like it should work.

    However, this approach requires multi-texturing graphics cards (as listed in the manual), and so is not as general a solution.

    Is there a better way? And has this question been addressed before in the forum?

    Ideally, I'd like something like this: a RaycastHit object (or something like it) retuns not only collision info, but mesh info as well, such as the triangle that was intersected. From that I could access vertices and their colors, do my own interpolation, and get my color. However, I haven't found any such capability in Unity; in fact I can't find any way to access Mesh objects of imported assets at all.

    Thanks for any insights or suggestions on what I'm trying to do.
     
  2. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    The render texture approach should work. I would not worry much about multi-texturing cards (anyways, I think all cards that can do rendertextures also have multi-texturing; i.e. at least two textures per pass). For extremely old hardware, you can just write a shader that does not tint the player character, right?

    The mesh access interface is documented here. You either access a mesh of existing object, or expose a variable in your script of Mesh type and assign the mesh directly to the script.

    A raycast returning mesh face plus more info (like barycentric coordinates on the triangle) would be useful, sure. We'll add that in the future.
     
  3. RockHound

    RockHound

    Joined:
    Apr 14, 2006
    Posts:
    132
    Aras, thank you for the info.

    I had missed the mesh access interface. It probably wouldn't be too time consuming to write up a bounding volume hierarchy class for a Mesh object and do my own intersection queries. For the time being though, I'll likely use render textures.
     
  4. RockHound

    RockHound

    Joined:
    Apr 14, 2006
    Posts:
    132
    Could someone briefly review the process of using a render-to-texture image in a shader? Despite picking through the manual and the CharacterShadow.cs script, I seem to be missing something. Here is how I do it:

    I create a second camera (besides Main Camera), and I initialize and orient it. By setting the viewport, I can see during run-time that it is rendering what I want.

    In script (Awake) I create a RenderTexture object, and assign it to the second camera's targetTexture. In Awake() I also call renderer.material.SetTexture() on the shaded object using the rendered texture. I think this should work, but unfortunately it appears that the texture is not being rendered into; it contains random/noisy data.

    Oddly, if I set the Main Camera's targetTexture to the RenderTexture, the process works (from the Scene View, 'cuz the Game View isn't rendered on-screen), so I am assuming that the RenderTexture object is created correctly and that my shader sees the correct rendered texture. But the process won't work for my second camera.

    I dislike airing my bugs to the public, but at the moment I am at a loss with this. Thank you for any input.
     
  5. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    First thing I'd try is not creating a render texture dynamically, instead create a small RT in the project, then you can see it's contents in the inspector. The other thing to watch out is that by default textures created from scripts are non-power-of-two (i.e. RECT textures) so they might need a special shader.
     
  6. RockHound

    RockHound

    Joined:
    Apr 14, 2006
    Posts:
    132
    Thanks (again) Aras. I couldn't get the render texture contents in the inspector, so I set is as the texture map to a dummy cube. Using your suggestion, I immediately saw my problem--I forgot to change my viewport back to (0,0,1,1) after setting the camera's output to the render texture.