MCPcopy Create free account
hub / github.com/defold/defold / Vector_Clamp

Function Vector_Clamp

engine/script/src/script_vmath.cpp:2556–2635  ·  view source on GitHub ↗

clamp input value in range [min, max] and return clamped value * * Clamp input value to be in range of [min, max]. In case if input value has vector3|vector4 type * return new vector3|vector4 with clamped value at every vector's element. * Min/max arguments can be vector3|vector4. In that case clamp excuted per every vector's element * * @name vmath.clamp * @para

Source from the content-addressed store, hash-verified

2554 * ```
2555 */
2556 static int Vector_Clamp(lua_State* L)
2557 {
2558 if (lua_type(L, 1) == LUA_TNUMBER)
2559 {
2560 float value = (float) luaL_checknumber(L, 1);
2561 float min = (float) luaL_checknumber(L, 2);
2562 float max = (float) luaL_checknumber(L, 3);
2563 lua_pushnumber(L, dmMath::Clamp(value, min, max));
2564 return 1;
2565 }
2566
2567 void* argument = 0;
2568 const ScriptUserType argument_type = CheckUserData(L, 1, &argument);
2569 int argument2_type = lua_type(L, 2);
2570 int argument3_type = lua_type(L, 3);
2571 if (argument_type == SCRIPT_TYPE_VECTOR3)
2572 {
2573 Vector3* value = (Vector3*)argument;
2574 Vector3 min_v, max_v;
2575 if (argument2_type == LUA_TNUMBER)
2576 {
2577 float min = (float) luaL_checknumber(L, 2);
2578 min_v = Vector3(min, min, min);
2579 }
2580 else
2581 {
2582 min_v = *CheckVector3(L, 2);
2583 }
2584 if (argument3_type == LUA_TNUMBER)
2585 {
2586 float max = (float) luaL_checknumber(L, 3);
2587 max_v = Vector3(max, max, max);
2588 }
2589 else
2590 {
2591 max_v = *CheckVector3(L, 3);
2592 }
2593
2594 PushVector3(L, Vector3(
2595 dmMath::Clamp(value->getX(), min_v.getX(), max_v.getX()),
2596 dmMath::Clamp(value->getY(), min_v.getY(), max_v.getY()),
2597 dmMath::Clamp(value->getZ(), min_v.getZ(), max_v.getZ())
2598 ));
2599 }
2600 else if (argument_type == SCRIPT_TYPE_VECTOR4)
2601 {
2602 Vector4* value = (Vector4*)argument;
2603 Vector4 min_v, max_v;
2604 if (argument2_type == LUA_TNUMBER)
2605 {
2606 float min = (float) luaL_checknumber(L, 2);
2607 min_v = Vector4(min, min, min, min);
2608 }
2609 else
2610 {
2611 min_v = *CheckVector4(L, 2);
2612 }
2613 if (argument3_type == LUA_TNUMBER)

Callers

nothing calls this directly

Calls 15

lua_typeFunction · 0.85
luaL_checknumberFunction · 0.85
lua_pushnumberFunction · 0.85
CheckUserDataFunction · 0.85
CheckVector3Function · 0.85
PushVector3Function · 0.85
CheckVector4Function · 0.85
PushVector4Function · 0.85
luaL_errorFunction · 0.85
ClampFunction · 0.50
Vector3Class · 0.50
Vector4Class · 0.50

Tested by

no test coverage detected