Search Unity

Multiple Shader Conflict

Discussion in 'Shaders' started by Predster, May 21, 2008.

  1. Predster

    Predster

    Joined:
    Sep 11, 2007
    Posts:
    145
    I'm really new at shaders, so I'm not sure if I'm doing things correctly and am hoping for some help.

    Right now, I have a model with 2 materials on it: 1 material using the bumped diffuse skin shader from the wiki, and the other material using the built-in specular shader. It looks wonderful on my iMac, but when I test it on a PC or on my macbook, I get the artifacts shown below. :x

    From what I gather, there are multiple passes fighting with one another, and it must only show up on certain graphics cards. Is there any way to fix this? I'd like to use pixel lighting. Can I use multiple shaders/materials on a model or do I need to program one shader to do everything?

    Thanks!
     

    Attached Files:

  2. cblarsen

    cblarsen

    Joined:
    Mar 10, 2007
    Posts:
    266
    Are you using two different shaders on polygons in the same exact position on the screen?
    If that is the case, then you have Z-buffer infighting, and you probably need to draw one of the shaders at an offset, so you are sure which one gets drawn first.

    If you want to have two kinds of pixel lighting on the same polygon, you need to write a combined shader.
    But that might not be as hard as it sounds, if you use the "UsePass" command.

    Or is the presence of material B affecting the rendering of polygons of material A. And the polygons are not overlapping?
     
  3. Predster

    Predster

    Joined:
    Sep 11, 2007
    Posts:
    145
    Yeah, I'm using two different shaders on polygons in the same position. I wasn't sure if that would be problematic.

    I'll have to try writing a combined shader. Just out of curiosity, can the same polygon have a pixel lit shader and vertex lit shader?



    Thanks
     
  4. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
  5. Predster

    Predster

    Joined:
    Sep 11, 2007
    Posts:
    145
    Yup, but I was running windows on my mac via bootcamp and couldn't figure out the mac keyboard equivalent to the printscreen button :oops:
     
  6. cblarsen

    cblarsen

    Joined:
    Mar 10, 2007
    Posts:
    266
    Take the following advice with a grain of salt. I am not used to writing simple shaders like this, only complicated ones :)

    Unless you are drawing the mesh directly, using the DrawMesh function, you can only have one material and therefore one shader per polygon. (I don't think DrawMesh works with skinned meshes)

    You can have two polygons in the same position (like you are doing now) By changing the shaders so that they belong to different queues, such as "Geometry" and "Transparent" you can be sure, that they are drawn in a particular order.
    However, if the shader drawn last was originally designed to be used as a geometry shader (i.e) no transparency, it will overwrite whatever the previous shader did.

    What you probably need to do is make a shader that

    - starts with a vertex/ambient pass. This should set the "base" colour of your surface.
    - reuses the pixel lighting passes from the shaders you want to combine, using a UsePass command for each
    pass, that you want to "borrow".
    Pixel lighting passes will usually work additively (i.e. add their lighting on top of what is already there, instead of overwriting completely, like a vertex or ambient pass)
     
  7. Predster

    Predster

    Joined:
    Sep 11, 2007
    Posts:
    145
    Wow! I'm used to working in Maya where you just layer things galore! I basically wanted to create a fleshy-looking surface and was using the skin/bumped/diffuse shader but it wasn't looking how I wanted, so I added another material with the built-in shader.

    To get this same effect: Could I potentially start with the pass that would be equivalent to the built-in diffuse shader, then use the pixel lighting passes from the skin shader? Can I set the fallback to be the diffuse?

    Now-- to just figure out how to do this :eek: !
     
  8. cblarsen

    cblarsen

    Joined:
    Mar 10, 2007
    Posts:
    266
    Without having looked a the source for either shader - yes, you can combine them that way.
    And you can set the fallback to any shader you want.
     
  9. Predster

    Predster

    Joined:
    Sep 11, 2007
    Posts:
    145
    Having never programmed a shader or used Cg, I am feeling a little overwhelmed and I was wondering if anyone could give me a little help combining shaders.

    The first image is the look I'd like to achieve: It's a model with two materials (one with the built-in diffuse shader, one with the skin/bumped diffuse), but they don't render properly on all machines.

    The second image is the model with the skin/bumped diffuse shader (from the wiki), which I wasn't happy with.


    So, I'd like to combine the pixel-lighting passes from both the shaders to get this effect, but I am so lost on how to do it. Can anyone help?
     

    Attached Files:

  10. cblarsen

    cblarsen

    Joined:
    Mar 10, 2007
    Posts:
    266
    I assume that the skinshader from the wiki you are talking about is this one:
    http://www.unifycommunity.com/wiki/index.php?title=SkinShader2

    I have looked at the shader code and compared it with the normal diffuse shader, and from a technical viewpoint, the skin shader wouldn't become better by having an extra diffuse pass added. You should already be able to tweak the diffuse colour of the skin shader a lot.

    The skin shader takes two special textures as its last two arguments. I am not an artist, but I think that by modifying them, you can get it to work.

    The Rim ramp
    The RGB part of the Rim ramp controls how the diffuse colour changes depending on the angle between the surface normal and the viewing direction.
    So if you are looking at the surface almost edge-on, the colour from the left part of the ramp will be used. If the surface is facing directly towards the camera, the right part of the ramp will be used.

    The RGB part of the rim ramp can be used to turn the lighting both up or down. It will be neutral if set to 1/4 intensity, which is what the default ramp that comes with the shader does for the part of the ramp that colours surfaces facing the camera.

    The alpha channel of the rim ramp can be used to change the specular intensitry in the same way.

    The wrap ramp
    The wrap ramp controls how the diffuse lighting changes depending on the angle between the surface and the light source. If the surface is facing completely away from the light, the left part of the ramp will be used. If the surface is facing directly towards the light, the right part of the ramp will be used.

    Hope this helps. Otherwise you should probably hope one of the artist dudes drops by.
     
  11. Predster

    Predster

    Joined:
    Sep 11, 2007
    Posts:
    145
    Thanks! If I can fool around with these settings to get the same result, and it will be easier! I'll give it a try.