very much working

This commit is contained in:
GoaLitiuM
2021-08-17 21:23:43 +03:00
parent f6f3d7ff2d
commit 7466590d4f
2 changed files with 138 additions and 101 deletions

View File

@@ -1136,8 +1136,14 @@ namespace Game
private List<Vector3> debugPoints = new List<Vector3>();
private List<Tuple<Vector3, Vector3>> debugNormals = new List<Tuple<Vector3, Vector3>>();
public override void OnDebugDraw()
{
foreach (var cn in debugNormals)
{
DebugDraw.DrawLine(cn.Item1, cn.Item1 + cn.Item2 * 50f, Color.Red, 0f, false);
}
return;
if (root == null)
return;
@@ -1433,14 +1439,14 @@ namespace Game
Vector3[] cubePoints = new[]
{
new Vector3(-cs, -cs, -cs),
new Vector3(cs, -cs, -cs),
new Vector3(-cs, cs, -cs),
new Vector3(cs, cs, -cs),
new Vector3(-cs, -cs, cs),
new Vector3(cs, -cs, cs),
new Vector3(-cs, cs, cs),
new Vector3(cs, cs, cs),
new Vector3(-1, -1, -1) * cs,
new Vector3(1, -1, -1) * cs,
new Vector3(-1, 1, -1) * cs,
new Vector3(1, 1, -1) * cs,
new Vector3(-1, -1, 1) * cs,
new Vector3(1, -1, 1) * cs,
new Vector3(-1, 1, 1) * cs,
new Vector3(1, 1, 1) * cs,
};
Vector3[] cubeVerts;
QuickHull(cubePoints, out cubeVerts);
@@ -1655,49 +1661,6 @@ namespace Game
}
asdf++;
/*var newMeshPoints = new List<Vector3>();
int duplis = 0;
foreach (var v in faceVertices)
{
bool found = false;
foreach (var vo in newMeshPoints)
{
if ((v - vo).Length < epsilon)
{
found = true;
duplis++;
break;
}
}
//if (!newMeshPoints.Contains(v))
if (!found)
newMeshPoints.Add(v);
}
if (duplis > 0)
Console.Print("duplicates: " + duplis);
if (newMeshPoints.Count > 0)
{
var tempPoints = newMeshPoints;
newMeshPoints = new List<Vector3>(tempPoints.Count);
foreach (var tp in tempPoints)
{
// Flip Y and Z
newMeshPoints.Add(new Vector3(tp.X, tp.Z, tp.Y));
}
var hullPoints = QuickHull(newMeshPoints.ToArray());
var ms = new MeshSimplifier();
var optimizedVerts = ms.Simplify(hullPoints);
brushVertices.Clear();
brushVertices.AddRange(hullPoints);
}
else
brushVertices.Clear();
*/
}
vertices = brushVertices.ToArray();
@@ -1715,7 +1678,6 @@ namespace Game
if (true)
{
int brushIndex = 0;
//foreach (var brush in root.entities[0].brushes.Skip(1).Take(1))
foreach (var brush in root.entities[0].brushes)
{
try
@@ -1726,9 +1688,6 @@ namespace Game
Vector2[] brushUvs = new Vector2[brushVertices.Length];
Vector3[] brushNormals = new Vector3[brushVertices.Length];
Vector2[] brushScaling = new Vector2[brushVertices.Length];
for (int i=0; i<brushVertices.Length; i+=3)
{
Vector3 v1 = brushVertices[i+0];
@@ -1737,9 +1696,7 @@ namespace Game
Vector3 normal = -Vector3.Cross(v3 - v1, v2 - v1).Normalized;
// fetch the texture parameters from the plane with matching normal
Vector2 uvScale = new Vector2(0f);
float uvRotation = 0f;
Vector2 uvOffset = new Vector2(0f);
@@ -1747,10 +1704,9 @@ namespace Game
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)
if ((brushPlane.plane.Normal - normal).Length < 0.01f)
{
normal = plane.Normal; // for consistency
normal = brushPlane.plane.Normal; // for consistency
uvScale = 1f / brushPlane.scale / textureSize; // texture size?
uvRotation = brushPlane.rotation;
uvOffset = brushPlane.offset * brushPlane.scale;
@@ -1759,55 +1715,55 @@ namespace Game
}
}
if (!found)
Console.Print("no found, bad geometry?");
Console.Print("no matching plane 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 []
Vector2 uv1, uv2, uv3;
// if quake format
{
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),
// The texture is projected to the surface from three angles, the axis with least
// distortion is chosen here.
new Tuple<float, Vector3>(Mathf.Abs(Vector3.Dot(normal2, Vector3.Up)), -Vector3.Up),
// Attempt to workaround most rounding errors at 45-degree angles which causes bias towards one axis.
// This behaviour is seemingly random in different engines and editors, so let's not bother.
Vector3 textureNormal = new Vector3((float)Math.Round(normal.X, 4),
(float)Math.Round(normal.Y, 4), (float)Math.Round(normal.Z, 4));
});
dots.Sort((l, r) => l.Item1.CompareTo(r.Item1));
Vector3 theUp = dots.Last().Item2;
var dotX = Math.Abs(Vector3.Dot(textureNormal, Vector3.Right));
var dotY = Math.Abs(Vector3.Dot(textureNormal, Vector3.Up));
var dotZ = Math.Abs(Vector3.Dot(textureNormal, Vector3.Forward));
/*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;*/
Vector3 axis;
if (dotY >= dotX && dotY >= dotZ)
axis = -Vector3.Up;
else if (dotX >= dotY && dotX >= dotZ)
axis = Vector3.Right;
else if (dotZ >= dotX && dotZ >= dotY)
axis = -Vector3.Forward;
else
axis = Vector3.Right;
var up1 = Vector3.Up;
var up2 = -Vector3.Forward;
var axisForward = Mathf.Abs(Vector3.Dot(axis, Vector3.Up)) > 0.01f
? -Vector3.Forward
: Vector3.Up;
var axisForward2 = Mathf.Abs(Vector3.Dot(axis, Vector3.Up)) > 0.01f
? Vector3.Up
: -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(up2, 180f * Mathf.DegreesToRadians);
rot = rot * Quaternion.RotationAxis(Mathf.Abs(Vector3.Dot(theUp, theRight)) > 0.01f ? theRight : finalUp2, uvRotation * Mathf.DegreesToRadians);
Quaternion rot = Quaternion.Identity;
rot = rot * Quaternion.LookRotation(axis, axisForward);
rot = rot * Quaternion.RotationAxis(-Vector3.Forward, 180f * Mathf.DegreesToRadians);
rot = rot * Quaternion.RotationAxis(
Mathf.Abs(Vector3.Dot(axis, Vector3.Right)) > 0.01f ? Vector3.Right : axisForward2,
uvRotation * Mathf.DegreesToRadians);
//if (Mathf.Abs(Vector3.Dot(theUp, theRight)) <= 0.01f)
// uvOffset *= 2;
uv1 = ((Vector2)(v1 * rot) + uvOffset) * uvScale;
uv2 = ((Vector2)(v2 * rot) + uvOffset) * uvScale;
uv3 = ((Vector2)(v3 * rot) + uvOffset) * uvScale;
}
brushUvs[i + 0] = ((Vector2)(v1 * rot) + (uvOffset)) * uvScale;
brushUvs[i + 1] = ((Vector2)(v2 * rot) + (uvOffset)) * uvScale;
brushUvs[i + 2] = ((Vector2)(v3 * rot) + (uvOffset)) * uvScale;
brushUvs[i + 0] = uv1;
brushUvs[i + 1] = uv2;
brushUvs[i + 2] = uv3;
brushNormals[i + 0] = normal;
brushNormals[i + 1] = normal;