This repository has been archived by the owner on Oct 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
PlatformManager.js
156 lines (126 loc) · 4.69 KB
/
PlatformManager.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
/**
*
* You can modify and use this source freely
* only for the development of application related Live2D.
*
* (c) Live2D Inc. All rights reserved.
*/
import {getContext} from "./webglcontext"
//============================================================
//============================================================
// class PlatformManager extend IPlatformManager
//============================================================
//============================================================
export default function PlatformManager()
{
}
//============================================================
// PlatformManager # loadBytes()
//============================================================
PlatformManager.prototype.loadBytes = function(path/*String*/, callback)
{
var request = new XMLHttpRequest();
request.open("GET", path, true);
request.responseType = "arraybuffer";
request.onload = function(){
switch(request.status){
case 200:
callback(request.response);
break;
default:
console.error("Failed to load (" + request.status + ") : " + path);
break;
}
}
request.send(null);
//return request;
}
//============================================================
// PlatformManager # loadString()
//============================================================
PlatformManager.prototype.loadString = function(path/*String*/)
{
this.loadBytes(path, function(buf) {
return buf;
});
}
//============================================================
// PlatformManager # loadLive2DModel()
//============================================================
PlatformManager.prototype.loadLive2DModel = function(path/*String*/, callback)
{
var model = null;
// load moc
this.loadBytes(path, function(buf){
model = Live2DModelWebGL.loadModel(buf);
callback(model);
});
}
//============================================================
// PlatformManager # loadTexture()
//============================================================
PlatformManager.prototype.loadTexture = function(model/*ALive2DModel*/, no/*int*/, path/*String*/, callback)
{
// load textures
var loadedImage = new Image();
loadedImage.src = path;
var thisRef = this;
loadedImage.onload = function() {
// create texture
var gl = getContext();
var texture = gl.createTexture();
if (!texture){ console.error("Failed to generate gl texture name."); return -1; }
if(model.isPremultipliedAlpha() == false){
// 乗算済アルファテクスチャ以外の場合
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 1);
}
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA,
gl.UNSIGNED_BYTE, loadedImage);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST);
gl.generateMipmap(gl.TEXTURE_2D);
model.setTexture(no, texture);
// テクスチャオブジェクトを解放
texture = null;
if (typeof callback == "function") callback();
};
loadedImage.onerror = function() {
console.error("Failed to load image : " + path);
}
}
//============================================================
// PlatformManager # parseFromBytes(buf)
//============================================================
PlatformManager.prototype.jsonParseFromBytes = function(buf){
var jsonStr;
var bomCode = new Uint8Array(buf, 0, 3);
if (bomCode[0] == 239 && bomCode[1] == 187 && bomCode[2] == 191) {
jsonStr = String.fromCharCode.apply(null, new Uint8Array(buf, 3));
} else {
jsonStr = String.fromCharCode.apply(null, new Uint8Array(buf));
}
var jsonObj = JSON.parse(jsonStr);
return jsonObj;
};
//============================================================
// PlatformManager # log()
//============================================================
PlatformManager.prototype.log = function(txt/*String*/)
{
//console.log(txt);
}
function getWebGLContext(canvas)
{
var NAMES = [ "webgl" , "experimental-webgl" , "webkit-3d" , "moz-webgl"];
for( var i = 0; i < NAMES.length; i++ ){
try{
var ctx = canvas.getContext(NAMES[i], {premultipliedAlpha : true});
if(ctx) return ctx;
}
catch(e){}
}
return null;
};