-
Notifications
You must be signed in to change notification settings - Fork 16
/
material.js
149 lines (121 loc) · 3.37 KB
/
material.js
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
const Signal = require('signals')
const { mat3, mat2x3 } = require('pex-math')
let MaterialID = 0
const tempMat2x3 = mat2x3.create()
const MATERIAL_MAPS = [
'baseColorMap',
'emissiveColorMap',
'matallicMap',
'roughnessMap',
'metallicRoughnessMap',
'normalMap',
'clearCoatNormalMap',
'occlusionMap',
'diffuseMap',
'specularGlossinessMap'
]
const PIPELINE_PROPS = [
'depthTest',
'depthWrite',
'depthFunc',
'blend',
'blendSrcRGBFactor',
'blendSrcAlphaFactor',
'blendDstRGBFactor',
'blendDstAlphaFactor',
'cullFace',
'cullFaceMode'
]
function Material(opts) {
this.type = 'Material'
this.id = 'Material_' + MaterialID++
this.enabled = true
this.changed = new Signal()
this._uniforms = {}
const ctx = opts.ctx
this.baseColor = [1, 1, 1, 1]
this.baseColorMap = null
this.useSpecularGlossinessWorkflow =
opts.useSpecularGlossinessWorkflow || false
this.unlit = opts.unlit || false
if (opts.useSpecularGlossinessWorkflow) {
// Specular Glossiness workflow
this.diffuse = [1, 1, 1, 1]
this.diffuseMap = null
this.specular = [1, 1, 1]
this.glossiness = 1
this.specularGlossinessMap = null
} else if (!this.unlit) {
// Metallic Roughness workflow
this.metallic = 1
this.matallicMap = null
this.roughness = 1
this.roughnessMap = null
this.metallicRoughnessMap = null
}
this.normalMap = null
this.normalScale = 1
this.displacementMap = null
this.displacement = 0
this.emissiveColor = null
this.emissiveIntensity = 1
this.emissiveColorMap = null
this.occlusionMap = null
this.reflectance = 0.5
this.clearCoat = null
this.clearCoatRoughness = null
this.clearCoatNormalMap = null
this.clearCoatNormalMapScale = 1 //TODO: what's clearCoatNormalMapScale
this.alphaMap = null
this.alphaTest = undefined
// pipeline props
this.depthTest = true
this.depthWrite = true
this.depthFunc = ctx.DepthFunc.LessEqual
this.blend = false
this.blendSrcRGBFactor = ctx.BlendFactor.One
this.blendSrcAlphaFactor = ctx.BlendFactor.One
this.blendDstRGBFactor = ctx.BlendFactor.One
this.blendDstAlphaFactor = ctx.BlendFactor.One
this.cullFace = true
this.cullFaceMode = ctx.Face.Back
this.pointSize = 1
this.castShadows = false
this.receiveShadows = false
this.needsPipelineUpdate = false
this.set(opts)
}
Material.prototype.init = function(entity) {
this.entity = entity
}
Material.prototype.set = function(opts) {
Object.assign(this, opts)
const optsKeys = Object.keys(opts)
const mapKeys = optsKeys.filter((opt) => MATERIAL_MAPS.includes(opt))
if (mapKeys.length) {
for (let i = 0; i < mapKeys.length; i++) {
const map = this[mapKeys[i]]
if (map && map.texture) {
mat2x3.identity(tempMat2x3)
mat2x3.translate(tempMat2x3, map.offset || [0, 0])
mat2x3.rotate(tempMat2x3, -map.rotation || 0)
mat2x3.scale(tempMat2x3, map.scale || [1, 1])
map.texCoordTransformMatrix = mat3.fromMat2x3(
map.texCoordTransformMatrix
? mat3.identity(map.texCoordTransformMatrix)
: mat3.create(),
tempMat2x3
)
}
}
}
for (let pipelineProp of PIPELINE_PROPS) {
if (opts[pipelineProp] !== undefined) {
this.needsPipelineUpdate = true
}
}
optsKeys.forEach((prop) => this.changed.dispatch(prop))
}
module.exports = function(opts) {
return new Material(opts)
}