Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Making certain polgyons/ vertices on mesh transparent.

Discussion in 'Scripting' started by CorWilson, Sep 26, 2014.

  1. CorWilson

    CorWilson

    Joined:
    Oct 3, 2012
    Posts:
    93
    I'm currently producing a mesh in script for procedural generations reasons. For part of the mesh, I want to create water. How would I go about making part of it have lower transparency to simulate the look of it?
     
  2. Jonny-Roy

    Jonny-Roy

    Joined:
    May 29, 2013
    Posts:
    666
    There is quite a few ways to achieve this depending on what exactly you are trying to do. But I would start with setting the alpha of each vertex then using a custom shader to render it. Although if you are only having a small portion of the mesh transparent, you'd be better off having a separate mesh.
     
  3. CorWilson

    CorWilson

    Joined:
    Oct 3, 2012
    Posts:
    93
    I'm not that savvy with shader code. Even more, I wouldn't exactly know how to go about making certain vertices a certain alpha, unless it's like OPenGL and DirectX where you can temporarily change the color to use on following vertices. And I was thinking a separate mesh, but I sort of don't want to go that option unless it's the last. It's for coding reasons.
     
  4. Jonny-Roy

    Jonny-Roy

    Joined:
    May 29, 2013
    Posts:
    666
    When you're creating the procedural mesh just set the colors property of the mesh, so:

    mesh.colors=new Color[]{
    etc
    };

    For the shader you'd want to take the alpha blended surface shader and look at modifying it, but generally you'll get much better performance using two meshes, rendering alpha is much slower, and depending on what you're doing you'll start to hit sorting issues.
     
  5. CorWilson

    CorWilson

    Joined:
    Oct 3, 2012
    Posts:
    93
    Yeah because the way I do it, I set up vertices in a list first, then in a separate function that's called during update, the new vertices and uvs are added to the mesh. So there's little way for the other function to know whether the next vertices are water-based or not. I have a matrix to keep track of what vertices are where, but I can only wonder if this will be a headache later on.
     
  6. Jonny-Roy

    Jonny-Roy

    Joined:
    May 29, 2013
    Posts:
    666
    I think it will, create a class that tracks it all, then have an update function that creates the different meshes needed, it will be a little work before you get that basic results, but long term will be way more flexible.
     
  7. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    If I understand correctly, you want to apply multiple materials to a mesh.

    To apply multiple materials to a mesh, the mesh is divided into several sub-meshes using Mesh.SetTriangles.

    After, assign materials in the renderer component in the same order as the sub-meshes.