Skip to content

Commit b7493c8

Browse files
committed
fix: TModel and TModel projection separated
- model.simba moved to utils - model ToBytes and FromBytes added
1 parent b7bad26 commit b7493c8

7 files changed

Lines changed: 273 additions & 88 deletions

File tree

osrs.simba

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ Summary: It includes this file until the current file is reached.
120120
{$IFNDEF WL_MM2MS_INCLUDED} {$INCLUDE_ONCE osrs/projection/mm2ms.simba}
121121
{$IFNDEF WL_CAMERA_INCLUDED} {$INCLUDE_ONCE osrs/projection/camera.simba}
122122
{$IFNDEF WL_PROJECTION_INCLUDED} {$INCLUDE_ONCE osrs/projection/projection.simba}
123-
{$IFNDEF WL_MODEL_INCLUDED} {$INCLUDE_ONCE osrs/projection/model.simba}
124123

125124
{$IFNDEF WL_ANTIBAN_INCLUDED} {$INCLUDE_ONCE osrs/antiban/antiban.simba}
126125

@@ -188,4 +187,4 @@ Summary: It includes this file until the current file is reached.
188187
{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}
189188
{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}
190189
{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}
191-
{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}
190+
{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}

osrs/position/map/entities.simba

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ var
212212
begin
213213
pt := me + vector.ToPoint() - Minimap.Center;
214214
if Self.Model <> nil then
215-
Exit(Self.Model.GetArea(Self.Model.Project(vector.ToVec3(Self.Walker^.Height(pt, False) - height), 0, radians)));
215+
Exit(Self.Model.GetArea(Projection.ProjectModel(Self.Model, vector.ToVec3(Self.Walker^.Height(pt, False) - height), 0, radians)));
216216

217217

218218
size.X := size.X*2;

osrs/position/map/objects.simba

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ var
222222
begin
223223
pt := me + vector.ToPoint() - Minimap.Center;
224224
if Self.Model <> nil then
225-
Exit(Self.Model.GetArea(Self.Model.Project(vector.ToVec3(Self.Walker^.Height(pt, False) - height), rotation, radians)));
225+
Exit(Self.Model.GetArea(Projection.ProjectModel(Self.Model, vector.ToVec3(Self.Walker^.Height(pt, False) - height), rotation, radians)));
226226

227227
size.X := size.X*2;
228228
size.Y := size.Y*2;

osrs/projection/projection.simba

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,77 @@ begin
119119
end;
120120

121121

122+
(*
123+
## Projection.ProjectModel
124+
```pascal
125+
function TProjection.ProjectModelEx(model: TModel; minimapCoord: TVector3; heights: array [0..3] of Single; rotation, radians: Single): TPointArray;
126+
function TProjection.ProjectModel(model: TModel; minimapCoord: TVector3; rotation, radians: Single): TPointArray;
127+
```
128+
Projects a model from a `Minimap Coordinate` to the `MainScreen`.
129+
130+
Example:
131+
```pascal
132+
{$I WaspLib/osrs.simba}
133+
var
134+
model: TModel;
135+
tpa: TPointArray;
136+
begin
137+
model := new TModel('path/to/my/model.obj');
138+
tpa := Projection.ProjectModel(Minimap.Center.ToVec3(), Minimap.CompassRadians, 0);
139+
ShowOnTarget(tpa);
140+
end.
141+
```
142+
143+
```{figure} ../../images/modelproject.png
144+
```
145+
*)
146+
function TProjection.ProjectModelEx(model: TModel; minimapCoord: TVector3; heights: array [0..3] of Single; rotation, radians: Single): TPointArray;
147+
var
148+
i: Integer;
149+
vertices: TVector3Array;
150+
width, depth: Single;
151+
begin
152+
width := model.Width/2;
153+
depth := model.Depth/2;
154+
155+
for i := 0 to High(model.Vertices) do
156+
begin
157+
vertices += model.Vertices[i].Rotate(rotation, minimapCoord.X, minimapCoord.Y);
158+
159+
if vertices[i].X < width then
160+
begin
161+
if vertices[i].Y < depth then vertices[i].Z += heights[0]
162+
else if vertices[i].Y > depth then vertices[i].Z += heights[2]
163+
else vertices[i].Z += (heights[0] + heights[2]) / 2;
164+
end
165+
else if vertices[i].X > width then
166+
begin
167+
if vertices[i].Y < depth then vertices[i].Z += heights[1]
168+
else if vertices[i].Y > depth then vertices[i].Z += heights[3]
169+
else vertices[i].Z += (heights[1] + heights[3]) / 2;
170+
end
171+
else
172+
begin
173+
if vertices[i].Y < depth then vertices[i].Z += (heights[0]+heights[1]) / 2
174+
else if vertices[i].Y > depth then vertices[i].Z += (heights[2]+heights[3]) / 2
175+
else vertices[i].Z += (heights[0] + heights[1] + heights[2] + heights[3]) / 4;
176+
end;
177+
178+
vertices[i].X += minimapCoord.X;
179+
vertices[i].Y += minimapCoord.Y;
180+
vertices[i].Z += minimapCoord.Z;
181+
end;
182+
183+
Result := Self.Run(vertices, radians);
184+
end;
185+
186+
function TProjection.ProjectModel(model: TModel; minimapCoord: TVector3; rotation, radians: Single): TPointArray;
187+
begin
188+
with minimapCoord do
189+
Result := Self.Run(model.Vertices.Offset([X,Y,Z]).Rotate(rotation,X,Y), radians);
190+
end;
191+
192+
122193
var
123194
(*
124195
## MM2MS variable
@@ -329,7 +400,6 @@ begin
329400
end;
330401

331402

332-
333403
(*
334404
## Minimap.ZoomQuad
335405
```pascal

utils.simba

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@
3030
{$IFNDEF WL_PROFILE_FORM_INCLUDED} {$INCLUDE_ONCE utils/forms/profile_form.simba}
3131
{$IFNDEF WL_RECORDER_INCLUDED} {$INCLUDE_ONCE utils/recorder.simba}
3232
{$IFNDEF WL_SCRIPTFORM_INCLUDED} {$INCLUDE_ONCE utils/forms/scriptform.simba}
33+
{$IFNDEF WL_MODEL_INCLUDED} {$INCLUDE_ONCE utils/math/model.simba}
34+
3335
{$IFNDEF WL_CONVERSIONS_INCLUDED} {$INCLUDE_ONCE utils/math/conversions.simba}
3436

3537
{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}
3638
{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}
3739
{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}
40+
{$ENDIF}

utils/math/conversions.simba

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,185 @@ begin
191191
SetLength(Result, len div SizeOf(TGraphNode));
192192
Move(bytes[0], Result[0], len);
193193
end;
194+
195+
196+
function TVector3Array.ToBytes(): TByteArray;
197+
var
198+
len: Integer;
199+
begin
200+
len := Length(Self) * SizeOf(TVector3);
201+
if len = 0 then Exit;
202+
SetLength(Result, len);
203+
Move(Self[0], Result[0], len);
204+
end;
205+
206+
function TVector3Array.CreateFromBytes(bytes: TByteArray): TVector3Array; static;
207+
var
208+
len: Integer;
209+
begin
210+
len := Length(bytes);
211+
if len = 0 then Exit;
212+
SetLength(Result, len div SizeOf(TVector3));
213+
Move(bytes[0], Result[0], len);
214+
end;
215+
216+
217+
function TColorArray.ToBytes(): TByteArray;
218+
var
219+
len: Integer;
220+
begin
221+
len := Length(Self) * SizeOf(Self[0]);
222+
if len = 0 then Exit;
223+
SetLength(Result, len);
224+
Move(Self[0], Result[0], len);
225+
end;
226+
227+
function TColorArray.CreateFromBytes(bytes: TByteArray): TColorArray; static;
228+
var
229+
len, size: Integer;
230+
begin
231+
len := Length(bytes);
232+
if len = 0 then Exit;
233+
size := SizeOf(Result[0]);
234+
SetLength(Result, len div size);
235+
Move(bytes[0], Result[0], len);
236+
end;
237+
238+
239+
function TModelFaceArray.ToBytes(): TByteArray;
240+
var
241+
len: Integer;
242+
begin
243+
len := Length(Self) * SizeOf(TModelFace);
244+
if len = 0 then Exit;
245+
SetLength(Result, len);
246+
Move(Self[0], Result[0], len);
247+
end;
248+
249+
function TModelFaceArray.CreateFromBytes(bytes: TByteArray): TModelFaceArray; static;
250+
var
251+
len: Integer;
252+
begin
253+
len := Length(bytes);
254+
if len = 0 then Exit;
255+
SetLength(Result, len div SizeOf(TModelFace));
256+
Move(bytes[0], Result[0], len);
257+
end;
258+
259+
194260
{$R+}
261+
262+
function TModel.ToBytes(): TByteArray;
263+
var
264+
size, offset: Integer;
265+
vertices, faces, materials: TByteArray;
266+
begin
267+
vertices := Self.Vertices.ToBytes();
268+
Inc(size, SizeOf(Int32) + Length(vertices));
269+
270+
faces := TModelFaceArray(Self.Faces).ToBytes();
271+
Inc(size, SizeOf(Int32) + Length(faces));
272+
273+
materials := Self.Materials.ToBytes();
274+
Inc(size, SizeOf(Int32) + Length(materials));
275+
276+
Inc(size, SizeOf(Int32) + Length(Self.Alphas));
277+
278+
Inc(size, SizeOf(Single) * 3);
279+
Inc(size, SizeOf(TVector3));
280+
281+
SetLength(Result, size);
282+
283+
Move(Length(vertices), Result[offset], SizeOf(Int32));
284+
Inc(offset, SizeOf(Int32));
285+
if Length(vertices) > 0 then
286+
begin
287+
Move(vertices[0], Result[offset], Length(vertices));
288+
Inc(offset, Length(vertices));
289+
end;
290+
291+
faces := TModelFaceArray(Self.Faces).ToBytes();
292+
Move(Length(faces), Result[offset], SizeOf(Int32));
293+
Inc(offset, SizeOf(Int32));
294+
if Length(faces) > 0 then
295+
begin
296+
Move(faces[0], Result[offset], Length(faces));
297+
Inc(offset, Length(faces));
298+
end;
299+
300+
materials := Self.Materials.ToBytes();
301+
Move(Length(materials), Result[offset], SizeOf(Int32));
302+
Inc(offset, SizeOf(Int32));
303+
if Length(materials) > 0 then
304+
begin
305+
Move(materials[0], Result[offset], Length(materials));
306+
Inc(offset, Length(materials));
307+
end;
308+
309+
Move(Length(Self.Alphas), Result[offset], SizeOf(Int32));
310+
Inc(offset, SizeOf(Int32));
311+
if Length(Self.Alphas) > 0 then
312+
begin
313+
Move(Self.Alphas[0], Result[offset], Length(Self.Alphas));
314+
Inc(offset, Length(Self.Alphas));
315+
end;
316+
317+
Move(Self.Width, Result[offset], SizeOf(Single)); Inc(offset, SizeOf(Single));
318+
Move(Self.Depth, Result[offset], SizeOf(Single)); Inc(offset, SizeOf(Single));
319+
Move(Self.Height, Result[offset], SizeOf(Single)); Inc(offset, SizeOf(Single));
320+
Move(Self.Center, Result[offset], SizeOf(TVector3));
321+
end;
322+
323+
function TModel.Construct(bytes: TByteArray): TModel; static; overload;
324+
var
325+
offset, len: Integer;
326+
tmp: TByteArray;
327+
begin
328+
//Vertices
329+
Move(bytes[offset], len, SizeOf(Int32));
330+
Inc(offset, SizeOf(Int32));
331+
if len > 0 then
332+
begin
333+
SetLength(tmp, len);
334+
Move(bytes[offset], tmp[0], len);
335+
Result.Vertices := TVector3Array.CreateFromBytes(tmp);
336+
Inc(offset, len);
337+
end;
338+
339+
// Faces
340+
Move(bytes[offset], len, SizeOf(Int32));
341+
Inc(offset, SizeOf(Int32));
342+
if len > 0 then
343+
begin
344+
SetLength(tmp, len);
345+
Move(bytes[offset], tmp[0], len);
346+
Result.Faces := TModelFaceArray.CreateFromBytes(tmp);
347+
Inc(offset, len);
348+
end;
349+
350+
// Materials
351+
Move(bytes[offset], len, SizeOf(Int32));
352+
Inc(offset, SizeOf(Int32));
353+
if len > 0 then
354+
begin
355+
SetLength(tmp, len);
356+
Move(bytes[offset], tmp[0], len);
357+
Result.Materials := TColorArray.CreateFromBytes(tmp);
358+
Inc(offset, len);
359+
end;
360+
361+
// Alphas
362+
Move(bytes[offset], len, SizeOf(Int32));
363+
Inc(offset, SizeOf(Int32));
364+
SetLength(Result.Alphas, len);
365+
if len > 0 then
366+
begin
367+
Move(bytes[offset], Result.Alphas[0], len);
368+
Inc(offset, len);
369+
end;
370+
371+
Move(bytes[offset], Result.Width, SizeOf(Single)); Inc(offset, SizeOf(Single));
372+
Move(bytes[offset], Result.Depth, SizeOf(Single)); Inc(offset, SizeOf(Single));
373+
Move(bytes[offset], Result.Height, SizeOf(Single)); Inc(offset, SizeOf(Single));
374+
Move(bytes[offset], Result.Center, SizeOf(TVector3));
375+
end;

0 commit comments

Comments
 (0)