-
Notifications
You must be signed in to change notification settings - Fork 4
/
renderer.js
216 lines (166 loc) · 5.55 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
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.
'use strict';
const gkCore = require('./lib/gk-core');
const logger = require('./lib/gk-logger');
const glw = require('./lib/gk-glwrap');
const mouse = require('./lib/gk-mouseorbit');
const resMgr = require('./lib/gk-resmgr');
const resPanel = require('./lib/gk-respanel');
const Framework = require('./lib/gk-framework');
const path = require('path');
// initial
window.onload = function(){
// init logger
logger.init(bid('console-log-container'));
logger.info('shadermonki started.');
// initialize rendering
var canvas = bid('canvas');
glw.initGL(canvas);
switch_sceneview(false);
if(!glw.ready){console.log('initialize error'); return;}
// initialize mouse orbit
mouse.init(canvas);
// filter it and put it into resource manager
resPanel.init(bid('res-container'), __dirname);
resPanel.rescan_resources();
resPanel.reconstruct_filetree();
resPanel.refresh();
// temporary feature
let holder = bid('mesh-holder');
let current_proj = '';
holder.ondrop = function( ev ) {
ev.preventDefault();
var filetoken = ev.dataTransfer.getData("restoken");
let type = 0;
let resobj = resMgr.gResmgr.get_res(filetoken);
if( resobj !== null )
{
current_proj = path.dirname(filetoken) + '/';
type = resobj.get_type();
if(type === resMgr.RESTYPE.MESH)
{
//create a mesh element
let _ret = resPanel.create_res_showobj(0, filetoken, type);
resPanel.clean_folder(this);
this.appendChild(_ret.obj_container);
// load mesh here
if(!resobj.loaded)
{
resobj.load( onFinish => {
framework.bindShowObj( resobj.gameObject );
} );
}
else
{
framework.bindShowObj( resobj.gameObject );
}
resPanel.refresh();
}
}
}
holder.ondragover = function (ev) {
ev.preventDefault();
}
let imgholder = bid('image-holder');
imgholder.ondrop = function( ev ) {
ev.preventDefault();
var filetoken = ev.dataTransfer.getData("restoken");
let type = 0;
let resobj = resMgr.gResmgr.get_res(filetoken);
if( resobj !== null )
{
type = resobj.get_type();
if(type === resMgr.RESTYPE.TEXTURE)
{
//create a mesh element
let _ret = resPanel.create_res_showobj(0, filetoken, type, false);
resPanel.clean_folder(this);
this.appendChild(_ret.obj_container);
// load mesh here
resobj.load();
resPanel.refresh();
//window.parent.renderer.updateTexure(resobj);
}
}
}
imgholder.ondragover = function (ev) {
ev.preventDefault();
}
// framework start
window.framework = new Framework();
window.framework.init();
// start and loop
this.running = true;
refresh_playbtn('btn_play', 'btn_play', window.running);
// create render loop
render_loop();
function render_loop()
{
if(window.running) {
window.framework.update();
}
requestAnimationFrame(render_loop);
}
// btn control
{
var playBtn = bid('btn_play');
playBtn.onclick = function () {
window.running = !window.running;
refresh_playbtn('btn_play', 'btn_play', window.running);
}
}
{
// btn control
let playBtn = bid('btn_auto_rotate');
playBtn.onclick = function () {
//refresh_playbtn('btn_auto_rotate', 'btn_square', renderer.autoRotate);
}
}
this.fullscreen = false;
{
// btn control
let targetBtn = bid('btn_next');
targetBtn.onclick = function () {
window.fullscreen = !window.fullscreen;
switch_sceneview(window.fullscreen);
}
}
};
function refresh_playbtn( element_id, element_class, swt ) {
var element = bid(element_id);
if ( swt ) {
element.className = element_class + '_on';
}
else {
element.className = element_class;
}
}
function switch_sceneview( fsmode ) {
var canvas_fsholder = bid('fullscreen');
var canvas = bid('canvas');
var canvas_defaultHolder = bid('sceneview');
//canvas_fsholder.style.visibility = 'hidden';
//canvas_fsholder.appendChild(canvas);
//canvas_defaultHolder.appendChild(canvas);
if( fsmode ) {
canvas_fsholder.style.visibility = 'visible';
canvas_fsholder.appendChild(canvas);
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
if( window.framework != undefined ) {
window.framework.renderer.resize( window.innerWidth, window.innerHeight );
}
}
else {
canvas_fsholder.style.visibility = 'hidden';
canvas_defaultHolder.appendChild(canvas);
canvas.width = 512;
canvas.height =512;
if( window.framework != undefined ) {
window.framework.renderer.resize(512, 512);
}
}
}
function bid(id){return document.getElementById(id);}