All files / lib meshUtils.js

67.32% Statements 68/101
59.32% Branches 35/59
75% Functions 24/32
65.93% Lines 60/91

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201                                1x 1x   1x 1x   1x                                 3x 2x 2x 2x   2x   1x 18x 4x 3x   1x 3x 1x   2x 3x 2x           1x                           1x 3x   1x 7x   1x                                           1x                                       1x 2x 1x 1x 13x 2x       1x       1x 3x 2x       1x 2x 1x       1x 3x 2x 2x     1x   1x       1x     1x 3x     3x 5x 2x 2x       3x     1x 4x           1x 3x             1x 3x          
import {
    MeshBasicMaterial,
    MeshLambertMaterial,
    MeshPhongMaterial,
    MeshDepthMaterial,
    MeshStandardMaterial,
    MeshToonMaterial,
} from "three";
 
import * as vivify from "vivifyjs";
import { omit } from "./object";
import Config from "../core/config";
import Lights from "../lights/Lights";
import ToonMaterial from "../materials/Toon";
import { MATERIALS, TEXTURES, UNDESIRED_SERIALISED_MATERIAL_PROPERTIES } from "./constants";
 
export const setUpLightsAndShadows = mesh => {
    const { textureAnisotropy, shadows } = Config.lights();
 
    mesh.castShadow = Boolean(shadows);
    mesh.receiveShadow = Boolean(shadows);
 
    Iif (hasMaterial(mesh)) {
        const setUpMaterial = material => {
            if (Lights.isUsingCascadeShadowMaps()) {
                Lights.csm.setupMaterial(material);
            }
 
            if (material.map) {
                material.map.anisotropy = textureAnisotropy;
            }
 
            return material;
        };
 
        mesh.material = processMaterial(mesh.material, setUpMaterial);
    }
};
 
export const isMesh = mesh => mesh.isMesh;
export const isSprite = mesh => mesh.isSprite;
export const isLine = mesh => mesh.isLine;
export const isScene = mesh => mesh.isScene;
 
export const notAScene = mesh => !mesh.isScene;
 
const isMeshOrSkinnedMesh = mesh => mesh.isMesh || mesh.isSkinnedMesh;
export const hasMaterial = mesh => Boolean(mesh.material);
export const hasGeometry = mesh => Boolean(mesh.geometry);
export const hasTexture = mesh => hasMaterial(mesh) && mesh.material.map;
 
export const applyMaterialChange = (elementBody, changeCallback) => {
    if (hasMaterial(elementBody)) {
        processMaterial(elementBody.material, changeCallback);
    } else {
        elementBody.traverse(child => {
            if (hasMaterial(child)) {
                processMaterial(child.material, changeCallback);
            }
        });
    }
};
 
export const extractMaterialProperty = (elementBody, property) => {
    if (hasMaterial(elementBody)) {
        return vivify.get(property, elementBody.material);
    } else {
        let found;
        elementBody.traverse(child => {
            if (hasMaterial(child) && !found) {
                found = vivify.get(child.material, property);
            }
        });
        return found;
    }
};
 
export const serialiseMaterial = material =>
    omit(UNDESIRED_SERIALISED_MATERIAL_PROPERTIES, material?.toJSON());
 
export const processMaterial = (material, callback) =>
    Array.isArray(material) ? material.map(callback) : callback(material);
 
export const replaceMaterialByName = (name, mesh, materialOptions) => {
    if (!hasMaterial(mesh)) return;
 
    switch (name) {
        case MATERIALS.LAMBERT:
            return replaceMaterial(MeshLambertMaterial, mesh, materialOptions);
        case MATERIALS.PHONG:
            return replaceMaterial(MeshPhongMaterial, mesh, materialOptions);
        case MATERIALS.DEPTH:
            return replaceMaterial(MeshDepthMaterial, mesh, materialOptions);
        case MATERIALS.STANDARD:
            return replaceMaterial(MeshStandardMaterial, mesh, materialOptions);
        case MATERIALS.TOON:
            return replaceMaterial(ToonMaterial, mesh, materialOptions);
        case MATERIALS.THREE_TOON:
            return replaceMaterial(MeshToonMaterial, mesh, materialOptions);
        case MATERIALS.BASIC:
        default:
            return replaceMaterial(MeshBasicMaterial, mesh, materialOptions);
    }
};
 
const replaceMaterial = (MeshMaterial, mesh, options = {}) => {
    const _replaceMaterial = material => {
        const clone = material.clone();
        const newMaterial = new MeshMaterial({
            map: clone.map,
            color: clone.color,
            ...options,
        });
 
        newMaterial.skinning = true;
        return newMaterial;
    };
 
    mesh.material = processMaterial(mesh.material, _replaceMaterial);
 
    setUpLightsAndShadows(mesh);
 
    return mesh.material;
};
 
export const disposeTextures = mesh => {
    if (hasMaterial(mesh)) {
        const _disposeTexture = material => {
            Object.values(TEXTURES).forEach(key => {
                if (material[key]) {
                    material[key].dispose();
                }
            });
        };
        processMaterial(mesh.material, _disposeTexture);
    }
};
 
export const disposeMaterial = mesh => {
    if (hasMaterial(mesh)) {
        mesh.material.dispose && mesh.material.dispose();
    }
};
 
export const disposeGeometry = mesh => {
    if (hasGeometry(mesh)) {
        mesh.geometry.dispose && mesh.geometry.dispose();
    }
};
 
export const prepareModel = model => {
    if (!model || typeof model.traverse !== 'function') {
        console.warn('[Mage] prepareModel received invalid model object:', model);
        return null;  // Return null for invalid models
    }
 
    setUpLightsAndShadows(model);
 
    model.traverse(mesh => {
        setUpLightsAndShadows(mesh);
    });
 
    return model;
};
 
export const findFirstInScene = (scene, filter) => {
    let found = false;
    let toReturn;
 
    scene.traverse(element => {
        if (filter(element) && !found) {
            found = true;
            toReturn = element;
        }
    });
 
    return toReturn;
};
 
export const serializeVector = vector =>
    vector && {
        x: vector.x,
        y: vector.y,
        z: vector.z,
    };
 
export const serializeQuaternion = quaternion =>
    quaternion && {
        x: quaternion.x,
        y: quaternion.y,
        z: quaternion.z,
        w: quaternion.w,
    };
 
export const serializeColor = color =>
    color && {
        r: color.r,
        g: color.g,
        b: color.b,
    };