- Home /
How to get to know a mesh' faces normals?
Hello everyone, Can someone help me with a script i'm trying to make, i got an idea on how I could have shadowing without placing lights or learn complicated things about shaders, but i do not know where to start or even if it is possible.
I'm explaining what i'm thinking about: I'm going to attach a script to every mesh i want to have shaded, that detects every face' normal and check if it faces a specific direction (an interval of angles or something like that); then, i would send a raycast from the center of the face that's parallel to the face normal (or aiming a plane i would place in the scene) and see if it detects any collider. Then i would split the mesh in two subsmeshes: if no collider is detected, and if the face normal is in the specified boundaries, then the face is assigned to submesh1, otherwise it goes to submesh2. then to submesh1 it would be assigned the "lit" material, and to submesh2 the "shaded" one.
i suppose something like this would look very cartoonish and stylized and that's exactly what i want. i also know this would take away lot of performance, and work better as shader other than javascript but i would want to see how it works before worrying about performance or having to learn shaders (can shaders even do raycasting? i'm a real noob...)
I do not know how to use vertices and faces in javascript and found little help in Unity script reference... Sorry for bad english.
You can get the normals from $$anonymous$$esh.normals:
var normals = GetComponent($$anonymous$$eshFilter).mesh.normals;
Wow that sounds about a hundred times more complicated than writing a shader! To get a face normal you need to average the normals of the vertices that make up the triangle. Your problem will be in working out how many triangles make up one of your "faces", I guess it might be something to do with a bunch of triangles that havea similar normal and share vertices.
Shaders can't ray cast but they can know the direction to the lights in the scene.
thanks, however i want proper shadows (if my computer doesn't explode trying to handle them), so i need physic raycasting to check for colliders... also i would prefear not to have lights at all in my project. For "face" i just meant a triangle. The question is how do i know that some three vertices make together a certain triangle, to be able to get the average of their normals...