Skip to content

Commit

Permalink
Implement displacements
Browse files Browse the repository at this point in the history
  • Loading branch information
H2xDev committed Feb 29, 2024
1 parent 6aca5f0 commit 18d6230
Show file tree
Hide file tree
Showing 13 changed files with 2,013 additions and 581 deletions.
124 changes: 124 additions & 0 deletions addons/godotvmf/utils/VMFDispTool.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
class_name VMFDispTool

var normals = [];
var distances = [];
var offsets = [];
var offsetNormals = [];
var alphas = [];
var triangleTags = [];
var allowedVerts = [];

var vertsCount = 0;
var edgesCount = 0;
var plane = null;
var dispInfo = null;
var startPoint = Vector3(0, 0, 0);
var side = null;

static func hasDisplacement(brush):
for side in brush.side:
if "dispinfo" in side:
return true;

return false;

func _init(side):
plane = side.plane.value;
dispInfo = side.dispinfo;
self.side = side;

var startNumbers = Array(dispInfo.startposition.split(" ")).map(func(x):
return float(x.replace('[', '').replace(']', '')));

startPoint = Vector3(startNumbers[0], startNumbers[1], startNumbers[2]);

vertsCount = pow(2, dispInfo.power) + 1;
edgesCount = vertsCount - 1;

normals = _parseVectors('normals');
distances = _parseFloats('distances');
offsets = _parseVectors('offsets');
offsetNormals = _parseVectors('offset_normals');
alphas = _parseFloats('alphas');

func convertVector(x, y):
var nx = x;
var ny = y;

return Vector2i(int(nx), int(ny));

func getNormal(x, y):
var index = y + x * vertsCount;
return normals[index];

func getOffset(x, y):
var index = y + x * vertsCount;
return offsets[index];

func getDistance(x, y):
var index = y + x * vertsCount;
return getNormal(x, y) * distances[index];

func getColor(x, y):
var index = y + x * vertsCount;
return Color(1, 1, 1, 1.0 - alphas[index] / 255);

func getVertices(side, brush):
var old = true;

var vertices = side.vertices_plus.v # VMFTool.calculateVertices(side, brush);

var res = [];

var startIndex = vertices.bsearch(vertices.find(func(v): v.distance_to(startPoint) < 0.001));

var tl = vertices[(0 + startIndex) % 4];
var tr = vertices[(1 + startIndex) % 4];
var br = vertices[(2 + startIndex) % 4];
var bl = vertices[(3 + startIndex) % 4];

for i in range(0, pow(vertsCount, 2)):
var x = i / int(vertsCount);
var y = i % int(vertsCount);

var rblend = 1 - x / edgesCount;

var vl = tl.lerp(bl, rblend);
var vr = tr.lerp(br, rblend);
var cblend = y / edgesCount;
var vert = vl.lerp(vr, cblend);

var offset = offsets[i];

vert += (
normals[i] * distances[i] + offset +
side.plane.value.normal * dispInfo.elevation
);

res.append(vert);
return res;

func _parseVectors(key):
var vects = [];

for row in dispInfo[key].values():
row = Array(row.split(" ")).map(func(x): return float(x));
var vecset = [];

for i in range(0, row.size() / 3):
vecset.append(Vector3(row[i * 3], row[i * 3 + 1], row[i * 3 + 2]));

vects.append_array(vecset);

return vects;

func _parseFloats(key):
var floats = [];

for row in dispInfo[key].values():
row = Array(row.split(" ")).map(func(x): return float(x));

floats.append_array(row);

return floats;

Loading

0 comments on commit 18d6230

Please sign in to comment.