Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

After lightmapping, some objects become invisible

Discussion in 'Editor & General Support' started by Aliakey, Jan 28, 2011.

  1. Aliakey

    Aliakey

    Joined:
    Dec 29, 2010
    Posts:
    5
    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.

    Any idea on what is going on? :confused:
     
    howong likes this.
  2. sbrk

    sbrk

    Joined:
    Feb 1, 2011
    Posts:
    4
    I have the same issue. Assets are some of the trees included in the "Terrain Assets" available in Asset Store. Namely Wollow and Alder.

    Anyone knows why?
     
  3. MaliceA4Thought

    MaliceA4Thought

    Joined:
    Feb 25, 2011
    Posts:
    406
    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.

    Would love an answer.

    Regards

    Graham
     
  4. Kuba

    Kuba

    Moderator

    Joined:
    Jan 13, 2009
    Posts:
    416
    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.
     
    howong likes this.
  5. dougmpham

    dougmpham

    Joined:
    Apr 27, 2012
    Posts:
    1
    I had the same problem. Solved it by re-baking all lightmaps with out trees
    - Bake lightmaps with trees
    - Save lightmaps to another folder or rename them
    - Trees should be invisible at this point. Uncheck static on these trees
    - Rebake lightmaps ( low resolution for quicker render time)
    - Trees should be visible after lightmap is done.
    - Move good lightmaps back or remap them

    These steps worked for me. Hope this helps!
     
  6. BurningthumbStudios

    BurningthumbStudios

    Joined:
    Apr 28, 2008
    Posts:
    95
    To solve this I selected each tree and in the Lightmapping window Object tab set the scale in lightmap to 0.

    This will cause the trees to affect other lighmapped objects but not be lightmapped themselves.

    The result is shadows and trees that stay visible.
     
    howong likes this.
  7. jinsuen

    jinsuen

    Joined:
    Nov 22, 2012
    Posts:
    8
    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.

     
    Last edited: Aug 15, 2013
  8. darkhog

    darkhog

    Joined:
    Dec 4, 2012
    Posts:
    2,218
    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):

    Code (csharp):
    1. Shader "Vegetation" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (.5, .5, .5, .5)
    4.         _MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
    5.         _Cutoff ("Base Alpha cutoff", Range (0,.9)) = .5
    6.     }
    7.     SubShader {
    8.         // Set up basic lighting
    9.         Material {
    10.             Diffuse [_Color]
    11.             Ambient [_Color]
    12.         }
    13.         Lighting On
    14.  
    15.         // Render both front and back facing polygons.
    16.         Cull Off
    17.  
    18.         // first pass:
    19.         //   render any pixels that are more than [_Cutoff] opaque
    20.         Pass {
    21.             AlphaTest Greater [_Cutoff]
    22.             SetTexture [_MainTex] {
    23.                 combine texture * primary, texture
    24.             }
    25.         }
    26.  
    27.         // Second pass:
    28.         //   render in the semitransparent details.
    29.         Pass {
    30.             // Dont write to the depth buffer
    31.             ZWrite off
    32.             // Don't write pixels we have already written.
    33.             ZTest Less
    34.             // Only render pixels less or equal to the value
    35.             AlphaTest LEqual [_Cutoff]
    36.  
    37.             // Set up alpha blending
    38.             Blend SrcAlpha OneMinusSrcAlpha
    39.  
    40.             SetTexture [_MainTex] {
    41.                 combine texture * primary, texture
    42.             }
    43.         }
    44.     }
    45. }
    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.
     
  9. WayAbove

    WayAbove

    Joined:
    Sep 5, 2013
    Posts:
    1
    THIS WORKS !!

    1) Select the tree obj/prefab then under Lightmapping > Object > Scale in lightmap-0....
    2) Bake the tree....
    3) After lightmapping select the tree obj/Prefab....
    then under Lightmapping > Object > Atlas > Lightmap Index-255(By default it should be -0)....

    ENJOY!;-)
     
    howong and Rolsct like this.
  10. munozpro

    munozpro

    Joined:
    Sep 30, 2013
    Posts:
    3
    THANK YOU! (THIS SOLUTION WORKS IN UNITY 5)
     
    howong likes this.