forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.js
More file actions
137 lines (116 loc) · 4.56 KB
/
basic.js
File metadata and controls
137 lines (116 loc) · 4.56 KB
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
pc.programlib.basic = {
generateKey: function (device, options) {
var key = 'basic';
if (options.fog) key += '_fog';
if (options.alphaTest) key += '_atst';
if (options.vertexColors) key += '_vcol';
if (options.diffuseMap) key += '_diff';
return key;
},
createShaderDefinition: function (device, options) {
/////////////////////////
// GENERATE ATTRIBUTES //
/////////////////////////
var attributes = {
vertex_position: pc.SEMANTIC_POSITION
};
if (options.skin) {
attributes.vertex_boneWeights = pc.SEMANTIC_BLENDWEIGHT;
attributes.vertex_boneIndices = pc.SEMANTIC_BLENDINDICES;
}
if (options.vertexColors) {
attributes.vertex_color = pc.SEMANTIC_COLOR;
}
if (options.diffuseMap) {
attributes.vertex_texCoord0 = pc.SEMANTIC_TEXCOORD0;
}
////////////////////////////
// GENERATE VERTEX SHADER //
////////////////////////////
var getSnippet = pc.programlib.getSnippet;
var code = '';
// VERTEX SHADER DECLARATIONS
code += getSnippet(device, 'vs_transform_decl');
if (options.skin) {
code += getSnippet(device, 'vs_skin_decl');
}
if (options.vertexColors) {
code += 'attribute vec4 vertex_color;\n';
code += 'varying vec4 vColor;\n';
}
if (options.diffuseMap) {
code += 'attribute vec2 vertex_texCoord0;\n';
code += 'varying vec2 vUv0;\n';
}
// VERTEX SHADER BODY
code += getSnippet(device, 'common_main_begin');
// SKINNING
if (options.skin) {
code += " mat4 modelMatrix = vertex_boneWeights.x * getBoneMatrix(vertex_boneIndices.x) +\n";
code += " vertex_boneWeights.y * getBoneMatrix(vertex_boneIndices.y) +\n";
code += " vertex_boneWeights.z * getBoneMatrix(vertex_boneIndices.z) +\n";
code += " vertex_boneWeights.w * getBoneMatrix(vertex_boneIndices.w);\n";
code += " vec4 positionW = modelMatrix * vec4(vertex_position, 1.0);\n";
code += " positionW.xyz += skinPosOffset;\n";
} else {
code += " mat4 modelMatrix = matrix_model;\n";
code += " vec4 positionW = modelMatrix * vec4(vertex_position, 1.0);\n";
}
code += "\n";
// TRANSFORM
code += " gl_Position = matrix_viewProjection * positionW;\n\n";
if (options.vertexColors) {
code += ' vColor = vertex_color;\n';
}
if (options.diffuseMap) {
code += ' vUv0 = vertex_texCoord0;\n';
}
code += getSnippet(device, 'common_main_end');
var vshader = code;
//////////////////////////////
// GENERATE FRAGMENT SHADER //
//////////////////////////////
code = getSnippet(device, 'fs_precision');
// FRAGMENT SHADER DECLARATIONS
if (options.vertexColors) {
code += 'varying vec4 vColor;\n';
} else {
code += 'uniform vec4 uColor;\n';
}
if (options.diffuseMap) {
code += 'varying vec2 vUv0;\n';
code += 'uniform sampler2D texture_diffuseMap;\n';
}
if (options.fog) {
code += getSnippet(device, 'fs_fog_decl');
}
if (options.alphatest) {
code += getSnippet(device, 'fs_alpha_test_decl');
}
// FRAGMENT SHADER BODY
code += getSnippet(device, 'common_main_begin');
// Read the map texels that the shader needs
if (options.vertexColors) {
code += ' gl_FragColor = vColor;\n';
} else {
code += ' gl_FragColor = uColor;\n';
}
if (options.diffuseMap) {
code += ' gl_FragColor *= texture2D(texture_diffuseMap, vUv0);\n';
}
if (options.alphatest) {
code += getSnippet(device, 'fs_alpha_test');
}
code += getSnippet(device, 'fs_clamp');
if (options.fog) {
code += getSnippet(device, 'fs_fog');
}
code += getSnippet(device, 'common_main_end');
var fshader = code;
return {
attributes: attributes,
vshader: vshader,
fshader: fshader
};
}
};