-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMaterialModifier.js
214 lines (143 loc) · 5.44 KB
/
MaterialModifier.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
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
import {
MeshBasicMaterial,
MeshLambertMaterial,
MeshPhongMaterial,
MeshStandardMaterial,
MeshDepthMaterial,
ShaderLib
} from 'three';
import defaultHooks from './defaultHooks';
const modifySource = ( source, hookDefs, hooks )=>{
let match;
for( let key in hookDefs ){
if( hooks[key] ){
match = /insert(before):(.*)|insert(after):(.*)/.exec( hookDefs[key] );
if( match ){
if( match[1] ){ // before
source = source.replace( match[2], hooks[key] + '\n' + match[2] );
}else
if( match[3] ){ // after
source = source.replace( match[4], match[4] + '\n' + hooks[key] );
}
}
}
}
return source;
}
const cloneUniforms = ( uniforms )=>{
let clone = {};
for( let key in uniforms ){ // non-promitive uniform values will be referenced
clone[ key ] = Object.assign( {}, uniforms[key] );
}
return clone;
}
let shaderMap = null;
const getShaderDef = ( classOrString )=>{
if( !shaderMap ){
let classes = {
standard: MeshStandardMaterial,
basic: MeshBasicMaterial,
lambert: MeshLambertMaterial,
phong: MeshPhongMaterial,
depth: MeshDepthMaterial
}
shaderMap = {};
for( let key in classes ){
shaderMap[ key ] = {
ShaderClass: classes[ key ],
ShaderLib: ShaderLib[ key ],
Key: key,
Count: 0,
ModifiedName: function(){
return `ModifiedMesh${ this.Key[0].toUpperCase() + this.Key.slice(1) }Material_${ ++this.Count }`;
},
TypeCheck: `isMesh${ key[0].toUpperCase() + key.slice(1) }Material`
}
}
}
let shaderDef;
if( typeof classOrString === 'function' ){
for( let key in shaderMap ){
if( shaderMap[ key ].ShaderClass === classOrString ){
shaderDef = shaderMap[ key ];
break;
}
}
}else{
shaderDef = shaderMap[ classOrString ];
}
if( !shaderDef ){
throw new Error( 'No Shader found to modify...' );
}
return shaderDef;
}
/**
* The main Material Modofier
*/
class MaterialModifier{
constructor( vertexHookDefs, fragmentHookDefs ){
this._vertexHooks = {};
this._fragmentHooks = {};
if( vertexHookDefs ){
this.defineVertexHooks( vertexHookDefs );
}
if( fragmentHookDefs ){
this.defineFragmentHooks( fragmentHookDefs );
}
}
modify( shader, opts ){
let def = getShaderDef( shader );
let vertexShader = modifySource( def.ShaderLib.vertexShader, this._vertexHooks, opts.vertexShader || {} );
let fragmentShader = modifySource( def.ShaderLib.fragmentShader, this._fragmentHooks, opts.fragmentShader || {} );
let uniforms = Object.assign( {}, def.ShaderLib.uniforms, opts.uniforms || {} );
return { vertexShader,fragmentShader,uniforms };
}
extend( shader, opts ){
let def = getShaderDef( shader ); // ADJUST THIS SHADER DEF - ONLY DEFINE ONCE - AND STORE A USE COUNT ON EXTENDED VERSIONS.
let vertexShader = modifySource( def.ShaderLib.vertexShader, this._vertexHooks, opts.vertexShader || {} );
let fragmentShader = modifySource( def.ShaderLib.fragmentShader, this._fragmentHooks, opts.fragmentShader || {} );
let uniforms = Object.assign( {}, def.ShaderLib.uniforms, opts.uniforms || {} );
let ClassName = opts.className || def.ModifiedName();
let extendMaterial = new Function( 'BaseClass', 'uniforms', 'vertexShader', 'fragmentShader', 'cloneUniforms',`
var cls = function ${ClassName}( params ){
BaseClass.call( this, params );
this.uniforms = cloneUniforms( uniforms );
this.vertexShader = vertexShader;
this.fragmentShader = fragmentShader;
this.type = '${ClassName}';
this.setValues( params );
}
cls.prototype = Object.create( BaseClass.prototype );
cls.prototype.constructor = cls;
cls.prototype.${ def.TypeCheck } = true;
cls.prototype.copy = function( source ){
BaseClass.prototype.copy.call( this, source );
this.uniforms = Object.assign( {}, source.uniforms );
this.vertexShader = vertexShader;
this.fragmentShader = fragmentShader;
this.type = '${ClassName}';
return this;
}
return cls;
`);
if( opts.postModifyVertexShader ){
vertexShader = opts.postModifyVertexShader( vertexShader );
}
if( opts.postModifyFragmentShader ){
fragmentShader = opts.postModifyFragmentShader( fragmentShader );
}
return extendMaterial( def.ShaderClass, uniforms, vertexShader, fragmentShader, cloneUniforms );
}
defineVertexHooks( defs ){
for( let key in defs ){
this._vertexHooks[ key ] = defs[key];
}
}
defineFragmentHooks( defs ){
for( let key in defs ){
this._fragmentHooks[ key ] = defs[key];
}
}
}
export { MaterialModifier }
export default new MaterialModifier( defaultHooks.vertexHooks, defaultHooks.fragmentHooks );