more edge cases handled, one more left

This commit is contained in:
GoaLitiuM
2021-08-15 22:52:40 +03:00
parent b703bb93dd
commit f6f3d7ff2d
5 changed files with 149 additions and 97 deletions

View File

@@ -1736,57 +1736,78 @@ namespace Game
Vector3 v3 = brushVertices[i+2];
Vector3 normal = -Vector3.Cross(v3 - v1, v2 - v1).Normalized;
Vector3 normal2 = new Vector3(-normal.X, -normal.Y, -normal.Z);
// texture is projected to the surface from the closest axis
var dotX = Mathf.Abs(Vector3.Dot(normal2, Vector3.Right));
var dotY = Mathf.Abs(Vector3.Dot(normal2, Vector3.Up));
var dotZ = Mathf.Abs(Vector3.Dot(normal2, Vector3.Forward));
Vector3 theUp;
if (dotX > dotY && dotX > dotZ)
theUp = Vector3.Right;
else if (dotZ > dotX && dotZ > dotY)
theUp = -Vector3.Forward;
else if (dotY > dotX && dotY > dotZ)
theUp = -Vector3.Up;
else
theUp = -Vector3.Up;
var up1 = Vector3.Up;
var up2 = -Vector3.Forward;
Vector2 uvScale = new Vector2(0f);
float uvRotation = 0f;
Vector2 uvOffset = new Vector2(0f);
bool found = false;
Vector2 textureSize = new Vector2(64f); // TODO: figure out the correct size for the material
foreach (var brushPlane in brush.planes)
{
Plane plane = brushPlane.plane;
if ((plane.Normal - normal).Length < 0.01f)
{
uvScale = 1f / brushPlane.scale / 64f; // texture size?
normal = plane.Normal; // for consistency
uvScale = 1f / brushPlane.scale / textureSize; // texture size?
uvRotation = brushPlane.rotation;
uvOffset = brushPlane.offset;
uvOffset = brushPlane.offset * brushPlane.scale;
found = true;
break;
}
}
if (!found)
Console.Print("no found, hidden surface?");
Console.Print("no found, bad geometry?");
// texture is projected to the surface from the closest axis
Vector3 normal2 = normal;
List<Tuple<float, Vector3>> dots = new List<Tuple<float, Vector3>>(new []
{
new Tuple<float, Vector3>(Mathf.Abs(Vector3.Dot(normal2, Vector3.Right)), Vector3.Right),
new Tuple<float, Vector3>(Mathf.Abs(Vector3.Dot(normal2, Vector3.Forward)), -Vector3.Forward),
new Tuple<float, Vector3>(Mathf.Abs(Vector3.Dot(normal2, Vector3.Up)), -Vector3.Up),
});
dots.Sort((l, r) => l.Item1.CompareTo(r.Item1));
Vector3 theUp = dots.Last().Item2;
/*var dotX = Mathf.Abs(Vector3.Dot(normal2, Vector3.Right));
var dotY = Mathf.Abs(Vector3.Dot(normal2, Vector3.Up));
var dotZ = Mathf.Abs(Vector3.Dot(normal2, Vector3.Forward));
Vector3 theUp;
if (false)
{}
else if (dotY > dotX && dotY > dotZ)
theUp = -Vector3.Up;
else if (dotX > dotY && dotX > dotZ)
theUp = Vector3.Right;
else if (dotZ > dotX && dotZ > dotY)
theUp = -Vector3.Forward;
else
theUp = Vector3.Right;*/
var up1 = Vector3.Up;
var up2 = -Vector3.Forward;
var finalUp = Mathf.Abs(Vector3.Dot(theUp, up1)) > 0.01f ? up2 : up1;
var finalUp2 = Mathf.Abs(Vector3.Dot(theUp, up1)) > 0.01f ? up1 : up2;
var theRight = Vector3.Right;
Quaternion rot = Quaternion.Identity;
rot = rot * Quaternion.LookRotation(theUp, finalUp);
rot = rot * Quaternion.RotationAxis(Vector3.Forward, 180f * Mathf.DegreesToRadians);
rot = rot * Quaternion.RotationAxis(theUp, uvRotation * Mathf.DegreesToRadians);
rot = rot * Quaternion.RotationAxis(up2, 180f * Mathf.DegreesToRadians);
rot = rot * Quaternion.RotationAxis(Mathf.Abs(Vector3.Dot(theUp, theRight)) > 0.01f ? theRight : finalUp2, uvRotation * Mathf.DegreesToRadians);
//if (Mathf.Abs(Vector3.Dot(theUp, theRight)) <= 0.01f)
// uvOffset *= 2;
brushUvs[i + 0] = ((Vector2)(v1 * rot) * uvScale) + uvOffset / 64f;
brushUvs[i + 1] = ((Vector2)(v2 * rot) * uvScale) + uvOffset / 64f;
brushUvs[i + 2] = ((Vector2)(v3 * rot) * uvScale) + uvOffset / 64f;
brushUvs[i + 0] = ((Vector2)(v1 * rot) + (uvOffset)) * uvScale;
brushUvs[i + 1] = ((Vector2)(v2 * rot) + (uvOffset)) * uvScale;
brushUvs[i + 2] = ((Vector2)(v3 * rot) + (uvOffset)) * uvScale;
brushNormals[i + 0] = normal;
brushNormals[i + 1] = normal;