-
Notifications
You must be signed in to change notification settings - Fork 2
/
Material.ts
692 lines (601 loc) · 25.3 KB
/
Material.ts
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
// Copyright 2022-2024, University of Colorado Boulder
/**
* Represents different materials that solids/liquids in the simulations can take, including density/viscosity/color.
*
* @author Jonathan Olson <[email protected]>
*/
import DynamicProperty from '../../../../axon/js/DynamicProperty.js';
import TProperty from '../../../../axon/js/TProperty.js';
import Property from '../../../../axon/js/Property.js';
import Utils from '../../../../dot/js/Utils.js';
import ThreeUtils from '../../../../mobius/js/ThreeUtils.js';
import optionize, { combineOptions } from '../../../../phet-core/js/optionize.js';
import { Color, ColorProperty, ColorState } from '../../../../scenery/js/imports.js';
import BooleanIO from '../../../../tandem/js/types/BooleanIO.js';
import IOType from '../../../../tandem/js/types/IOType.js';
import NullableIO from '../../../../tandem/js/types/NullableIO.js';
import NumberIO from '../../../../tandem/js/types/NumberIO.js';
import ReferenceIO, { ReferenceIOState } from '../../../../tandem/js/types/ReferenceIO.js';
import StringIO from '../../../../tandem/js/types/StringIO.js';
import densityBuoyancyCommon from '../../densityBuoyancyCommon.js';
import DensityBuoyancyCommonStrings from '../../DensityBuoyancyCommonStrings.js';
import DensityBuoyancyCommonColors from '../view/DensityBuoyancyCommonColors.js';
import TReadOnlyProperty from '../../../../axon/js/TReadOnlyProperty.js';
import TinyProperty from '../../../../axon/js/TinyProperty.js';
import ReadOnlyProperty from '../../../../axon/js/ReadOnlyProperty.js';
import MappedProperty from '../../../../axon/js/MappedProperty.js';
import Range from '../../../../dot/js/Range.js';
const NullableColorPropertyReferenceType = NullableIO( ReferenceIO( Property.PropertyIO( Color.ColorIO ) ) );
type MaterialState = {
identifier: null | keyof typeof Material;
name: ReferenceIOState;
tandemName: string | null;
density: number;
viscosity: number;
custom: boolean;
hidden: boolean;
staticCustomColor: null | ColorState;
customColor: null | ColorState;
staticLiquidColor: null | ColorState;
liquidColor: null | ColorState;
};
export const CUSTOM_MATERIAL_NAME = 'CUSTOM';
export type CustomMaterialName = typeof CUSTOM_MATERIAL_NAME;
export type MaterialName = keyof ( typeof Material ) | CustomMaterialName;
type DensityRangeOption = { densityRange?: Range };
export type MaterialOptions = {
nameProperty?: TReadOnlyProperty<string>;
// If set, this material will be available at Material[ identifier ] as a global
identifier?: MaterialName | null;
// Used for tandems
tandemName?: string | null;
// in SI (kg/m^3)
density?: number;
// in SI (Pa * s). For reference a poise is 1e-2 Pa*s, and a centipoise is 1e-3 Pa*s.
viscosity?: number;
custom?: boolean;
// If true, don't show the density in number pickers/readouts
hidden?: boolean;
// Uses the color for a solid material's color
customColor?: Property<Color> | null;
// Uses the alpha channel for opacity
liquidColor?: Property<Color> | null;
// Used for the color of depth lines added on top of the Material
depthLinesColor?: TReadOnlyProperty<Color> | null;
};
export default class Material {
public readonly nameProperty: TReadOnlyProperty<string>;
public readonly identifier: MaterialName | null;
public readonly tandemName: string | null;
public readonly density: number; // in SI (kg/m^3)
public readonly viscosity: number;
public readonly custom: boolean;
public readonly hidden: boolean;
public readonly customColor: Property<Color> | null;
public readonly liquidColor: Property<Color> | null;
public readonly depthLinesColor: TReadOnlyProperty<Color> | null;
public constructor( providedOptions: MaterialOptions ) {
const options = optionize<MaterialOptions, MaterialOptions>()( {
nameProperty: new TinyProperty( 'unknown' ),
identifier: null,
tandemName: null,
density: 1,
viscosity: 1e-3,
custom: false,
hidden: false,
customColor: null,
liquidColor: null,
depthLinesColor: DensityBuoyancyCommonColors.depthLinesDarkColorProperty
}, providedOptions );
assert && assert( isFinite( options.density ), 'density should be finite, but it was: ' + options.density );
this.nameProperty = options.nameProperty;
this.identifier = options.identifier;
this.tandemName = options.tandemName;
this.density = options.density;
this.viscosity = options.viscosity;
this.custom = options.custom;
this.hidden = options.hidden;
this.customColor = options.customColor;
this.liquidColor = options.liquidColor;
this.depthLinesColor = options.depthLinesColor;
}
/**
* Returns a custom material that can be modified at will.
*/
public static createCustomMaterial( options: MaterialOptions ): Material {
return new Material( combineOptions<MaterialOptions>( {
nameProperty: DensityBuoyancyCommonStrings.material.customStringProperty,
tandemName: 'custom',
custom: true
}, options ) );
}
/**
* Returns a custom material that can be modified at will, but with a liquid color specified.
*/
public static createCustomLiquidMaterial( options: MaterialOptions & Required<Pick<MaterialOptions, 'density'> & Required<DensityRangeOption>> ): Material {
return Material.createCustomMaterial( combineOptions<MaterialOptions>( {
liquidColor: Material.getCustomLiquidColor( options.density, options.densityRange )
}, options ) );
}
/**
* Returns a custom material that can be modified at will, but with a solid color specified
*/
public static createCustomSolidMaterial( options: MaterialOptions & Required<Pick<MaterialOptions, 'density'>> & DensityRangeOption ): Material {
assert && assert( options.hasOwnProperty( 'customColor' ) || options.hasOwnProperty( 'densityRange' ), 'we need a way to have a material color' );
const solidColorProperty = options.customColor || Material.getCustomSolidColor( options.density, options.densityRange! );
const depthLinesColorProperty = new MappedProperty( solidColorProperty, {
map: solidColor => {
// The lighter depth line color has better contrast, so use that for more than half
const isDark = ( solidColor.r + solidColor.g + solidColor.b ) / 3 < 255 * 0.6;
return isDark ? DensityBuoyancyCommonColors.depthLinesLightColorProperty.value : DensityBuoyancyCommonColors.depthLinesDarkColorProperty.value;
}
} );
return Material.createCustomMaterial( combineOptions<MaterialOptions>( {
customColor: solidColorProperty,
// Also provide as a liquid color because some solid colors use Material.linkLiquidColor() (like the Bottle)
liquidColor: solidColorProperty,
depthLinesColor: depthLinesColorProperty
}, options ) );
}
/**
* Returns a value suitable for use in colors (0-255 value) that should be used as a grayscale value for
* a material of a given density. The mappíng is inverted, i.e. larger densities yield darker colors.
*/
private static getCustomLightness( density: number, densityRange: Range ): number {
return Utils.roundSymmetric( this.getNormalizedLightness( density, densityRange ) * 255 );
}
/**
* Returns a lightness factor from 0-1 that can be used to map a density to a desired color.
*/
public static getNormalizedLightness( density: number, densityRange: Range ): number {
const scaleFactor = 1000;
const scaleMax = Utils.log10( densityRange.max / scaleFactor ); // 1 for the default
const scaleMin = Utils.log10( densityRange.min / scaleFactor ); // -2 for the default
const scaleValue = Utils.log10( density / scaleFactor );
return Utils.clamp( Utils.linear( scaleMax, scaleMin, 0, 1, scaleValue ), 0, 1 );
}
/**
* Similar to getCustomLightness, but returns the generated color, with an included alpha effect.
*/
public static getCustomLiquidColor( density: number, densityRange: Range ): ColorProperty {
const lightnessFactor = Material.getNormalizedLightness( density, densityRange );
return new ColorProperty(
Color.interpolateRGBA(
DensityBuoyancyCommonColors.customLiquidDarkColorProperty.value,
DensityBuoyancyCommonColors.customLiquidLightColorProperty.value,
lightnessFactor
) );
}
/**
* Similar to getCustomLightness, but returns the generated color
*/
private static getCustomSolidColor( density: number, densityRange: Range ): ColorProperty {
const lightness = Material.getCustomLightness( density, densityRange );
return new ColorProperty( new Color( lightness, lightness, lightness ) );
}
/**
* Keep a material's color and opacity to match the liquid color from a given Property<Material>
*
* NOTE: Only call this for things that exist for the lifetime of this simulation (otherwise it would leak memory)
*/
public static linkLiquidColor( property: TProperty<Material>, threeMaterial: THREE.MeshPhongMaterial | THREE.MeshLambertMaterial | THREE.MeshBasicMaterial ): void {
new DynamicProperty<Color, Color, Material>( property, {
derive: material => {
assert && assert( material.liquidColor );
return material.liquidColor!;
}
} ).link( ( color: Color ) => {
threeMaterial.color = ThreeUtils.colorToThree( color );
threeMaterial.opacity = color.alpha;
} );
}
////////////////// SOLIDS //////////////////
public static readonly ALUMINUM = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.aluminumStringProperty,
tandemName: 'aluminum',
identifier: 'ALUMINUM',
density: 2700
} );
public static readonly APPLE = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.appleStringProperty,
tandemName: 'apple',
identifier: 'APPLE',
// "Some Physical Properties of Apple" - Averaged the two cultivars' densities for this
// http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.548.1131&rep=rep1&type=pdf
density: 832
} );
// In essence identical to aluminum, but with a different name for the Density readout
public static readonly BOAT_BODY = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.boatBodyStringProperty,
tandemName: 'boatBody',
identifier: 'BOAT_BODY',
density: Material.ALUMINUM.density
} );
public static readonly BRICK = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.brickStringProperty,
tandemName: 'brick',
identifier: 'BRICK',
density: 2000,
depthLinesColor: DensityBuoyancyCommonColors.depthLinesLightColorProperty
} );
public static readonly CONCRETE = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.concreteStringProperty,
tandemName: 'concrete',
identifier: 'CONCRETE',
density: 3150,
liquidColor: DensityBuoyancyCommonColors.materialConcreteColorProperty
} );
public static readonly COPPER = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.copperStringProperty,
tandemName: 'copper',
identifier: 'COPPER',
density: 8960,
liquidColor: DensityBuoyancyCommonColors.materialCopperColorProperty
} );
public static readonly DIAMOND = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.diamondStringProperty,
tandemName: 'diamond',
identifier: 'DIAMOND',
density: 3510
} );
public static readonly GLASS = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.glassStringProperty,
tandemName: 'glass',
identifier: 'GLASS',
density: 2700
} );
public static readonly GOLD = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.goldStringProperty,
tandemName: 'gold',
identifier: 'GOLD',
density: 19320
} );
public static readonly HUMAN = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.humanStringProperty,
tandemName: 'human',
identifier: 'HUMAN',
density: 950
} );
public static readonly ICE = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.iceStringProperty,
tandemName: 'ice',
identifier: 'ICE',
density: 919
} );
public static readonly LEAD = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.leadStringProperty,
tandemName: 'lead',
identifier: 'LEAD',
density: 11342,
liquidColor: DensityBuoyancyCommonColors.materialLeadColorProperty
} );
public static readonly PLATINUM = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.platinumStringProperty,
tandemName: 'platinum',
identifier: 'PLATINUM',
density: 21450
} );
public static readonly PVC = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.pvcStringProperty,
tandemName: 'pvc',
identifier: 'PVC',
density: 1440
} );
public static readonly PYRITE = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.pyriteStringProperty,
tandemName: 'pyrite',
identifier: 'PYRITE',
density: 5010
} );
public static readonly SILVER = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.silverStringProperty,
tandemName: 'silver',
identifier: 'SILVER',
density: 10490
} );
public static readonly STEEL = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.steelStringProperty,
tandemName: 'steel',
identifier: 'STEEL',
density: 7800
} );
public static readonly STYROFOAM = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.styrofoamStringProperty,
tandemName: 'styrofoam',
identifier: 'STYROFOAM',
// From Flash version: between 25 and 200 according to http://wiki.answers.com/Q/What_is_the_density_of_styrofoam;
// chose 150, so it isn't too low to show on slider, but not 200, so it's not half of wood
density: 150
} );
public static readonly TANTALUM = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.tantalumStringProperty,
tandemName: 'tantalum',
identifier: 'TANTALUM',
density: 16650
} );
public static readonly TITANIUM = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.titaniumStringProperty,
tandemName: 'titanium',
identifier: 'TITANIUM',
density: 4500
} );
public static readonly WOOD = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.woodStringProperty,
tandemName: 'wood',
identifier: 'WOOD',
density: 400,
depthLinesColor: DensityBuoyancyCommonColors.depthLinesLightColorProperty
} );
////////////////// LIQUIDS //////////////////
public static readonly AIR = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.airStringProperty,
tandemName: 'air',
identifier: 'AIR',
density: 1.2,
viscosity: 0,
liquidColor: DensityBuoyancyCommonColors.materialAirColorProperty
} );
public static readonly DENSITY_A = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.densityAStringProperty,
tandemName: 'densityA',
identifier: 'DENSITY_A',
density: 3100,
liquidColor: DensityBuoyancyCommonColors.materialDensityAColorProperty,
hidden: true
} );
public static readonly DENSITY_B = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.densityBStringProperty,
tandemName: 'densityB',
identifier: 'DENSITY_B',
density: 790,
liquidColor: DensityBuoyancyCommonColors.materialDensityBColorProperty,
hidden: true
} );
public static readonly DENSITY_C = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.densityCStringProperty,
tandemName: 'densityC',
identifier: 'DENSITY_C',
density: 490,
liquidColor: DensityBuoyancyCommonColors.materialDensityCColorProperty,
hidden: true
} );
public static readonly DENSITY_D = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.densityDStringProperty,
tandemName: 'densityD',
identifier: 'DENSITY_D',
density: 2890,
liquidColor: DensityBuoyancyCommonColors.materialDensityDColorProperty,
hidden: true
} );
public static readonly DENSITY_E = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.densityEStringProperty,
tandemName: 'densityE',
identifier: 'DENSITY_E',
density: 1260,
liquidColor: DensityBuoyancyCommonColors.materialDensityEColorProperty,
hidden: true
} );
public static readonly DENSITY_F = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.densityFStringProperty,
tandemName: 'densityF',
identifier: 'DENSITY_F',
density: 6440,
liquidColor: DensityBuoyancyCommonColors.materialDensityFColorProperty,
hidden: true
} );
public static readonly GASOLINE = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.gasolineStringProperty,
tandemName: 'gasoline',
identifier: 'GASOLINE',
density: 680,
viscosity: 6e-4,
liquidColor: DensityBuoyancyCommonColors.materialGasolineColorProperty
} );
public static readonly HONEY = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.honeyStringProperty,
tandemName: 'honey',
identifier: 'HONEY',
density: 1440,
viscosity: 0.03, // NOTE: actual value around 2.5, but we can get away with this for animation
liquidColor: DensityBuoyancyCommonColors.materialHoneyColorProperty
} );
public static readonly MERCURY = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.mercuryStringProperty,
tandemName: 'mercury',
identifier: 'MERCURY',
density: 13593,
viscosity: 1.53e-3,
liquidColor: DensityBuoyancyCommonColors.materialMercuryColorProperty
} );
public static readonly OIL = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.oilStringProperty,
tandemName: 'oil',
identifier: 'OIL',
density: 920,
viscosity: 0.02, // Too much bigger and it won't work, not particularly physical
liquidColor: DensityBuoyancyCommonColors.materialOilColorProperty
} );
public static readonly SAND = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.sandStringProperty,
tandemName: 'sand',
identifier: 'SAND',
density: 1442,
viscosity: 0.03, // Too much bigger and it won't work, not particularly physical
liquidColor: DensityBuoyancyCommonColors.materialSandColorProperty
} );
public static readonly SEAWATER = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.seawaterStringProperty,
tandemName: 'seawater',
identifier: 'SEAWATER',
density: 1029,
viscosity: 1.88e-3,
liquidColor: DensityBuoyancyCommonColors.materialSeawaterColorProperty
} );
public static readonly WATER = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.waterStringProperty,
tandemName: 'water',
identifier: 'WATER',
density: 1000,
viscosity: 8.9e-4,
liquidColor: DensityBuoyancyCommonColors.materialWaterColorProperty
} );
////////////////// MYSTERY MATERIALS //////////////////
public static readonly MATERIAL_O = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.materialOStringProperty,
tandemName: 'materialO',
identifier: 'MATERIAL_O',
hidden: true,
customColor: new Property( new Color( '#f00' ) ),
density: 950 // Same as the Human's average density
} );
public static readonly MATERIAL_P = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.materialPStringProperty,
tandemName: 'materialP',
identifier: 'MATERIAL_P',
hidden: true,
customColor: new Property( new Color( '#0f0' ) ),
density: Material.DIAMOND.density
} );
public static readonly MATERIAL_R = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.materialRStringProperty,
tandemName: 'materialR',
identifier: 'MATERIAL_R',
hidden: true,
liquidColor: DensityBuoyancyCommonColors.materialRColorProperty,
density: Material.ICE.density
} );
public static readonly MATERIAL_S = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.materialSStringProperty,
tandemName: 'materialS',
identifier: 'MATERIAL_S',
hidden: true,
liquidColor: DensityBuoyancyCommonColors.materialSColorProperty,
density: Material.LEAD.density
} );
public static readonly MATERIAL_V = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.materialVStringProperty,
tandemName: 'materialV',
identifier: 'MATERIAL_V',
hidden: true,
customColor: new Property( new Color( '#ff0' ) ),
density: Material.TITANIUM.density
} );
public static readonly MATERIAL_W = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.materialWStringProperty,
tandemName: 'materialW',
identifier: 'MATERIAL_W',
hidden: true,
customColor: new Property( new Color( '#0af' ) ),
density: Material.MERCURY.density
} );
public static readonly MATERIAL_X = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.materialXStringProperty,
tandemName: 'materialX',
identifier: 'MATERIAL_X',
hidden: true,
density: Material.PYRITE.density
} );
public static readonly MATERIAL_Y = new Material( {
nameProperty: DensityBuoyancyCommonStrings.material.materialYStringProperty,
tandemName: 'mysteryY',
identifier: 'MATERIAL_Y',
hidden: true,
density: Material.GOLD.density
} );
private static readonly MATERIALS = [
Material.AIR,
Material.ALUMINUM,
Material.APPLE,
Material.BRICK,
Material.CONCRETE,
Material.COPPER,
Material.DENSITY_E,
Material.DENSITY_F,
Material.DENSITY_A,
Material.DENSITY_B,
Material.DIAMOND,
Material.GASOLINE,
Material.GLASS,
Material.GOLD,
Material.HONEY,
Material.HUMAN,
Material.ICE,
Material.LEAD,
Material.MATERIAL_O,
Material.MATERIAL_P,
Material.MATERIAL_V,
Material.MATERIAL_W,
Material.MATERIAL_X,
Material.MATERIAL_Y,
Material.MERCURY,
Material.OIL,
Material.PLATINUM,
Material.PYRITE,
Material.SAND,
Material.SEAWATER,
Material.SILVER,
Material.STEEL,
Material.STYROFOAM,
Material.TANTALUM,
Material.TITANIUM,
Material.WATER,
Material.WOOD
];
public static readonly MaterialIO = new IOType<Material, MaterialState>( 'MaterialIO', {
valueType: Material,
documentation: 'Represents different materials that solids/liquids in the simulations can take, including density (kg/m^3), viscosity (Pa * s), and color.',
stateSchema: {
name: ReferenceIO( ReadOnlyProperty.PropertyIO( StringIO ) ),
identifier: NullableIO( StringIO ),
tandemName: NullableIO( StringIO ),
density: NumberIO,
viscosity: NumberIO,
custom: BooleanIO,
hidden: BooleanIO,
staticCustomColor: NullableIO( Color.ColorIO ),
customColor: NullableColorPropertyReferenceType,
staticLiquidColor: NullableIO( Color.ColorIO ),
liquidColor: NullableColorPropertyReferenceType
},
toStateObject( material: Material ): MaterialState {
const isCustomColorUninstrumented = material.customColor && !material.customColor.isPhetioInstrumented();
const isLiquidColorUninstrumented = material.liquidColor && !material.liquidColor.isPhetioInstrumented();
return {
name: ReferenceIO( ReadOnlyProperty.PropertyIO( StringIO ) ).toStateObject( material.nameProperty ),
identifier: NullableIO( StringIO ).toStateObject( material.identifier ),
tandemName: NullableIO( StringIO ).toStateObject( material.tandemName ),
density: material.density,
viscosity: material.viscosity,
custom: material.custom,
hidden: material.hidden,
staticCustomColor: NullableIO( Color.ColorIO ).toStateObject( isCustomColorUninstrumented ? material.customColor.value : null ),
customColor: NullableColorPropertyReferenceType.toStateObject( isCustomColorUninstrumented ? null : material.customColor ),
staticLiquidColor: NullableIO( Color.ColorIO ).toStateObject( isLiquidColorUninstrumented ? material.liquidColor.value : null ),
liquidColor: NullableColorPropertyReferenceType.toStateObject( isLiquidColorUninstrumented ? null : material.liquidColor )
};
},
fromStateObject( obj: MaterialState ): Material {
if ( obj.identifier ) {
const material = Material[ obj.identifier ];
assert && assert( material, `Unknown material: ${obj.identifier}` );
return material as Material;
}
else {
const staticCustomColor = NullableIO( Color.ColorIO ).fromStateObject( obj.staticCustomColor );
const staticLiquidColor = NullableIO( Color.ColorIO ).fromStateObject( obj.staticLiquidColor );
return new Material( {
nameProperty: ReferenceIO( ReadOnlyProperty.PropertyIO( StringIO ) ).fromStateObject( obj.name ),
identifier: NullableIO( StringIO ).fromStateObject( obj.identifier ),
tandemName: NullableIO( StringIO ).fromStateObject( obj.tandemName ),
density: obj.density,
viscosity: obj.viscosity,
custom: obj.custom,
hidden: obj.hidden,
customColor: staticCustomColor ? new ColorProperty( staticCustomColor ) : NullableColorPropertyReferenceType.fromStateObject( obj.customColor ),
liquidColor: staticLiquidColor ? new ColorProperty( staticLiquidColor ) : NullableColorPropertyReferenceType.fromStateObject( obj.liquidColor )
} );
}
}
} );
}
densityBuoyancyCommon.register( 'Material', Material );