- Home /
How to make mesh ripples fade c#
Hi, I am new to coding/this forum and I am using a sin function to produce ripples on a mesh from the position of a game object, which seems to work. However, the ripples never fade away which looks ugly. Here is a copy pasted version of my code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class waterscrv2 : MonoBehaviour { Mesh mymesh; float speed = 8f; public GameObject cube; private GameObject[] objects = new GameObject[0]; // Start is called before the first frame update void Start() { objects = GameObject.FindGameObjectsWithTag("Respawn"); Debug.Log(objects.Length); //mymesh = GetComponent().mesh;
//CreateRipples();
}
// Update is called once per frame
void Update()
{
//float size = transform.localScale.x/2;
mymesh = GetComponent<MeshFilter>().mesh;
var vertices = mymesh.vertices;
float distanceX1 = (transform.position.x - objects[0].transform.position.x)/mymesh.bounds.size.x;
float distanceZ1 = (transform.position.z - objects[0].transform.position.z)/mymesh.bounds.size.z;
float distanceX2 = (transform.position.x - objects[1].transform.position.x)/mymesh.bounds.size.x;
float distanceZ2 = (transform.position.z - objects[1].transform.position.z)/mymesh.bounds.size.z;
for (var i = 0; i < vertices.Length; i++)
{
float offset = (vertices[i].x * vertices[i].x) + (vertices[i].z * vertices[i].z);
var ripples1 = Mathf.Sin(Time.time * -speed + (offset + (vertices[i].x * distanceX1) + (vertices[i].z * distanceZ1)) * 4) * .01f;
var ripples2 = Mathf.Sin(Time.time * -speed + (offset + (vertices[i].x * distanceX2) + (vertices[i].z * distanceZ2)) * 4) * .01f;
vertices[i].y = ripples1;
}
mymesh.vertices = vertices;
mymesh.RecalculateNormals();
}
}
Can anyone tell me how to damp the ripples based on the distance from the game object? Sorry if this is hard to read/understand.
Answer by ecv80 · Dec 02, 2018 at 04:02 AM
Okay, first let me tell you how COOL this thing is. Secondly I'm not a mathematician and I have no idea what your equation is doing, so I dealt with the matter in a very rudimentary way. Here's the resulting code and the comments should make it self explanatory:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ripples : MonoBehaviour {
Mesh mymesh;
float speed = 8f;
public GameObject cube;
private GameObject[] objects = new GameObject[0];
float yFactor=1f;
// Start is called before the first frame update
void Start() {
objects = GameObject.FindGameObjectsWithTag("Respawn");
Debug.Log(objects.Length); //mymesh = GetComponent().mesh;
//CreateRipples();
}
// Update is called once per frame
void Update()
{
//float size = transform.localScale.x/2;
mymesh = GetComponent<MeshFilter>().mesh;
var vertices = mymesh.vertices;
float distanceX1 = (transform.position.x - objects[0].transform.position.x)/mymesh.bounds.size.x;
float distanceZ1 = (transform.position.z - objects[0].transform.position.z)/mymesh.bounds.size.z;
// float distanceX2 = (transform.position.x - objects[1].transform.position.x)/mymesh.bounds.size.x;
// float distanceZ2 = (transform.position.z - objects[1].transform.position.z)/mymesh.bounds.size.z;
//1, Find furthest vertex distance
Vector2 otherObjPos=new Vector2(objects[0].transform.position.x, objects[0].transform.position.z);
float furthestDistance=0f;
for (int i=0; i<vertices.Length; i++) {
float vertexDistance=Vector2.Distance(new Vector2(vertices[i].x, vertices[i].z), otherObjPos);
if (vertexDistance>furthestDistance)
furthestDistance=vertexDistance;
}
for (var i = 0; i < vertices.Length; i++)
{
float offset = (vertices[i].x * vertices[i].x) + (vertices[i].z * vertices[i].z);
var ripples1 = Mathf.Sin(Time.time * -speed + (offset + (vertices[i].x * distanceX1) + (vertices[i].z * distanceZ1)) * 4) * yFactor;
// var ripples2 = Mathf.Sin(Time.time * -speed + (offset + (vertices[i].x * distanceX2) + (vertices[i].z * distanceZ2)) * 4) * .01f;
vertices[i].y = ripples1;
//2. Height should be a function of the relationship between vertex distance and furthest distance
float vertexDistance=Vector2.Distance(new Vector2(vertices[i].x, vertices[i].z), otherObjPos);
vertices[i].y*=(furthestDistance-vertexDistance)/furthestDistance;
}
mymesh.vertices = vertices;
mymesh.RecalculateNormals();
}
}
The efficiency of this is dubious... Also I'd avise you to move mymesh = GetComponent<MeshFilter>().mesh;
from Update to Start. Also, if you're looking into incorporating this into a full blown game, I'd advise you not to and to look into animated normal maps or something akin.
Cheers!
EDIT: Come to think of it, you probably want to fade off the ripples based on a radius around your movable object. This is even easier as you don't need to look for the furthest vertex in your mesh, you just need to use this distance in the second point of the code. ALSO. In my tests I didn't bother to get the actual world vertex position. Now that I tested it by turning the rippling plane, I see it doesn't work well. Vector3 worldVertexPos=transform.TransformPoint(vertices[i]); float vertexDistance=Vector2.Distance(new Vector2(worldVertexPos.x, worldVertexPos.z), otherObjPos);
should work.
Alright, I'll try and implement this into my game. You are right about this possibly being too demanding though, so I may or may not implement this into my game. Thanks though!
Your answer
Follow this Question
Related Questions
Why is my job running slowly the first few frames then speeding up? 1 Answer
How to detect when two colliding objects create a corner? 1 Answer
TextMesh/3DText modify shape 0 Answers
Runtime mesh decimation 1 Answer
Multiple Cars not working 1 Answer