View difference between Paste ID: wTeF3Et9 and
SHOW: | | - or go back to the newest paste.
1-
1+
var vertexShaderSource3D =
2
  "attribute vec3 Vertex;" +
3
  "attribute vec3 Normal;" +
4
  "attribute vec4 aColor;" +
5
  "attribute vec2 aTexture;" +
6
  "varying   vec2 vTexture;" +
7
8
  "uniform vec4 color;" +
9
10
  "uniform bool usingMat;" +
11
  "uniform vec3 specular;" +
12
  "uniform vec3 mat_emissive;" +
13
  "uniform vec3 mat_ambient;" +
14
  "uniform vec3 mat_specular;" +
15
  "uniform float shininess;" +
16
17
  "uniform mat4 model;" +
18
  "uniform mat4 view;" +
19
  "uniform mat4 projection;" +
20
  "uniform mat4 normalTransform;" +
21
22
  "uniform int lightCount;" +
23
  "uniform vec3 falloff;" +
24
25
  "struct Light {" +
26
  "  bool dummy;" +
27
  "  int type;" +
28
  "  vec3 color;" +
29
  "  vec3 position;" +
30
  "  vec3 direction;" +
31
  "  float angle;" +
32
  "  vec3 halfVector;" +
33
  "  float concentration;" +
34
  "};" +
35
  "uniform Light lights[8];" +
36
37
  "void AmbientLight( inout vec3 totalAmbient, in vec3 ecPos, in Light light ) {" +
38
  // Get the vector from the light to the vertex
39
  // Get the distance from the current vector to the light position
40
  "  float d = length( light.position - ecPos );" +
41
  "  float attenuation = 1.0 / ( falloff[0] + ( falloff[1] * d ) + ( falloff[2] * d * d ));" + "  totalAmbient += light.color * attenuation;" +
42
  "}" +
43
44
  "void DirectionalLight( inout vec3 col, in vec3 ecPos, inout vec3 spec, in vec3 vertNormal, in Light light ) {" +
45
  "  float powerfactor = 0.0;" +
46
  "  float nDotVP = max(0.0, dot( vertNormal, light.position ));" +
47
  "  float nDotVH = max(0.0, dot( vertNormal, normalize( light.position-ecPos )));" +
48
49
  "  if( nDotVP != 0.0 ){" +
50
  "    powerfactor = pow( nDotVH, shininess );" +
51
  "  }" +
52
53
  "  col += light.color * nDotVP;" +
54
  "  spec += specular * powerfactor;" +
55
  "}" +
56
57
  "void PointLight( inout vec3 col, inout vec3 spec, in vec3 vertNormal, in vec3 ecPos, in vec3 eye, in Light light ) {" +
58
  "  float powerfactor;" +
59
60
  // Get the vector from the light to the vertex
61
  "   vec3 VP = light.position - ecPos;" +
62
63
  // Get the distance from the current vector to the light position
64
  "  float d = length( VP ); " +
65
66
  // Normalize the light ray so it can be used in the dot product operation.
67
  "  VP = normalize( VP );" +
68
69
  "  float attenuation = 1.0 / ( falloff[0] + ( falloff[1] * d ) + ( falloff[2] * d * d ));" +
70
71
  "  float nDotVP = max( 0.0, dot( vertNormal, VP ));" +
72
  "  vec3 halfVector = normalize( VP + eye );" +
73
  "  float nDotHV = max( 0.0, dot( vertNormal, halfVector ));" +
74
75
  "  if( nDotVP == 0.0) {" +
76
  "    powerfactor = 0.0;" +
77
  "  }" +
78
  "  else{" +
79
  "    powerfactor = pow( nDotHV, shininess );" +
80
  "  }" +
81
82
  "  spec += specular * powerfactor * attenuation;" +
83
  "  col += light.color * nDotVP * attenuation;" +
84
  "}" +
85
86
  /*
87
  */
88
  "void SpotLight( inout vec3 col, inout vec3 spec, in vec3 vertNormal, in vec3 ecPos, in vec3 eye, in Light light ) {" +
89
  "  float spotAttenuation;" +
90
  "  float powerfactor;" +
91
92
  // calculate the vector from the current vertex to the light.
93
  "  vec3 VP = light.position - ecPos; " +
94
  "  vec3 ldir = normalize( light.direction );" +
95
96
  // get the distance from the spotlight and the vertex
97
  "  float d = length( VP );" +
98
  "  VP = normalize( VP );" +
99
100
  "  float attenuation = 1.0 / ( falloff[0] + ( falloff[1] * d ) + ( falloff[2] * d * d ) );" +
101
102
  // dot product of the vector from vertex to light and light direction.
103
  "  float spotDot = dot( VP, ldir );" +
104
105
  // if the vertex falls inside the cone
106
  "  if( spotDot < cos( light.angle ) ) {" +
107
  "    spotAttenuation = pow( spotDot, light.concentration );" +
108
  "  }" +
109
  "  else{" +
110
  "    spotAttenuation = 1.0;" +
111
  "  }" +
112
  "  attenuation *= spotAttenuation;" +
113
114
  "  float nDotVP = max( 0.0, dot( vertNormal, VP ));" +
115
  "  vec3 halfVector = normalize( VP + eye );" +
116
  "  float nDotHV = max( 0.0, dot( vertNormal, halfVector ));" +
117
118
  "  if( nDotVP == 0.0 ) {" +
119
  "    powerfactor = 0.0;" +
120
  "  }" +
121
  "  else {" +
122
  "    powerfactor = pow( nDotHV, shininess );" +
123
  "  }" +
124
125
  "  spec += specular * powerfactor * attenuation;" +
126
  "  col += light.color * nDotVP * attenuation;" +
127
  "}" +
128
129
  "void main(void) {" +
130
  "  vec3 finalAmbient = vec3( 0.0, 0.0, 0.0 );" +
131
  "  vec3 finalDiffuse = vec3( 0.0, 0.0, 0.0 );" +
132
  "  vec3 finalSpecular = vec3( 0.0, 0.0, 0.0 );" +
133
134
  "  vec4 col = color;" +
135
  "  if(color[0] == -1.0){" +
136
  "    col = aColor;" +
137
  "  }" +
138
139
  "  vec3 norm = vec3( normalTransform * vec4( Normal, 0.0 ) );" +
140
141
  "  vec4 ecPos4 = view * model * vec4(Vertex,1.0);" +
142
  "  vec3 ecPos = (vec3(ecPos4))/ecPos4.w;" +
143
  "  vec3 eye = vec3( 0.0, 0.0, 1.0 );" +
144
145
  // If there were no lights this draw call, just use the
146
  // assigned fill color of the shape and the specular value
147
  "  if( lightCount == 0 ) {" +
148
  "    gl_FrontColor = col + vec4(mat_specular,1.0);" +
149
  "  }" +
150
  "  else {" +
151
  "    for( int i = 0; i < lightCount; i++ ) {" +
152
  "      if( lights[i].type == 0 ) {" +
153
  "        AmbientLight( finalAmbient, ecPos, lights[i] );" +
154
  "      }" +
155
  "      else if( lights[i].type == 1 ) {" +
156
  "        DirectionalLight( finalDiffuse,ecPos, finalSpecular, norm, lights[i] );" +
157
  "      }" +
158
  "      else if( lights[i].type == 2 ) {" +
159
  "        PointLight( finalDiffuse, finalSpecular, norm, ecPos, eye, lights[i] );" +
160
  "      }" +
161
  "      else if( lights[i].type == 3 ) {" +
162
  "        SpotLight( finalDiffuse, finalSpecular, norm, ecPos, eye, lights[i] );" +
163
  "      }" +
164
  "    }" +
165
166
  "   if( usingMat == false ) {" +
167
  "    gl_FrontColor = vec4(  " +
168
  "      vec3(col) * finalAmbient +" +
169
  "      vec3(col) * finalDiffuse +" +
170
  "      vec3(col) * finalSpecular," +
171
  "      col[3] );" +
172
  "   }" +
173
  "   else{" +
174
  "     gl_FrontColor = vec4( " +
175
  "       mat_emissive + " +
176
  "       (vec3(col) * mat_ambient * finalAmbient) + " +
177
  "       (vec3(col) * finalDiffuse) + " +
178
  "       (mat_specular * finalSpecular), " +
179
  "       col[3] );" +
180
  "    }" +
181
  "  }" +
182
  "  vTexture.xy = aTexture.xy;" +
183
  "  gl_Position = projection * view * model * vec4( Vertex, 1.0 );" +
184
  "}";
185
186
var fragmentShaderSource3D =
187
  "uniform sampler2D sampler;" +
188
  "uniform bool usingTexture;" +
189
  "varying vec2 vTexture;" +
190
191
  // In Processing, when a texture is used, the fill color is ignored
192
  "void main(void){" +
193
  "  if(usingTexture){" +
194
  "    gl_FragColor =  vec4(texture2D(sampler, vTexture.xy));" +
195
  "  }"+
196
  "  else{" +
197
  "    gl_FragColor = vec4(gl_Color);" +
198
  "  }" +
199
  "}";