forked from unconed/fuse10
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.js
220 lines (172 loc) · 5.34 KB
/
renderer.js
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
Acko.Renderer = function (options) {
options = options || {};
this.parameters = options.parameters || {};
this.container = options.container || null;
this.klass = options.klass || THREE.WebGLRenderer;
this.type = options.type || 'generic';
this.camera = options.camera || new THREE.PerspectiveCamera();
this.autoSize = options.autoSize || false;
this.dead = options.dead || false;
this.autoRender = options.autoRender !== undefined ? options.autoRender : true;
this.isVisible = options.visible !== undefined ? options.visible : true;
this.lastVisible = -1;
var effects = options.effects || [];
this.renderer = null;
this.scene = null;
this.depthScene = null;
this.frames = 0;
this.fps = 58;
this.capWidth = 0;
this.capHeight = 0;
this.warmUp = 2;
this.skipFrames = 0;
this.handlers = {};
this.exports = {};
this.effects = effects.filter(function (effect) {
var r = effect.renderer;
return r == this.type;
}.bind(this));
this.effects.forEach(function (effect) {
effects.splice(effects.indexOf(effect), 1);
});
}
Acko.Renderer.prototype = {
init: function () {
var renderer = this.renderer = new (this.klass)(this.parameters);
var scene = this.scene = new THREE.Scene();
var depthScene = this.depthScene = new THREE.Scene();
var camera = this.camera;
var el = this.renderer.domElement;
el.style.display = 'none';
if (!this.renderer.domElement.parentNode) {
(this.container || document.body).appendChild(el);
}
el.className = 'frame fixed';
if (this.dead) el.classList.add('dead');
window.addEventListener('resize', this.resize.bind(this));
var exports = this.exports = {
parent: this,
scene: scene,
depthScene: depthScene,
camera: camera,
renderer: renderer,
context: renderer.context,
scroll: { x: 0, y: 0 },
time: 0,
debug: {},
};
var effects = this.effects;
effects.sort(function (a, b) {
return a.order - b.order;
});
var tock = tick();
_.each(effects, function (effect) {
effect.build(exports);
});
tock(this.type.toUpperCase() +' effect build');
this.resize();
},
scroll: function (scroll) {
this.exports.scroll = scroll;
},
resize: function (e) {
if (this.autoSize && e) {
this.capWidth = 0;
this.capHeight = 0;
}
var exports = this.exports;
var c = this.container;
var vw, vh;
var w = vw = this.width = c ? c.offsetWidth : window.innerWidth;
var h = vh = this.height = c ? c.offsetHeight : window.innerHeight;
exports.viewWidth = w;
exports.viewHeight = h;
this.fps = 58;
this.frames = 0;
if (this.capWidth || this.capHeight) {
var w2 = Math.min(this.capWidth || Infinity, w);
var h2 = Math.min(this.capHeight || Infinity, h);
var scale = Math.min(w2 / w, h2 / h);
w *= scale;
h *= scale;
}
w = Math.round(w);
h = Math.round(h);
// Ensure even size for 2x2 upscale
var renderW = w + (w & 1);
var renderH = h + (h & 1);
var renderAspect = this.aspect = renderW / renderH;
if (exports.width != renderW || exports.height != renderH) {
exports.width = renderW;
exports.height = renderH;
exports.aspect = renderAspect;
_.each(this.effects, function (effect) {
effect.resize(exports);
});
this.renderer.setSize(renderW, renderH, false);
var el;
if (el = this.renderer.domElement) {
// Fix odd width/height if rendering at native ress.
var ml = 0, mt = 0;
if (vw + 1 == renderW) {
ml = -1;
vw = renderW;
}
if (vh + 1 == renderH) {
mt = -1;
vh = renderH;
}
el.style.marginLeft = ml + "px";
el.style.marginTop = mt + "px";
el.style.width = vw + "px";
el.style.height = vh + "px";
}
}
},
tick: function () {
// Delay clock because first few frames are slow
var exports = this.exports;
var now = (this.warmUp-- > 0) ? 0 : Time.clock(this.type);
var delta = now - exports.time;
if (!Time.isBackground() && ++this.frames > 5) {
var fps = 1 / Math.max(.0166, delta);
this.fps = Time.isSlow() ? 60 : (this.fps + (fps - this.fps) * .15);
}
if (exports) {
exports.delta = delta;
exports.time = now;
exports.fps = this.fps;
}
_.each(this.effects, function (effect) {
effect.tick(exports);
}.bind(this));
},
visible: function (visible) {
if (visible !== undefined) {
this.isVisible = visible;
}
return this.isVisible;
},
update: function () {
var exports = this.exports;
if (this.skipFrames-- > 0) return;
if (this.lastVisible != this.isVisible) {
this.renderer.domElement.style.display = this.isVisible ? 'block' : 'none';
this.lastVisible = this.isVisible;
}
if (!this.visible) return;
if (this.autoSize && this.fps < 48) {
this.capWidth = Math.max(960, this.capWidth || window.innerWidth);
this.capHeight = Math.max(540, this.capHeight || window.innerHeight);
this.capWidth *= .9;
this.capHeight *= .9;
this.resize();
}
_.each(this.effects, function (effect) {
effect.update(exports);
}.bind(this));
if (this.autoRender) {
this.renderer.render(this.scene, this.camera);
}
},
}