在Graphics.Blit中传递每个对象的着色器参数?

时间:2018-07-13 18:56:18

标签: unity3d shader

我有一个传递给Graphics.Blit的轮廓着色器,它以某种颜色绘制轮廓。 我将如何使某些对象使用不同的轮廓颜色?我正在想象将制服传递给该对象的着色器并以某种方式在我的轮廓着色器中引用它的方法,但是我不确定如何!< / p>

void Start () {
    edgeDetectMaterial = new Material (Shader.Find ("Hidden/EdgeDetectColors"));
    edgeDetectMaterial.SetVector ("_Color", Color.red);
}

void OnRenderImage (RenderTexture source, RenderTexture destination) {
    Graphics.Blit (source, destination, edgeDetectMaterial);
}

1 个答案:

答案 0 :(得分:0)

我前一阵子做了,对不起,不好的编码和响应 我上次这样做的方法是使用命令缓冲区,在该命令缓冲区中,您将获得一个对象,其中包含要概述的颜色信息以及对渲染器的引用。

public class OutlineObj
{
    public Color outlineColor;
    private Color currentColor;
    public Color targetColor;
    public float speed;
    public Renderer[] renderers;
    public void Outline ()
    {
        currentColor = targetColor;
    }
}

对于执行轮廓的主着色器,您只需要获取对象(相机)的顶点并将“轮廓”颜色设置为全局变量即可。

v2f vert (appdata v)
{
    v2f o;
    o.vertex = UnityObjectToClipPos (v.vertex);
    return o;
}

fixed4 _OutlineColor;

fixed4 frag (v2f i) : SV_Target
{
    return _OutlineColor;
}

然后是一个控制器,您可以在其中注册对象并构建命令缓冲区,我记不清所有内容,但是几乎您必须清除命令缓冲区,在图像效果之前获取渲染纹理,然后设置渲染目标,清除目标并使用

设置所需的颜色
public class OutlineController
{
    private CommandBuffer buffer;
    public List<OutlineObj> registerObjects;    

    private void Awake ()
    {
        //GetAll the shaders and their ID
        //Add buffer to camera as before image effects.
    }

    public void Register (OutlineObj obj)
    {
        //register the object
        registerObjects.Add(obj);
    }

private void BuildCommandBuffer ()
{
    //Clear command buffer
    //Get the RT and set the render target
    //Per every object that wants an outline add their color and renders
    foreach (OutlineObj obj in registerObjects)
    {
        buffer.SetGlobalColor (outlieColor, obj.currentColor);
        for (int i = 0; i < obj.renderers.Length; ++i)
        {
            buffer.DrawRenderer(obj.renderers[i], controllerMat);
        }
    }
    //Set the renderPassTexture on the buffers blit
}
    private void Update ()
    {
        BuildCommandBuffer ();
    }
}

希望它会有所帮助,研究如何使用命令缓冲区来制作复合轮廓。