void ReprojectUVNoY(List<Triangle> res, bool forceXZLayerProject = true)
{
/*double minX = points[0].x, maxX = points[0].x, minZ = points[0].z, maxZ = points[0].z;
double minXUV = uvs[0].x,maxXUV = uvs[0].x, minZUV = uvs[0].y, maxZUV = uvs[0].y;
for (int i=1;i<3;i++){
if (minX > points[i].x){
minX = points[i].x;
minXUV = uvs[0].x;
}
if (maxX < points[i].x){
maxX = points[i].x;
maxXUV = uvs[0].x;
}
if (minZ > points[i].z){
minZ = points[i].z;
minZUV = uvs[0].y;
}
if (maxZ < points[i].z){
maxZ = points[i].z;
maxZUV = uvs[0].y;
}
}
double deltaX = maxX - minX;
double deltaZ = maxZ - minZ;
double deltaUVX = maxXUV - minXUV;
double deltaUVZ = maxZUV - minZUV;
foreach (var t in res) {
for (int i = 0; i < 3; i++) {
var pos = t.Vertex (i);
t.uvs [i] = new Vector2D(
minXUV + deltaUVX*((pos.x -minX)/(deltaX)),
minZUV + deltaUVZ*((pos.z -minZ)/(deltaZ))
);
if (deltaX == 0) {
Debug.LogWarning ("deltaX is 0");
t.uvs[i].x = minXUV;
}
if (deltaZ == 0) {
Debug.LogWarning ("deltaZ is 0");
t.uvs[i].y = minZUV;
}
}
}
*/
// http://answers.unity3d.com/questions/383804/calculate-uv-coordinates-of-3d-point-on-plane-of-m.html
var p1 = points[0];
var p2 = points[1];
var p3 = points[2];
if (forceXZLayerProject) {
p1.y = 0;
p2.y = 0;
p3.y = 0;
}
foreach (var t in res){
for (int i=0;i<3;i++){
var pos = t.Vertex(i);
if (forceXZLayerProject) {
pos.y = 0;
}
// calculate vectors from point f to vertices p1, p2 and p3:
var f1 = p1-pos;
var f2 = p2-pos;
var f3 = p3-pos;
// calculate the areas and factors (order of parameters doesn't matter):
double a = Vector3D.Cross(p1-p2, p1-p3).magnitude; // main triangle area a
double a1 = (Vector3D.Cross(f2, f3).magnitude / a); // p1's triangle area / a
double a2 = (Vector3D.Cross(f3, f1).magnitude / a); // p2's triangle area / a
double a3 = (Vector3D.Cross(f1, f2).magnitude / a); // p3's triangle area / a
// find the uv corresponding to point f (uv1/uv2/uv3 are associated to p1/p2/p3):
t.uvs[i] = uvs[0] * a1 + uvs[1] * a2 + uvs[2] * a3;
}
}
/*
var p1 = points[0];
var p2 = points[1];
var p3 = points[2];
if (forceXZLayerProject) {
p1.y = 0;
p2.y = 0;
p3.y = 0;
}
// calculate vectors from point f to vertices p1, p2 and p3:
foreach (var t in res) {
for (int i = 0; i < 3; i++) {
var pos = t.Vertex (i);
if (forceXZLayerProject) {
pos.y = 0;
}
var f1 = p1 - pos;
var f2 = p2 - pos;
var f3 = p3 - pos;
// calculate the areas (parameters order is essential in this case):
var va = Vector3D.Cross (p1 - p2, p1 - p3); // main triangle cross product
var va1 = Vector3D.Cross (f2, f3); // p1's triangle cross product
var va2 = Vector3D.Cross (f3, f1); // p2's triangle cross product
var va3 = Vector3D.Cross (f1, f2); // p3's triangle cross product
double a = va.magnitude; // main triangle area
// calculate barycentric coordinates with sign:
double a1 = va1.magnitude / a * Mathf.Sign ((float)Vector3D.Dot (va, va1));
double a2 = va2.magnitude / a * Mathf.Sign ((float)Vector3D.Dot (va, va2));
double a3 = va3.magnitude / a * Mathf.Sign ((float)Vector3D.Dot (va, va3));
// find the uv corresponding to point f (uv1/uv2/uv3 are associated to p1/p2/p3):
t.uvs [i] = uvs[0] * (float)a1 + uvs[1] * (float)a2 + uvs[2] * (float)a3;
}
}*/
}