mesh - Unity3D Procedural Generation : Grass? -
since yesterday have been trying create procedural infinite world generation , new game , moment goes pretty well, except can't figure out how add grass mesh. world generated using procedurally generated meshes of different levels of details. created terrain based on 3 "layers", ground layer, hills layer , mountains layer. texture on top of terrain entirely managed custom shader (which primitive moment because there no blending).
so an't figure out how add grass such world. tried different technique grass gameobjects, planes etc not worked... if can me awesome! can explain how manage vegetation script. call generatevegetation()
function on chunk load , randomly place trees on top of terrain using raycasting point , normal (i check slope y axis of vertex's normal).
here vegetation script (very basic moment done now)
using system.collections; using system.collections.generic; using unityengine; public class vegetationgeneration { public static ienumerator generate(vector2 chunk) { // setting vegetation holder child of global chunk (hierachy); transform vegetationchunk = new gameobject ("vegetation").transform; vegetationchunk.parent = gamesettings.chunks [chunk]; // vegetation instances , loop start iterating placement foreach(vegetation vegetation in mappreview.instance.vegetation) { float amount = random.range (vegetation.amount.x, vegetation.amount.y); for(int = 0; < amount; i++) { // splitting generation calculations each 15 trees (only while playtime) (performance) if (!application.iseditor && % 15 == 0) yield return null; // getting random position in chunk float chunkdiameter = gamesettings.worldchunksize / 2f; vector3 position = new vector3 (random.range (-chunkdiameter, chunkdiameter), 0f, random.range (-chunkdiameter, chunkdiameter)) + new vector3(chunk.x, 0f, chunk.y); // shooting raycast down hit point , hit normal // check slope angle raycasthit hit; float slope; if(physics.raycast(new ray(position + vector3.up * 100f, vector3.down), out hit, 200f, mappreview.instance.groundlayer) && (slope = hit.normal.y) > vegetation.maxslope) { // possible spawn vegetation @ given point // spawning entity vegetation chunk parent gameobject prefab = vegetation.vegetation [random.range (0, vegetation.vegetation.length)]; gameobject entity = gameobject.instantiate (prefab, vegetationchunk); // setting parameters entity.transform.position = hit.point; entity.transform.localeulerangles += new vector3 (0f, random.range (0f, 360f), 0f); // getting random scale , converting uniform vector float scale = random.range (vegetation.scale.x, vegetation.scale.y); entity.transform.localscale = vector3.one * scale; // putting vegetation ground avoit flying trees entity.transform.position -= vector3.up * vegetation.depth.evaluate (slope); // set different layre ground avoid trees on other trees (raycasting) setlayer (entity.transform); } } } } // simple recusrive function loop thru every child of root // transform , apply right layer static void setlayer(transform parent) { parent.gameobject.layer = layermask.nametolayer (mappreview.instance.vegetationlayer); foreach(transform child in parent) setlayer (child); } } // vegetation object [system.serializable] public class vegetation { public string name; [tooltip("amount of vegetation per generated chunk (does not represent real amount because won't placed if place not survivable)")] public vector2 amount = new vector2(50,70); public gameobject[] vegetation; public vector2 scale; [range(0f,1f)] public float maxslope = 0.7f; public animationcurve depth = new animationcurve(new keyframe[] { new keyframe(0f, 0.05f), new keyframe(1f, 0.25f) }); }
i appreciate insight.
Comments
Post a Comment