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

Terrain Shadow Cast

Discussion in 'Made With Unity' started by User340, Apr 14, 2008.

  1. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    I created a script which casts shadows from the terrain. Just place this script onto a terrain and press play. It's about 90% done, I just have a few more things I have to fix up. What do you guys think of it?

    Demo:
    http://webgames3d.com/TerrainShadows.html
     

    Attached Files:

  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Aha, so this is what you were asking about the terrain mesh for. ;) Use Terrain.SampleHeight instead of raycasting; it will be faster. Since you can figure the size of the array to begin with, it would also be faster to just use built-in arrays instead of dynamic arrays and Push(). Given that you're just doing it once, that's not a big deal...however, having an entire mesh the size of the terrain with an alpha shader might be a bit hard on the fill rate of the graphics card; ideally you'd create a mesh just big enough for the shadow(s) and deform it on the fly. Of course, that's harder....

    --Eric
     
  3. yangmeng

    yangmeng

    Joined:
    Dec 5, 2006
    Posts:
    573
    This sounds cool. I gave it a try but got this warning: "Shader wants texture coordinates but the mesh doesn't have them"...
     
  4. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    It does slow the frame rate down some, but if you really want the terrain to cast shadows onto objects, this works. Here's a link to a demo.

    http://webgames3d.com/TerrainShadows.html

    Feel free to modify or optimize the script.
     
  5. tim

    tim

    Joined:
    Feb 3, 2007
    Posts:
    113
    Nice job, thanks for sharing!
     
  6. holmeren

    holmeren

    Joined:
    Dec 12, 2006
    Posts:
    300
    hmm I dont see any shadows on the terrain from the moving box...