import * as THREE from "three"; import { LineMaterial } from "three/examples/jsm/lines/LineMaterial.js"; import type { ColorValue, MaterialAppearance } from "../core/types.js"; /** * Options for MaterialFactory constructor */ interface MaterialFactoryOptions { defaultOpacity?: number; metalness?: number; roughness?: number; edgeColor?: number; transparent?: boolean; } /** * Options for face materials */ interface FaceMaterialOptions { color: ColorValue; alpha: number; visible?: boolean; } /** * Options for back face materials */ interface BackFaceMaterialOptions extends FaceMaterialOptions { polygonOffsetUnits?: number; } /** * Options for edge materials */ interface EdgeMaterialOptions { lineWidth: number; color?: ColorValue | null; vertexColors?: boolean; visible?: boolean; resolution?: { width: number; height: number; }; } /** * Options for simple edge materials */ interface SimpleEdgeMaterialOptions { color?: ColorValue | null; visible?: boolean; } /** * Options for vertex materials */ interface VertexMaterialOptions { size: number; color?: ColorValue | null; visible?: boolean; } /** * Options for texture materials */ interface TextureMaterialOptions { texture: THREE.Texture; visible?: boolean; } /** * Interface for the TextureCache dependency. * The actual TextureCache class is defined in texture-cache.ts. * We depend only on its get() method for loose coupling. */ interface TextureCacheInterface { get(ref: string, textureRole: string): Promise; } /** * Options for Studio mode materials. */ interface StudioMaterialOptions { /** Resolved MaterialAppearance definition (already looked up from materials table / presets) */ materialDef: MaterialAppearance; /** Fallback CSS hex color from the leaf node (e.g., "#cc0000") */ fallbackColor: ColorValue; /** Fallback alpha from the leaf node (0-1) */ fallbackAlpha: number; /** TextureCache for resolving texture references */ textureCache: TextureCacheInterface | null; } /** * Options for updating factory settings */ interface UpdateOptions { metalness?: number; roughness?: number; transparent?: boolean; defaultOpacity?: number; edgeColor?: number; } /** * Factory for creating THREE.js materials with consistent CAD viewer settings. * Centralizes material creation for testability and consistency. */ declare class MaterialFactory { defaultOpacity: number; metalness: number; roughness: number; edgeColor: number; transparent: boolean; /** * Create a MaterialFactory instance. */ constructor(options?: MaterialFactoryOptions); /** * Create base properties shared by all face materials. */ private _createBaseProps; /** * Create a standard material for front faces with PBR properties. */ createFrontFaceMaterial({ color, alpha, visible }: FaceMaterialOptions, label?: string): THREE.MeshStandardMaterial; /** * Create a standard material for back faces with PBR properties. * Used for polygon rendering where back faces need full shading. */ createBackFaceStandardMaterial({ color, alpha, polygonOffsetUnits, visible, }: BackFaceMaterialOptions, label?: string): THREE.MeshStandardMaterial; /** * Create a basic material for back faces (no lighting/PBR). * Used for shape rendering where back faces are simple fills. */ createBackFaceBasicMaterial({ color, alpha, polygonOffsetUnits, visible, }: BackFaceMaterialOptions, label?: string): THREE.MeshBasicMaterial; /** * Create a basic front face material (no PBR, for simple shapes). */ createBasicFaceMaterial({ color, alpha, visible }: FaceMaterialOptions, label?: string): THREE.MeshBasicMaterial; /** * Create a fat line material (LineMaterial from Three.js examples). */ createEdgeMaterial({ lineWidth, color, vertexColors, visible, resolution, }: EdgeMaterialOptions, label?: string): LineMaterial; /** * Create a basic line material for simple edges (e.g., polygon outlines). */ createSimpleEdgeMaterial({ color, visible }: SimpleEdgeMaterialOptions, label?: string): THREE.LineBasicMaterial; /** * Create a point material for vertex rendering. */ createVertexMaterial({ size, color, visible }: VertexMaterialOptions, label?: string): THREE.PointsMaterial; /** * Create a basic material for texture-mapped surfaces. */ createTextureMaterial({ texture, visible }: TextureMaterialOptions, label?: string): THREE.MeshBasicMaterial; /** * Create a Studio mode material from a resolved MaterialAppearance. * * Always creates MeshPhysicalMaterial (except when `unlit: true`, which * uses MeshBasicMaterial). MeshPhysicalMaterial is a superset of * MeshStandardMaterial; when advanced features are off (transmission=0, * clearcoat=0, sheen=0, etc.), the shader compiles to essentially the * same cost. * * @param options - Studio material options * @param label - Optional label for GPU tracking * @returns Configured MeshPhysicalMaterial (or MeshBasicMaterial if unlit) */ createStudioMaterial({ materialDef, fallbackColor, fallbackAlpha, textureCache, }: StudioMaterialOptions, label?: string): Promise; /** * Create a Studio mode material from a threejs-materials format entry. * * threejs-materials `properties` uses simplified property names (e.g., "color", * "roughness", "normal") where each entry has an optional `value` (scalar or * [r,g,b] array in **linear RGB**) and/or `texture` (inline data URI). * * @param values - Scalar PBR values from threejs-materials * @param textures - Texture map references from threejs-materials * @param textureRepeat - Optional [u, v] texture tiling applied to all loaded textures * @param textureRotation - Optional texture rotation in radians (counterclockwise), * pivoting around the texture center (0.5, 0.5) * @param textureCache - TextureCache for resolving data URI textures * @param label - Optional label for GPU tracking * @returns Configured MeshPhysicalMaterial */ createStudioMaterialFromMaterialX(values: Record, textures: Record, textureRepeat: [number, number] | undefined, textureRotation: number | undefined, textureCache: TextureCacheInterface | null, label?: string): Promise; /** * Apply alpha mode settings to a material. * * - OPAQUE: fully opaque, no transparency * - MASK: alpha testing with cutoff threshold * - BLEND: standard alpha blending * - Default (no alphaMode): transparent: true (current viewer behavior) */ private _applyAlphaMode; /** * Resolve and apply texture references from a MaterialAppearance onto a * MeshPhysicalMaterial via the TextureCache. * * Color-data textures (base color, emissive, sheen color, specular color) * are requested with SRGBColorSpace. All other textures (normal, metallic- * roughness, occlusion, roughness maps, transmission, thickness) are * requested with LinearSRGBColorSpace (the default). * * The metallicRoughnessTexture is a single combined texture where * B channel = metalness and G channel = roughness. It is assigned to * both metalnessMap and roughnessMap on the material. */ private _applyStudioTextures; /** * Update global settings. */ update(options: UpdateOptions): void; } export { MaterialFactory }; export type { MaterialFactoryOptions, UpdateOptions, StudioMaterialOptions, TextureCacheInterface, };