-
-
Notifications
You must be signed in to change notification settings - Fork 149
/
materials-sheen.ts
149 lines (129 loc) · 4.77 KB
/
materials-sheen.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
import { Extension, GLTF, PropertyType, ReaderContext, WriterContext, vec3 } from '@gltf-transform/core';
import { KHR_MATERIALS_SHEEN } from '../constants.js';
import { Sheen } from './sheen.js';
const NAME = KHR_MATERIALS_SHEEN;
interface SheenDef {
sheenColorFactor?: vec3;
sheenRoughnessFactor?: number;
sheenColorTexture?: GLTF.ITextureInfo;
sheenRoughnessTexture?: GLTF.ITextureInfo;
}
/**
* [`KHR_materials_sheen`](https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_sheen/)
* defines a velvet-like sheen layered on a glTF PBR material.
*
* ![Illustration](/media/extensions/khr-materials-sheen.png)
*
* > _**Figure:** A cushion, showing high material roughness and low sheen roughness. Soft
* > highlights at edges of the material show backscattering from microfibers. Source: Khronos
* > Group._
*
* A sheen layer is a common technique used in Physically-Based Rendering to represent
* cloth and fabric materials.
*
* Properties:
* - {@link Sheen}
*
* ### Example
*
* The `KHRMaterialsSheen` class provides a single {@link ExtensionProperty} type, `Sheen`,
* which may be attached to any {@link Material} instance. For example:
*
* ```typescript
* import { KHRMaterialsSheen, Sheen } from '@gltf-transform/extensions';
*
* // Create an Extension attached to the Document.
* const sheenExtension = document.createExtension(KHRMaterialsSheen);
*
* // Create a Sheen property.
* const sheen = sheenExtension.createSheen()
* .setSheenColorFactor([1.0, 1.0, 1.0]);
*
* // Attach the property to a Material.
* material.setExtension('KHR_materials_sheen', sheen);
* ```
*/
export class KHRMaterialsSheen extends Extension {
public static readonly EXTENSION_NAME = NAME;
public readonly extensionName = NAME;
public readonly prereadTypes = [PropertyType.MESH];
public readonly prewriteTypes = [PropertyType.MESH];
/** Creates a new Sheen property for use on a {@link Material}. */
public createSheen(): Sheen {
return new Sheen(this.document.getGraph());
}
/** @hidden */
public read(_context: ReaderContext): this {
return this;
}
/** @hidden */
public write(_context: WriterContext): this {
return this;
}
/** @hidden */
public preread(context: ReaderContext): this {
const jsonDoc = context.jsonDoc;
const materialDefs = jsonDoc.json.materials || [];
const textureDefs = jsonDoc.json.textures || [];
materialDefs.forEach((materialDef, materialIndex) => {
if (materialDef.extensions && materialDef.extensions[NAME]) {
const sheen = this.createSheen();
context.materials[materialIndex].setExtension(NAME, sheen);
const sheenDef = materialDef.extensions[NAME] as SheenDef;
// Factors.
if (sheenDef.sheenColorFactor !== undefined) {
sheen.setSheenColorFactor(sheenDef.sheenColorFactor);
}
if (sheenDef.sheenRoughnessFactor !== undefined) {
sheen.setSheenRoughnessFactor(sheenDef.sheenRoughnessFactor);
}
// Textures.
if (sheenDef.sheenColorTexture !== undefined) {
const textureInfoDef = sheenDef.sheenColorTexture;
const texture = context.textures[textureDefs[textureInfoDef.index].source!];
sheen.setSheenColorTexture(texture);
context.setTextureInfo(sheen.getSheenColorTextureInfo()!, textureInfoDef);
}
if (sheenDef.sheenRoughnessTexture !== undefined) {
const textureInfoDef = sheenDef.sheenRoughnessTexture;
const texture = context.textures[textureDefs[textureInfoDef.index].source!];
sheen.setSheenRoughnessTexture(texture);
context.setTextureInfo(sheen.getSheenRoughnessTextureInfo()!, textureInfoDef);
}
}
});
return this;
}
/** @hidden */
public prewrite(context: WriterContext): this {
const jsonDoc = context.jsonDoc;
this.document
.getRoot()
.listMaterials()
.forEach((material) => {
const sheen = material.getExtension<Sheen>(NAME);
if (sheen) {
const materialIndex = context.materialIndexMap.get(material)!;
const materialDef = jsonDoc.json.materials![materialIndex];
materialDef.extensions = materialDef.extensions || {};
// Factors.
const sheenDef = (materialDef.extensions[NAME] = {
sheenColorFactor: sheen.getSheenColorFactor(),
sheenRoughnessFactor: sheen.getSheenRoughnessFactor(),
} as SheenDef);
// Textures.
if (sheen.getSheenColorTexture()) {
const texture = sheen.getSheenColorTexture()!;
const textureInfo = sheen.getSheenColorTextureInfo()!;
sheenDef.sheenColorTexture = context.createTextureInfoDef(texture, textureInfo);
}
if (sheen.getSheenRoughnessTexture()) {
const texture = sheen.getSheenRoughnessTexture()!;
const textureInfo = sheen.getSheenRoughnessTextureInfo()!;
sheenDef.sheenRoughnessTexture = context.createTextureInfoDef(texture, textureInfo);
}
}
});
return this;
}
}