Changing a mesh with code but it doesn't react to the lighting
Hello, I'm trying to make some low-poly water. So I have a plane and I put a script on it that I found online:
using UnityEngine;
using System.Collections;
public class Water : MonoBehaviour
{
public float scale = 7.0f;
public float heightScale = 1.0f;
private Vector2 v2SampleStart = new Vector2(0f, 0f);
void Update ()
{
MeshFilter mf = GetComponent<MeshFilter>();
Vector3[] vertices = mf.mesh.vertices;
for (int i = 0; i < vertices.Length; i++)
{
vertices[i].y = heightScale * Mathf.PerlinNoise(Time.time + (vertices[i].x * scale), Time.time + (vertices[i].z * scale));
}
mf.mesh.vertices = vertices;
}
}
Ok, great. So it makes the plane wiggle and looks like water. However the changes that are made by this script do not cause the material to react to the lighting. Since the plane starts totally flat, and is uniformly lit with no shadows or specular on anything, once this script starts, the vertices move around but there's no shading at all. It's totally flat. I've confirmed the lighting/material works by exporting a plane from Cinema4D that has a little bit of displacement baked onto it, and this is affected by the lighting. However, even when I put the script on this one, the lighting doesn't change! Polygons that were lit/shadowed from the beginning retain their same exact color.
Am I missing something simple here?
Thanks!
plane with displacement from script:
plane with displacement baked into model:
Answer by shapirog · Mar 16, 2017 at 09:30 AM
Well I made some progress... I got it to update the shading with RecalculateNormals(); but it resets the smoothing to the default so it no longer looks flat shaded. Is there a way to have it update the normals but keep the flat-shaded look? Or would it be better to try a totally different approach to get the water to look low-poly/flat shaded?
Your answer
Follow this Question
Related Questions
some sprites are turning transparent while using a normal map and others don't 0 Answers
How to get the name of a model file(fbx) contained in a certain prefab with scripts? 1 Answer
URGENT How to get triangles of mesh in world space? 0 Answers
Thin black lines between meshes 1 Answer
Create a custom quad mesh from script 0 Answers