Search Unity

How do I access the "source mesh" of a geometry shader?

Discussion in 'Shaders' started by LizardBrain451, Jul 16, 2019.

  1. LizardBrain451

    LizardBrain451

    Joined:
    Apr 22, 2019
    Posts:
    10
    I am working on a shader that creates geometry per vertex and I finally have the geometry part working. However, what I want to do now is apply a texture map to it.
    When I attempted this, it placed the geometry that I created at each vertex, but then it applied the entire _MainTex texture to each of the new geometries (spheres), which wasn't what I was expecting.

    What I am trying to do is sample the color at a given uv coordinate based upon the "source" mesh. To clarify, if I have a plane with a ten by ten grid, and I place a sphere at each vertex, I want to be able to color all 100 spheres, by making a single texture map and applying it to the grid. The spheres would generate their color value based upon a textures UVs coords, and allow me to recreate a photographic image on the spheres (assume the plane is laying flat and the us are 0-1). My question is it possible to access the "source mesh" on a geometry shader and apply it to run time created geometry? I couldn't readily find any examples that mimic the effect I was going for. But it would be like using a photograph (texture), to drive the colors on a digital lite-brite, I am not interested in coloring the individual lights with an array,

    upload_2019-7-15_20-8-38.png
    I realize that this is a really broad question, but I am not seeking a specific solution, as much as the knowledge of how the relationships within the geometry shader work.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    6,994
    Presumably you'd want the UV coordinate of the vertex you're spawning the sphere at? In that case you just need to pass that value from the vertex shader to the geomerty shader. The geomerty shader can only access data that the vertex shader passed to it, and only as many vertices as the current geometry shader is set to receive (one or three depending on point or tri).
     
  3. LizardBrain451

    LizardBrain451

    Joined:
    Apr 22, 2019
    Posts:
    10
    That worked. Thanks bgolus!