-
Notifications
You must be signed in to change notification settings - Fork 0
/
material.js
66 lines (47 loc) · 2.05 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
/*
* WebGL core teaching framwork
* (C)opyright Hartmut Schirmacher, hschirmacher.beuth-hochschule.de
*
* Module: Material
*
* This module defines different types of materials for shading.
*
*/
/* requireJS module definition */
define([],
( function() {
"use strict";
/*
Object: PhongMaterial
Sets uniform variables in the shader to define
a Phong or Blinn-Phong material.
Parameters specified via a config object:
- uniformName: the name of the uniform struct to be used in the shader
- config: a configuration object defining the following optional attributes:
- ambient [3 floats]: ambient reflection coefficient
- diffuse [3 floats]: diffuse reflection coefficient
- specular [3 floats]: specular reflection coefficient
- shininess [float]: Phong exponent
Key Methods:
- draw(): sets uniform variables in the shader
*/
var PhongMaterial = function(uniformName, config) {
config = config || {};
this.uniformName = uniformName || "material";
this.ambient = config.ambient || [0.0, 0.0, 0.0];
this.diffuse = config.diffuse || [0.4, 0.0, 0.0];
this.specular = config.specular || [0.6, 0.6, 0.6];
this.shininess = config.shininess|| 40;
};
// activate this light by setting the respective uniform variables
PhongMaterial.prototype.draw = function(gl, program, mvMatrix) {
var name = this.uniformName;
program.use();
program.setUniform(name+".ambient", "vec3", this.ambient);
program.setUniform(name+".diffuse", "vec3", this.diffuse);
program.setUniform(name+".specular", "vec3", this.specular);
program.setUniform(name+".shininess", "float", this.shininess);
};
// this module returns constructors for various light types
return { "PhongMaterial": PhongMaterial };
})); // define module