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

Terrain tree baked shadows?

Discussion in 'Editor & General Support' started by CloudyVR, Nov 9, 2017.

  1. CloudyVR

    CloudyVR

    Joined:
    Mar 26, 2017
    Posts:
    709
    I need a way to have my terrain trees cast shadows on the terrain's lightmap when baking but I can not find any way to make it work in Unity 2017.

    I was thinking of making some static "stand in" geometry that I can use for baking purposes only.

    But I was really hoping someone else might have better ideas?

    If the "stand in" geometry approach is the only way, then how can I find the position/rotation/tree type and tree size for each terrain tree on a specified terrain so I can write a script for instantiating and orienting the "stand in" geometry.

    Thanks
     
  2. CloudyVR

    CloudyVR

    Joined:
    Mar 26, 2017
    Posts:
    709
    I think I just answered my second question, and have found a way to quickly generate static "stand in" tree objects for producing shadows.
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEditor;
    5.  
    6. // Replaces Unity terrain trees with prefab GameObject. // http://answers.unity3d.com/questions/723266/converting-all-terrain-trees-to-gameobjects.html
    7. [ExecuteInEditMode] public class TreeReplacer : EditorWindow {
    8.  
    9.    [Header("References")]
    10.    public Terrain _terrain;
    11.    //============================================
    12.    [MenuItem("Window/My/TreeReplacer")]
    13.    static void Init()
    14.    {
    15.        TreeReplacer window = (TreeReplacer)GetWindow(typeof(TreeReplacer));
    16.    }
    17.    void OnGUI()
    18.    {
    19.        _terrain = (Terrain)EditorGUILayout.ObjectField(_terrain, typeof(Terrain), true);
    20.        if (GUILayout.Button("Convert to objects"))
    21.        {
    22.            Convert();
    23.        }
    24.        if (GUILayout.Button("Clear generated trees"))
    25.        {
    26.            Clear();
    27.        }
    28.    }
    29.    //============================================
    30.    public void Convert()
    31.    {
    32.        TerrainData data = _terrain.terrainData;
    33.        float width = data.size.x;
    34.        float height = data.size.z;
    35.        float y = data.size.y;
    36.        // Create parent
    37.        GameObject parent = GameObject.Find("TREES_GENERATED");
    38.        if (parent == null)
    39.        {
    40.            parent = new GameObject("TREES_GENERATED");
    41.        }
    42.  
    43.        // Create tree objects
    44.        foreach (TreeInstance tree in data.treeInstances)
    45.        {
    46.            Vector3 position = new Vector3(tree.position.x * width, tree.position.y * y, tree.position.z * height);
    47.            var _tree = data.treePrototypes[tree.prototypeIndex].prefab;
    48.            Instantiate(_tree, position, Quaternion.identity, parent.transform);
    49.        }
    50.  
    51.        parent.transform.parent = _terrain.transform;
    52.        parent.transform.localPosition = Vector3.zero;
    53.        parent.transform.localRotation = Quaternion.identity;
    54.    }
    55.    public void Clear()
    56.    {
    57.        DestroyImmediate(GameObject.Find("TREES_GENERATED"));
    58.    }
    59. }
    The script should iterate over each terrain tree on a given terrain, then create a bunch of identical trees as objects in the scene (which can be deleted after baking). Just be sure the tree prefabs are set to static. After baking there appears to be faint shadows from the trees on the terrain now.

    I'm attempting to adjust the texels resolution of my lightmap parameters to a higher value and hoping that makes them a little sharper/darker. Either way, progress...

    [UPDATE]
    The higher texel resolution on my terrain helped, but the shadows are still very faint. I have a feeling that my terrain is still too large, as even a standard cube barely creates a darkened area (even if placed right over the terrain) and there is still light underneath it.

    [UPDATE]
    I made my terrain much smaller and used lightmap parameters: Default-HightResolution and the results look way better:

    [Left = terrain tree - realtime shadow] [Right = tree object - baked shadow]
     
    Last edited: Nov 9, 2017
  3. Shinobi1507

    Shinobi1507

    Joined:
    Sep 8, 2010
    Posts:
    220
    Did we ever find out if this is really needed? Is that really not possible anymore?

    Edit: Yes, I know, necro thread. Sorry
     
  4. magmagma

    magmagma

    Joined:
    Oct 27, 2017
    Posts:
    41
  5. Shinobi1507

    Shinobi1507

    Joined:
    Sep 8, 2010
    Posts:
    220
    I wish they would give more insight into that choice. You can't just have a regression like that and just say it's "by design". I'm not super salty since in the last year I've been getting around it but just wish I had some context to go with it.
     
    mowax74 and M_R_M like this.