Skip to content

Commit 90dab37

Browse files
committed
Remove attributes array and cleanup Shader
1 parent 7663c2f commit 90dab37

File tree

2 files changed

+17
-51
lines changed

2 files changed

+17
-51
lines changed

src/core/shader.js

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,29 +50,27 @@ Shader.prototype = {
5050
* Called during shader initialization and is only run once.
5151
*/
5252
init: function (data) {
53-
this.attributes = this.initVariables(data, 'attribute');
54-
this.uniforms = this.initVariables(data, 'uniform');
53+
this.uniforms = this.initUniforms();
5554
this.material = new (this.raw ? THREE.RawShaderMaterial : THREE.ShaderMaterial)({
56-
// attributes: this.attributes,
5755
uniforms: this.uniforms,
5856
vertexShader: this.vertexShader,
5957
fragmentShader: this.fragmentShader
6058
});
6159
return this.material;
6260
},
6361

64-
initVariables: function (data, type) {
62+
initUniforms: function () {
6563
var key;
6664
var schema = this.schema;
6765
var variables = {};
6866
var varType;
6967

7068
for (key in schema) {
71-
if (schema[key].is !== type) { continue; }
69+
if (schema[key].is !== 'uniform') { continue; }
7270
varType = propertyToThreeMapping[schema[key].type];
7371
variables[key] = {
7472
type: varType,
75-
value: undefined // Let updateVariables handle setting these.
73+
value: undefined // Let update handle setting these.
7674
};
7775
}
7876
return variables;
@@ -85,36 +83,30 @@ Shader.prototype = {
8583
* @param {object} data - New material data.
8684
*/
8785
update: function (data) {
88-
this.updateVariables(data, 'attribute');
89-
this.updateVariables(data, 'uniform');
90-
},
91-
92-
updateVariables: function (data, type) {
9386
var key;
9487
var materialKey;
9588
var schema = this.schema;
96-
var variables;
89+
var uniforms = this.uniforms;
9790

98-
variables = type === 'uniform' ? this.uniforms : this.attributes;
9991
for (key in data) {
100-
if (!schema[key] || schema[key].is !== type) { continue; }
92+
if (!schema[key] || schema[key].is !== 'uniform') { continue; }
10193

10294
if (schema[key].type === 'map') {
10395
// If data unchanged, get out early.
104-
if (!variables[key] || variables[key].value === data[key]) { continue; }
96+
if (!uniforms[key] || uniforms[key].value === data[key]) { continue; }
10597

10698
// Special handling is needed for textures.
10799
materialKey = '_texture_' + key;
108100

109101
// We can't actually set the variable correctly until we've loaded the texture.
110-
this.setMapOnTextureLoad(variables, key, materialKey);
102+
this.setMapOnTextureLoad(uniforms, key, materialKey);
111103

112104
// Kick off the texture update now that handler is added.
113105
utils.material.updateMapMaterialFromData(materialKey, key, this, data);
114106
continue;
115107
}
116-
variables[key].value = this.parseValue(schema[key].type, data[key]);
117-
variables[key].needsUpdate = true;
108+
uniforms[key].value = this.parseValue(schema[key].type, data[key]);
109+
uniforms[key].needsUpdate = true;
118110
}
119111
},
120112

@@ -143,11 +135,11 @@ Shader.prototype = {
143135
}
144136
},
145137

146-
setMapOnTextureLoad: function (variables, key, materialKey) {
138+
setMapOnTextureLoad: function (uniforms, key, materialKey) {
147139
var self = this;
148140
this.el.addEventListener('materialtextureloaded', function () {
149-
variables[key].value = self.material[materialKey];
150-
variables[key].needsUpdate = true;
141+
uniforms[key].value = self.material[materialKey];
142+
uniforms[key].needsUpdate = true;
151143
});
152144
}
153145
};
@@ -172,7 +164,7 @@ module.exports.registerShader = function (name, definition) {
172164
});
173165

174166
if (shaders[name]) {
175-
throw new Error('The shader ' + name + ' has been already registered');
167+
throw new Error('The shader ' + name + ' has already been registered');
176168
}
177169
NewShader = function () { Shader.call(this); };
178170
NewShader.prototype = Object.create(Shader.prototype, proto);

tests/core/shader.test.js

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ suite('shader', function () {
3333
assert.ok(shader.prototype.vertexShader);
3434
assert.ok(shader.prototype.fragmentShader);
3535
assert.notOk(shader.prototype.uniforms);
36-
assert.notOk(shader.prototype.attributes);
3736
});
3837

3938
test('shader instance receives methods and properties', function () {
@@ -49,7 +48,6 @@ suite('shader', function () {
4948
assert.equal(instance.vertexShader, shader.prototype.vertexShader);
5049
assert.equal(instance.fragmentShader, shader.prototype.fragmentShader);
5150
assert.equal(Object.keys(instance.uniforms).length, 0);
52-
assert.equal(Object.keys(instance.attributes).length, 0);
5351
assert.ok(instance.material);
5452
});
5553

@@ -94,8 +92,7 @@ suite('shader data binding', function () {
9492
src: {type: 'map', is: 'uniform'},
9593
otherMap: {type: 'map', is: 'uniform'},
9694
vec2Uniform: {type: 'vec2', default: {x: 1, y: 2}, is: 'uniform'},
97-
vec2Attribute: {type: 'vec2', default: {x: 3, y: 4}, is: 'attribute'},
98-
vec2Neither: {type: 'vec2', default: {x: 5, y: 6}}
95+
vec2NotUniform: {type: 'vec2', default: {x: 5, y: 6}}
9996
}
10097
});
10198

@@ -126,8 +123,6 @@ suite('shader data binding', function () {
126123
assert.ok(updateSpy.calledOnce);
127124
// The value won't be assigned until the texture loads.
128125
assert.ok(instance.uniforms['src']);
129-
assert.notOk(instance.attributes && (instance.attributes['map'] ||
130-
instance.attributes['src']));
131126
});
132127

133128
test('src loads inline video', function (done) {
@@ -152,8 +147,6 @@ suite('shader data binding', function () {
152147
assert.ok(updateSpy.calledOnce);
153148
// The value won't be assigned until the texture loads.
154149
assert.ok(instance.uniforms['src']);
155-
assert.notOk(instance.attributes && (instance.attributes['map'] ||
156-
instance.attributes['src']));
157150
});
158151

159152
test('otherMap loads inline video', function (done) {
@@ -178,8 +171,6 @@ suite('shader data binding', function () {
178171
assert.ok(initSpy.calledOnce);
179172
assert.ok(updateSpy.calledOnce);
180173
assert.ok(instance.uniforms['otherMap']);
181-
// The value won't be assigned until the texture loads.
182-
assert.notOk(instance.attributes && instance.attributes['otherMap']);
183174
});
184175

185176
test('vec2Uniform parameter is uniform', function () {
@@ -194,25 +185,9 @@ suite('shader data binding', function () {
194185
assert.ok(instance.uniforms['vec2Uniform']);
195186
assert.equal(instance.uniforms['vec2Uniform'].value.x, 1);
196187
assert.equal(instance.uniforms['vec2Uniform'].value.y, 2);
197-
assert.notOk(instance.attributes['vec2Uniform']);
198-
});
199-
200-
test('vec2Attribute parameter is attribute', function () {
201-
assert.notOk(initSpy.called);
202-
assert.notOk(updateSpy.called);
203-
el.setAttribute('material', 'shader: testShader');
204-
const material = el.components.material;
205-
const instance = material.shader;
206-
assert.ok(instance);
207-
assert.ok(initSpy.calledOnce);
208-
assert.ok(updateSpy.calledOnce);
209-
assert.ok(instance.attributes['vec2Attribute']);
210-
assert.equal(instance.attributes['vec2Attribute'].value.x, 3);
211-
assert.equal(instance.attributes['vec2Attribute'].value.y, 4);
212-
assert.notOk(instance.uniforms['vec2Attribute']);
213188
});
214189

215-
test('vec2Neither parameter is neither uniform nor attribute', function () {
190+
test('vec2NotUniform parameter is not a uniform', function () {
216191
assert.notOk(initSpy.called);
217192
assert.notOk(updateSpy.called);
218193
el.setAttribute('material', 'shader: testShader');
@@ -221,7 +196,6 @@ suite('shader data binding', function () {
221196
assert.ok(instance);
222197
assert.ok(initSpy.calledOnce);
223198
assert.ok(updateSpy.calledOnce);
224-
assert.notOk(instance.attributes['vec2Neither']);
225-
assert.notOk(instance.uniforms['vec2Neither']);
199+
assert.notOk(instance.uniforms['vec2NotUniform']);
226200
});
227201
});

0 commit comments

Comments
 (0)