| 1 |
{ |
| 2 |
Copyright 2008-2023 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ CastleScript vector and matrix types and built-in functions. } |
| 17 |
unit CastleScriptVectors; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses CastleVectors, CastleScript; |
| 24 |
|
| 25 |
{$ifdef CASTLE_SCRIPT_FPC} // TODO: Our current usage of generics doesn't compile with Delphi |
| 26 |
|
| 27 |
type |
| 28 |
TCasScriptVector = class; |
| 29 |
TCasScriptVectorD = class; |
| 30 |
|
| 31 |
{$ifdef FPC}generic{$endif} |
| 32 |
TCasScriptVec< |
| 33 |
TVectorXxx, |
| 34 |
TCasScriptVectorFunXxx> = class(TCasScriptValue) |
| 35 |
private |
| 36 |
type |
| 37 |
TSelfClass = TCasScriptVec {$ifndef FPC} < |
| 38 |
TVectorXxx, |
| 39 |
TCasScriptVectorFunXxx> {$endif}; |
| 40 |
|
| 41 |
{ Create and make Value an instance of TSelfClass. } |
| 42 |
class function CreateValueIfNeededSelf(var Value: TCasScriptValue; |
| 43 |
var ParentOfValue: boolean): TSelfClass; |
| 44 |
|
| 45 |
class procedure HandleAdd(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 46 |
class procedure HandleSubtract(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 47 |
class procedure HandleNegate(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 48 |
class procedure HandleEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 49 |
class procedure HandleNotEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 50 |
|
| 51 |
class procedure HandleMultiply(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 52 |
class procedure HandleDivide(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 53 |
class procedure HandleLerp(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 54 |
|
| 55 |
class procedure HandleMax(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 56 |
class procedure HandleMin(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 57 |
|
| 58 |
class procedure HandleVector(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 59 |
class procedure HandleVectorGet(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 60 |
class procedure HandleVectorSet(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 61 |
class procedure HandleVectorGetCount(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 62 |
class procedure HandleVectorLength(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 63 |
class procedure HandleVectorSqrLength(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 64 |
class procedure HandleVectorDot(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 65 |
|
| 66 |
class function VectorGetCount: Integer; |
| 67 |
class procedure RegisterFunctions; |
| 68 |
private |
| 69 |
FValue: TVectorXxx; |
| 70 |
procedure SetValue(const AValue: TVectorXxx); |
| 71 |
public |
| 72 |
property Value: TVectorXxx read FValue write SetValue; |
| 73 |
|
| 74 |
procedure AssignValue(Source: TCasScriptValue); override; |
| 75 |
end; |
| 76 |
|
| 77 |
TCasScriptVec2f = class({$ifdef FPC}specialize{$endif} TCasScriptVec< |
| 78 |
TVector2, |
| 79 |
TCasScriptVector>) |
| 80 |
end; |
| 81 |
|
| 82 |
TCasScriptVec3f = class({$ifdef FPC}specialize{$endif} TCasScriptVec< |
| 83 |
TVector3, |
| 84 |
TCasScriptVector>) |
| 85 |
private |
| 86 |
class procedure HandleVectorCross(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 87 |
class procedure HandleGrayscale(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 88 |
end; |
| 89 |
|
| 90 |
TCasScriptVec4f = class({$ifdef FPC}specialize{$endif} TCasScriptVec< |
| 91 |
TVector4, |
| 92 |
TCasScriptVector>) |
| 93 |
private |
| 94 |
class procedure HandleOrientationFromDirectionUp(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 95 |
class procedure HandleRotateCore( |
| 96 |
const Rotation: TVector4; const Point: TVector3; |
| 97 |
var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 98 |
class procedure HandleRotate(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 99 |
class procedure HandleOrientationToDirection(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 100 |
class procedure HandleOrientationToUp(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 101 |
class procedure HandleSlerp(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 102 |
end; |
| 103 |
|
| 104 |
TCasScriptVec2d = class({$ifdef FPC}specialize{$endif} TCasScriptVec< |
| 105 |
TVector2Double, |
| 106 |
TCasScriptVectorD>) |
| 107 |
end; |
| 108 |
|
| 109 |
TCasScriptVec3d = class({$ifdef FPC}specialize{$endif} TCasScriptVec< |
| 110 |
TVector3Double, |
| 111 |
TCasScriptVectorD>) |
| 112 |
private |
| 113 |
class procedure HandleVectorCross(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 114 |
end; |
| 115 |
|
| 116 |
TCasScriptVec4d = class({$ifdef FPC}specialize{$endif} TCasScriptVec< |
| 117 |
TVector4Double, |
| 118 |
TCasScriptVectorD>) |
| 119 |
end; |
| 120 |
|
| 121 |
{$ifdef FPC}generic{$endif} |
| 122 |
TCasScriptMatrix< |
| 123 |
TCasScriptVecXxx, |
| 124 |
TMatrixXxx, |
| 125 |
TVectorXxx> = class(TCasScriptValue) |
| 126 |
private |
| 127 |
type |
| 128 |
TSelfClass = TCasScriptMatrix {$ifndef FPC} < |
| 129 |
TCasScriptVecXxx, |
| 130 |
TMatrixXxx, |
| 131 |
TVectorXxx> {$endif}; |
| 132 |
|
| 133 |
{ Create and make Value an instance of TSelfClass. } |
| 134 |
class function CreateValueIfNeededSelf(var Value: TCasScriptValue; |
| 135 |
var ParentOfValue: boolean): TSelfClass; |
| 136 |
|
| 137 |
class procedure HandleAdd(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 138 |
class procedure HandleSubtract(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 139 |
class procedure HandleNegate(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 140 |
class procedure HandleMultiply(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 141 |
class procedure HandleDivide(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 142 |
class procedure HandleLerp(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 143 |
class procedure HandleEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 144 |
class procedure HandleNotEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 145 |
|
| 146 |
class procedure HandleMatrix(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 147 |
class procedure HandleMatrixGet(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 148 |
class procedure HandleMatrixSet(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 149 |
class procedure HandleMatrixGetCount(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 150 |
|
| 151 |
class function MatrixGetCount: Integer; |
| 152 |
class procedure RegisterFunctions; |
| 153 |
private |
| 154 |
FValue: TMatrixXxx; |
| 155 |
procedure SetValue(const AValue: TMatrixXxx); |
| 156 |
public |
| 157 |
property Value: TMatrixXxx read FValue write SetValue; |
| 158 |
|
| 159 |
procedure AssignValue(Source: TCasScriptValue); override; |
| 160 |
end; |
| 161 |
|
| 162 |
TCasScriptMatrix3f = class({$ifdef FPC}specialize{$endif} TCasScriptMatrix< |
| 163 |
TCasScriptVec3f, |
| 164 |
TMatrix3, |
| 165 |
TVector3>) |
| 166 |
end; |
| 167 |
|
| 168 |
TCasScriptMatrix4f = class({$ifdef FPC}specialize{$endif} TCasScriptMatrix< |
| 169 |
TCasScriptVec4f, |
| 170 |
TMatrix4, |
| 171 |
TVector4>) |
| 172 |
end; |
| 173 |
|
| 174 |
TCasScriptMatrix3d = class({$ifdef FPC}specialize{$endif} TCasScriptMatrix< |
| 175 |
TCasScriptVec3d, |
| 176 |
TMatrix3Double, |
| 177 |
TVector3Double>) |
| 178 |
end; |
| 179 |
|
| 180 |
TCasScriptMatrix4d = class({$ifdef FPC}specialize{$endif} TCasScriptMatrix< |
| 181 |
TCasScriptVec4d, |
| 182 |
TMatrix4Double, |
| 183 |
TVector4Double>) |
| 184 |
end; |
| 185 |
|
| 186 |
TCasScriptVector = class(TCasScriptFunction) |
| 187 |
public |
| 188 |
class function ShortName: string; override; |
| 189 |
end; |
| 190 |
|
| 191 |
TCasScriptVectorD = class(TCasScriptFunction) |
| 192 |
public |
| 193 |
class function ShortName: string; override; |
| 194 |
end; |
| 195 |
|
| 196 |
TCasScriptVectorGet = class(TCasScriptFunction) |
| 197 |
public |
| 198 |
class function ShortName: string; override; |
| 199 |
end; |
| 200 |
|
| 201 |
TCasScriptVectorSet = class(TCasScriptFunction) |
| 202 |
public |
| 203 |
class function ShortName: string; override; |
| 204 |
class function ArgumentMustBeAssignable(const Index: Integer): boolean; override; |
| 205 |
end; |
| 206 |
|
| 207 |
TCasScriptVectorGetCount = class(TCasScriptFunction) |
| 208 |
public |
| 209 |
class function ShortName: string; override; |
| 210 |
end; |
| 211 |
|
| 212 |
TCasScriptVectorLength = class(TCasScriptFunction) |
| 213 |
public |
| 214 |
class function ShortName: string; override; |
| 215 |
end; |
| 216 |
|
| 217 |
TCasScriptVectorSqrLength = class(TCasScriptFunction) |
| 218 |
public |
| 219 |
class function ShortName: string; override; |
| 220 |
end; |
| 221 |
|
| 222 |
TCasScriptVectorDot = class(TCasScriptFunction) |
| 223 |
public |
| 224 |
class function ShortName: string; override; |
| 225 |
end; |
| 226 |
|
| 227 |
TCasScriptVectorCross = class(TCasScriptFunction) |
| 228 |
public |
| 229 |
class function ShortName: string; override; |
| 230 |
end; |
| 231 |
|
| 232 |
TCasScriptGrayscale = class(TCasScriptFunction) |
| 233 |
public |
| 234 |
class function ShortName: string; override; |
| 235 |
end; |
| 236 |
|
| 237 |
TCasScriptOrientationFromDirectionUp = class(TCasScriptFunction) |
| 238 |
public |
| 239 |
class function ShortName: string; override; |
| 240 |
end; |
| 241 |
|
| 242 |
TCasScriptRotate = class(TCasScriptFunction) |
| 243 |
public |
| 244 |
class function ShortName: string; override; |
| 245 |
end; |
| 246 |
|
| 247 |
TCasScriptOrientationToDirection = class(TCasScriptFunction) |
| 248 |
public |
| 249 |
class function ShortName: string; override; |
| 250 |
end; |
| 251 |
|
| 252 |
TCasScriptOrientationToUp = class(TCasScriptFunction) |
| 253 |
public |
| 254 |
class function ShortName: string; override; |
| 255 |
end; |
| 256 |
|
| 257 |
TCasScriptSlerp = class(TCasScriptFunction) |
| 258 |
public |
| 259 |
class function ShortName: string; override; |
| 260 |
end; |
| 261 |
|
| 262 |
TCasScriptMatrixFun = class(TCasScriptFunction) |
| 263 |
public |
| 264 |
class function ShortName: string; override; |
| 265 |
end; |
| 266 |
|
| 267 |
TCasScriptMatrixGet = class(TCasScriptFunction) |
| 268 |
public |
| 269 |
class function ShortName: string; override; |
| 270 |
end; |
| 271 |
|
| 272 |
TCasScriptMatrixSet = class(TCasScriptFunction) |
| 273 |
public |
| 274 |
class function ShortName: string; override; |
| 275 |
class function ArgumentMustBeAssignable(const Index: Integer): boolean; override; |
| 276 |
end; |
| 277 |
|
| 278 |
TCasScriptMatrixGetCount = class(TCasScriptFunction) |
| 279 |
public |
| 280 |
class function ShortName: string; override; |
| 281 |
end; |
| 282 |
|
| 283 |
{$endif CASTLE_SCRIPT_FPC} |
| 284 |
|
| 285 |
implementation |
| 286 |
|
| 287 |
uses Math, |
| 288 |
CastleScriptCoreFunctions, CastleUtils, CastleLog, CastleTransform, |
| 289 |
CastleQuaternions, CastleColors; |
| 290 |
|
| 291 |
{$ifdef CASTLE_SCRIPT_FPC} // TODO: Our current usage of generics doesn't compile with Delphi |
| 292 |
|
| 293 |
{ TCasScriptVec ---------------------------------------------------------- } |
| 294 |
|
| 295 |
class function TCasScriptVec {$ifndef FPC} < |
| 296 |
TVectorXxx, |
| 297 |
TCasScriptVectorFunXxx> {$endif} . |
| 298 |
CreateValueIfNeededSelf(var Value: TCasScriptValue; var ParentOfValue: boolean): TSelfClass; |
| 299 |
begin |
| 300 |
CreateValueIfNeeded(Value, ParentOfValue, |
| 301 |
{$ifdef FPC}TCasScriptValueClass(ClassType){$else}Self{$endif}); |
| 302 |
Result := TSelfClass(Value); |
| 303 |
end; |
| 304 |
|
| 305 |
class procedure TCasScriptVec {$ifndef FPC} < |
| 306 |
TVectorXxx, |
| 307 |
TCasScriptVectorFunXxx> {$endif} . |
| 308 |
HandleAdd(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 309 |
var |
| 310 |
I: Integer; |
| 311 |
MyResult: TSelfClass; |
| 312 |
{$ifndef FPC} |
| 313 |
Arg: TSelfClass; |
| 314 |
{$endif} |
| 315 |
begin |
| 316 |
MyResult := CreateValueIfNeededSelf(AResult, ParentOfResult); |
| 317 |
|
| 318 |
{ The function allows only >= 1 arguments, and this handler is |
| 319 |
registered only for TCasScriptVec values, so we can safely take |
| 320 |
the first arg as TCasScriptVec. } |
| 321 |
MyResult.Value := TSelfClass(Arguments[0]).Value; |
| 322 |
|
| 323 |
for I := 1 to Length(Arguments) - 1 do |
| 324 |
begin |
| 325 |
{$ifndef FPC}Arg := (Arguments[I] as TSelfClass);{$endif} |
| 326 |
MyResult.Value := MyResult.Value + {$ifdef FPC}TSelfClass(Arguments[I]){$else}Arg{$endif}.Value; |
| 327 |
end; |
| 328 |
end; |
| 329 |
|
| 330 |
class procedure TCasScriptVec {$ifndef FPC} < |
| 331 |
TVectorXxx, |
| 332 |
TCasScriptVectorFunXxx> {$endif} . |
| 333 |
HandleSubtract(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 334 |
var |
| 335 |
I: Integer; |
| 336 |
MyResult: TSelfClass; |
| 337 |
begin |
| 338 |
MyResult := CreateValueIfNeededSelf(AResult, ParentOfResult); |
| 339 |
MyResult.Value := TSelfClass(Arguments[0]).Value; |
| 340 |
for I := 1 to Length(Arguments) - 1 do |
| 341 |
MyResult.Value := MyResult.Value - TSelfClass(Arguments[I]).Value; |
| 342 |
end; |
| 343 |
|
| 344 |
class procedure TCasScriptVec {$ifndef FPC} < |
| 345 |
TVectorXxx, |
| 346 |
TCasScriptVectorFunXxx> {$endif} . |
| 347 |
HandleNegate(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 348 |
var |
| 349 |
MyResult: TSelfClass; |
| 350 |
begin |
| 351 |
MyResult := CreateValueIfNeededSelf(AResult, ParentOfResult); |
| 352 |
MyResult.Value := -TSelfClass(Arguments[0]).Value; |
| 353 |
end; |
| 354 |
|
| 355 |
class procedure TCasScriptVec {$ifndef FPC} < |
| 356 |
TVectorXxx, |
| 357 |
TCasScriptVectorFunXxx> {$endif} . |
| 358 |
HandleEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 359 |
begin |
| 360 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 361 |
TCasScriptBoolean(AResult).Value := |
| 362 |
TVectorXxx.PerfectlyEquals( |
| 363 |
TSelfClass(Arguments[0]).Value, |
| 364 |
TSelfClass(Arguments[1]).Value); |
| 365 |
end; |
| 366 |
|
| 367 |
class procedure TCasScriptVec {$ifndef FPC} < |
| 368 |
TVectorXxx, |
| 369 |
TCasScriptVectorFunXxx> {$endif} . |
| 370 |
HandleNotEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 371 |
begin |
| 372 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 373 |
TCasScriptBoolean(AResult).Value := |
| 374 |
not TVectorXxx.PerfectlyEquals( |
| 375 |
TSelfClass(Arguments[0]).Value, |
| 376 |
TSelfClass(Arguments[1]).Value); |
| 377 |
end; |
| 378 |
|
| 379 |
class procedure TCasScriptVec {$ifndef FPC} < |
| 380 |
TVectorXxx, |
| 381 |
TCasScriptVectorFunXxx> {$endif} . |
| 382 |
HandleMultiply(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 383 |
var |
| 384 |
MyResult: TSelfClass; |
| 385 |
begin |
| 386 |
MyResult := CreateValueIfNeededSelf(AResult, ParentOfResult); |
| 387 |
if Arguments[0] is TCasScriptFloat then |
| 388 |
begin |
| 389 |
MyResult.Value := |
| 390 |
TSelfClass(Arguments[1]).Value * |
| 391 |
TCasScriptFloat(Arguments[0]).Value; |
| 392 |
end else |
| 393 |
begin |
| 394 |
MyResult.Value := |
| 395 |
TSelfClass(Arguments[0]).Value * |
| 396 |
TCasScriptFloat(Arguments[1]).Value; |
| 397 |
end; |
| 398 |
end; |
| 399 |
|
| 400 |
class procedure TCasScriptVec {$ifndef FPC} < |
| 401 |
TVectorXxx, |
| 402 |
TCasScriptVectorFunXxx> {$endif} . |
| 403 |
HandleDivide(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 404 |
var |
| 405 |
MyResult: TSelfClass; |
| 406 |
begin |
| 407 |
MyResult := CreateValueIfNeededSelf(AResult, ParentOfResult); |
| 408 |
MyResult.Value := |
| 409 |
TSelfClass(Arguments[0]).Value * |
| 410 |
1/TCasScriptFloat(Arguments[1]).Value; |
| 411 |
end; |
| 412 |
|
| 413 |
class procedure TCasScriptVec {$ifndef FPC} < |
| 414 |
TVectorXxx, |
| 415 |
TCasScriptVectorFunXxx> {$endif} . |
| 416 |
HandleLerp(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 417 |
var |
| 418 |
MyResult: TSelfClass; |
| 419 |
begin |
| 420 |
MyResult := CreateValueIfNeededSelf(AResult, ParentOfResult); |
| 421 |
MyResult.Value := TVectorXxx.Lerp( |
| 422 |
TCasScriptFloat(Arguments[0]).Value, |
| 423 |
TSelfClass(Arguments[1]).Value, |
| 424 |
TSelfClass(Arguments[2]).Value); |
| 425 |
end; |
| 426 |
|
| 427 |
class procedure TCasScriptVec {$ifndef FPC} < |
| 428 |
TVectorXxx, |
| 429 |
TCasScriptVectorFunXxx> {$endif} . |
| 430 |
HandleMax(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 431 |
var |
| 432 |
I: Integer; |
| 433 |
MyResult: TSelfClass; |
| 434 |
begin |
| 435 |
MyResult := CreateValueIfNeededSelf(AResult, ParentOfResult); |
| 436 |
for I := 0 to VectorGetCount - 1 do |
| 437 |
MyResult.FValue.Data[I] := |
| 438 |
Max( TSelfClass(Arguments[0]).FValue[I], |
| 439 |
TSelfClass(Arguments[1]).FValue[I] ); |
| 440 |
MyResult.ValueAssigned := true; |
| 441 |
end; |
| 442 |
|
| 443 |
class procedure TCasScriptVec {$ifndef FPC} < |
| 444 |
TVectorXxx, |
| 445 |
TCasScriptVectorFunXxx> {$endif} . |
| 446 |
HandleMin(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 447 |
var |
| 448 |
I: Integer; |
| 449 |
MyResult: TSelfClass; |
| 450 |
begin |
| 451 |
MyResult := CreateValueIfNeededSelf(AResult, ParentOfResult); |
| 452 |
for I := 0 to VectorGetCount - 1 do |
| 453 |
MyResult.FValue.Data[I] := |
| 454 |
Min( TSelfClass(Arguments[0]).FValue[I], |
| 455 |
TSelfClass(Arguments[1]).FValue[I] ); |
| 456 |
MyResult.ValueAssigned := true; |
| 457 |
end; |
| 458 |
|
| 459 |
class procedure TCasScriptVec {$ifndef FPC} < |
| 460 |
TVectorXxx, |
| 461 |
TCasScriptVectorFunXxx> {$endif} . |
| 462 |
HandleVector(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 463 |
var |
| 464 |
I: Integer; |
| 465 |
MyResult: TSelfClass; |
| 466 |
begin |
| 467 |
MyResult := CreateValueIfNeededSelf(AResult, ParentOfResult); |
| 468 |
for I := 0 to VectorGetCount - 1 do |
| 469 |
MyResult.FValue.Data[I] := TCasScriptFloat(Arguments[I]).Value; |
| 470 |
MyResult.ValueAssigned := true; |
| 471 |
end; |
| 472 |
|
| 473 |
class procedure TCasScriptVec {$ifndef FPC} < |
| 474 |
TVectorXxx, |
| 475 |
TCasScriptVectorFunXxx> {$endif} . |
| 476 |
HandleVectorGet(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 477 |
var |
| 478 |
Index: Integer; |
| 479 |
begin |
| 480 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 481 |
|
| 482 |
Index := TCasScriptInteger(Arguments[1]).Value; |
| 483 |
if not Between(Index, 0, VectorGetCount - 1) then |
| 484 |
raise ECasScriptError.CreateFmt('Invalid index %d for vector_get on %d-element vector', |
| 485 |
[Index, VectorGetCount]); |
| 486 |
|
| 487 |
TCasScriptFloat(AResult).Value := TSelfClass(Arguments[0]).Value[Index]; |
| 488 |
end; |
| 489 |
|
| 490 |
class procedure TCasScriptVec {$ifndef FPC} < |
| 491 |
TVectorXxx, |
| 492 |
TCasScriptVectorFunXxx> {$endif} . |
| 493 |
HandleVectorSet(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 494 |
var |
| 495 |
Index: Integer; |
| 496 |
begin |
| 497 |
if ParentOfResult then |
| 498 |
AResult.FreeByParentExpression; |
| 499 |
AResult := nil; |
| 500 |
ParentOfResult := false; |
| 501 |
|
| 502 |
Index := TCasScriptInteger(Arguments[1]).Value; |
| 503 |
if not Between(Index, 0, VectorGetCount - 1) then |
| 504 |
raise ECasScriptError.CreateFmt('Invalid index %d for vector_set on %d-element vector', |
| 505 |
[Index, VectorGetCount]); |
| 506 |
|
| 507 |
TSelfClass(Arguments[0]).FValue.Data[Index] := TCasScriptFloat(Arguments[2]).Value; |
| 508 |
TSelfClass(Arguments[0]).ValueAssigned := true; |
| 509 |
|
| 510 |
AResult := Arguments[0]; |
| 511 |
end; |
| 512 |
|
| 513 |
class procedure TCasScriptVec {$ifndef FPC} < |
| 514 |
TVectorXxx, |
| 515 |
TCasScriptVectorFunXxx> {$endif} . |
| 516 |
HandleVectorGetCount(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 517 |
begin |
| 518 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger); |
| 519 |
TCasScriptInteger(AResult).Value := VectorGetCount; |
| 520 |
end; |
| 521 |
|
| 522 |
class procedure TCasScriptVec {$ifndef FPC} < |
| 523 |
TVectorXxx, |
| 524 |
TCasScriptVectorFunXxx> {$endif} . |
| 525 |
HandleVectorLength(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 526 |
begin |
| 527 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 528 |
TCasScriptFloat(AResult).Value := TSelfClass(Arguments[0]).Value.Length; |
| 529 |
end; |
| 530 |
|
| 531 |
class procedure TCasScriptVec {$ifndef FPC} < |
| 532 |
TVectorXxx, |
| 533 |
TCasScriptVectorFunXxx> {$endif} . |
| 534 |
HandleVectorSqrLength(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 535 |
begin |
| 536 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 537 |
TCasScriptFloat(AResult).Value := TSelfClass(Arguments[0]).Value.LengthSqr; |
| 538 |
end; |
| 539 |
|
| 540 |
class procedure TCasScriptVec {$ifndef FPC} < |
| 541 |
TVectorXxx, |
| 542 |
TCasScriptVectorFunXxx> {$endif} . |
| 543 |
HandleVectorDot(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 544 |
begin |
| 545 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 546 |
TCasScriptFloat(AResult).Value := |
| 547 |
TVectorXxx.DotProduct( |
| 548 |
TSelfClass(Arguments[0]).Value, |
| 549 |
TSelfClass(Arguments[1]).Value ); |
| 550 |
end; |
| 551 |
|
| 552 |
procedure TCasScriptVec {$ifndef FPC} < |
| 553 |
TVectorXxx, |
| 554 |
TCasScriptVectorFunXxx> {$endif} . |
| 555 |
AssignValue(Source: TCasScriptValue); |
| 556 |
begin |
| 557 |
if Source is TSelfClass then |
| 558 |
Value := TSelfClass(Source).Value |
| 559 |
else |
| 560 |
raise ECasScriptAssignError.CreateFmt('Assignment from %s to %s not possible', [Source.ClassName, ClassName]); |
| 561 |
end; |
| 562 |
|
| 563 |
procedure TCasScriptVec {$ifndef FPC} < |
| 564 |
TVectorXxx, |
| 565 |
TCasScriptVectorFunXxx> {$endif} . |
| 566 |
SetValue(const AValue: TVectorXxx); |
| 567 |
begin |
| 568 |
FValue := AValue; |
| 569 |
ValueAssigned := true; |
| 570 |
end; |
| 571 |
|
| 572 |
class function TCasScriptVec {$ifndef FPC} < |
| 573 |
TVectorXxx, |
| 574 |
TCasScriptVectorFunXxx> {$endif} . |
| 575 |
VectorGetCount: Integer; |
| 576 |
begin |
| 577 |
Result := High(TVectorXxx.TIndex) + 1; |
| 578 |
end; |
| 579 |
|
| 580 |
class procedure TCasScriptVec {$ifndef FPC} < |
| 581 |
TVectorXxx, |
| 582 |
TCasScriptVectorFunXxx> {$endif} . |
| 583 |
RegisterFunctions; |
| 584 |
var |
| 585 |
VectorArgClasses: array of TCasScriptValueClass; |
| 586 |
I: Integer; |
| 587 |
begin |
| 588 |
{ functions from CastleScriptCoreFunctions } |
| 589 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleAdd, TCasScriptAdd, [TSelfClass], true); |
| 590 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleSubtract, TCasScriptSubtract, [TSelfClass], true); |
| 591 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleNegate, TCasScriptNegate, [TSelfClass], false); |
| 592 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleEqual, TCasScriptEqual, [TSelfClass, TSelfClass], false); |
| 593 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleNotEqual, TCasScriptNotEqual, [TSelfClass, TSelfClass], false); |
| 594 |
|
| 595 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleMultiply, TCasScriptMultiply, [TSelfClass, TCasScriptFloat], false); |
| 596 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleMultiply, TCasScriptMultiply, [TCasScriptFloat, TSelfClass], false); |
| 597 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleDivide, TCasScriptFloatDivide, [TSelfClass, TCasScriptFloat], false); |
| 598 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleLerp, TCasScriptLerp, [TCasScriptFloat, TSelfClass, TSelfClass], false); |
| 599 |
|
| 600 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleMax, TCasScriptMax, [TSelfClass, TSelfClass], false); |
| 601 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleMin, TCasScriptMin, [TSelfClass, TSelfClass], false); |
| 602 |
|
| 603 |
{ functions from CastleScriptVectors } |
| 604 |
SetLength(VectorArgClasses, VectorGetCount); |
| 605 |
for I := 0 to VectorGetCount - 1 do |
| 606 |
VectorArgClasses[I] := TCasScriptFloat; |
| 607 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleVector, TCasScriptVectorFunXxx, VectorArgClasses, false); |
| 608 |
|
| 609 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleVectorGet, TCasScriptVectorGet, [TSelfClass, TCasScriptInteger], false); |
| 610 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleVectorSet, TCasScriptVectorSet, [TSelfClass, TCasScriptInteger, TCasScriptFloat], false); |
| 611 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleVectorGetCount, TCasScriptVectorGetCount, [TSelfClass], false); |
| 612 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleVectorLength, TCasScriptVectorLength, [TSelfClass], false); |
| 613 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleVectorSqrLength, TCasScriptVectorSqrLength, [TSelfClass], false); |
| 614 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleVectorDot, TCasScriptVectorDot, [TSelfClass, TSelfClass], false); |
| 615 |
end; |
| 616 |
|
| 617 |
{ TCasScriptVec3f ------------------------------------------------------------ } |
| 618 |
|
| 619 |
class procedure TCasScriptVec3f.HandleVectorCross(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 620 |
begin |
| 621 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptVec3f); |
| 622 |
TCasScriptVec3f(AResult).Value := TVector3.CrossProduct( |
| 623 |
TCasScriptVec3f(Arguments[0]).Value, |
| 624 |
TCasScriptVec3f(Arguments[1]).Value ); |
| 625 |
end; |
| 626 |
|
| 627 |
class procedure TCasScriptVec3f.HandleGrayscale(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 628 |
begin |
| 629 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat); |
| 630 |
TCasScriptFloat(AResult).Value := |
| 631 |
GrayscaleValue( TCasScriptVec3f(Arguments[0]).Value ); |
| 632 |
end; |
| 633 |
|
| 634 |
{ TCasScriptVec4f ------------------------------------------------------------ } |
| 635 |
|
| 636 |
class procedure TCasScriptVec4f.HandleOrientationFromDirectionUp(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 637 |
var |
| 638 |
Dir, Up: TVector3; |
| 639 |
begin |
| 640 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptVec4f); |
| 641 |
|
| 642 |
Dir := TCasScriptVec3f(Arguments[0]).Value; |
| 643 |
Up := TCasScriptVec3f(Arguments[1]).Value; |
| 644 |
MakeVectorsOrthoOnTheirPlane(Up, Dir); |
| 645 |
{ no need to normalize Dir, Up here (OrientationFromDirectionUp will do it) } |
| 646 |
|
| 647 |
TCasScriptVec4f(AResult).Value := OrientationFromDirectionUp(Dir, Up); |
| 648 |
end; |
| 649 |
|
| 650 |
class procedure TCasScriptVec4f.HandleRotateCore( |
| 651 |
const Rotation: TVector4; const Point: TVector3; |
| 652 |
var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 653 |
var |
| 654 |
Axis: TVector3 absolute Rotation; |
| 655 |
begin |
| 656 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptVec3f); |
| 657 |
|
| 658 |
if not Axis.IsZero then |
| 659 |
TCasScriptVec3f(AResult).Value := RotatePointAroundAxis(Rotation, Point) |
| 660 |
else |
| 661 |
begin |
| 662 |
{ Safeguard against rotation around zero vector, which produces unpredictable |
| 663 |
results (actually, Result would be filled with Nan values). |
| 664 |
VRML/X3D specs says that SFRotation should always specify a normalized vector. } |
| 665 |
TCasScriptVec3f(AResult).Value := Point; |
| 666 |
WritelnWarning('CastleScript', 'Rotation around zero vector'); |
| 667 |
end; |
| 668 |
end; |
| 669 |
|
| 670 |
class procedure TCasScriptVec4f.HandleRotate(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 671 |
begin |
| 672 |
HandleRotateCore( |
| 673 |
TCasScriptVec4f(Arguments[0]).Value, |
| 674 |
TCasScriptVec3f(Arguments[1]).Value, AResult, ParentOfResult); |
| 675 |
end; |
| 676 |
|
| 677 |
class procedure TCasScriptVec4f.HandleOrientationToDirection(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 678 |
begin |
| 679 |
HandleRotateCore( |
| 680 |
TCasScriptVec4f(Arguments[0]).Value, |
| 681 |
DefaultCameraDirection, AResult, ParentOfResult); |
| 682 |
end; |
| 683 |
|
| 684 |
class procedure TCasScriptVec4f.HandleOrientationToUp(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 685 |
begin |
| 686 |
HandleRotateCore( |
| 687 |
TCasScriptVec4f(Arguments[0]).Value, |
| 688 |
DefaultCameraUp, AResult, ParentOfResult); |
| 689 |
end; |
| 690 |
|
| 691 |
class procedure TCasScriptVec4f.HandleSlerp(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 692 |
begin |
| 693 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptVec4f); |
| 694 |
TCasScriptVec4f(AResult).Value := SLerp( |
| 695 |
TCasScriptFloat(Arguments[0]).Value, |
| 696 |
TCasScriptVec4f(Arguments[1]).Value, |
| 697 |
TCasScriptVec4f(Arguments[2]).Value); |
| 698 |
end; |
| 699 |
|
| 700 |
{ TCasScriptVec3d ------------------------------------------------------------ } |
| 701 |
|
| 702 |
class procedure TCasScriptVec3d.HandleVectorCross(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 703 |
begin |
| 704 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptVec3d); |
| 705 |
TCasScriptVec3d(AResult).Value := |
| 706 |
TVector3Double.CrossProduct( |
| 707 |
TCasScriptVec3d(Arguments[0]).Value, |
| 708 |
TCasScriptVec3d(Arguments[1]).Value ); |
| 709 |
end; |
| 710 |
|
| 711 |
{ TCasScriptMatrix ---------------------------------------------------------- } |
| 712 |
|
| 713 |
class function TCasScriptMatrix {$ifndef FPC} <TCasScriptVecXxx, |
| 714 |
TMatrixXxx, |
| 715 |
TVectorXxx> {$endif} . |
| 716 |
CreateValueIfNeededSelf(var Value: TCasScriptValue; var ParentOfValue: boolean): TSelfClass; |
| 717 |
begin |
| 718 |
CreateValueIfNeeded(Value, ParentOfValue, TCasScriptValueClass(ClassType)); |
| 719 |
Result := TSelfClass(Value); |
| 720 |
end; |
| 721 |
|
| 722 |
class procedure TCasScriptMatrix {$ifndef FPC} <TCasScriptVecXxx, |
| 723 |
TMatrixXxx, |
| 724 |
TVectorXxx> {$endif} . |
| 725 |
HandleAdd(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 726 |
var |
| 727 |
I: Integer; |
| 728 |
MyResult: TSelfClass; |
| 729 |
begin |
| 730 |
MyResult := CreateValueIfNeededSelf(AResult, ParentOfResult); |
| 731 |
{ The function allows only >= 1 arguments, and this handler is |
| 732 |
registered only for TSelfClass values, so we can safely take |
| 733 |
the first arg as TSelfClass. } |
| 734 |
MyResult.Value := TSelfClass(Arguments[0]).Value; |
| 735 |
for I := 1 to Length(Arguments) - 1 do |
| 736 |
MyResult.Value := MyResult.Value + TSelfClass(Arguments[I]).Value; |
| 737 |
end; |
| 738 |
|
| 739 |
class procedure TCasScriptMatrix {$ifndef FPC} <TCasScriptVecXxx, |
| 740 |
TMatrixXxx, |
| 741 |
TVectorXxx> {$endif} . |
| 742 |
HandleSubtract(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 743 |
var |
| 744 |
I: Integer; |
| 745 |
MyResult: TSelfClass; |
| 746 |
begin |
| 747 |
MyResult := CreateValueIfNeededSelf(AResult, ParentOfResult); |
| 748 |
MyResult.Value := TSelfClass(Arguments[0]).Value; |
| 749 |
for I := 1 to Length(Arguments) - 1 do |
| 750 |
MyResult.Value := MyResult.Value - TSelfClass(Arguments[I]).Value; |
| 751 |
end; |
| 752 |
|
| 753 |
class procedure TCasScriptMatrix {$ifndef FPC} <TCasScriptVecXxx, |
| 754 |
TMatrixXxx, |
| 755 |
TVectorXxx> {$endif} . |
| 756 |
HandleNegate(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 757 |
var |
| 758 |
MyResult: TSelfClass; |
| 759 |
begin |
| 760 |
MyResult := CreateValueIfNeededSelf(AResult, ParentOfResult); |
| 761 |
MyResult.Value := -TSelfClass(Arguments[0]).Value; |
| 762 |
end; |
| 763 |
|
| 764 |
class procedure TCasScriptMatrix {$ifndef FPC} <TCasScriptVecXxx, |
| 765 |
TMatrixXxx, |
| 766 |
TVectorXxx> {$endif} . |
| 767 |
HandleMultiply(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 768 |
var |
| 769 |
I: Integer; |
| 770 |
var |
| 771 |
MyResult: TSelfClass; |
| 772 |
begin |
| 773 |
if (Length(Arguments) = 2) and |
| 774 |
(Arguments[0] is TCasScriptFloat) and |
| 775 |
(Arguments[1] is TSelfClass) then |
| 776 |
begin |
| 777 |
MyResult := CreateValueIfNeededSelf(AResult, ParentOfResult); |
| 778 |
MyResult.Value := |
| 779 |
TSelfClass(Arguments[1]).Value * |
| 780 |
TCasScriptFloat(Arguments[0]).Value; |
| 781 |
end else |
| 782 |
if (Length(Arguments) = 2) and |
| 783 |
(Arguments[1] is TCasScriptFloat) and |
| 784 |
(Arguments[0] is TSelfClass) then |
| 785 |
begin |
| 786 |
MyResult := CreateValueIfNeededSelf(AResult, ParentOfResult); |
| 787 |
MyResult.Value := |
| 788 |
TSelfClass(Arguments[0]).Value * |
| 789 |
TCasScriptFloat(Arguments[1]).Value; |
| 790 |
end else |
| 791 |
if (Length(Arguments) = 2) and |
| 792 |
(Arguments[0] is TSelfClass) and |
| 793 |
(Arguments[1].InheritsFrom(TCasScriptVecXxx)) then |
| 794 |
begin |
| 795 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptVecXxx); |
| 796 |
TCasScriptVecXxx(AResult).Value := |
| 797 |
TSelfClass(Arguments[0]).Value * |
| 798 |
TCasScriptVecXxx(Arguments[1]).Value; |
| 799 |
end else |
| 800 |
begin |
| 801 |
{ So this is matrix * matrix... operation } |
| 802 |
MyResult := CreateValueIfNeededSelf(AResult, ParentOfResult); |
| 803 |
MyResult.Value := TSelfClass(Arguments[0]).Value; |
| 804 |
for I := 1 to Length(Arguments) - 1 do |
| 805 |
MyResult.Value := |
| 806 |
MyResult.Value * |
| 807 |
TSelfClass(Arguments[I]).Value; |
| 808 |
end; |
| 809 |
end; |
| 810 |
|
| 811 |
class procedure TCasScriptMatrix {$ifndef FPC} <TCasScriptVecXxx, |
| 812 |
TMatrixXxx, |
| 813 |
TVectorXxx> {$endif} . |
| 814 |
HandleDivide(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 815 |
var |
| 816 |
MyResult: TSelfClass; |
| 817 |
begin |
| 818 |
MyResult := CreateValueIfNeededSelf(AResult, ParentOfResult); |
| 819 |
MyResult.Value := |
| 820 |
TSelfClass(Arguments[0]).Value * |
| 821 |
(1/TCasScriptFloat(Arguments[1]).Value); |
| 822 |
end; |
| 823 |
|
| 824 |
class procedure TCasScriptMatrix {$ifndef FPC} <TCasScriptVecXxx, |
| 825 |
TMatrixXxx, |
| 826 |
TVectorXxx> {$endif} . |
| 827 |
HandleLerp(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 828 |
var |
| 829 |
MyResult: TSelfClass; |
| 830 |
begin |
| 831 |
MyResult := CreateValueIfNeededSelf(AResult, ParentOfResult); |
| 832 |
MyResult.Value := TMatrixXxx.Lerp( |
| 833 |
TCasScriptFloat(Arguments[0]).Value, |
| 834 |
TSelfClass(Arguments[1]).Value, |
| 835 |
TSelfClass(Arguments[2]).Value); |
| 836 |
end; |
| 837 |
|
| 838 |
class procedure TCasScriptMatrix {$ifndef FPC} <TCasScriptVecXxx, |
| 839 |
TMatrixXxx, |
| 840 |
TVectorXxx> {$endif} . |
| 841 |
HandleEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 842 |
begin |
| 843 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 844 |
TCasScriptBoolean(AResult).Value := |
| 845 |
TMatrixXxx.PerfectlyEquals( |
| 846 |
TSelfClass(Arguments[0]).Value, |
| 847 |
TSelfClass(Arguments[1]).Value); |
| 848 |
end; |
| 849 |
|
| 850 |
class procedure TCasScriptMatrix {$ifndef FPC} <TCasScriptVecXxx, |
| 851 |
TMatrixXxx, |
| 852 |
TVectorXxx> {$endif} . |
| 853 |
HandleNotEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 854 |
begin |
| 855 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean); |
| 856 |
TCasScriptBoolean(AResult).Value := |
| 857 |
not TMatrixXxx.PerfectlyEquals( |
| 858 |
TSelfClass(Arguments[0]).Value, |
| 859 |
TSelfClass(Arguments[1]).Value); |
| 860 |
end; |
| 861 |
|
| 862 |
class procedure TCasScriptMatrix {$ifndef FPC} <TCasScriptVecXxx, |
| 863 |
TMatrixXxx, |
| 864 |
TVectorXxx> {$endif} . |
| 865 |
HandleMatrix(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 866 |
var |
| 867 |
I: Integer; |
| 868 |
MyResult: TSelfClass; |
| 869 |
begin |
| 870 |
MyResult := CreateValueIfNeededSelf(AResult, ParentOfResult); |
| 871 |
for I := 0 to MatrixGetCount - 1 do |
| 872 |
MyResult.FValue.Columns[I] := TCasScriptVecXxx(Arguments[I]).Value; |
| 873 |
MyResult.ValueAssigned := true; |
| 874 |
end; |
| 875 |
|
| 876 |
class procedure TCasScriptMatrix {$ifndef FPC} <TCasScriptVecXxx, |
| 877 |
TMatrixXxx, |
| 878 |
TVectorXxx> {$endif} . |
| 879 |
HandleMatrixGet(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 880 |
var |
| 881 |
Index: Integer; |
| 882 |
V: TVectorXxx; |
| 883 |
begin |
| 884 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptVecXxx); |
| 885 |
|
| 886 |
Index := TCasScriptInteger(Arguments[1]).Value; |
| 887 |
if not Between(Index, 0, MatrixGetCount - 1) then |
| 888 |
raise ECasScriptError.CreateFmt('Invalid index %d for matrix_get on %d-column matrix', |
| 889 |
[Index, MatrixGetCount]); |
| 890 |
|
| 891 |
V := TSelfClass(Arguments[0]).Value.Columns[Index]; |
| 892 |
TCasScriptVecXxx(AResult).Value := V; |
| 893 |
end; |
| 894 |
|
| 895 |
class procedure TCasScriptMatrix {$ifndef FPC} <TCasScriptVecXxx, |
| 896 |
TMatrixXxx, |
| 897 |
TVectorXxx> {$endif} . |
| 898 |
HandleMatrixSet(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 899 |
var |
| 900 |
Index: Integer; |
| 901 |
begin |
| 902 |
if ParentOfResult then |
| 903 |
AResult.FreeByParentExpression; |
| 904 |
AResult := nil; |
| 905 |
ParentOfResult := false; |
| 906 |
|
| 907 |
Index := TCasScriptInteger(Arguments[1]).Value; |
| 908 |
if not Between(Index, 0, MatrixGetCount - 1) then |
| 909 |
raise ECasScriptError.CreateFmt('Invalid index %d for matrix_set on %d-column matrix', |
| 910 |
[Index, MatrixGetCount]); |
| 911 |
|
| 912 |
TSelfClass(Arguments[0]).FValue.Columns[Index] := TCasScriptVecXxx(Arguments[2]).Value; |
| 913 |
TSelfClass(Arguments[0]).ValueAssigned := true; |
| 914 |
|
| 915 |
AResult := Arguments[0]; |
| 916 |
end; |
| 917 |
|
| 918 |
class procedure TCasScriptMatrix {$ifndef FPC} <TCasScriptVecXxx, |
| 919 |
TMatrixXxx, |
| 920 |
TVectorXxx> {$endif} . |
| 921 |
HandleMatrixGetCount(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean); |
| 922 |
begin |
| 923 |
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger); |
| 924 |
TCasScriptInteger(AResult).Value := MatrixGetCount; |
| 925 |
end; |
| 926 |
|
| 927 |
procedure TCasScriptMatrix {$ifndef FPC} <TCasScriptVecXxx, |
| 928 |
TMatrixXxx, |
| 929 |
TVectorXxx> {$endif} . |
| 930 |
AssignValue(Source: TCasScriptValue); |
| 931 |
begin |
| 932 |
if Source is TSelfClass then |
| 933 |
Value := TSelfClass(Source).Value else |
| 934 |
raise ECasScriptAssignError.CreateFmt('Assignment from %s to %s not possible', [Source.ClassName, ClassName]); |
| 935 |
end; |
| 936 |
|
| 937 |
procedure TCasScriptMatrix {$ifndef FPC} <TCasScriptVecXxx, |
| 938 |
TMatrixXxx, |
| 939 |
TVectorXxx> {$endif} . |
| 940 |
SetValue(const AValue: TMatrixXxx); |
| 941 |
begin |
| 942 |
FValue := AValue; |
| 943 |
ValueAssigned := true; |
| 944 |
end; |
| 945 |
|
| 946 |
class function TCasScriptMatrix {$ifndef FPC} <TCasScriptVecXxx, |
| 947 |
TMatrixXxx, |
| 948 |
TVectorXxx> {$endif} . |
| 949 |
MatrixGetCount: Integer; |
| 950 |
begin |
| 951 |
Result := High(TVectorXxx.TIndex) + 1; |
| 952 |
end; |
| 953 |
|
| 954 |
class procedure TCasScriptMatrix {$ifndef FPC} <TCasScriptVecXxx, |
| 955 |
TMatrixXxx, |
| 956 |
TVectorXxx> {$endif} . |
| 957 |
RegisterFunctions; |
| 958 |
var |
| 959 |
MatrixArgClasses: array of TCasScriptValueClass; |
| 960 |
I: Integer; |
| 961 |
begin |
| 962 |
{ functions from CastleScriptCoreFunctions } |
| 963 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleAdd, TCasScriptAdd, [TSelfClass], true); |
| 964 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleSubtract, TCasScriptSubtract, [TSelfClass], true); |
| 965 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleNegate, TCasScriptNegate, [TSelfClass], false); |
| 966 |
|
| 967 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleMultiply, TCasScriptMultiply, [TSelfClass, TCasScriptFloat], false); |
| 968 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleMultiply, TCasScriptMultiply, [TCasScriptFloat, TSelfClass], false); |
| 969 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleMultiply, TCasScriptMultiply, [TSelfClass, TCasScriptVecXxx], false); |
| 970 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleMultiply, TCasScriptMultiply, [TSelfClass], true); |
| 971 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleDivide, TCasScriptFloatDivide, [TSelfClass, TCasScriptFloat], false); |
| 972 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleLerp, TCasScriptLerp, [TCasScriptFloat, TSelfClass, TSelfClass], false); |
| 973 |
|
| 974 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleEqual, TCasScriptEqual, [TSelfClass, TSelfClass], false); |
| 975 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleNotEqual, TCasScriptNotEqual, [TSelfClass, TSelfClass], false); |
| 976 |
|
| 977 |
{ functions from CastleScriptVectors } |
| 978 |
SetLength(MatrixArgClasses, MatrixGetCount); |
| 979 |
for I := 0 to MatrixGetCount - 1 do |
| 980 |
MatrixArgClasses[I] := TCasScriptVecXxx; |
| 981 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleMatrix, TCasScriptMatrixFun, MatrixArgClasses, false); |
| 982 |
|
| 983 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleMatrixGet, TCasScriptMatrixGet, [TSelfClass, TCasScriptInteger], false); |
| 984 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleMatrixSet, TCasScriptMatrixSet, [TSelfClass, TCasScriptInteger, TCasScriptVecXxx], false); |
| 985 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} HandleMatrixGetCount, TCasScriptMatrixGetCount, [TSelfClass], false); |
| 986 |
end; |
| 987 |
|
| 988 |
{ TCasScriptFunction descendants --------------------------------------------- } |
| 989 |
|
| 990 |
class function TCasScriptVector.ShortName: string; |
| 991 |
begin |
| 992 |
Result := 'vector'; |
| 993 |
end; |
| 994 |
|
| 995 |
class function TCasScriptVectorD.ShortName: string; |
| 996 |
begin |
| 997 |
Result := 'vector_d'; |
| 998 |
end; |
| 999 |
|
| 1000 |
class function TCasScriptVectorGet.ShortName: string; |
| 1001 |
begin |
| 1002 |
Result := 'vector_get'; |
| 1003 |
end; |
| 1004 |
|
| 1005 |
class function TCasScriptVectorSet.ShortName: string; |
| 1006 |
begin |
| 1007 |
Result := 'vector_set'; |
| 1008 |
end; |
| 1009 |
|
| 1010 |
class function TCasScriptVectorSet.ArgumentMustBeAssignable(const Index: Integer): boolean; |
| 1011 |
begin |
| 1012 |
Result := Index = 0; |
| 1013 |
end; |
| 1014 |
|
| 1015 |
class function TCasScriptVectorGetCount.ShortName: string; |
| 1016 |
begin |
| 1017 |
Result := 'vector_get_count'; |
| 1018 |
end; |
| 1019 |
|
| 1020 |
class function TCasScriptVectorLength.ShortName: string; |
| 1021 |
begin |
| 1022 |
Result := 'vector_length'; |
| 1023 |
end; |
| 1024 |
|
| 1025 |
class function TCasScriptVectorSqrLength.ShortName: string; |
| 1026 |
begin |
| 1027 |
Result := 'vector_sqr_length'; |
| 1028 |
end; |
| 1029 |
|
| 1030 |
class function TCasScriptVectorDot.ShortName: string; |
| 1031 |
begin |
| 1032 |
Result := 'vector_dot'; |
| 1033 |
end; |
| 1034 |
|
| 1035 |
class function TCasScriptVectorCross.ShortName: string; |
| 1036 |
begin |
| 1037 |
Result := 'vector_cross'; |
| 1038 |
end; |
| 1039 |
|
| 1040 |
class function TCasScriptGrayscale.ShortName: string; |
| 1041 |
begin |
| 1042 |
Result := 'grayscale'; |
| 1043 |
end; |
| 1044 |
|
| 1045 |
class function TCasScriptOrientationFromDirectionUp.ShortName: string; |
| 1046 |
begin |
| 1047 |
Result := 'orientation_from_direction_up'; |
| 1048 |
end; |
| 1049 |
|
| 1050 |
class function TCasScriptRotate.ShortName: string; |
| 1051 |
begin |
| 1052 |
Result := 'rotate'; |
| 1053 |
end; |
| 1054 |
|
| 1055 |
class function TCasScriptOrientationToDirection.ShortName: string; |
| 1056 |
begin |
| 1057 |
Result := 'orientation_to_direction'; |
| 1058 |
end; |
| 1059 |
|
| 1060 |
class function TCasScriptOrientationToUp.ShortName: string; |
| 1061 |
begin |
| 1062 |
Result := 'orientation_to_up'; |
| 1063 |
end; |
| 1064 |
|
| 1065 |
class function TCasScriptSlerp.ShortName: string; |
| 1066 |
begin |
| 1067 |
Result := 'slerp'; |
| 1068 |
end; |
| 1069 |
|
| 1070 |
{ matrix functions ----------------------------------------------------------- } |
| 1071 |
|
| 1072 |
class function TCasScriptMatrixFun.ShortName: string; |
| 1073 |
begin |
| 1074 |
Result := 'matrix'; |
| 1075 |
end; |
| 1076 |
|
| 1077 |
class function TCasScriptMatrixGet.ShortName: string; |
| 1078 |
begin |
| 1079 |
Result := 'matrix_get'; |
| 1080 |
end; |
| 1081 |
|
| 1082 |
class function TCasScriptMatrixSet.ShortName: string; |
| 1083 |
begin |
| 1084 |
Result := 'matrix_set'; |
| 1085 |
end; |
| 1086 |
|
| 1087 |
class function TCasScriptMatrixSet.ArgumentMustBeAssignable(const Index: Integer): boolean; |
| 1088 |
begin |
| 1089 |
Result := Index = 0; |
| 1090 |
end; |
| 1091 |
|
| 1092 |
class function TCasScriptMatrixGetCount.ShortName: string; |
| 1093 |
begin |
| 1094 |
Result := 'matrix_get_count'; |
| 1095 |
end; |
| 1096 |
|
| 1097 |
{ unit init/fini ------------------------------------------------------------- } |
| 1098 |
|
| 1099 |
initialization |
| 1100 |
TCasScriptVec2f.RegisterFunctions; |
| 1101 |
TCasScriptVec3f.RegisterFunctions; |
| 1102 |
TCasScriptVec4f.RegisterFunctions; |
| 1103 |
|
| 1104 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptVec3f {$ifdef FPC}(nil){$endif} .HandleVectorCross, TCasScriptVectorCross, [TCasScriptVec3f, TCasScriptVec3f], false); |
| 1105 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptVec3f {$ifdef FPC}(nil){$endif} .HandleGrayscale, TCasScriptGrayscale, [TCasScriptVec3f], false); |
| 1106 |
|
| 1107 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptVec4f {$ifdef FPC}(nil){$endif} .HandleOrientationFromDirectionUp, TCasScriptOrientationFromDirectionUp, [TCasScriptVec3f, TCasScriptVec3f], false); |
| 1108 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptVec4f {$ifdef FPC}(nil){$endif} .HandleRotate, TCasScriptRotate, [TCasScriptVec4f, TCasScriptVec3f], false); |
| 1109 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptVec4f {$ifdef FPC}(nil){$endif} .HandleOrientationToDirection, TCasScriptOrientationToDirection, [TCasScriptVec4f], false); |
| 1110 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptVec4f {$ifdef FPC}(nil){$endif} .HandleOrientationToUp, TCasScriptOrientationToUp, [TCasScriptVec4f], false); |
| 1111 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptVec4f {$ifdef FPC}(nil){$endif} .HandleSlerp, TCasScriptSlerp, [TCasScriptFloat, TCasScriptVec4f, TCasScriptVec4f], false); |
| 1112 |
|
| 1113 |
TCasScriptMatrix3f.RegisterFunctions; |
| 1114 |
TCasScriptMatrix4f.RegisterFunctions; |
| 1115 |
|
| 1116 |
TCasScriptVec2d.RegisterFunctions; |
| 1117 |
TCasScriptVec3d.RegisterFunctions; |
| 1118 |
TCasScriptVec4d.RegisterFunctions; |
| 1119 |
|
| 1120 |
FunctionHandlers.RegisterHandler({$ifdef FPC}@{$endif} TCasScriptVec3d {$ifdef FPC}(nil){$endif}. HandleVectorCross, TCasScriptVectorCross, [TCasScriptVec3d, TCasScriptVec3d], false); |
| 1121 |
|
| 1122 |
TCasScriptMatrix3d.RegisterFunctions; |
| 1123 |
TCasScriptMatrix4d.RegisterFunctions; |
| 1124 |
|
| 1125 |
{$endif CASTLE_SCRIPT_FPC} |
| 1126 |
end. |