Draw Target Object is a component for rendering a shadow of an object and its child objects onto a render texture at runtime. Add this component to a projector object.

Field Description
Target Set the target object of which you want render a shadow.
Render Children Set true if you want to render the children of the target as well.
Layer Mask Choose layers for children to be rendered (Applicable if Render Children is true)
Auto Detect Hierarchy Changes If enabled, changes of the hierarchy under the Target is checked every frame. If a Renderer is added/deleted/enabled/disabled, the command buffer in this component will be updated. Instead of enabling this property, you can manually call SetCommandBufferDirty() function when you add/delete/enable/disable a Renderer under the Target (recommended).
Texture Alignment Rotate the transform of the projector object so that the shadow image along specified axis of the target object can be aligned to Y-axis of a render texture.
Follow Target If true, the projector object will follow the target.
Update Function Choose a function where the transform of the projector will be updated. You don’t need to care about this field so much. You can always use OnPreCull. If you want to refer to the projector transform in Update or LateUpdate function of your script, you need to call UpdateTransform() of this component or SendMessage(“UpdateTransform”) to the projector object before you use the projector transform. In such case, it is a little bit better to choose UpdateTransform, otherwise the transform of the projector will be updated twice a frame. Fast Shadow Receiver components also refer to the projector transform. These components will send “UpdateTransform” message to the projector object in LateUpdate function. Please make sure that the transform of the target object is updated in Update function if it is required to update the transform of the projector in LateUpdate function.
Shadow Shader Choose a shader to render a shadow for opaque materials.
Replacement Shaders Set pairs of a render type and a shader to render a shadow for the render type.

Links

13 thoughts on “Draw Target Object component

  • 2015/05/28 at 6:01 PM
    Permalink

    I have crash when I have Dynamic Shadow Projector in scene and try delete last camera on level.

    Reply
    • 2015/05/28 at 8:26 PM
      Permalink

      Thank you for your feedback!
      What do you mean “last camera”? How did you delete it?

      Reply
      • 2016/07/05 at 11:34 AM
        Permalink

        Now I understood what you meant. Unity Editor crashes when all the cameras are deleted from a scene. I sent a bug report to Unity. Thanks!

        Reply
  • 2020/09/13 at 10:10 PM
    Permalink

    Hi,

    I have just upgraded to v1.1.4 and do not see the “Auto Detect Hierarchy Changes” field.

    I originally upgraded because I saw in the changelog that:

    – Fix the bug whereby disabled meshes casted shadows.

    I am still seeing disabled meshes casting shadows as well though.

    It’s almost like I’m not seeing any of the v1.1.4 features even though I should be on that version… Any ideas?

    Thanks,

    Niall

    Reply
    • 2020/09/14 at 11:48 AM
      Permalink

      Hi,

      Can you check the line 51 in DrawTargetObject.cs? There must be the declaration of “Auto Detect Hierarchy Changes” field.

      Also, “AddDrawCommandForGameObject” function at line 325 must have disabled mesh check like this:

      Reply
      • 2020/09/14 at 4:09 PM
        Permalink

        How are you formatting the code like that by the way? I tried using in my reply below but it looks very different!

        Reply
        • 2020/09/14 at 4:41 PM
          Permalink

          Hi, this site has a wordpress plugin that can format code and highlight syntax between <pre> and </pre> like above.

          Reply
  • 2020/09/14 at 4:08 PM
    Permalink

    Hi,

    I have just done a fresh import into a new project and I’m still not seeing the new changes:


    [SerializeField]
    private bool m_renderChildren = true;
    [SerializeField]
    private bool m_followTarget = true;

    // public properties
    public Transform target
    {
    get { return m_target; }
    set {
    if (m_target != value) {
    m_target = value;
    SetCommandBufferDirty();
    }
    }
    }


    void AddDrawCommandForGameObject(GameObject obj, bool recursive)
    {
    Renderer renderer = obj.GetComponent();
    if (renderer != null)
    {
    if ((m_layerMask & (1 << renderer.gameObject.layer)) != 0) {
    int materialCount = m_replacementShaders == null ? 0 : m_replacementShaders.Length;
    for (int i = -1; i < materialCount; ++i) {
    AddDrawCommand (renderer, i);
    }
    }
    }
    else if ((!recursive) && (Debug.isDebugBuild || Application.isEditor))
    {
    Debug.LogError("The target object does not have a Renderer component!", m_target);
    }
    if (recursive)
    {
    Transform t = obj.transform;
    for (int i = 0, count = t.childCount; i < count; ++i)
    {
    AddDrawCommandForGameObject(t.GetChild(i).gameObject, recursive);
    }
    }
    }

    I am running 2018.4.23, I don’t suppose the new version is somehow locked to a different Unity version?

    Thanks,

    Niall

    Reply
    • 2020/09/14 at 4:17 PM
      Permalink

      Hi,

      I have solved this by deleting the .unityPackage in C:\Users\\AppData\Roaming\Unity\Asset Store-5.x\ and re-downloading from the Asset Store. It prompted me for my username/[password at that point so maybe that was the real cause all along. It’s odd that it just imported twice (in separate projects) rather than downloading and importing (and prompting if necessary) as I’d expect.

      Thanks for your quick reply anyway!

      All the best,

      Niall

      Reply
  • 2022/09/09 at 12:18 PM
    Permalink

    I try add this component and set target in playing mode,but the shadow isn’t show on both of scene and shadow texture

    Reply
      • 2022/09/13 at 10:53 AM
        Permalink

        Hi,I had imported them, but it Still useless,here is my code, can you help check it?Thank you.

        public void DrawSomeOneShadow(GameObject go,Material shadowMat = null,bool follow = false)
        {
        if (go == null)
        {
        return;
        }

        string objName = go.name;

        if (_hadDrewMap.ContainsKey(objName))
        {
        #if UNITY_EDITOR
        LoggerManager.GetLogger(ESystemType.Common).LogError($"{objName} had drew, can't draw its shadow again!");
        #endif
        return;
        }

        DrawTargetObject drawTargetObject = gameObject.AddComponent();
        drawTargetObject.shadowShader = shadowMat == null ? UIUtil.GetShadowMat("ShadowOpaque") : shadowMat;
        drawTargetObject.replacementShaders = new DrawTargetObject.ReplaceShader[2];
        drawTargetObject.replacementShaders[0].renderType = "Transparent";
        drawTargetObject.replacementShaders[0].shader = Shader.Find("DynamicShadowProjector/Shadow/Transparent");
        drawTargetObject.replacementShaders[1].renderType = "TransparentCutout";
        drawTargetObject.replacementShaders[1].shader = Shader.Find("DynamicShadowProjector/Shadow/Transparent Cutout");
        drawTargetObject.followTarget = follow;
        drawTargetObject.target = go.transform;

        _hadDrewMap[objName] = drawTargetObject;
        drawTargetObject.UpdateCommandBuffer();
        drawTargetObject.UpdateTransform();
        }

        Reply
        • 2022/09/14 at 11:41 AM
          Permalink

          I resolve it, beacause of i add drawtargetobject component during the play mode, so the drawTargetObject property of shadowDrawTexture is not assign.

          Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Anti Spam Code *