Back to home page

EIC code displayed by LXR

 
 

    


Warning, /firebird/firebird-ng/src/app/utils/three-geometry-editor.ts is written in an unsupported language. File is not indexed.

0001 import {Color, Material, Mesh, Object3D} from "three";
0002 import {createOutline, disposeOriginalMeshesAfterMerge, findObject3DNodes, pruneEmptyNodes} from "./three.utils";
0003 import {mergeBranchGeometries, mergeMeshList, MergeResult} from "./three-geometry-merge";
0004 import * as THREE from "three";
0005 import {ColorRepresentation} from "three/src/math/Color";
0006 
0007 export enum EditThreeNodeActions {
0008 
0009   Merge,   /** Merge children matching patterns (if patterns are provided) or all meshes of the node*/
0010 
0011 }
0012 
0013 export interface EditThreeNodeRule {
0014 
0015   patterns?: string[] | string;
0016   merge?: boolean;
0017   newName?:string;
0018   deleteOrigins?:boolean;
0019   cleanupNodes?:boolean;
0020   outline?:boolean;
0021   outlineThresholdAngle?:number;  /** [degrees] */
0022   outlineColor?:ColorRepresentation;
0023   material?: Material;
0024   color?: ColorRepresentation;
0025 
0026 }
0027 
0028 function mergeWhatever(node: Object3D, rule: EditThreeNodeRule): MergeResult| undefined {
0029 
0030   let newName = !rule.newName? node.name + "_merged" : rule.newName;
0031 
0032   if(!rule.patterns) {
0033     // If user provided patterns only children matching patterns (search goes over whole branch) will be merged,
0034     // But if no patterns given, we will merge whole node
0035     return mergeBranchGeometries(node, newName, rule.material);  // Children auto removed
0036   }
0037 
0038   // If we are here, we need to collect what to merge first
0039 
0040   let mergeSubjects = [];
0041   // merge whole node
0042   if(typeof rule.patterns === "string") {
0043     rule.patterns = [rule.patterns];
0044   }
0045 
0046   for(const pattern of rule.patterns) {
0047     mergeSubjects.push(...findObject3DNodes(node, pattern, "Mesh").nodes);
0048   }
0049 
0050   let result = mergeMeshList(mergeSubjects, node, newName, rule.material);
0051   const deleteOrigins = rule?.deleteOrigins ?? true;
0052   if(result && deleteOrigins) {
0053     disposeOriginalMeshesAfterMerge(result);
0054   }
0055   return result;
0056 }
0057 
0058 export function editThreeNodeContent(node: Object3D, rule: EditThreeNodeRule) {
0059 
0060   let {patterns, deleteOrigins = true, cleanupNodes=true, outline = true, outlineThresholdAngle = 40, outlineColor, material, color, merge=true, newName=""} = rule;
0061 
0062   let targetMesh: Mesh;
0063 
0064   if(merge) {
0065     let result = mergeWhatever(node, rule);
0066     if(!result) {
0067       console.warn("didn't find children to merge. Patterns:");
0068       console.log(patterns)
0069       return;
0070     }
0071     targetMesh = result.mergedMesh;
0072   }
0073   else {
0074     targetMesh = node as Mesh;
0075   }
0076 
0077   if(color !== undefined && color !== null) {
0078     if(targetMesh.material) {
0079       (targetMesh.material as any).color =  new Color(color);
0080     }
0081   }
0082 
0083   if(outline) {
0084     createOutline(targetMesh, {color: outlineColor, thresholdAngle: outlineThresholdAngle});
0085   }
0086 
0087   if(targetMesh && cleanupNodes) {
0088     pruneEmptyNodes(node);
0089   }
0090 }