- Home /
Sprite grass/trees and wind simulation
Folks, would you please recommend the best way to simulate slight bending of grass and trees?
My grass and trees are 2d sprites, rendered with the sprite renderer and the game uses orthographic camera. I replaced default sprite shader with my own one which skewed vertices by some angle. Well, it kinda works but I'm not satisfied with results.
I've been searching for a solution for this as well. Any updates?
Yep! What I'm currently using is animation of vertices. However for this I had to migrate all my sprites to mesh renderers. Looks like sprite won't allow you to play with its mesh.
Answer by pachash · Mar 05, 2015 at 11:15 AM
Looks like I managed to find an answer to this problem myself. The way to go is to play around with mesh vertices or UVs. However looks like it is only possible for regular meshes not sprites. So I had to make all my sprites regular meshes with mesh renderers attached.
After this I attach some script to every instance I'd like to be animated, e.g:
using UnityEngine;
using System.Collections.Generic;
public class AnimateTree : MonoBehaviour
{
Mesh mesh;
Vector3[] verts_ref;
Vector3[] verts;
float time_offset;
void Start()
{
mesh = GetComponent<MeshFilter>().mesh;
verts_ref = mesh.vertices;
verts = new Vector3[verts_ref.Length];
System.Array.Copy(verts_ref, verts, verts_ref.Length);
time_offset = Random.Range(0, 10000);
}
void OnBecameInvisible()
{
enabled = false;
}
void OnBecameVisible()
{
enabled = true;
}
void Update()
{
float time = time_offset + Time.time;
float x_tweak = 0.060f;
float y_tweak = 0.0085f;
float val_x = Mathf.Sin(time);
float val_y = Mathf.Cos(time);
float k = 1.0f;
for(int i=(int)(verts.Length*0.4f);i<verts.Length;++i)
{
Vector3 v = verts_ref[i];
if(i%5 == 0) k = 1.0f;
if(i%5 == 1) k = 1.2f;
v.x = v.x + k * x_tweak*val_x;
v.y = v.y + k * y_tweak*val_y;
verts[i] = v;
}
mesh.vertices = verts;
}
Answer by rubendariohh · Dec 31, 2017 at 04:41 PM
Hi Pachash, how you convert your sprites into meshes? thanks!
Your answer
Follow this Question
Related Questions
Multiple terrains and how LoD works 0 Answers
[SOLVED] Which is faster and more efficient? 2 Answers
Wind: Detail meshes are moved by wind back and forth, rather than swaying: how do I make them sway? 7 Answers
Shimmering waves of wind-blown grass possible? 1 Answer
making grass move at different speeds 0 Answers