-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw.js
More file actions
66 lines (65 loc) · 2.68 KB
/
Copy pathdraw.js
File metadata and controls
66 lines (65 loc) · 2.68 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
/**
** @desc Объект для отрисовки фигур
**/
class Draw {
/**
** @desc Основной метод. Рисует 3д фигуру.
** @vars (Shape3D) figure - объект фигуры, (Color) color - цвет для отрисовки фигуры
**/
Draw3DFigure( figure , color ) {
let triangles = figure.GetTranslatedTriangles();
let light = globalLight;
for( let t = 0; t < triangles.length; t++ ) {
let triangle = triangles[ t ];
let points = triangle.GetPoints();
let normal = triangle.GetNormal();
let frags = triangle.GetFrags();
let triColor = this.GetColorFromPoint( points[ 0 ] , normal , frags[ 0 ] , light , color );
context.fillStyle = triColor;
context.strokeStyle = triColor;
context.lineWidth = 0;
this.DrawTriangle( points );
}
}
/**
** @desc Получает цвет точки и смешиваем с источником света, возвращает строчное значение цвета
** @vars (Vector3F) point - точка для получения цвета, (Vector3F) normal - нормаль точки, (Vector3F) frag - мировые координаты точки, (object) light - объект цвета, (Color) color - цвет фигуры
**/
GetColorFromPoint( point , normal , frag , light , color ) {
let dot = normal.Dot( light.position.Subtract( frag ).Normalize() );
let fragColorDiff = Math.max( Math.min( dot , 1 ) , light.ambient );
let fragColorDist = light.position.Distance( frag ) * ( light.length / zFar );
let distDiff = fragColorDiff * fragColorDist;
let fragColor = color.Copy().Multiply( fragColorDiff );
let lightColor = light.color.Multiply( distDiff ).Mix( fragColor );
return lightColor.ToString();
}
/**
** @desc Рисует треугольник
** @vars (array) points - массив точек треугольника
**/
DrawTriangle( points ) {
context.beginPath();
this.MoveToPoint( points[ 0 ] );
this.LineToPoint( points[ 1 ] );
this.LineToPoint( points[ 2 ] );
this.LineToPoint( points[ 0 ] );
context.fill();
context.stroke();
}
/**
** @desc Рисует линию к точке
** @vars (Vector3F) point - точка к которой идёт линия
**/
LineToPoint( point ) {
context.lineTo( widthH + point.x * figscaleX , heightH + point.y * figscaleY );
}
/**
** @desc Стартовая точка для начала отрисовки треугольника
** @vars (Vector3F) point - точка начала отрисовки
**/
MoveToPoint( point ) {
context.moveTo( widthH + point.x * figscaleX , heightH + point.y * figscaleY );
}
}
const draw = new Draw();