纳金网

标题: 运行时创建场景物体Runtime objects in Unity3D(二) [打印本页]

作者: 会飞的鱼    时间: 2011-11-29 14:48
标题: 运行时创建场景物体Runtime objects in Unity3D(二)


           In the above example I have 6 predefined textures with different types of windows, so I don’t create those from scratch. However I use a procedural texture for the illumination maps. I generate a 64×64 bitmap where the value of each pixel determines the brightness of a single window (i.e. how much light it emits). It’s pretty basic: after the Texture2D is created I just***n through all it’s pixels and assign a random value to it’s alpha component.
         

           Creating materials
         

           There are 6 different textures representing a different type of window each and one illumination map. How to put these together? That’s what materials are used for. As with any other element, materials are represented by the
           

           Material
           

            object in scripts, and they can be created with code. The cons***ctor takes only one argument: a shader.
         

           Shaders are one of the most ***cial elements of Unity3D (and any other 3D rendering engine for that matter). They describe how a 3D geometry will be rendered, what colors and/or textures will be used and how it will interact with light. For the buildings I use a shader called Self-Illuminated Diffuse. It’s one of Unity3D built-in shaders.
         

           Shaders usually need some parameters to work with – like colors or textures. A material is essentially a shader and a number of objects that define it’s parameters. Self-Illuminated shaders expect one texture for the color and the other that determines the amount of light emitted by this texture. It’s great for creating objects that emit light like bulbs, neon signs or… windows in a night scenery (it’s important to understand however that the object emits light but this light doesn’t illuminate any other objects around it).
         

           I use the window image as the color texture, and the procedural image with random alpha values as the illumination map texture. The trick is that I set the scale of the color texture to 64. This way the window is repeated 64×64 times on the material and each pixel of the procedural illumination map (scaled 1:1) corresponds to one single window. I also set the filtering mode of the illumination map texture to Point, so that it doesn’t get blurred when seen form a bigger distance.
           



           unity3d8.com整理翻译
         

           A word on optimization
         

           The generated buildings have different width, height and depth. A natural solution to apply textures on all of them was to create a material that repeats the windows the appropriate amount of times for each building. Ex. a 4 story building with 3 windows in a row would have a material where the x-scale of the texture is set to 3 and y-scale to 4. The emission map scale needs to be divided by 64 in this case, giving values of 3/64 and 4/64 respectively.
         

           While this seems quite ok, it poses an optimization problem. Unity3D is much more effective when there’s a lower amount of meshes composed of large amount of vertices, rather that the other way around – lots of meshes with few vertices. One building has 20 vertices and I create 2500 separate meshes! It’s far from optimal, and you can really feel the framerate going down because of that. Fortunately there is a built-in script called CombineChildren that automatically combines all the buildings into one huge mesh making everything work much faster and smoother. But (of course) there is a trick!
         

           Unity3D will combine objects only if they share the same, exact material. The scale of a texture on a material is unique, so if there are two materials that are identical and the only difference is the scale of the texture, we still need 2 different materials. In the above scenario almost each building uses a different material, unless it has exactly the same number of stories and the same number of windows in a row. I had to come up with a way to use only one material, and to deal with texture coordinates in a different way.
         

           The answer is to use UVs instead of the texture scaling. So I started by creating only one material, which contains 64×64 windows. If a building is ex. 20 stories high and has 6 windows in a row, this means that the wall of the building needs to be mapped to the texture with this coordinates [0, 0, 6/64, 20/64]. Remember that UV values are normalized, i.e have values between 0 and 1, 0 pointing to the left/top pixel of the texture, and 1 to the right/bottom. unity3d8.com
         

           This way I can reuse the same material on every building regardless of it’s size. It works even on buildings that are higher that 64 stories, because UV values are repeated – ex. an UV value of 1.2 is the same as 0.2. Perfect!
         

           One little problem still persist though. Since each building uses the same material it also has the same pattern of lit and unlit windows. It doesn’t look nice. Fortunately there was an easy solution for this: to offset the UVs of each building by a random value multiplied by 1/64. That way each building is mapped to a different area of the of the material and things look more natural.
         

           Effects
         

           To make the cityscape more interesting I added a Glow image effect to the camera. It adds a subtle glow around lit areas of the scene – in this case the windows, giving it a slightly blurry look.
         

           本文转自everyday3d,版权归原作者所有!
         

作者: C.R.CAN    时间: 2012-1-24 23:27
新年新气象,我来把歌唱:祝你身体棒,来年不发胖;家和万事畅,心花怒绽放;事业无风浪,生意蒸蒸上;要是把我忘,就找你算账。新年快乐!

作者: 菜刀吻电线    时间: 2012-1-27 23:24
春节即将来到,我用祝福捻制成的绒线,烛光下为您织起一件红色的毛衣:前身是平安,后身是幸福,吉祥是厚厚的肩,如意戴在袖子里,领子蕴藏着体贴,口袋把快乐盛满,穿在身上让暖和包裹着您,让我的心陪伴您度过新年。

作者: tc    时间: 2012-3-25 23:20
路过……

作者: C.R.CAN    时间: 2012-3-28 23:25
不会吧,太恐怖了

作者: 菜刀吻电线    时间: 2012-4-7 23:30
你们都躲开,我来顶

作者: 菜刀吻电线    时间: 2012-4-26 23:20
不错哦,顶一下......

作者: 晃晃    时间: 2012-5-15 23:21
不会吧,太恐怖了

作者: 奇    时间: 2012-5-22 23:24
佩服,好多阿 ,哈哈

作者: 菜刀吻电线    时间: 2012-10-26 23:18
跑着去顶朋友滴铁

作者: tc    时间: 2013-3-17 23:25
先垫一块,再说鸟





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5