-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
137 lines (137 loc) · 5.33 KB
/
index.html
File metadata and controls
137 lines (137 loc) · 5.33 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
<!DOCTYPE html>
<html>
<head>
<title>3д Куб на Javascript</title>
<script src="js/vector.js"></script>
<script src="js/shape.js"></script>
<script src="js/camera.js"></script>
<script src="js/matrix.js"></script>
<script src="js/triangles.js"></script>
<script src="js/color.js"></script>
<script src="js/draw.js"></script>
<script src="js/light.js"></script>
<script src="js/clip.js"></script>
</head>
<body>
<canvas id="cube-canvas" width="1280" height="720" ></canvas>
<style>
#cube-canvas {
margin: 0 auto;
background-color: black;
}
</style>
<script>
//Основные константы
const canvas = document.querySelector( "#cube-canvas" );
const context = canvas.getContext( "2d" );
context.font = "20px roboto";
const widthH = parseInt( canvas.width / 2 );
const heightH = parseInt( canvas.height / 2 );
const zNear = 0.1;
const zFar = 100;
const fovDeg = 45;
const aspectRation = canvas.height / canvas.width;
const fFovRad = 1 / Math.tan( ( fovDeg * 0.5 ) / 180 * Math.PI );
const figscaleX = widthH;
const figscaleY = heightH;
//Объект куба
const cube = new Shape3D( [
new Vector3F( -1 , -1 , 1 ) , new Vector3F( -1 , -1 , 1 ) , new Vector3F( -1 , -1 , 1 ) ,
new Vector3F( -1 , 1 , 1 ) , new Vector3F( -1 , 1 , 1 ) , new Vector3F( -1 , 1 , 1 ) ,
new Vector3F( -1 , -1 , -1 ) , new Vector3F( -1 , -1 , -1 ) , new Vector3F( -1 , -1 , -1 ) ,
new Vector3F( -1 , 1 , -1 ) , new Vector3F( -1 , 1 , -1 ) , new Vector3F( -1 , 1 , -1 ) ,
new Vector3F( 1 , -1 , 1 ) , new Vector3F( 1 , -1 , 1 ) , new Vector3F( 1 , -1 , 1 ) ,
new Vector3F( 1 , 1 , 1 ) , new Vector3F( 1 , 1 , 1 ) , new Vector3F( 1 , 1 , 1 ) ,
new Vector3F( 1 , -1 , -1 ) , new Vector3F( 1 , -1 , -1 ) , new Vector3F( 1 , -1 , -1 ) ,
new Vector3F( 1 , 1 , -1 ) , new Vector3F( 1 , 1 , -1 ) , new Vector3F( 1 , 1 , -1 )
] );
cube.SetIndices( [
0 , 3 , 9 ,
0 , 9 , 6 ,
8 , 10 , 21 ,
8 , 21 , 19 ,
20 , 23 , 17 ,
20 , 17 , 14 ,
13 , 15 , 4 ,
13 , 4 , 2 ,
7 , 18 , 12 ,
7 , 12 , 1 ,
22 , 11 , 5 ,
22 , 5 , 16
] );
cube.SetNormals( [
new Vector3F( -1 , 0 , 0 ) , new Vector3F( 0 , -1 , 0 ) , new Vector3F( 0 , 0 , 1 ) ,
new Vector3F( -1 , 0 , 0 ) , new Vector3F( 0 , 0 , 1 ) , new Vector3F( 0 , 1 , 0 ) ,
new Vector3F( -1 , 0 , 0 ) , new Vector3F( 0 , -1 , 0 ) , new Vector3F( 0 , 0 , -1 ),
new Vector3F( -1 , 0 , 0 ) , new Vector3F( 0 , 0 , -1 ) , new Vector3F( 0 , 1 , 0 ),
new Vector3F( 0 , -1 , 0 ) , new Vector3F( 0 , 0 , 1 ) , new Vector3F( 1 , 0 , 0 ) ,
new Vector3F( 0 , 0 , 1 ) , new Vector3F( 0 , 1 , 0 ) , new Vector3F( 1 , 0 , 0 ) ,
new Vector3F( 0 , -1 , 0 ) , new Vector3F( 0 , 0 , -1 ) , new Vector3F( 1 , 0 , 0 ) ,
new Vector3F( 0 , 0 , -1 ) , new Vector3F( 0 , 1 , 0 ) , new Vector3F( 1 , 0 , 0 )
] );
cube.CreateTriangles();
//Объекты матриц
const projectionMatrix = mat4.Perspective( mat4.Create() , fFovRad , aspectRation , zNear , zFar );
const viewMatrix = mat4.Create();
//Положение куба
cube.translate.x = 0;
cube.translate.y = 0;
cube.translate.z = 5;
//Объект освещения
const globalLight = new Light( new Vector3F( 1 , 10 , 1 ) , new Color( 0 , 0 , 255 ) , 30 , 0.1 );
//Вывод текста сообщения вверху экрана
const text = "WSAD для перемещения | RGB для смены цвета";
const textInfo = context.measureText( text );
const textPos = parseInt( widthH - textInfo.width * 0.5 );
//Основной цикл
function Frame() {
mat4.LookAt( viewMatrix , camera.eye , camera.GetCenter() , camera.up );
context.clearRect(0, 0, canvas.width, canvas.height);
cube.RotateZ( 0.01 );
cube.RotateY( 0.01 );
cube.RotateX( 0.01 );
cube.Update();
cube.Draw();
context.fillStyle = "white";
context.fillText( text , textPos , 50 )
window.requestAnimationFrame( Frame );
}
canvas.onmousemove = function( e ) {
let offset = camera.GetOffset( e.movementX , -e.movementY );
camera.yaw += offset[ 0 ];
camera.pitch = Math.max( Math.min( camera.pitch + offset[ 1 ] , 1.4 ) , -0.9 ); //Ограничения на поворот
camera.Rotate();
}
window.addEventListener( "keydown" , function( e ) {
if( e.keyCode == 87 ) {
camera.MoveFront( 1 );
}
if( e.keyCode == 83 ) {
camera.MoveFront( -1 );
}
if( e.keyCode == 65 ) {
camera.MoveStrafe( 1 );
}
if( e.keyCode == 68 ) {
camera.MoveStrafe( -1 );
}
if( e.keyCode == 82 ) {
globalLight.SetColor( new Color( 255 , 0 , 0 ) );
}
if( e.keyCode == 71 ) {
globalLight.SetColor( new Color( 0 , 255 , 0 ) );
}
if( e.keyCode == 66 ) {
globalLight.SetColor( new Color( 0 , 0 , 255 ) );
}
});
window.requestAnimationFrame( Frame );
canvas.addEventListener("click", async () => {
await canvas.requestFullscreen();
await canvas.requestPointerLock( {
unadjustedMovement: true,
});
});
</script>
</body>
</html>