Skip to content

Commit 2310457

Browse files
committed
Add a new center mode "Centered on Z only" for 3D model objects
1 parent 7371537 commit 2310457

File tree

5 files changed

+168
-137
lines changed

5 files changed

+168
-137
lines changed

Extensions/3D/JsExtension.js

Lines changed: 91 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3071,9 +3071,9 @@ module.exports = {
30713071
/** @type {number} */
30723072
_defaultDepth;
30733073

3074-
/** @type {[number, number, number] | null} */
3074+
/** @type {[number | null, number | null, number | null]} */
30753075
_originPoint;
3076-
/** @type {[number, number, number] | null} */
3076+
/** @type {[number | null, number | null, number | null]} */
30773077
_centerPoint;
30783078

30793079
/** @type {[number, number, number]} */
@@ -3168,11 +3168,31 @@ module.exports = {
31683168
}
31693169

31703170
getOriginPoint() {
3171-
return this._originPoint || this._modelOriginPoint;
3171+
return [
3172+
this._originPoint[0] === null
3173+
? this._modelOriginPoint[0]
3174+
: this._originPoint[0],
3175+
this._originPoint[1] === null
3176+
? this._modelOriginPoint[1]
3177+
: this._originPoint[1],
3178+
this._originPoint[2] === null
3179+
? this._modelOriginPoint[2]
3180+
: this._originPoint[2],
3181+
];
31723182
}
31733183

31743184
getCenterPoint() {
3175-
return this._centerPoint || this._modelOriginPoint;
3185+
return [
3186+
this._centerPoint[0] === null
3187+
? this._modelOriginPoint[0]
3188+
: this._centerPoint[0],
3189+
this._centerPoint[1] === null
3190+
? this._modelOriginPoint[1]
3191+
: this._centerPoint[1],
3192+
this._centerPoint[2] === null
3193+
? this._modelOriginPoint[2]
3194+
: this._centerPoint[2],
3195+
];
31763196
}
31773197

31783198
_updateDefaultTransformation(
@@ -3219,12 +3239,23 @@ module.exports = {
32193239

32203240
// Center the model.
32213241
const centerPoint = this._centerPoint;
3222-
if (centerPoint) {
3223-
threeObject.position.set(
3224-
-(boundingBox.min.x + modelWidth * centerPoint[0]),
3225-
// The model is flipped on Y axis.
3226-
-(boundingBox.min.y + modelHeight * (1 - centerPoint[1])),
3227-
-(boundingBox.min.z + modelDepth * centerPoint[2])
3242+
if (centerPoint[0]) {
3243+
threeObject.position.x = -(
3244+
boundingBox.min.x +
3245+
modelWidth * centerPoint[0]
3246+
);
3247+
}
3248+
if (centerPoint[1]) {
3249+
// The model is flipped on Y axis.
3250+
threeObject.position.y = -(
3251+
boundingBox.min.y +
3252+
modelHeight * (1 - centerPoint[1])
3253+
);
3254+
}
3255+
if (centerPoint[2]) {
3256+
threeObject.position.z = -(
3257+
boundingBox.min.z +
3258+
modelDepth * centerPoint[2]
32283259
);
32293260
}
32303261

@@ -3320,8 +3351,8 @@ module.exports = {
33203351
}
33213352

33223353
/**
3323-
* @param {[number, number, number] | null} point1
3324-
* @param {[number, number, number] | null} point2
3354+
* @param {[number | null, number | null, number | null]} point1
3355+
* @param {[number | null, number | null, number | null]} point2
33253356
* @returns {boolean}
33263357
*/
33273358
const isSamePoint = (point1, point2) => {
@@ -3337,22 +3368,24 @@ module.exports = {
33373368

33383369
/**
33393370
* @param {string} location
3340-
* @returns {[number, number, number] | null}
3371+
* @returns {[number | null, number | null, number | null]}
33413372
*/
33423373
const getPointForLocation = (location) => {
33433374
switch (location) {
33443375
case 'ModelOrigin':
3345-
return null;
3376+
return [null, null, null];
33463377
case 'ObjectCenter':
33473378
return [0.5, 0.5, 0.5];
3379+
case 'CenteredOnZ':
3380+
return [null, null, 0.5];
33483381
case 'BottomCenterZ':
33493382
return [0.5, 0.5, 0];
33503383
case 'BottomCenterY':
33513384
return [0.5, 1, 0.5];
33523385
case 'TopLeft':
33533386
return [0, 0, 0];
33543387
default:
3355-
return null;
3388+
return [null, null, null];
33563389
}
33573390
};
33583391

@@ -3367,10 +3400,10 @@ module.exports = {
33673400
_rotationY = 0;
33683401
_rotationZ = 0;
33693402
_keepAspectRatio = false;
3370-
/** @type {[number, number, number] | null} */
3371-
_originPoint = null;
3372-
/** @type {[number, number, number] | null} */
3373-
_centerPoint = null;
3403+
/** @type {[number | null, number | null, number | null]} */
3404+
_originPoint = [null, null, null];
3405+
/** @type {[number | null, number | null, number | null]} */
3406+
_centerPoint = [null, null, null];
33743407

33753408
/** @type {[number, number, number]} */
33763409
_modelOriginPoint = [0, 0, 0];
@@ -3436,11 +3469,31 @@ module.exports = {
34363469
}
34373470

34383471
getOriginPoint() {
3439-
return this._originPoint || this._modelOriginPoint;
3472+
return [
3473+
this._originPoint[0] === null
3474+
? this._modelOriginPoint[0]
3475+
: this._originPoint[0],
3476+
this._originPoint[1] === null
3477+
? this._modelOriginPoint[1]
3478+
: this._originPoint[1],
3479+
this._originPoint[2] === null
3480+
? this._modelOriginPoint[2]
3481+
: this._originPoint[2],
3482+
];
34403483
}
34413484

34423485
getCenterPoint() {
3443-
return this._centerPoint || this._modelOriginPoint;
3486+
return [
3487+
this._centerPoint[0] === null
3488+
? this._modelOriginPoint[0]
3489+
: this._centerPoint[0],
3490+
this._centerPoint[1] === null
3491+
? this._modelOriginPoint[1]
3492+
: this._centerPoint[1],
3493+
this._centerPoint[2] === null
3494+
? this._modelOriginPoint[2]
3495+
: this._centerPoint[2],
3496+
];
34443497
}
34453498

34463499
_updateDefaultTransformation() {
@@ -3493,12 +3546,23 @@ module.exports = {
34933546

34943547
// Center the model.
34953548
const centerPoint = this._centerPoint;
3496-
if (centerPoint) {
3497-
threeModelGroup.position.set(
3498-
-(boundingBox.min.x + modelWidth * centerPoint[0]),
3499-
// The model is flipped on Y axis.
3500-
-(boundingBox.min.y + modelHeight * (1 - centerPoint[1])),
3501-
-(boundingBox.min.z + modelDepth * centerPoint[2])
3549+
if (centerPoint[0] !== null) {
3550+
threeModelGroup.position.x = -(
3551+
boundingBox.min.x +
3552+
modelWidth * centerPoint[0]
3553+
);
3554+
}
3555+
if (centerPoint[1] !== null) {
3556+
// The model is flipped on Y axis.
3557+
threeModelGroup.position.y = -(
3558+
boundingBox.min.y +
3559+
modelHeight * (1 - centerPoint[1])
3560+
);
3561+
}
3562+
if (centerPoint[2] !== null) {
3563+
threeModelGroup.position.z = -(
3564+
boundingBox.min.z +
3565+
modelDepth * centerPoint[2]
35023566
);
35033567
}
35043568

Extensions/3D/Model3DObjectConfiguration.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ using namespace std;
2222
Model3DObjectConfiguration::Model3DObjectConfiguration()
2323
: width(100), height(100), depth(100), rotationX(90), rotationY(0),
2424
rotationZ(90), modelResourceName(""), materialType("StandardWithoutMetalness"),
25-
originLocation("ModelOrigin"), centerLocation("ModelOrigin"),
25+
originLocation("ModelOrigin"), centerLocation("CenteredOnZ"),
2626
keepAspectRatio(true), crossfadeDuration(0.1f), isCastingShadow(true), isReceivingShadow(true) {}
2727

2828
bool Model3DObjectConfiguration::UpdateProperty(const gd::String &propertyName,
@@ -89,6 +89,8 @@ bool Model3DObjectConfiguration::UpdateProperty(const gd::String &propertyName,
8989
centerLocation = "ModelOrigin";
9090
else if (normalizedValue == "objectcenter")
9191
centerLocation = "ObjectCenter";
92+
else if (normalizedValue == "centeredonz")
93+
centerLocation = "CenteredOnZ";
9294
else if (normalizedValue == "bottomcenterz")
9395
centerLocation = "BottomCenterZ";
9496
else if (normalizedValue == "bottomcentery")
@@ -206,6 +208,7 @@ Model3DObjectConfiguration::GetProperties() const {
206208
.SetType("choice")
207209
.AddChoice("ModelOrigin", _("Model origin"))
208210
.AddChoice("ObjectCenter", _("Object center"))
211+
.AddChoice("CenteredOnZ", _("Centered on Z only"))
209212
.AddChoice("BottomCenterZ", _("Bottom center (Z)"))
210213
.AddChoice("BottomCenterY", _("Bottom center (Y)"))
211214
.SetLabel(_("Center point"))

Extensions/3D/Model3DRuntimeObject.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ namespace gdjs {
33

44
type Model3DObjectNetworkSyncDataType = {
55
mt: number;
6-
op: FloatPoint3D | null;
7-
cp: FloatPoint3D | null;
6+
op: LocationPoint | null;
7+
cp: LocationPoint | null;
88
anis: Model3DAnimation[];
99
ai: integer;
1010
ass: float;
@@ -38,6 +38,7 @@ namespace gdjs {
3838
centerLocation:
3939
| 'ModelOrigin'
4040
| 'ObjectCenter'
41+
| 'CenteredOnZ'
4142
| 'BottomCenterZ'
4243
| 'BottomCenterY';
4344
animations: Model3DAnimation[];
@@ -47,12 +48,14 @@ namespace gdjs {
4748
};
4849
}
4950

50-
type FloatPoint3D = [float, float, float];
51+
type LocationPoint = [float | null, float | null, float | null];
5152

52-
const getPointForLocation = (location: string): FloatPoint3D | null => {
53+
const getPointForLocation = (location: string): LocationPoint => {
5354
switch (location) {
5455
case 'ModelOrigin':
55-
return null;
56+
return [null, null, null];
57+
case 'CenteredOnZ':
58+
return [null, null, 0.5];
5659
case 'ObjectCenter':
5760
return [0.5, 0.5, 0.5];
5861
case 'BottomCenterZ':
@@ -62,7 +65,7 @@ namespace gdjs {
6265
case 'TopLeft':
6366
return [0, 0, 0];
6467
default:
65-
return null;
68+
return [null, null, null];
6669
}
6770
};
6871

@@ -90,7 +93,7 @@ namespace gdjs {
9093
* configuration.
9194
* @see gdjs.Model3DRuntimeObject3DRenderer.getOriginPoint
9295
*/
93-
_originPoint: FloatPoint3D | null;
96+
_originPoint: LocationPoint;
9497
/**
9598
* The local point of the model that is used as rotation center.
9699
*
@@ -101,7 +104,7 @@ namespace gdjs {
101104
* configuration.
102105
* @see gdjs.Model3DRuntimeObject3DRenderer.getCenterPoint
103106
*/
104-
_centerPoint: FloatPoint3D | null;
107+
_centerPoint: LocationPoint;
105108

106109
_animations: Model3DAnimation[];
107110
_currentAnimationIndex: integer = 0;
@@ -275,10 +278,10 @@ namespace gdjs {
275278
this._materialType = networkSyncData.mt;
276279
}
277280
if (networkSyncData.op !== undefined) {
278-
this._originPoint = networkSyncData.op;
281+
this._originPoint = networkSyncData.op || [null, null, null];
279282
}
280283
if (networkSyncData.cp !== undefined) {
281-
this._centerPoint = networkSyncData.cp;
284+
this._centerPoint = networkSyncData.cp || [null, null, null];
282285
}
283286
if (networkSyncData.anis !== undefined) {
284287
this._animations = networkSyncData.anis;

Extensions/3D/Model3DRuntimeObject3DRenderer.ts

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,34 @@ namespace gdjs {
130130
);
131131
}
132132

133-
getOriginPoint() {
134-
return this._model3DRuntimeObject._originPoint || this._modelOriginPoint;
133+
getOriginPoint(): FloatPoint3D {
134+
//@ts-ignore
135+
const point: FloatPoint3D = gdjs.staticArray(
136+
Model3DRuntimeObject3DRenderer.prototype.getOriginPoint
137+
);
138+
const originPoint = this._model3DRuntimeObject._originPoint;
139+
point[0] =
140+
originPoint[0] === null ? this._modelOriginPoint[0] : originPoint[0];
141+
point[1] =
142+
originPoint[1] === null ? this._modelOriginPoint[1] : originPoint[1];
143+
point[2] =
144+
originPoint[2] === null ? this._modelOriginPoint[2] : originPoint[2];
145+
return point;
135146
}
136147

137-
getCenterPoint() {
138-
return this._model3DRuntimeObject._centerPoint || this._modelOriginPoint;
148+
getCenterPoint(): FloatPoint3D {
149+
//@ts-ignore
150+
const point: FloatPoint3D = gdjs.staticArray(
151+
Model3DRuntimeObject3DRenderer.prototype.getCenterPoint
152+
);
153+
const centerPoint = this._model3DRuntimeObject._centerPoint;
154+
point[0] =
155+
centerPoint[0] === null ? this._modelOriginPoint[0] : centerPoint[0];
156+
point[1] =
157+
centerPoint[1] === null ? this._modelOriginPoint[1] : centerPoint[1];
158+
point[2] =
159+
centerPoint[2] === null ? this._modelOriginPoint[2] : centerPoint[2];
160+
return point;
139161
}
140162

141163
/**
@@ -177,12 +199,23 @@ namespace gdjs {
177199

178200
// Center the model.
179201
const centerPoint = this._model3DRuntimeObject._centerPoint;
180-
if (centerPoint) {
181-
threeObject.position.set(
182-
-(boundingBox.min.x + modelWidth * centerPoint[0]),
183-
// The model is flipped on Y axis.
184-
-(boundingBox.min.y + modelHeight * (1 - centerPoint[1])),
185-
-(boundingBox.min.z + modelDepth * centerPoint[2])
202+
if (centerPoint[0] !== null) {
203+
threeObject.position.x = -(
204+
boundingBox.min.x +
205+
modelWidth * centerPoint[0]
206+
);
207+
}
208+
if (centerPoint[1] !== null) {
209+
// The model is flipped on Y axis.
210+
threeObject.position.y = -(
211+
boundingBox.min.y +
212+
modelHeight * (1 - centerPoint[1])
213+
);
214+
}
215+
if (centerPoint[2] !== null) {
216+
threeObject.position.z = -(
217+
boundingBox.min.z +
218+
modelDepth * centerPoint[2]
186219
);
187220
}
188221

0 commit comments

Comments
 (0)