Bläddra i källkod

allow model normalization
Adding `model.normalize="true"` to the HTML element of the viewer will normalize the model (assuming GLTF - parent is in index 0 of the loaded meshes). It can be configured further:
* `model.normalize.unitSize="true"` - scale to unit size
* `model.normalize.center="true"` - should the model be centered
* `model.normalize.parentIndex="0"` - location of the parent in the meshes' array that is being loaded. not defining it will use all meshes with no parents! (unless the global `model.normalize="true"` is used, then 0 is assumed)

Raanan Weber 7 år sedan
förälder
incheckning
dbe9a26df4

+ 5 - 0
Viewer/src/configuration/configuration.ts

@@ -75,6 +75,11 @@ export interface IModelConfiguration {
     parentObjectIndex?: number; // the index of the parent object of the model in the loaded meshes array.
     parentObjectIndex?: number; // the index of the parent object of the model in the loaded meshes array.
 
 
     castShadow?: boolean;
     castShadow?: boolean;
+    normalize?: boolean | {
+        center?: boolean;
+        unitSize?: boolean;
+        parentIndex?: number;
+    }; // shoud the model be scaled to unit-size
 
 
     title: string;
     title: string;
     subtitle?: string;
     subtitle?: string;

+ 46 - 0
Viewer/src/viewer/viewer.ts

@@ -529,6 +529,52 @@ export abstract class AbstractViewer {
                 Tags.AddTagsTo(mesh, 'castShadow');
                 Tags.AddTagsTo(mesh, 'castShadow');
             });
             });
         }
         }
+
+        if (modelConfiguration.normalize) {
+            let center = false;
+            let unitSize = false;
+            let parentIndex;
+            if (modelConfiguration.normalize === true) {
+                center = true;
+                unitSize = true;
+                parentIndex = 0;
+            } else {
+                center = !!modelConfiguration.normalize.center;
+                unitSize = !!modelConfiguration.normalize.unitSize;
+                parentIndex = modelConfiguration.normalize.parentIndex;
+            }
+
+            let meshesToNormalize: Array<AbstractMesh> = [];
+            if (parentIndex !== undefined) {
+                meshesToNormalize.push(focusMeshes[parentIndex]);
+            } else {
+                meshesToNormalize = meshesWithNoParent;
+            }
+
+            if (unitSize) {
+                meshesToNormalize.forEach(mesh => {
+                    console.log(mesh.scaling.x)
+                    mesh.normalizeToUnitCube(true);
+                    mesh.computeWorldMatrix(true);
+                    console.log(mesh.scaling.x)
+                });
+            }
+            if (center) {
+                meshesToNormalize.forEach(mesh => {
+                    const boundingInfo = mesh.getHierarchyBoundingVectors(true);
+                    const sizeVec = boundingInfo.max.subtract(boundingInfo.min);
+                    const halfSizeVec = sizeVec.scale(0.5);
+                    const center = boundingInfo.min.add(halfSizeVec);
+                    mesh.position = center.scale(-1);
+
+                    // Set on ground.
+                    mesh.position.y += halfSizeVec.y;
+
+                    // Recompute Info.
+                    mesh.computeWorldMatrix(true);
+                });
+            }
+        }
     }
     }
 
 
     public dispose() {
     public dispose() {

+ 1 - 0
dist/preview release/what's new.md

@@ -47,6 +47,7 @@
 - AssetContainer Class and loading methods. ([trevordev](https://github.com/trevordev))
 - AssetContainer Class and loading methods. ([trevordev](https://github.com/trevordev))
 - KeepAssets class and AssetContainer.moveAllFromScene ([HoloLite](http://www.html5gamedevs.com/profile/28694-hololite/), [trevordev](https://github.com/trevordev))
 - KeepAssets class and AssetContainer.moveAllFromScene ([HoloLite](http://www.html5gamedevs.com/profile/28694-hololite/), [trevordev](https://github.com/trevordev))
 - (Viewer) It is now possible to update parts of the configuration without rcreating the objects. ([RaananW](https://github.com/RaananW))
 - (Viewer) It is now possible to update parts of the configuration without rcreating the objects. ([RaananW](https://github.com/RaananW))
+- (Viewer) Model can be normalized using configuration. ([RaananW](https://github.com/RaananW))
 
 
 ## Bug fixes
 ## Bug fixes
 - `setPivotMatrix` ws not setting pivot correctly. This is now fixed. We also introduced a new `setPreTransformMatrix` to reproduce the sometimes needed behavior of the previous `setPivotMatrix` function ([deltakosh](https://github.com/deltakosh))
 - `setPivotMatrix` ws not setting pivot correctly. This is now fixed. We also introduced a new `setPreTransformMatrix` to reproduce the sometimes needed behavior of the previous `setPivotMatrix` function ([deltakosh](https://github.com/deltakosh))