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
155 lines (132 loc) · 4.82 KB
/
basic.js
File metadata and controls
155 lines (132 loc) · 4.82 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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';
key += '_' + options.pass;
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;
}
var chunks = pc.shaderChunks;
////////////////////////////
// GENERATE VERTEX SHADER //
////////////////////////////
var code = '';
// VERTEX SHADER DECLARATIONS
code += chunks.transformDeclVS;
if (options.skin) {
code += pc.programlib.skinCode(device);
code += chunks.transformSkinnedVS;
} else {
code += chunks.transformVS;
}
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';
}
if (options.pass === pc.SHADER_DEPTH) {
code += 'varying float vDepth;\n';
code += '#ifndef VIEWMATRIX\n';
code += '#define VIEWMATRIX\n';
code += 'uniform mat4 matrix_view;\n';
code += '#endif\n';
code += '#ifndef CAMERAPLANES\n';
code += '#define CAMERAPLANES\n';
code += 'uniform vec4 camera_params;\n\n';
code += '#endif\n';
}
// VERTEX SHADER BODY
code += pc.programlib.begin();
code += " gl_Position = getPosition();\n";
if (options.pass === pc.SHADER_DEPTH) {
code += " vDepth = -(matrix_view * vec4(getWorldPosition(),1.0)).z * camera_params.x;\n";
}
if (options.vertexColors) {
code += ' vColor = vertex_color;\n';
}
if (options.diffuseMap) {
code += ' vUv0 = vertex_texCoord0;\n';
}
code += pc.programlib.end();
var vshader = code;
//////////////////////////////
// GENERATE FRAGMENT SHADER //
//////////////////////////////
code = pc.programlib.precisionCode(device);
// 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 += pc.programlib.fogCode(options.fog);
}
if (options.alphatest) {
code += chunks.alphaTestPS;
}
if (options.pass === pc.SHADER_DEPTH) {
// ##### SCREEN DEPTH PASS #####
code += 'varying float vDepth;\n';
code += chunks.packDepthPS;
}
// FRAGMENT SHADER BODY
code += pc.programlib.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 += " alphaTest(gl_FragColor.a);\n";
}
if (options.pass === pc.SHADER_PICK) {
// ##### PICK PASS #####
} else if (options.pass === pc.SHADER_DEPTH) {
// ##### SCREEN DEPTH PASS #####
code += " gl_FragColor = packFloat(vDepth);\n";
} else {
// ##### FORWARD PASS #####
if (options.fog) {
code += " glFragColor.rgb = addFog(gl_FragColor.rgb);\n";
}
}
code += pc.programlib.end();
var fshader = code;
return {
attributes: attributes,
vshader: vshader,
fshader: fshader
};
}
};