Couldn’t find an explanation of why this may be occuring, but hoping someone has an idea…
I created a small scene with the following objects:
Palm trees: Standard asset placed using the terrain tool for trees
Alder trees: Placed as an object from the Terrain Assets package downloaded from this site
Banana plants: Placed as an object from the Terrain Assets package downloaded from this site
Blocks: Imported fbx model
When I create a lightmap for this scene using the free version of Unity 3, the shadows for all of the objects (listed above) will appear properly. However, as soon as the lightmap is shown, the Alder trees and Banana plants (trees) suddenly become invisible. The shadow remains, and if one of these invisible models is selected from the Hierarchy folder, the structure of the model (polys) will be shown in the scene, so the object still exists. In the inspector, the textures and other values appear unchanged from pre-lightmapping state.
The palm trees, grass, and imported blocks remain visible in the scene.
If I clear the lightmap, all of the objects are visible again.
The only unusual thing I can see that caused the problem is that instead of painting the alder and banana tree assets using the terrain tool, I grabbed the objects from the Project folder and dropped it on scene. I did this in order to more accurately place, scale, and rotate these specific plants for the scene since the terrain tree tool offers only random placement and scaling. Now, if I paint areas of the terrain with these same trees (alder and banana) using the terrain tree tool, they will not disappear after lightmapping.
My imported models (blocks) do not have this problem. Also, if I drop a bush from the Terrain Assets package onto the scene, it will generate a shadow and not disappear. Seems the problem occurs only when I drop objects from the Trees Ambient-Occlusion folder on to the scene.
I also have the same issue, and not just with Assets in terrain assets. I have some other models that have alpha channels in the textures that loose the alpha parts when lightmapping takes place.
In short:
Second UVs are used to control how different branches/leaves are affected by wind, thus tree shaders do not support lightmapping out of the box.
Detailed explanation:
Trees created with the Tree Creator use TreeVertBark / TreeVertLeaf functions as vertex shaders for trunk and leaves respectively. Those in turn use AnimateVertex to move tree vertices around when the tree is affected by a wind zone. 4 parameters are necessary for that animation:
animParams.x - branch phase - comes from v.color.x
animParams.y - edge flutter factor - comes from v.color.y
animParams.z - primary factor - comes from v.texcoord1.x
animParams.w - secondary factor - comes from .texcoord1.y
Problem?:
Before you go into trouble to search for a solution, consider whether it is worth lightmapping your trees. The problem is that a tree’s surface is huge relatively to its bounding box size and it will need to take quite a bit of area in the lightmap to look good. The trees that you place on the terrain just store one lightmap color value for the whole tree…
Solution?:
If you can live without wind affecting trees or with a simpler model handling the tree animation, you can of course create a tree shader that supports lightmapping.
Duplicate the TreeCreatorLeaves.shader and TreeCreatorLeavesOptimized.shader and append SimpleWind at the end of the filename and shader name.
In TreeCreatorLeaves.shader change the line: Dependency "OptimizedShader" = "Hidden/Nature/Tree Creator Leaves Optimized"
to Dependency "OptimizedShader" = "Hidden/Nature/Tree Creator Leaves Optimized SimpleWind"
In TreeCreatorLeavesOptimizedSimpleWind.shader change: #pragma surface surf TreeLeaf alphatest:_Cutoff vertex:TreeVertLeaf nolightmap
to #pragma surface surf TreeLeaf alphatest:_Cutoff vertex:TreeVertLeafSimpleWind - notice we removed the nolightmap keyword.
Create TreeVertLeafSimpleWind and AnimateVertexSimpleWind based on the functions from TerrainEngine.cginc and make sure your AnimateVertexSimpleWind does not depend on v.texcoord1.
You also need to make sure that the mesh you get from the Tree Creator gets proper second UVs for lightmapping, you can use Unwrapping.GenerateSecondaryUVSet on a copy of your mesh and then save it as an asset.
Thank you so much for this explanation, I’m relatively new to Unity and this has been bugging me for the past week or so! So I am using asset store trees like Alder and Japanese Maple, I removed their lightmaps and they are lighting up fine. Either way it has solved my problem for now.
Hello. I’m experiencing same issue. For my models that contain alpha-channel textures, I’m using following shader since I’m not too fond with 1bit alpha and full alpha gave me bad results (from shader lab):
Shader "Vegetation" {
Properties {
_Color ("Main Color", Color) = (.5, .5, .5, .5)
_MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
_Cutoff ("Base Alpha cutoff", Range (0,.9)) = .5
}
SubShader {
// Set up basic lighting
Material {
Diffuse [_Color]
Ambient [_Color]
}
Lighting On
// Render both front and back facing polygons.
Cull Off
// first pass:
// render any pixels that are more than [_Cutoff] opaque
Pass {
AlphaTest Greater [_Cutoff]
SetTexture [_MainTex] {
combine texture * primary, texture
}
}
// Second pass:
// render in the semitransparent details.
Pass {
// Dont write to the depth buffer
ZWrite off
// Don't write pixels we have already written.
ZTest Less
// Only render pixels less or equal to the value
AlphaTest LEqual [_Cutoff]
// Set up alpha blending
Blend SrcAlpha OneMinusSrcAlpha
SetTexture [_MainTex] {
combine texture * primary, texture
}
}
}
}
How can it be modified to support lightmapping? Models themselves were made by hand using Blender and lightmapping UVs were generated using Generate lightmapping UVs option.
I’m using Unity Free, so any Pro-only solutions are out of question.