The Materials System provides a comprehensive, type-safe material management architecture for the Vibe Coder 3D engine. It supports both runtime material creation/modification and editor-based material authoring with full serialization support.
The central hub for material management:
interface IMaterialRegistry {
register(material: IMaterial): string;
get(id: string): IMaterial | undefined;
getAll(): Map<string, IMaterial>;
update(id: string, updates: Partial<IMaterial>): void;
remove(id: string): boolean;
clone(id: string, newName?: string): string;
}Key Features:
- Type Safety: Full TypeScript support with Zod schemas
- Serialization: Materials serialize to/from JSON with full fidelity
- Validation: Runtime validation of material properties
- Performance: Efficient lookups and caching
- Editor Integration: Seamless editor/runtime synchronization
Comprehensive type system defining material structure:
interface IMaterial {
id: string;
name: string;
type: 'standard' | 'phong' | 'lambert' | 'basic' | 'physical';
properties: IMaterialProperties;
textures: IMaterialTextures;
metadata: IMaterialMetadata;
}
interface IMaterialProperties {
color?: string | number;
metalness?: number;
roughness?: number;
emissive?: string | number;
emissiveIntensity?: number;
transparent?: boolean;
opacity?: number;
// ... additional properties
}Handles conversion between different material representations:
interface IMaterialConverter {
fromThreeJS(material: THREE.Material): IMaterial;
toThreeJS(material: IMaterial): THREE.Material;
fromJSON(data: unknown): IMaterial;
toJSON(material: IMaterial): unknown;
validate(material: IMaterial): ValidationResult;
}Provides a complete material authoring interface:
interface IMaterialsPanelProps {
selectedMaterialId?: string;
onMaterialSelect?: (id: string) => void;
onMaterialCreate?: (material: IMaterial) => void;
onMaterialUpdate?: (id: string, updates: Partial<IMaterial>) => void;
}Features:
- Material Browser: Grid/list view of all materials
- Property Inspector: Real-time editing of material properties
- Preview System: Live preview of material changes
- Template System: Pre-configured material templates
- Texture Management: Drag-and-drop texture assignment
- Export/Import: Material library management
Advanced material editing interface:
interface IMaterialInspectorProps {
material: IMaterial;
onChange: (updates: Partial<IMaterial>) => void;
preview?: boolean;
advanced?: boolean;
}Advanced Features:
- Shader Graph: Visual shader node editing (planned)
- Procedural Materials: Noise, gradient, and pattern generation
- Animation Support: Time-based material properties
- Layer System: Complex material layering
- Real-time Preview: WYSIWYG editing experience
// Core material access hook
export function useMaterial(id: string): IMaterial | undefined;
// Material creation and management
export function useMaterialManager(): {
create: (template?: IMaterial) => string;
update: (id: string, updates: Partial<IMaterial>) => void;
remove: (id: string) => void;
clone: (id: string) => string;
};
// Material serialization
export function useMaterialSerialization(): {
exportMaterial: (id: string) => string;
importMaterial: (json: string) => string;
exportLibrary: () => string;
importLibrary: (json: string) => void;
};// Declarative material usage in React components
<MaterialProvider materialId="my-material">
<mesh>
<boxGeometry />
<MaterialRenderer /> {/* Automatically applies material */}
</mesh>
</MaterialProvider>
// Hook-based material application
const MyComponent = () => {
const material = useMaterial('my-material');
return (
<mesh material={material?.threeMaterial}>
<boxGeometry />
</mesh>
);
};Full-featured serialization with versioning:
interface IMaterialSerialization {
serialize(material: IMaterial): ISerializedMaterial;
deserialize(data: ISerializedMaterial): IMaterial;
migrate(fromVersion: string, toVersion: string): ISerializedMaterial;
validate(data: unknown): ValidationResult;
}Serialization Features:
- Version Management: Automatic migration between versions
- Texture References: Proper handling of texture dependencies
- Optimization: Compression and delta serialization
- Validation: Schema validation with detailed error reporting
- Streaming: Support for large material libraries
// Efficient material reuse
class MaterialPool {
private static pool = new Map<string, THREE.Material[]>();
static get(material: IMaterial): THREE.Material {
const key = this.getMaterialKey(material);
const pooled = this.pool.get(key)?.pop();
return pooled || this.createMaterial(material);
}
static release(material: THREE.Material): void {
// Return to pool for reuse
}
}// GPU instancing for performance
interface IMaterialInstancing {
createInstances(count: number): InstancedMaterial;
updateInstance(index: number, properties: Partial<IMaterialProperties>): void;
optimizeForInstancing(material: IMaterial): IMaterial;
}sequenceDiagram
participant U as User
participant E as Editor UI
participant M as Material Registry
participant R as Runtime Engine
U->>E: Create New Material
E->>M: Register Material Template
M-->>E: Generate Unique ID
E->>U: Show Material Inspector
U->>E: Modify Properties
E->>M: Update Material
M-->>R: Sync to Runtime
R-->>E: Update Preview
U->>E: Save Material
E->>M: Persist to Storage
stateDiagram-v2
[*] --> MaterialSelected
MaterialSelected --> PropertyEditing: Edit Properties
PropertyEditing --> LivePreview: Auto-Update Preview
LivePreview --> PropertyEditing: Continue Editing
PropertyEditing --> SaveChanges: User Saves
SaveChanges --> [*]: Material Updated
// Material component for entities
export const MaterialComponent = defineComponent({
materialId: Types.ui32, // Reference to material registry
materialHash: Types.ui32, // For fast lookups
instanceId: Types.ui32, // For instanced materials
});// Material references in asset manifest
interface IMaterialAsset {
type: 'material';
id: string;
source: 'editor' | 'imported' | 'procedural';
dependencies: string[]; // Texture/shader dependencies
metadata: {
author: string;
tags: string[];
usage: 'character' | 'environment' | 'ui';
};
}interface IProceduralMaterial {
generateNoise(config: INoiseConfig): IMaterial;
generateGradient(config: IGradientConfig): IMaterial;
generatePattern(config: IPatternConfig): IMaterial;
combine(materials: IMaterial[], operation: 'mix' | 'add' | 'multiply'): IMaterial;
}interface IMaterialAnimation {
animate(material: IMaterial, time: number): IMaterial;
createKeyframe(material: IMaterial, time: number, properties: Partial<IMaterialProperties>): void;
play(materialId: string, animationId: string): void;
pause(materialId: string, animationId: string): void;
}describe('Materials System', () => {
describe('MaterialRegistry', () => {
it('should register and retrieve materials', () => {
const registry = new MaterialRegistry();
const material = createTestMaterial();
const id = registry.register(material);
expect(registry.get(id)).toEqual(material);
});
it('should handle serialization round-trips', () => {
const material = createComplexMaterial();
const serialized = MaterialSerializer.serialize(material);
const deserialized = MaterialSerializer.deserialize(serialized);
expect(deserialized).toEqual(material);
});
});
});- Visual Shader Graph: Node-based shader creation
- Material Libraries: Community-contributed material packs
- Real-time Collaboration: Multi-user material editing
- AI-Powered Materials: Generate materials from descriptions
- Material Market: Asset store integration
- Performance Profiling: Material performance analysis tools
interface IMaterialExtension {
name: string;
version: string;
materialTypes: string[];
editorComponents: React.ComponentType[];
runtimeHooks: Function[];
serializationHandlers: SerializationHandler[];
}- Material Registry: ~50KB base + 1KB per material
- Serialization Cache: Configurable size with LRU eviction
- Preview Textures: Shared texture atlas for efficiency
- Material Access: O(1) hash-based lookups
- Property Updates: O(log n) tree-based updates
- Serialization: ~2ms for complex materials
- Preview Updates: <16ms for real-time editing
This materials system provides a solid foundation for advanced material authoring while maintaining performance and type safety throughout the editor and runtime environments.