Grass! MultiMesh? or another method?

newmodelsnewmodels Posts: 479Member

I wanted to populte a terrain with grass. Should I use multimesh for doing this? Should I just place them on the terrain with a ray? I want to cover large area.

Comments

  • newmodelsnewmodels Posts: 479Member

    how about particles?

  • TwistedTwiglegTwistedTwigleg Posts: 4,589Admin

    I would recommend using a MultiMesh or Particles, as either will lead to all of the grass being drawn in a single pass. I think currently it's easier to place instances of a MultiMesh than it is to place individual particles, though I believe Godot 4 is going to make this easier.

  • newmodelsnewmodels Posts: 479Member

    The multimesh is what I decided to use. It's working well.

  • Erich_LErich_L Posts: 92Member

    wouldn't mind seeing the result =)

  • newmodelsnewmodels Posts: 479Member

    I'll have to spawn some grass and take a screen shot. I think I know where I would like to put some. But, I'll have to do this later.

  • newmodelsnewmodels Posts: 479Member
    edited June 12

    I took a screen shot of the grass working.

    #tool
    extends MultiMeshInstance
    
    export var numGrass=100
    export var xsize=40
    export var zsize=40
    export var dis=false
    
    var ray=null
    var raynode=null
    export var raydis=100
    
    # Called when the node enters the scene tree for the first tim.
    func _ready():
    
    
        if(dis):
            return
    
        raynode=get_node("rayNode")
        ray=RayCast.new()
        ray.collide_with_areas=false
        ray.collide_with_bodies=true
    
        raynode.add_child(ray)
        ray.enabled=true
    
    
        placeGrass()
        pass # Replace with function body.
    
    func findraypoint(xs,zs):
    
        var ret : Vector3
        raynode.transform.origin.x=xs
        raynode.transform.origin.z=zs
    
        var v : Vector3=Vector3(0,-raydis,0)
        ray.cast_to=v
        ray.force_raycast_update()
        if(ray.is_colliding()):
            ret=ray.get_collision_point()
            ret=ret-self.transform.origin
        else:
            ret=v
    
    
        return ret
        pass
    
    func placeGrass():
    
        self.multimesh.instance_count=numGrass
    
    
        var point : Vector3
        var yrotby=23
        var ran=RandomNumberGenerator.new()
        var ranx=0
        var ranz=0
        var trans=Transform()
        ran.randomize()
    
        var scan=0
    
    
        while(scan<numGrass):
    
            ranx=ran.randf_range(-(xsize/2),(xsize/2))
            ranz=ran.randf_range(-(zsize/2),(zsize/2))
            yrotby=ran.randi_range(-180,180)
    
            point=findraypoint(ranx,ranz)
    
            trans.origin=self.transform.origin
    
            trans.origin=point
    
            self.multimesh.set_instance_transform(scan,trans)
            scan=scan+1
    
    
        pass
    
    
    # Called every frame. 'delta' is the elapsed time since the previous frame.
    #func _process(delta):
    #   pass
    
    
    
Sign In or Register to comment.