Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Extensions/3D/Model3DRuntimeObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace gdjs {
Model3DObjectNetworkSyncDataType;

/**
* Base parameters for {@link gdjs.Cube3DRuntimeObject}
* Base parameters for {@link gdjs.Model3DRuntimeObject}
* @category Objects > 3D Model
*/
export interface Model3DObjectData extends Object3DData {
Expand Down Expand Up @@ -110,12 +110,14 @@ namespace gdjs {
_crossfadeDuration: float = 0;
_isCastingShadow: boolean = true;
_isReceivingShadow: boolean = true;
_data: Model3DObjectData;

constructor(
instanceContainer: gdjs.RuntimeInstanceContainer,
objectData: Model3DObjectData
) {
super(instanceContainer, objectData);
this._data = objectData;
this._modelResourceName = objectData.content.modelResourceName;
this._animations = objectData.content.animations;
this._originPoint = getPointForLocation(
Expand Down
61 changes: 44 additions & 17 deletions Extensions/3D/Model3DRuntimeObject3DRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,20 @@ namespace gdjs {
return this._model3DRuntimeObject._centerPoint || this._modelOriginPoint;
}

private _updateDefaultTransformation(
/**
* Transform `threeObject` to fit in a 1x1x1 cube.
*
* When the object change of size, rotation or position,
* the transformation is done on the parent of `threeObject`.
*
* This function doesn't mutate anything outside of `threeObject`.
*/
stretchModelIntoUnitaryCube(
threeObject: THREE.Object3D,
rotationX: float,
rotationY: float,
rotationZ: float,
originalWidth: float,
originalHeight: float,
originalDepth: float,
keepAspectRatio: boolean
) {
rotationZ: float
): THREE.Box3 {
// These formulas are also used in:
// - Model3DEditor.modelSize
// - Model3DRendered2DInstance
Expand All @@ -166,19 +170,9 @@ namespace gdjs {
// It also avoids to have the origin outside of the object box.
boundingBox.expandByPoint(new THREE.Vector3(0, 0, 0));
}

const modelWidth = boundingBox.max.x - boundingBox.min.x;
const modelHeight = boundingBox.max.y - boundingBox.min.y;
const modelDepth = boundingBox.max.z - boundingBox.min.z;
this._modelOriginPoint[0] =
modelWidth < epsilon ? 0 : -boundingBox.min.x / modelWidth;
this._modelOriginPoint[1] =
modelHeight < epsilon ? 0 : -boundingBox.min.y / modelHeight;
this._modelOriginPoint[2] =
modelDepth < epsilon ? 0 : -boundingBox.min.z / modelDepth;

// The model is flipped on Y axis.
this._modelOriginPoint[1] = 1 - this._modelOriginPoint[1];

// Center the model.
const centerPoint = this._model3DRuntimeObject._centerPoint;
Expand Down Expand Up @@ -211,6 +205,39 @@ namespace gdjs {
threeObject.updateMatrix();
threeObject.applyMatrix4(scaleMatrix);

return boundingBox;
}

private _updateDefaultTransformation(
threeObject: THREE.Object3D,
rotationX: float,
rotationY: float,
rotationZ: float,
originalWidth: float,
originalHeight: float,
originalDepth: float,
keepAspectRatio: boolean
) {
const boundingBox = this.stretchModelIntoUnitaryCube(
threeObject,
rotationX,
rotationY,
rotationZ
);
const modelWidth = boundingBox.max.x - boundingBox.min.x;
const modelHeight = boundingBox.max.y - boundingBox.min.y;
const modelDepth = boundingBox.max.z - boundingBox.min.z;

this._modelOriginPoint[0] =
modelWidth < epsilon ? 0 : -boundingBox.min.x / modelWidth;
this._modelOriginPoint[1] =
modelHeight < epsilon ? 0 : -boundingBox.min.y / modelHeight;
this._modelOriginPoint[2] =
modelDepth < epsilon ? 0 : -boundingBox.min.z / modelDepth;

// The model is flipped on Y axis.
this._modelOriginPoint[1] = 1 - this._modelOriginPoint[1];

if (keepAspectRatio) {
// Reduce the object dimensions to keep aspect ratio.
const widthRatio =
Expand Down
20 changes: 10 additions & 10 deletions Extensions/Physics2Behavior/JsExtension.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ module.exports = {
.setType('Choice')
.setLabel('Type')
.setQuickCustomizationVisibility(gd.QuickCustomization.Hidden)
.addExtraInfo('Static')
.addExtraInfo('Dynamic')
.addExtraInfo('Kinematic')
.addChoice('Static', _('Static'))
.addChoice('Dynamic', _('Dynamic'))
.addChoice('Kinematic', _('Kinematic'))
.setDescription(
_(
"A static object won't move (perfect for obstacles). Dynamic objects can move. Kinematic will move according to forces applied to it only (useful for characters or specific mechanisms)."
Expand Down Expand Up @@ -247,10 +247,10 @@ module.exports = {
.setType('Choice')
.setLabel('Shape')
.setQuickCustomizationVisibility(gd.QuickCustomization.Hidden)
.addExtraInfo('Box')
.addExtraInfo('Circle')
.addExtraInfo('Edge')
.addExtraInfo('Polygon');
.addChoice('Box', _('Box'))
.addChoice('Circle', _('Circle'))
.addChoice('Edge', _('Edge'))
.addChoice('Polygon', _('Polygon'));
behaviorProperties
.getOrCreate('shapeDimensionA')
.setValue(
Expand Down Expand Up @@ -306,9 +306,9 @@ module.exports = {
)
.setType('Choice')
.setLabel('Polygon Origin')
.addExtraInfo('Center')
.addExtraInfo('Origin')
.addExtraInfo('TopLeft')
.addChoice('Center', _('Center'))
.addChoice('Origin', _('Origin'))
.addChoice('TopLeft', _('TopLeft'))
.setQuickCustomizationVisibility(gd.QuickCustomization.Hidden)
.setHidden(true); // Hidden as required to be changed in the full editor.
behaviorProperties
Expand Down
51 changes: 40 additions & 11 deletions Extensions/Physics3DBehavior/JsExtension.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ module.exports = {

if (propertyName === 'bodyType') {
behaviorContent.getChild('bodyType').setStringValue(newValue);
if (
newValue !== 'Static' &&
behaviorContent.getChild('shape').getStringValue() === 'Mesh'
) {
behaviorContent.getChild('shape').setStringValue('Box');
}
return true;
}

Expand All @@ -66,6 +72,16 @@ module.exports = {

if (propertyName === 'shape') {
behaviorContent.getChild('shape').setStringValue(newValue);
if (newValue === 'Mesh') {
behaviorContent.getChild('bodyType').setStringValue('Static');
}
return true;
}

if (propertyName === 'meshShapeResourceName') {
behaviorContent
.getChild('meshShapeResourceName')
.setStringValue(newValue);
return true;
}

Expand Down Expand Up @@ -243,14 +259,15 @@ module.exports = {
.setType('Choice')
.setLabel('Type')
.setQuickCustomizationVisibility(gd.QuickCustomization.Hidden)
.addExtraInfo('Static')
.addExtraInfo('Dynamic')
.addExtraInfo('Kinematic')
.addChoice('Static', _('Static'))
.addChoice('Dynamic', _('Dynamic'))
.addChoice('Kinematic', _('Kinematic'))
.setDescription(
_(
"A static object won't move (perfect for obstacles). Dynamic objects can move. Kinematic will move according to forces applied to it only (useful for characters or specific mechanisms)."
)
);
)
.setHasImpactOnOtherProperties(true);
behaviorProperties
.getOrCreate('bullet')
.setValue(
Expand Down Expand Up @@ -288,10 +305,21 @@ module.exports = {
.setType('Choice')
.setLabel('Shape')
.setQuickCustomizationVisibility(gd.QuickCustomization.Hidden)
.addExtraInfo('Box')
.addExtraInfo('Capsule')
.addExtraInfo('Cylinder')
.addExtraInfo('Sphere');
.addChoice('Box', _('Box'))
.addChoice('Capsule', _('Capsule'))
.addChoice('Sphere', _('Sphere'))
.addChoice('Mesh', _('Mesh (works for Static only)'));
behaviorProperties
.getOrCreate('meshShapeResourceName')
.setValue(
behaviorContent.getChild('meshShapeResourceName').getStringValue()
)
.setType('resource')
.addExtraInfo('model3D')
.setLabel(_("Simplified 3D model (leave empty to use object's one)"))
// Hidden as required to be changed in the full editor.
.setHidden(true)
.setHasImpactOnOtherProperties(true);
behaviorProperties
.getOrCreate('shapeOrientation')
.setValue(
Expand All @@ -300,9 +328,9 @@ module.exports = {
.setType('Choice')
.setLabel('Shape orientation')
.setQuickCustomizationVisibility(gd.QuickCustomization.Hidden)
.addExtraInfo('Z')
.addExtraInfo('Y')
.addExtraInfo('X');
.addChoice('Z', _('Z'))
.addChoice('Y', _('Y'))
.addChoice('X', _('X'));
behaviorProperties
.getOrCreate('shapeDimensionA')
.setValue(
Expand Down Expand Up @@ -580,6 +608,7 @@ module.exports = {
behaviorContent.addChild('bullet').setBoolValue(false);
behaviorContent.addChild('fixedRotation').setBoolValue(false);
behaviorContent.addChild('shape').setStringValue('Box');
behaviorContent.addChild('meshShapeResourceName').setStringValue('');
behaviorContent.addChild('shapeOrientation').setStringValue('Z');
behaviorContent.addChild('shapeDimensionA').setDoubleValue(0);
behaviorContent.addChild('shapeDimensionB').setDoubleValue(0);
Expand Down
Loading