SDK : transform editor issues when toggling multiples time the activation button.

1. A concise explanation of the problem you're experiencing.

Hi,

I'm using the sandcastle transform editor code, auto generate from the SDK documentations. (See this link for informations : https://groups.google.com/forum/?hl=en#!topic/cesium-dev/LexAuOtuX74

I have one method to add and one to remove the transform editor from my tileset. See code below.

My problem is the following :
1- I want to add the transform editor feature.
2- I want to remove the transform editor.
3- I want to re-add the transform editor
-> At this 3rd step, my matrix is completely broken. See my matrices below.

Matrix at the 1st step, (the good one).

0: -0.03951865649472124
1: 0.999218832783316
2: 0
3: 0
4: -0.7521127170183508
5: -0.02974572049084378
6: 0.6583628581654306
7: 0
8: 0.6578485666839492
9: 0.026017615640722525
10: 0.7527007021308304
11: 0
12: 4203770.257306522
13: 166368.59177953008
14: 4777829.734155258
15: 1

Matrix at the 3rd step, (the broken one).

0: 0
1: 1
2: 0
3: 0
4: 0
5: 0
6: 1
7: 0
8: 1
9: 0
10: 0
11: 0
12: 6378137
13: 0
14: 0
15: 1

I've tried things but nothing works.
- Copy the matrix from the 1st step to the 3rd step.
- Do my treatment out of my readyPromise
- Set the transform editor to "null" and not use the destroy method.
- Use the method desactivate instead of destroy.

Do you have any ideas?
Thanks.

2. A minimal code example. If you've found a bug, this helps us reproduce and repair it.

Method to add the transform editor to my tileset.

this.transformEditor = new Cesium.TransformEditor({
    container: this.viewer.container,
    scene: this.viewer.scene,
    transform: tileset.modelMatrix,
    boundingSphere: tileset.boundingSphere
});

this.transformEditor.viewModel.activate();

Method to remove the transform editor to my tileset.

this.transformEditor.destroy();

Following my entire code.

toggleTransformEditor(tileset) {
   tileset.readyPromise.then(() => {
     const rootTransform = tileset.root.transform;
     const modelMatrix = tileset.modelMatrix;

     // Transfer root transform to the model matrix
     Cesium.Matrix4.clone(rootTransform, modelMatrix);
     Cesium.Matrix4.clone(Cesium.Matrix4.IDENTITY, rootTransform);

     //Définition du widget transformEditor
     this.transformEditor = new Cesium.TransformEditor({
       container: this.viewer.container,
       scene: this.viewer.scene,
       transform: tileset.modelMatrix,
       boundingSphere: tileset.boundingSphere
     });

     //Enable widget.
     this.transformEditor.viewModel.activate();

     //Circle widget position
     // this.initialPosition =
       Cesium.Cartesian3.clone(this.transformEditor.viewModel.position);
     // this.initialHeadingPitchRoll =
      Cesium.HeadingPitchRoll.clone(this.transformEditor.viewModel.headingPitchRoll);
     // this.initialScale = Cesium.Cartesian3.clone(this.transformEditor.viewModel.scale);
   })
}

destroyTransformEditor(){
   if(this.transformEditor){
     this.transformEditor.destroy();
     // this.transformEditor = null;
   }
}

3. Context. Why do you need to do this? We might know a better way to accomplish your goal.

The context is simple, we want to have the possibility to add and remove the transform editor with a toggle button.

4. The Cesium version you're using, your operating system and browser.

Cesium 1.57
Version 75.0.3770.100 (64 bits)

It looks like your issue is that you re-copy the root transform onto the modelMatrix, but since that gets reset the first time, the second time it will overwrite the correct matrix. So you should only do the transformation in the lines below only on the first time the tileset is loaded, not every time you activate the transform editor:

const rootTransform = tileset.root.transform;
const modelMatrix = tileset.modelMatrix;

 // Transfer root transform to the model matrix
 Cesium.Matrix4.clone(rootTransform, modelMatrix);
 Cesium.Matrix4.clone(Cesium.Matrix4.IDENTITY, rootTransform);

``

Here’s a Sandcastle link you can load if you have the server running locally that correctly removes and re-adds the transform editor.

The reason this matrix copying is necessary on first load is because the transform editor uses the model matrix to move a primitive, so this moves the transform from the root tile to the tileset’s model matrix.