@holoscript/core 1.0.0-alpha.1 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +10 -9
- package/src/HoloScript2DParser.js +227 -0
- package/src/HoloScript2DParser.ts +5 -0
- package/src/HoloScriptCodeParser.js +1102 -0
- package/src/HoloScriptCodeParser.ts +145 -20
- package/src/HoloScriptDebugger.js +458 -0
- package/src/HoloScriptParser.js +338 -0
- package/src/HoloScriptPlusParser.js +371 -0
- package/src/HoloScriptPlusParser.ts +543 -0
- package/src/HoloScriptRuntime.js +1399 -0
- package/src/HoloScriptRuntime.test.js +351 -0
- package/src/HoloScriptRuntime.ts +257 -3
- package/src/HoloScriptTypeChecker.js +356 -0
- package/src/__tests__/GraphicsServices.test.js +357 -0
- package/src/__tests__/GraphicsServices.test.ts +427 -0
- package/src/__tests__/HoloScriptPlusParser.test.js +317 -0
- package/src/__tests__/HoloScriptPlusParser.test.ts +392 -0
- package/src/__tests__/integration.test.js +336 -0
- package/src/__tests__/performance.bench.js +218 -0
- package/src/__tests__/type-checker.test.js +60 -0
- package/src/__tests__/type-checker.test.ts +73 -0
- package/src/index.js +217 -0
- package/src/index.ts +158 -18
- package/src/interop/Interoperability.js +413 -0
- package/src/interop/Interoperability.ts +494 -0
- package/src/logger.js +42 -0
- package/src/parser/EnhancedParser.js +205 -0
- package/src/parser/EnhancedParser.ts +251 -0
- package/src/parser/HoloScriptPlusParser.js +928 -0
- package/src/parser/HoloScriptPlusParser.ts +1089 -0
- package/src/runtime/HoloScriptPlusRuntime.js +674 -0
- package/src/runtime/HoloScriptPlusRuntime.ts +861 -0
- package/src/runtime/PerformanceTelemetry.js +323 -0
- package/src/runtime/PerformanceTelemetry.ts +467 -0
- package/src/runtime/RuntimeOptimization.js +361 -0
- package/src/runtime/RuntimeOptimization.ts +416 -0
- package/src/services/HololandGraphicsPipelineService.js +506 -0
- package/src/services/HololandGraphicsPipelineService.ts +662 -0
- package/src/services/PlatformPerformanceOptimizer.js +356 -0
- package/src/services/PlatformPerformanceOptimizer.ts +503 -0
- package/src/state/ReactiveState.js +427 -0
- package/src/state/ReactiveState.ts +572 -0
- package/src/tools/DeveloperExperience.js +376 -0
- package/src/tools/DeveloperExperience.ts +438 -0
- package/src/traits/AIDriverTrait.js +322 -0
- package/src/traits/AIDriverTrait.test.js +329 -0
- package/src/traits/AIDriverTrait.test.ts +357 -0
- package/src/traits/AIDriverTrait.ts +474 -0
- package/src/traits/LightingTrait.js +313 -0
- package/src/traits/LightingTrait.test.js +410 -0
- package/src/traits/LightingTrait.test.ts +462 -0
- package/src/traits/LightingTrait.ts +505 -0
- package/src/traits/MaterialTrait.js +194 -0
- package/src/traits/MaterialTrait.test.js +286 -0
- package/src/traits/MaterialTrait.test.ts +329 -0
- package/src/traits/MaterialTrait.ts +324 -0
- package/src/traits/RenderingTrait.js +356 -0
- package/src/traits/RenderingTrait.test.js +363 -0
- package/src/traits/RenderingTrait.test.ts +427 -0
- package/src/traits/RenderingTrait.ts +555 -0
- package/src/traits/VRTraitSystem.js +740 -0
- package/src/traits/VRTraitSystem.ts +1040 -0
- package/src/traits/VoiceInputTrait.js +284 -0
- package/src/traits/VoiceInputTrait.test.js +226 -0
- package/src/traits/VoiceInputTrait.test.ts +252 -0
- package/src/traits/VoiceInputTrait.ts +401 -0
- package/src/types/AdvancedTypeSystem.js +226 -0
- package/src/types/AdvancedTypeSystem.ts +494 -0
- package/src/types/HoloScriptPlus.d.ts +853 -0
- package/src/types.js +6 -0
- package/src/types.ts +96 -1
- package/tsconfig.json +1 -1
- package/tsup.config.d.ts +2 -0
- package/tsup.config.js +18 -0
- package/LICENSE +0 -21
- package/dist/chunk-3X2EGU7Z.cjs +0 -52
- package/dist/chunk-3X2EGU7Z.cjs.map +0 -1
- package/dist/chunk-723TPVHD.js +0 -1074
- package/dist/chunk-723TPVHD.js.map +0 -1
- package/dist/chunk-EOKNAVDO.cjs +0 -424
- package/dist/chunk-EOKNAVDO.cjs.map +0 -1
- package/dist/chunk-HQZ3HUMY.js +0 -1087
- package/dist/chunk-HQZ3HUMY.js.map +0 -1
- package/dist/chunk-KWYIVRIH.js +0 -344
- package/dist/chunk-KWYIVRIH.js.map +0 -1
- package/dist/chunk-LKH4ZAN6.js +0 -421
- package/dist/chunk-LKH4ZAN6.js.map +0 -1
- package/dist/chunk-SATNCODL.js +0 -45
- package/dist/chunk-SATNCODL.js.map +0 -1
- package/dist/chunk-VMZN4EVR.cjs +0 -347
- package/dist/chunk-VMZN4EVR.cjs.map +0 -1
- package/dist/chunk-VV3UUUYP.cjs +0 -1089
- package/dist/chunk-VV3UUUYP.cjs.map +0 -1
- package/dist/chunk-XRYTSQHZ.cjs +0 -1076
- package/dist/chunk-XRYTSQHZ.cjs.map +0 -1
- package/dist/debugger.cjs +0 -19
- package/dist/debugger.cjs.map +0 -1
- package/dist/debugger.d.cts +0 -171
- package/dist/debugger.d.ts +0 -171
- package/dist/debugger.js +0 -6
- package/dist/debugger.js.map +0 -1
- package/dist/index.cjs +0 -755
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -169
- package/dist/index.d.ts +0 -169
- package/dist/index.js +0 -699
- package/dist/index.js.map +0 -1
- package/dist/parser.cjs +0 -13
- package/dist/parser.cjs.map +0 -1
- package/dist/parser.d.cts +0 -154
- package/dist/parser.d.ts +0 -154
- package/dist/parser.js +0 -4
- package/dist/parser.js.map +0 -1
- package/dist/runtime.cjs +0 -13
- package/dist/runtime.cjs.map +0 -1
- package/dist/runtime.d.cts +0 -147
- package/dist/runtime.d.ts +0 -147
- package/dist/runtime.js +0 -4
- package/dist/runtime.js.map +0 -1
- package/dist/type-checker.cjs +0 -16
- package/dist/type-checker.cjs.map +0 -1
- package/dist/type-checker.d.cts +0 -105
- package/dist/type-checker.d.ts +0 -105
- package/dist/type-checker.js +0 -3
- package/dist/type-checker.js.map +0 -1
- package/dist/types-WQSk1Qs2.d.cts +0 -238
- package/dist/types-WQSk1Qs2.d.ts +0 -238
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach } from 'vitest';
|
|
2
|
+
import { MaterialTrait, createMaterialTrait, MATERIAL_PRESETS, } from '../traits/MaterialTrait';
|
|
3
|
+
describe('MaterialTrait', () => {
|
|
4
|
+
let material;
|
|
5
|
+
beforeEach(() => {
|
|
6
|
+
const config = {
|
|
7
|
+
type: 'pbr',
|
|
8
|
+
pbr: {
|
|
9
|
+
baseColor: { r: 1, g: 1, b: 1 },
|
|
10
|
+
metallic: 0,
|
|
11
|
+
roughness: 0.5,
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
material = new MaterialTrait(config);
|
|
15
|
+
});
|
|
16
|
+
describe('initialization', () => {
|
|
17
|
+
it('should create material with default PBR type', () => {
|
|
18
|
+
const result = material.getMaterial();
|
|
19
|
+
expect(result.type).toBe('pbr');
|
|
20
|
+
});
|
|
21
|
+
it('should initialize with provided configuration', () => {
|
|
22
|
+
const config = {
|
|
23
|
+
type: 'pbr',
|
|
24
|
+
name: 'my-material',
|
|
25
|
+
pbr: {
|
|
26
|
+
baseColor: { r: 0.5, g: 0.5, b: 0.5 },
|
|
27
|
+
metallic: 0.8,
|
|
28
|
+
roughness: 0.2,
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
const mat = new MaterialTrait(config);
|
|
32
|
+
const result = mat.getMaterial();
|
|
33
|
+
expect(result.name).toBe('my-material');
|
|
34
|
+
expect(result.pbr?.metallic).toBe(0.8);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
describe('getPBRProperties', () => {
|
|
38
|
+
it('should return PBR material properties', () => {
|
|
39
|
+
const pbr = material.getPBRProperties();
|
|
40
|
+
expect(pbr).toBeDefined();
|
|
41
|
+
expect(pbr?.baseColor).toEqual({ r: 1, g: 1, b: 1 });
|
|
42
|
+
expect(pbr?.metallic).toBe(0);
|
|
43
|
+
expect(pbr?.roughness).toBe(0.5);
|
|
44
|
+
});
|
|
45
|
+
it('should return undefined for non-PBR material types', () => {
|
|
46
|
+
const unlit = new MaterialTrait({ type: 'unlit' });
|
|
47
|
+
expect(unlit.getPBRProperties()).toBeUndefined();
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
describe('setProperty', () => {
|
|
51
|
+
it('should update material property', () => {
|
|
52
|
+
material.setProperty('type', 'transparent');
|
|
53
|
+
const result = material.getMaterial();
|
|
54
|
+
expect(result.type).toBe('transparent');
|
|
55
|
+
});
|
|
56
|
+
it('should update blend mode', () => {
|
|
57
|
+
material.setProperty('blendMode', 'additive');
|
|
58
|
+
const result = material.getMaterial();
|
|
59
|
+
expect(result.blendMode).toBe('additive');
|
|
60
|
+
});
|
|
61
|
+
it('should toggle double-sided rendering', () => {
|
|
62
|
+
material.setProperty('doubleSided', true);
|
|
63
|
+
const result = material.getMaterial();
|
|
64
|
+
expect(result.doubleSided).toBe(true);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
describe('updatePBR', () => {
|
|
68
|
+
it('should update PBR material properties', () => {
|
|
69
|
+
material.updatePBR({
|
|
70
|
+
metallic: 1,
|
|
71
|
+
roughness: 0.1,
|
|
72
|
+
});
|
|
73
|
+
const pbr = material.getPBRProperties();
|
|
74
|
+
expect(pbr?.metallic).toBe(1);
|
|
75
|
+
expect(pbr?.roughness).toBe(0.1);
|
|
76
|
+
});
|
|
77
|
+
it('should preserve existing properties when updating', () => {
|
|
78
|
+
material.updatePBR({ metallic: 0.5 });
|
|
79
|
+
const pbr = material.getPBRProperties();
|
|
80
|
+
expect(pbr?.metallic).toBe(0.5);
|
|
81
|
+
expect(pbr?.roughness).toBe(0.5); // Unchanged
|
|
82
|
+
});
|
|
83
|
+
it('should create default PBR if not exists', () => {
|
|
84
|
+
const unlit = new MaterialTrait({ type: 'unlit' });
|
|
85
|
+
unlit.updatePBR({ metallic: 0.7 });
|
|
86
|
+
const pbr = unlit.getPBRProperties();
|
|
87
|
+
expect(pbr).toBeDefined();
|
|
88
|
+
expect(pbr?.metallic).toBe(0.7);
|
|
89
|
+
});
|
|
90
|
+
it('should support emission properties', () => {
|
|
91
|
+
material.updatePBR({
|
|
92
|
+
emission: {
|
|
93
|
+
color: { r: 1, g: 0, b: 0 },
|
|
94
|
+
intensity: 2.0,
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
const pbr = material.getPBRProperties();
|
|
98
|
+
expect(pbr?.emission?.color.r).toBe(1);
|
|
99
|
+
expect(pbr?.emission?.intensity).toBe(2.0);
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
describe('texture management', () => {
|
|
103
|
+
it('should add texture map', () => {
|
|
104
|
+
material.addTexture({
|
|
105
|
+
path: '/textures/diffuse.png',
|
|
106
|
+
channel: 'baseColor',
|
|
107
|
+
});
|
|
108
|
+
const textures = material.getTextures();
|
|
109
|
+
expect(textures).toHaveLength(1);
|
|
110
|
+
expect(textures[0].path).toBe('/textures/diffuse.png');
|
|
111
|
+
});
|
|
112
|
+
it('should support multiple textures', () => {
|
|
113
|
+
material.addTexture({ path: '/t1.png', channel: 'baseColor' });
|
|
114
|
+
material.addTexture({ path: '/t2.png', channel: 'normalMap' });
|
|
115
|
+
material.addTexture({ path: '/t3.png', channel: 'roughnessMap' });
|
|
116
|
+
expect(material.getTextures()).toHaveLength(3);
|
|
117
|
+
});
|
|
118
|
+
it('should preserve texture configuration', () => {
|
|
119
|
+
material.addTexture({
|
|
120
|
+
path: '/textures/normal.png',
|
|
121
|
+
channel: 'normalMap',
|
|
122
|
+
scale: { x: 2, y: 2 },
|
|
123
|
+
filter: 'anisotropic',
|
|
124
|
+
anisotropy: 16,
|
|
125
|
+
});
|
|
126
|
+
const textures = material.getTextures();
|
|
127
|
+
expect(textures[0].scale).toEqual({ x: 2, y: 2 });
|
|
128
|
+
expect(textures[0].filter).toBe('anisotropic');
|
|
129
|
+
expect(textures[0].anisotropy).toBe(16);
|
|
130
|
+
});
|
|
131
|
+
it('should clear texture cache', () => {
|
|
132
|
+
material.addTexture({ path: '/t1.png', channel: 'baseColor' });
|
|
133
|
+
material.clearTextureCache();
|
|
134
|
+
// Verify no error and textures still accessible
|
|
135
|
+
expect(material.getTextures()).toHaveLength(1);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
describe('custom shaders', () => {
|
|
139
|
+
it('should set custom shader code', () => {
|
|
140
|
+
const shader = {
|
|
141
|
+
fragment: 'void main() { gl_FragColor = vec4(1.0); }',
|
|
142
|
+
shaderLanguage: 'glsl',
|
|
143
|
+
};
|
|
144
|
+
material.setCustomShader(shader);
|
|
145
|
+
const result = material.getCustomShader();
|
|
146
|
+
expect(result?.fragment).toBe(shader.fragment);
|
|
147
|
+
expect(result?.shaderLanguage).toBe('glsl');
|
|
148
|
+
});
|
|
149
|
+
it('should support vertex and fragment shaders', () => {
|
|
150
|
+
const shader = {
|
|
151
|
+
vertex: 'void main() { gl_Position = vec4(1.0); }',
|
|
152
|
+
fragment: 'void main() { gl_FragColor = vec4(1.0); }',
|
|
153
|
+
shaderLanguage: 'glsl',
|
|
154
|
+
};
|
|
155
|
+
material.setCustomShader(shader);
|
|
156
|
+
const result = material.getCustomShader();
|
|
157
|
+
expect(result?.vertex).toBeDefined();
|
|
158
|
+
expect(result?.fragment).toBeDefined();
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
describe('optimization hints', () => {
|
|
162
|
+
it('should enable texture streaming', () => {
|
|
163
|
+
material.setTextureStreaming(true);
|
|
164
|
+
const result = material.getMaterial();
|
|
165
|
+
expect(result.optimization?.streamTextures).toBe(true);
|
|
166
|
+
});
|
|
167
|
+
it('should set texture compression', () => {
|
|
168
|
+
material.setCompression('astc');
|
|
169
|
+
const result = material.getMaterial();
|
|
170
|
+
expect(result.optimization?.compression).toBe('astc');
|
|
171
|
+
});
|
|
172
|
+
it('should enable material instancing', () => {
|
|
173
|
+
material.setInstanced(true);
|
|
174
|
+
const result = material.getMaterial();
|
|
175
|
+
expect(result.optimization?.instanced).toBe(true);
|
|
176
|
+
});
|
|
177
|
+
it('should get optimization configuration', () => {
|
|
178
|
+
material.setTextureStreaming(true);
|
|
179
|
+
material.setCompression('dxt');
|
|
180
|
+
const opt = material.getOptimization();
|
|
181
|
+
expect(opt?.streamTextures).toBe(true);
|
|
182
|
+
expect(opt?.compression).toBe('dxt');
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
describe('material presets', () => {
|
|
186
|
+
it('should create chrome material', () => {
|
|
187
|
+
const chrome = MATERIAL_PRESETS.chrome();
|
|
188
|
+
expect(chrome.pbr?.metallic).toBe(1.0);
|
|
189
|
+
expect(chrome.pbr?.roughness).toBe(0.1);
|
|
190
|
+
});
|
|
191
|
+
it('should create plastic material', () => {
|
|
192
|
+
const plastic = MATERIAL_PRESETS.plastic();
|
|
193
|
+
expect(plastic.pbr?.metallic).toBe(0);
|
|
194
|
+
expect(plastic.pbr?.roughness).toBe(0.8);
|
|
195
|
+
});
|
|
196
|
+
it('should create wood material', () => {
|
|
197
|
+
const wood = MATERIAL_PRESETS.wood();
|
|
198
|
+
expect(wood.pbr?.baseColor.r).toBe(0.6);
|
|
199
|
+
expect(wood.pbr?.roughness).toBe(0.4);
|
|
200
|
+
});
|
|
201
|
+
it('should create glass material', () => {
|
|
202
|
+
const glass = MATERIAL_PRESETS.glass();
|
|
203
|
+
expect(glass.type).toBe('transparent');
|
|
204
|
+
expect(glass.blendMode).toBe('blend');
|
|
205
|
+
expect(glass.pbr?.transmission).toBe(0.9);
|
|
206
|
+
});
|
|
207
|
+
it('should create emissive material', () => {
|
|
208
|
+
const emissive = MATERIAL_PRESETS.emissive();
|
|
209
|
+
expect(emissive.pbr?.emission).toBeDefined();
|
|
210
|
+
expect(emissive.pbr?.emission?.intensity).toBe(2.0);
|
|
211
|
+
});
|
|
212
|
+
it('should create skin material', () => {
|
|
213
|
+
const skin = MATERIAL_PRESETS.skin();
|
|
214
|
+
expect(skin.pbr?.roughness).toBe(0.5);
|
|
215
|
+
expect(skin.pbr?.ambientOcclusion).toBe(0.8);
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
describe('factory function', () => {
|
|
219
|
+
it('should create material via factory', () => {
|
|
220
|
+
const config = {
|
|
221
|
+
type: 'pbr',
|
|
222
|
+
pbr: {
|
|
223
|
+
baseColor: { r: 0.8, g: 0.8, b: 0.8 },
|
|
224
|
+
metallic: 0.5,
|
|
225
|
+
roughness: 0.5,
|
|
226
|
+
},
|
|
227
|
+
};
|
|
228
|
+
const mat = createMaterialTrait(config);
|
|
229
|
+
expect(mat.getMaterial().type).toBe('pbr');
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
describe('disposal', () => {
|
|
233
|
+
it('should dispose and cleanup', () => {
|
|
234
|
+
material.addTexture({ path: '/t.png', channel: 'baseColor' });
|
|
235
|
+
material.dispose();
|
|
236
|
+
// Verify no error after disposal
|
|
237
|
+
expect(() => material.clearTextureCache()).not.toThrow();
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
describe('material copying', () => {
|
|
241
|
+
it('should create independent material copy', () => {
|
|
242
|
+
const original = material.getMaterial();
|
|
243
|
+
const copy = new MaterialTrait(original);
|
|
244
|
+
copy.updatePBR({ metallic: 0.9 });
|
|
245
|
+
const originalAfter = material.getMaterial();
|
|
246
|
+
expect(originalAfter.pbr?.metallic).toBe(0); // Unchanged
|
|
247
|
+
expect(copy.getPBRProperties()?.metallic).toBe(0.9);
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
describe('complex material setup', () => {
|
|
251
|
+
it('should configure full PBR material with all properties', () => {
|
|
252
|
+
const config = {
|
|
253
|
+
type: 'pbr',
|
|
254
|
+
name: 'realistic-paint',
|
|
255
|
+
pbr: {
|
|
256
|
+
baseColor: { r: 0.8, g: 0.2, b: 0.2, a: 1 },
|
|
257
|
+
metallic: 0.1,
|
|
258
|
+
roughness: 0.6,
|
|
259
|
+
ambientOcclusion: 0.9,
|
|
260
|
+
emission: { color: { r: 0, g: 0, b: 0 }, intensity: 0 },
|
|
261
|
+
normalStrength: 1.0,
|
|
262
|
+
parallaxHeight: 0.02,
|
|
263
|
+
ior: 1.5,
|
|
264
|
+
},
|
|
265
|
+
textures: [
|
|
266
|
+
{ path: '/diffuse.png', channel: 'baseColor' },
|
|
267
|
+
{ path: '/normal.png', channel: 'normalMap', normalStrength: 1.5 },
|
|
268
|
+
{ path: '/roughness.png', channel: 'roughnessMap' },
|
|
269
|
+
],
|
|
270
|
+
doubleSided: false,
|
|
271
|
+
blendMode: 'opaque',
|
|
272
|
+
optimization: {
|
|
273
|
+
compression: 'dxt',
|
|
274
|
+
instanced: true,
|
|
275
|
+
lodBias: 0.5,
|
|
276
|
+
},
|
|
277
|
+
};
|
|
278
|
+
const mat = new MaterialTrait(config);
|
|
279
|
+
const result = mat.getMaterial();
|
|
280
|
+
expect(result.name).toBe('realistic-paint');
|
|
281
|
+
expect(result.pbr?.baseColor.r).toBe(0.8);
|
|
282
|
+
expect(result.textures).toHaveLength(3);
|
|
283
|
+
expect(result.optimization?.compression).toBe('dxt');
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
});
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
MaterialTrait,
|
|
4
|
+
createMaterialTrait,
|
|
5
|
+
MATERIAL_PRESETS,
|
|
6
|
+
type MaterialConfig,
|
|
7
|
+
type PBRMaterial,
|
|
8
|
+
} from '../traits/MaterialTrait';
|
|
9
|
+
|
|
10
|
+
describe('MaterialTrait', () => {
|
|
11
|
+
let material: MaterialTrait;
|
|
12
|
+
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
const config: MaterialConfig = {
|
|
15
|
+
type: 'pbr',
|
|
16
|
+
pbr: {
|
|
17
|
+
baseColor: { r: 1, g: 1, b: 1 },
|
|
18
|
+
metallic: 0,
|
|
19
|
+
roughness: 0.5,
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
material = new MaterialTrait(config);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
describe('initialization', () => {
|
|
26
|
+
it('should create material with default PBR type', () => {
|
|
27
|
+
const result = material.getMaterial();
|
|
28
|
+
expect(result.type).toBe('pbr');
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('should initialize with provided configuration', () => {
|
|
32
|
+
const config: MaterialConfig = {
|
|
33
|
+
type: 'pbr',
|
|
34
|
+
name: 'my-material',
|
|
35
|
+
pbr: {
|
|
36
|
+
baseColor: { r: 0.5, g: 0.5, b: 0.5 },
|
|
37
|
+
metallic: 0.8,
|
|
38
|
+
roughness: 0.2,
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
const mat = new MaterialTrait(config);
|
|
42
|
+
const result = mat.getMaterial();
|
|
43
|
+
expect(result.name).toBe('my-material');
|
|
44
|
+
expect(result.pbr?.metallic).toBe(0.8);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe('getPBRProperties', () => {
|
|
49
|
+
it('should return PBR material properties', () => {
|
|
50
|
+
const pbr = material.getPBRProperties();
|
|
51
|
+
expect(pbr).toBeDefined();
|
|
52
|
+
expect(pbr?.baseColor).toEqual({ r: 1, g: 1, b: 1 });
|
|
53
|
+
expect(pbr?.metallic).toBe(0);
|
|
54
|
+
expect(pbr?.roughness).toBe(0.5);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('should return undefined for non-PBR material types', () => {
|
|
58
|
+
const unlit = new MaterialTrait({ type: 'unlit' });
|
|
59
|
+
expect(unlit.getPBRProperties()).toBeUndefined();
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe('setProperty', () => {
|
|
64
|
+
it('should update material property', () => {
|
|
65
|
+
material.setProperty('type', 'transparent');
|
|
66
|
+
const result = material.getMaterial();
|
|
67
|
+
expect(result.type).toBe('transparent');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('should update blend mode', () => {
|
|
71
|
+
material.setProperty('blendMode', 'additive');
|
|
72
|
+
const result = material.getMaterial();
|
|
73
|
+
expect(result.blendMode).toBe('additive');
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('should toggle double-sided rendering', () => {
|
|
77
|
+
material.setProperty('doubleSided', true);
|
|
78
|
+
const result = material.getMaterial();
|
|
79
|
+
expect(result.doubleSided).toBe(true);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
describe('updatePBR', () => {
|
|
84
|
+
it('should update PBR material properties', () => {
|
|
85
|
+
material.updatePBR({
|
|
86
|
+
metallic: 1,
|
|
87
|
+
roughness: 0.1,
|
|
88
|
+
});
|
|
89
|
+
const pbr = material.getPBRProperties();
|
|
90
|
+
expect(pbr?.metallic).toBe(1);
|
|
91
|
+
expect(pbr?.roughness).toBe(0.1);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('should preserve existing properties when updating', () => {
|
|
95
|
+
material.updatePBR({ metallic: 0.5 });
|
|
96
|
+
const pbr = material.getPBRProperties();
|
|
97
|
+
expect(pbr?.metallic).toBe(0.5);
|
|
98
|
+
expect(pbr?.roughness).toBe(0.5); // Unchanged
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('should create default PBR if not exists', () => {
|
|
102
|
+
const unlit = new MaterialTrait({ type: 'unlit' });
|
|
103
|
+
unlit.updatePBR({ metallic: 0.7 });
|
|
104
|
+
const pbr = unlit.getPBRProperties();
|
|
105
|
+
expect(pbr).toBeDefined();
|
|
106
|
+
expect(pbr?.metallic).toBe(0.7);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('should support emission properties', () => {
|
|
110
|
+
material.updatePBR({
|
|
111
|
+
emission: {
|
|
112
|
+
color: { r: 1, g: 0, b: 0 },
|
|
113
|
+
intensity: 2.0,
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
const pbr = material.getPBRProperties();
|
|
117
|
+
expect(pbr?.emission?.color.r).toBe(1);
|
|
118
|
+
expect(pbr?.emission?.intensity).toBe(2.0);
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
describe('texture management', () => {
|
|
123
|
+
it('should add texture map', () => {
|
|
124
|
+
material.addTexture({
|
|
125
|
+
path: '/textures/diffuse.png',
|
|
126
|
+
channel: 'baseColor',
|
|
127
|
+
});
|
|
128
|
+
const textures = material.getTextures();
|
|
129
|
+
expect(textures).toHaveLength(1);
|
|
130
|
+
expect(textures[0].path).toBe('/textures/diffuse.png');
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('should support multiple textures', () => {
|
|
134
|
+
material.addTexture({ path: '/t1.png', channel: 'baseColor' });
|
|
135
|
+
material.addTexture({ path: '/t2.png', channel: 'normalMap' });
|
|
136
|
+
material.addTexture({ path: '/t3.png', channel: 'roughnessMap' });
|
|
137
|
+
expect(material.getTextures()).toHaveLength(3);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it('should preserve texture configuration', () => {
|
|
141
|
+
material.addTexture({
|
|
142
|
+
path: '/textures/normal.png',
|
|
143
|
+
channel: 'normalMap',
|
|
144
|
+
scale: { x: 2, y: 2 },
|
|
145
|
+
filter: 'anisotropic',
|
|
146
|
+
anisotropy: 16,
|
|
147
|
+
});
|
|
148
|
+
const textures = material.getTextures();
|
|
149
|
+
expect(textures[0].scale).toEqual({ x: 2, y: 2 });
|
|
150
|
+
expect(textures[0].filter).toBe('anisotropic');
|
|
151
|
+
expect(textures[0].anisotropy).toBe(16);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('should clear texture cache', () => {
|
|
155
|
+
material.addTexture({ path: '/t1.png', channel: 'baseColor' });
|
|
156
|
+
material.clearTextureCache();
|
|
157
|
+
// Verify no error and textures still accessible
|
|
158
|
+
expect(material.getTextures()).toHaveLength(1);
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
describe('custom shaders', () => {
|
|
163
|
+
it('should set custom shader code', () => {
|
|
164
|
+
const shader = {
|
|
165
|
+
fragment: 'void main() { gl_FragColor = vec4(1.0); }',
|
|
166
|
+
shaderLanguage: 'glsl' as const,
|
|
167
|
+
};
|
|
168
|
+
material.setCustomShader(shader);
|
|
169
|
+
const result = material.getCustomShader();
|
|
170
|
+
expect(result?.fragment).toBe(shader.fragment);
|
|
171
|
+
expect(result?.shaderLanguage).toBe('glsl');
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it('should support vertex and fragment shaders', () => {
|
|
175
|
+
const shader = {
|
|
176
|
+
vertex: 'void main() { gl_Position = vec4(1.0); }',
|
|
177
|
+
fragment: 'void main() { gl_FragColor = vec4(1.0); }',
|
|
178
|
+
shaderLanguage: 'glsl' as const,
|
|
179
|
+
};
|
|
180
|
+
material.setCustomShader(shader);
|
|
181
|
+
const result = material.getCustomShader();
|
|
182
|
+
expect(result?.vertex).toBeDefined();
|
|
183
|
+
expect(result?.fragment).toBeDefined();
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
describe('optimization hints', () => {
|
|
188
|
+
it('should enable texture streaming', () => {
|
|
189
|
+
material.setTextureStreaming(true);
|
|
190
|
+
const result = material.getMaterial();
|
|
191
|
+
expect(result.optimization?.streamTextures).toBe(true);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it('should set texture compression', () => {
|
|
195
|
+
material.setCompression('astc');
|
|
196
|
+
const result = material.getMaterial();
|
|
197
|
+
expect(result.optimization?.compression).toBe('astc');
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('should enable material instancing', () => {
|
|
201
|
+
material.setInstanced(true);
|
|
202
|
+
const result = material.getMaterial();
|
|
203
|
+
expect(result.optimization?.instanced).toBe(true);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('should get optimization configuration', () => {
|
|
207
|
+
material.setTextureStreaming(true);
|
|
208
|
+
material.setCompression('dxt');
|
|
209
|
+
const opt = material.getOptimization();
|
|
210
|
+
expect(opt?.streamTextures).toBe(true);
|
|
211
|
+
expect(opt?.compression).toBe('dxt');
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
describe('material presets', () => {
|
|
216
|
+
it('should create chrome material', () => {
|
|
217
|
+
const chrome = MATERIAL_PRESETS.chrome();
|
|
218
|
+
expect(chrome.pbr?.metallic).toBe(1.0);
|
|
219
|
+
expect(chrome.pbr?.roughness).toBe(0.1);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it('should create plastic material', () => {
|
|
223
|
+
const plastic = MATERIAL_PRESETS.plastic();
|
|
224
|
+
expect(plastic.pbr?.metallic).toBe(0);
|
|
225
|
+
expect(plastic.pbr?.roughness).toBe(0.8);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it('should create wood material', () => {
|
|
229
|
+
const wood = MATERIAL_PRESETS.wood();
|
|
230
|
+
expect(wood.pbr?.baseColor.r).toBe(0.6);
|
|
231
|
+
expect(wood.pbr?.roughness).toBe(0.4);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it('should create glass material', () => {
|
|
235
|
+
const glass = MATERIAL_PRESETS.glass();
|
|
236
|
+
expect(glass.type).toBe('transparent');
|
|
237
|
+
expect(glass.blendMode).toBe('blend');
|
|
238
|
+
expect(glass.pbr?.transmission).toBe(0.9);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it('should create emissive material', () => {
|
|
242
|
+
const emissive = MATERIAL_PRESETS.emissive();
|
|
243
|
+
expect(emissive.pbr?.emission).toBeDefined();
|
|
244
|
+
expect(emissive.pbr?.emission?.intensity).toBe(2.0);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it('should create skin material', () => {
|
|
248
|
+
const skin = MATERIAL_PRESETS.skin();
|
|
249
|
+
expect(skin.pbr?.roughness).toBe(0.5);
|
|
250
|
+
expect(skin.pbr?.ambientOcclusion).toBe(0.8);
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
describe('factory function', () => {
|
|
255
|
+
it('should create material via factory', () => {
|
|
256
|
+
const config: MaterialConfig = {
|
|
257
|
+
type: 'pbr',
|
|
258
|
+
pbr: {
|
|
259
|
+
baseColor: { r: 0.8, g: 0.8, b: 0.8 },
|
|
260
|
+
metallic: 0.5,
|
|
261
|
+
roughness: 0.5,
|
|
262
|
+
},
|
|
263
|
+
};
|
|
264
|
+
const mat = createMaterialTrait(config);
|
|
265
|
+
expect(mat.getMaterial().type).toBe('pbr');
|
|
266
|
+
});
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
describe('disposal', () => {
|
|
270
|
+
it('should dispose and cleanup', () => {
|
|
271
|
+
material.addTexture({ path: '/t.png', channel: 'baseColor' });
|
|
272
|
+
material.dispose();
|
|
273
|
+
// Verify no error after disposal
|
|
274
|
+
expect(() => material.clearTextureCache()).not.toThrow();
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
describe('material copying', () => {
|
|
279
|
+
it('should create independent material copy', () => {
|
|
280
|
+
const original = material.getMaterial();
|
|
281
|
+
const copy = new MaterialTrait(original);
|
|
282
|
+
|
|
283
|
+
copy.updatePBR({ metallic: 0.9 });
|
|
284
|
+
const originalAfter = material.getMaterial();
|
|
285
|
+
|
|
286
|
+
expect(originalAfter.pbr?.metallic).toBe(0); // Unchanged
|
|
287
|
+
expect(copy.getPBRProperties()?.metallic).toBe(0.9);
|
|
288
|
+
});
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
describe('complex material setup', () => {
|
|
292
|
+
it('should configure full PBR material with all properties', () => {
|
|
293
|
+
const config: MaterialConfig = {
|
|
294
|
+
type: 'pbr',
|
|
295
|
+
name: 'realistic-paint',
|
|
296
|
+
pbr: {
|
|
297
|
+
baseColor: { r: 0.8, g: 0.2, b: 0.2, a: 1 },
|
|
298
|
+
metallic: 0.1,
|
|
299
|
+
roughness: 0.6,
|
|
300
|
+
ambientOcclusion: 0.9,
|
|
301
|
+
emission: { color: { r: 0, g: 0, b: 0 }, intensity: 0 },
|
|
302
|
+
normalStrength: 1.0,
|
|
303
|
+
parallaxHeight: 0.02,
|
|
304
|
+
ior: 1.5,
|
|
305
|
+
},
|
|
306
|
+
textures: [
|
|
307
|
+
{ path: '/diffuse.png', channel: 'baseColor' },
|
|
308
|
+
{ path: '/normal.png', channel: 'normalMap', normalStrength: 1.5 },
|
|
309
|
+
{ path: '/roughness.png', channel: 'roughnessMap' },
|
|
310
|
+
],
|
|
311
|
+
doubleSided: false,
|
|
312
|
+
blendMode: 'opaque',
|
|
313
|
+
optimization: {
|
|
314
|
+
compression: 'dxt',
|
|
315
|
+
instanced: true,
|
|
316
|
+
lodBias: 0.5,
|
|
317
|
+
},
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
const mat = new MaterialTrait(config);
|
|
321
|
+
const result = mat.getMaterial();
|
|
322
|
+
|
|
323
|
+
expect(result.name).toBe('realistic-paint');
|
|
324
|
+
expect(result.pbr?.baseColor.r).toBe(0.8);
|
|
325
|
+
expect(result.textures).toHaveLength(3);
|
|
326
|
+
expect(result.optimization?.compression).toBe('dxt');
|
|
327
|
+
});
|
|
328
|
+
});
|
|
329
|
+
});
|