Terrain tree baked shadows?

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

I think I just answered my second question, and have found a way to quickly generate static “stand in” tree objects for producing shadows.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;

// Replaces Unity terrain trees with prefab GameObject. // http://answers.unity3d.com/questions/723266/converting-all-terrain-trees-to-gameobjects.html
[ExecuteInEditMode] public class TreeReplacer : EditorWindow {

   [Header("References")]
   public Terrain _terrain;
   //============================================
   [MenuItem("Window/My/TreeReplacer")]
   static void Init()
   {
       TreeReplacer window = (TreeReplacer)GetWindow(typeof(TreeReplacer));
   }
   void OnGUI()
   {
       _terrain = (Terrain)EditorGUILayout.ObjectField(_terrain, typeof(Terrain), true);
       if (GUILayout.Button("Convert to objects"))
       {
           Convert();
       }
       if (GUILayout.Button("Clear generated trees"))
       {
           Clear();
       }
   }
   //============================================
   public void Convert()
   {
       TerrainData data = _terrain.terrainData;
       float width = data.size.x;
       float height = data.size.z;
       float y = data.size.y;
       // Create parent
       GameObject parent = GameObject.Find("TREES_GENERATED");
       if (parent == null)
       {
           parent = new GameObject("TREES_GENERATED");
       }

       // Create tree objects
       foreach (TreeInstance tree in data.treeInstances)
       {
           Vector3 position = new Vector3(tree.position.x * width, tree.position.y * y, tree.position.z * height);
           var _tree = data.treePrototypes[tree.prototypeIndex].prefab;
           Instantiate(_tree, position, Quaternion.identity, parent.transform);
       }

       parent.transform.parent = _terrain.transform;
       parent.transform.localPosition = Vector3.zero;
       parent.transform.localRotation = Quaternion.identity;
   }
   public void Clear()
   {
       DestroyImmediate(GameObject.Find("TREES_GENERATED"));
   }
}

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]

5 Likes

Did we ever find out if this is really needed? Is that really not possible anymore?

Edit: Yes, I know, necro thread. Sorry

Well, according to Unity, this is by design Unity Issue Tracker - [Light] Terrain's speed tree shadows are not getting baked when 'Mode' in Light component is set to "Baked" :confused:

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.

2 Likes