18 private Vector3 sourcePosition;
19 private readonly
float sourceDiffuseRed;
20 private readonly
float sourceDiffuseGreen;
21 private readonly
float sourceDiffuseBlue;
22 private readonly
float sourceDiffuseAlpha;
23 private readonly
float sourceSpecularRed;
24 private readonly
float sourceSpecularGreen;
25 private readonly
float sourceSpecularBlue;
26 private readonly
float sourceSpecularAlpha;
27 private readonly
float ambientRed;
28 private readonly
float ambientGreen;
29 private readonly
float ambientBlue;
30 private readonly
float ambientAlpha;
31 private readonly
float ambientReflectionConstant;
32 private readonly
float diffuseReflectionConstant;
33 private readonly
float specularReflectionConstant;
34 private readonly
float shininess;
35 private readonly
int nrSources;
36 private readonly
bool hasSpecularReflectionConstant;
37 private readonly
bool singleSource;
38 private readonly
bool opaque;
52 this.sources = LightSources;
57 this.hasSpecularReflectionConstant = this.specularReflectionConstant != 0;
60 this.nrSources = LightSources.Length;
61 this.singleSource = this.nrSources == 1;
63 if (this.singleSource)
65 this.source = LightSources[0];
66 this.sourcePosition = this.source.Position;
70 this.sourceDiffuseRed = I.
Red;
71 this.sourceDiffuseGreen = I.
Green;
72 this.sourceDiffuseBlue = I.
Blue;
73 this.sourceDiffuseAlpha = I.
Alpha;
75 I = this.source.Specular;
77 this.sourceSpecularRed = I.
Red;
78 this.sourceSpecularGreen = I.
Green;
79 this.sourceSpecularBlue = I.
Blue;
80 this.sourceSpecularAlpha = I.
Alpha;
83 this.ambientRed = this.ambientReflectionConstant *
Ambient.
Red;
84 this.ambientGreen = this.ambientReflectionConstant *
Ambient.
Green;
85 this.ambientBlue = this.ambientReflectionConstant *
Ambient.
Blue;
86 this.ambientAlpha = this.ambientReflectionConstant *
Ambient.
Alpha;
88 if (this.ambientAlpha < 255 ||
89 this.sourceDiffuseAlpha < 255 ||
90 this.sourceSpecularAlpha < 255)
138 Vector3 P =
new Vector3(X, Y, Z);
145 if (this.singleSource)
147 L = Vector3.Normalize(this.sourcePosition - P);
148 d = Vector3.Dot(L, Normal);
150 Red = this.ambientRed;
151 Green = this.ambientGreen;
152 Blue = this.ambientBlue;
153 Alpha = this.ambientAlpha;
157 d *= this.diffuseReflectionConstant;
159 if (this.hasSpecularReflectionConstant)
161 R = 2 * d * Normal - L;
163 d2 = Math.Abs(Vector3.Dot(R, V));
164 d2 = this.specularReflectionConstant * (float)Math.Pow(d2,
this.shininess);
166 Red += d2 * this.sourceSpecularRed;
167 Green += d2 * this.sourceSpecularGreen;
168 Blue += d2 * this.sourceSpecularBlue;
169 Alpha += d2 * this.sourceSpecularAlpha;
172 Red += d * this.sourceDiffuseRed;
173 Green += d * this.sourceDiffuseGreen;
174 Blue += d * this.sourceDiffuseBlue;
175 Alpha += d * this.sourceDiffuseAlpha;
185 Red = Green = Blue = Alpha = 0;
187 if (this.hasSpecularReflectionConstant)
192 for (j = 0; j < this.nrSources; j++)
194 Source = this.sources[j];
198 L = Vector3.Normalize(Source.
Position - P);
199 d = Vector3.Dot(L, Normal);
201 Red += this.ambientRed;
202 Green += this.ambientGreen;
203 Blue += this.ambientBlue;
204 Alpha += this.ambientAlpha;
208 d *= this.diffuseReflectionConstant;
210 if (this.hasSpecularReflectionConstant)
212 R = 2 * d * Normal - L;
213 d2 = Math.Abs(Vector3.Dot(R, V));
214 d2 = this.specularReflectionConstant * (float)Math.Pow(d2,
this.shininess);
216 Red += d2 * Specular.
Red;
217 Green += d2 * Specular.
Green;
218 Blue += d2 * Specular.
Blue;
219 Alpha += d2 * Specular.
Alpha;
222 Red += d * Diffuse.
Red;
223 Green += d * Diffuse.
Green;
224 Blue += d * Diffuse.
Blue;
225 Alpha += d * Diffuse.
Alpha;
238 Rest = (int)(Red - 254.5f);
242 R2 = (byte)(Red + 0.5f);
246 else if (Green > 255)
248 Rest += (int)(Green - 254.5f);
252 G2 = (byte)(Green + 0.5f);
258 Rest += (int)(Blue - 254.5f);
262 B2 = (byte)(Blue + 0.5f);
266 else if (Alpha > 255)
269 A2 = (byte)(Alpha + 0.5f);
303 return new SKColor(R2, G2, B2, A2);
316 public void GetColors(
float[] X,
float[] Y,
float[] Z, Vector3[] Normals,
int N,
323 Vector3 P =
new Vector3();
334 if (this.singleSource)
336 for (i = 0; i < N; i++)
343 L = Vector3.Normalize(this.sourcePosition - P);
344 d = Vector3.Dot(L, Normal);
346 Red = this.ambientRed;
347 Green = this.ambientGreen;
348 Blue = this.ambientBlue;
349 Alpha = this.ambientAlpha;
353 d *= this.diffuseReflectionConstant;
355 if (this.hasSpecularReflectionConstant)
357 R = 2 * d * Normal - L;
359 d2 = Math.Abs(Vector3.Dot(R, V));
360 d2 = this.specularReflectionConstant * (float)Math.Pow(d2,
this.shininess);
362 Red += d2 * this.sourceSpecularRed;
363 Green += d2 * this.sourceSpecularGreen;
364 Blue += d2 * this.sourceSpecularBlue;
365 Alpha += d2 * this.sourceSpecularAlpha;
368 Red += d * this.sourceDiffuseRed;
369 Green += d * this.sourceDiffuseGreen;
370 Blue += d * this.sourceDiffuseBlue;
371 Alpha += d * this.sourceDiffuseAlpha;
380 Rest = (int)(Red - 254.5f);
384 R2 = (byte)(Red + 0.5f);
388 else if (Green > 255)
390 Rest += (int)(Green - 254.5f);
394 G2 = (byte)(Green + 0.5f);
400 Rest += (int)(Blue - 254.5f);
404 B2 = (byte)(Blue + 0.5f);
408 else if (Alpha > 255)
411 A2 = (byte)(Alpha + 0.5f);
445 Colors[i] =
new SKColor(R2, G2, B2, A2);
455 for (i = 0; i < N; i++)
462 Red = Green = Blue = Alpha = 0;
464 if (this.hasSpecularReflectionConstant)
469 for (j = 0; j < this.nrSources; j++)
471 Source = this.sources[j];
475 L = Vector3.Normalize(Source.
Position - P);
476 d = Vector3.Dot(L, Normal);
478 Red += this.ambientRed;
479 Green += this.ambientGreen;
480 Blue += this.ambientBlue;
481 Alpha += this.ambientAlpha;
485 d *= this.diffuseReflectionConstant;
487 if (this.hasSpecularReflectionConstant)
489 R = 2 * d * Normal - L;
490 d2 = Math.Abs(Vector3.Dot(R, V));
491 d2 = this.specularReflectionConstant * (float)Math.Pow(d2,
this.shininess);
493 Red += d2 * Specular.
Red;
494 Green += d2 * Specular.
Green;
495 Blue += d2 * Specular.
Blue;
496 Alpha += d2 * Specular.
Alpha;
499 Red += d * Diffuse.
Red;
500 Green += d * Diffuse.
Green;
501 Blue += d * Diffuse.
Blue;
502 Alpha += d * Diffuse.
Alpha;
512 Rest = (int)(Red - 254.5f);
516 R2 = (byte)(Red + 0.5f);
520 else if (Green > 255)
522 Rest += (int)(Green - 254.5f);
526 G2 = (byte)(Green + 0.5f);
532 Rest += (int)(Blue - 254.5f);
536 B2 = (byte)(Blue + 0.5f);
540 else if (Alpha > 255)
543 A2 = (byte)(Alpha + 0.5f);
577 Colors[i] =
new SKColor(R2, G2, B2, A2);
Vector3 ViewerPosition
Viewer position
Contains information about the intensity of a light component, as used in the Phong reflection model....
float Alpha
Alpha intensity.
float Blue
Blue intensity.
float Green
Green intensity.
Contains information about a light source, as used in the Phong reflection model. https://en....
bool Opaque
If shader is 100% opaque.
Vector3 Position
Position of light source..
PhongIntensity Diffuse
Diffuse intensity.
PhongIntensity Specular
Specular intensity.
Contains information about a material, as used in the Phong reflection model. https://en....
float AmbientReflectionConstant
Ratio of reflection of the ambient term present in all points in the scene rendered.
float DiffuseReflectionConstant
Ratio of reflection of the diffuse term of incoming light.
float Shininess
Shininess coefficient.
float SpecularReflectionConstant
Ratio of reflection of the specular term of incoming light.
The Phong Shader uses the Phong Reflection model to generate colors. https://en.wikipedia....
PhongShader(PhongMaterial Material, PhongIntensity Ambient, params PhongLightSource[] LightSources)
The Phong Shader uses the Phong Reflection model to generate colors. https://en.wikipedia....
PhongMaterial Material
Material settings.
void GetColors(float[] X, float[] Y, float[] Z, Vector3[] Normals, int N, SKColor[] Colors, Canvas3D Canvas)
Gets an array of colors.
SKColor GetColor(float X, float Y, float Z, Vector3 Normal, Canvas3D Canvas)
Gets a color for a position.
PhongIntensity Ambient
Ambient settings
PhongLightSource[] Sources
Light Sources
bool Opaque
If shader is 100% opaque.
Interface for 3D shaders.