cione-comp-lib
Version:
`$ yarn add cione-comp-lib` 或者 `$ npm i cione-comp-lib -S`
85 lines (84 loc) • 3.3 kB
JavaScript
import { Sphere, FrontSide } from "../../../../../_three@0.150.1@three/build/three.module.mjs";
import { GLTFInstance, $prepare, $preparedGLTF, $clone } from "../GLTFInstance.mjs";
import { CorrelatedSceneGraph } from "./correlated-scene-graph.mjs";
/* @license
* Copyright 2020 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const $correlatedSceneGraph = Symbol("correlatedSceneGraph");
class ModelViewerGLTFInstance extends GLTFInstance {
static [$prepare](source) {
const prepared = super[$prepare](source);
if (prepared[$correlatedSceneGraph] == null) {
prepared[$correlatedSceneGraph] = CorrelatedSceneGraph.from(prepared);
}
const { scene } = prepared;
const nullSphere = new Sphere(void 0, Infinity);
scene.traverse((node) => {
node.renderOrder = 1e3;
node.frustumCulled = false;
if (!node.name) {
node.name = node.uuid;
}
const mesh = node;
if (mesh.isMesh) {
const { geometry } = mesh;
mesh.castShadow = true;
if (mesh.isSkinnedMesh) {
geometry.boundingSphere = nullSphere;
geometry.boundingBox = null;
}
const material = mesh.material;
if (material.isMeshBasicMaterial === true) {
material.toneMapped = false;
}
material.shadowSide = FrontSide;
if (material.aoMap) {
const { gltf, threeObjectMap } = prepared[$correlatedSceneGraph];
const gltfRef = threeObjectMap.get(material);
if (gltf.materials != null && gltfRef != null && gltfRef.materials != null) {
const gltfMaterial = gltf.materials[gltfRef.materials];
if (gltfMaterial.occlusionTexture && gltfMaterial.occlusionTexture.texCoord === 0 && geometry.attributes.uv != null) {
geometry.setAttribute("uv2", geometry.attributes.uv);
}
}
}
}
});
return prepared;
}
get correlatedSceneGraph() {
return this[$preparedGLTF][$correlatedSceneGraph];
}
[$clone]() {
const clone = super[$clone]();
const sourceUUIDToClonedMaterial = /* @__PURE__ */ new Map();
clone.scene.traverse((node) => {
if (node.isMesh) {
const mesh = node;
const material = mesh.material;
if (material != null) {
if (sourceUUIDToClonedMaterial.has(material.uuid)) {
mesh.material = sourceUUIDToClonedMaterial.get(material.uuid);
return;
}
mesh.material = material.clone();
sourceUUIDToClonedMaterial.set(material.uuid, mesh.material);
}
}
});
clone[$correlatedSceneGraph] = CorrelatedSceneGraph.from(clone, this.correlatedSceneGraph);
return clone;
}
}
export { ModelViewerGLTFInstance };