-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scene.ts
142 lines (120 loc) · 4.86 KB
/
Scene.ts
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
///<reference path="references.ts" />
module Jv.Games.WebGL {
import MeshRenderer = Jv.Games.WebGL.Components.MeshRenderer;
export interface IDrawable {
draw();
}
export class Scene extends GameObject {
cameras: Camera[];
clearColor: Color;
drawables: GameObject[];
ambientLight = Color.Rgb(1, 1, 1);
specularLight = Color.Rgb(1, 1, 1);
mainLight: Materials.DirectionalLight;
directionalLight: Materials.DirectionalLight;
constructor(public webgl: Jv.Games.WebGL.Core.WebGL) {
super();
this.cameras = [];
this.drawables = [];
this.clearColor = Color.Rgb(0, 0, 0);
}
update(deltaTime: number) {
super.update(deltaTime);
this.cameras.forEach(c => {
c.update(deltaTime);
});
}
add<Type extends GameObject>(child: Type): Type;
add(camera: Camera): Camera;
add<Type extends Components.Component<GameObject>, Arguments>(behaviorType: { new (object: GameObject, args?: Arguments): Type }, args?: Arguments);
add(item, args?) {
if (item instanceof Camera) {
this.cameras.push(item);
(<Camera>item).parent = this;
return item;
}
else {
var res = super.add(item);
if (item instanceof GameObject)
this.registerDrawable(<GameObject>item);
return res;
}
}
registerDrawable(item: GameObject) {
if (this.drawables.indexOf(item) < 0) {
var components = item.getComponents(Components.Component, false);
for (var i in components) {
if (typeof (<any>components[i]).draw === "function")
this.drawables.push(item);
}
}
item.children.forEach(c => this.registerDrawable(c));
}
unregisterDrawable(item: GameObject) {
var idx = this.drawables.indexOf(item);
if (idx >= 0)
this.drawables.splice(idx, 1);
item.children.forEach(c => this.unregisterDrawable(c));
}
init() {
var gl = this.webgl.context;
gl.clearColor(this.clearColor.red, this.clearColor.green, this.clearColor.blue, this.clearColor.alpha);
gl.enable(gl.DEPTH_TEST);
gl.depthFunc(gl.LEQUAL);
gl.clearDepth(1.0);
this.children.forEach(c => c.init());
this.cameras.forEach(c => c.init());
}
draw() {
var gl = this.webgl.context;
var canvas = this.webgl.canvas;
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
for (var ic = 0; ic < this.cameras.length; ic++) {
var cam = this.cameras[ic];
gl.viewport(canvas.width * cam.viewport.left, canvas.height * cam.viewport.top, canvas.width * cam.viewport.width, canvas.height * cam.viewport.height);
var materials = [];
for (var id = 0; id < this.drawables.length; id++) {
var obj = this.drawables[id];
var viewCheck = obj;
while (typeof viewCheck !== "undefined") {
if (!viewCheck.visible)
break;
viewCheck = viewCheck.parent;
}
if (typeof viewCheck !== "undefined")
continue;
var components = obj.getComponents(Components.Component, false);
for (var i = 0; i < components.length; i++) {
if (typeof (<any>components[i]).draw === "function") {
var material = (<MeshRenderer>components[i]).material;
if (typeof material !== "undefined") {
if(materials.indexOf(material) < 0) {
material.setWorld(this, cam);
materials.push(material);
}
material.setModel(obj);
}
(<any>components[i]).draw();
}
}
}
}
gl.flush();
}
private static contains<T>(a: T[], v) {
for (var i = 0; i < a.length; i++) {
if (a[i] === v) return true;
}
return false;
}
private static unique<T>(a: T[]) {
var arr: T[] = [];
for (var i = 0; i < a.length; i++) {
if (!Scene.contains(arr, a[i])) {
arr.push(a[i]);
}
}
return arr;
}
}
}