lappsubdelegate.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /**
  2. * Copyright(c) Live2D Inc. All rights reserved.
  3. *
  4. * Use of this source code is governed by the Live2D Open Software license
  5. * that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
  6. */
  7. import * as LAppDefine from './lappdefine';
  8. import { LAppGlManager } from './lappglmanager';
  9. import { LAppLive2DManager } from './lapplive2dmanager';
  10. import { LAppPal } from './lapppal';
  11. import { LAppTextureManager } from './lapptexturemanager';
  12. import { LAppView } from './lappview';
  13. import { ResourceModel } from '@/lib/protocol';
  14. /**
  15. * Canvasに関連する操作を取りまとめるクラス
  16. */
  17. export class LAppSubdelegate {
  18. /**
  19. * コンストラクタ
  20. */
  21. public constructor() {
  22. this._canvas = null;
  23. this._glManager = new LAppGlManager();
  24. this._textureManager = new LAppTextureManager();
  25. this._live2dManager = new LAppLive2DManager();
  26. this._view = new LAppView();
  27. this._frameBuffer = null;
  28. this._captured = false;
  29. }
  30. /**
  31. * デストラクタ相当の処理
  32. */
  33. public release(): void {
  34. this._resizeObserver.unobserve(this._canvas);
  35. this._resizeObserver.disconnect();
  36. this._resizeObserver = null;
  37. this._live2dManager.release();
  38. this._live2dManager = null;
  39. this._view.release();
  40. this._view = null;
  41. this._textureManager.release();
  42. this._textureManager = null;
  43. this._glManager.release();
  44. this._glManager = null;
  45. }
  46. /**
  47. * APPに必要な物を初期化する。
  48. */
  49. public initialize(canvas: HTMLCanvasElement): boolean {
  50. if (!this._glManager.initialize(canvas)) {
  51. return false;
  52. }
  53. this._canvas = canvas;
  54. if (LAppDefine.CanvasSize === 'auto') {
  55. this.resizeCanvas();
  56. } else {
  57. canvas.width = LAppDefine.CanvasSize.width;
  58. canvas.height = LAppDefine.CanvasSize.height;
  59. }
  60. this._textureManager.setGlManager(this._glManager);
  61. const gl = this._glManager.getGl();
  62. if (!this._frameBuffer) {
  63. this._frameBuffer = gl.getParameter(gl.FRAMEBUFFER_BINDING);
  64. }
  65. // 透過設定
  66. gl.enable(gl.BLEND);
  67. gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
  68. // AppViewの初期化
  69. this._view.initialize(this);
  70. this._view.initializeSprite();
  71. this._live2dManager.initialize(this);
  72. this._resizeObserver = new ResizeObserver(
  73. (entries: ResizeObserverEntry[], observer: ResizeObserver) =>
  74. this.resizeObserverCallback.call(this, entries, observer)
  75. );
  76. this._resizeObserver.observe(this._canvas);
  77. return true;
  78. }
  79. /**
  80. * Resize canvas and re-initialize view.
  81. */
  82. public onResize(): void {
  83. this.resizeCanvas();
  84. this._view.initialize(this);
  85. this._view.initializeSprite();
  86. }
  87. private resizeObserverCallback(
  88. entries: ResizeObserverEntry[],
  89. observer: ResizeObserver
  90. ): void {
  91. if (LAppDefine.CanvasSize === 'auto') {
  92. this._needResize = true;
  93. }
  94. }
  95. /**
  96. * ループ処理
  97. */
  98. public update(): void {
  99. if (this._glManager.getGl().isContextLost()) {
  100. return;
  101. }
  102. // キャンバスのサイズが変わっている場合はリサイズに必要な処理をする。
  103. if (this._needResize) {
  104. this.onResize();
  105. this._needResize = false;
  106. }
  107. const gl = this._glManager.getGl();
  108. // 画面的初始化.默认就是白色(rgb: 1.0, 1.0, 1.0)且不透明(alpha: 1.0)
  109. gl.clearColor(0.0, 0.0, 0.0, 0.0);
  110. // 启用深度测试
  111. gl.enable(gl.DEPTH_TEST);
  112. // 附近的物体会掩盖远处的物体(也就是说如果近的点已经被渲染了,一个新的像素更远就不用渲染了)
  113. gl.depthFunc(gl.LEQUAL);
  114. // 清除颜色缓冲区和深度缓冲区,每次渲染的时候都做清除处理
  115. gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  116. // 设置深度缓冲区的初始值1.0, 也就是大家都是最远的初始化值
  117. gl.clearDepth(1.0);
  118. // 透明设置
  119. gl.enable(gl.BLEND);
  120. gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
  121. // 描画更新
  122. this._view.render();
  123. }
  124. /**
  125. * シェーダーを登録する。
  126. */
  127. public createShader(): WebGLProgram {
  128. const gl = this._glManager.getGl();
  129. // バーテックスシェーダーのコンパイル
  130. const vertexShaderId = gl.createShader(gl.VERTEX_SHADER);
  131. if (vertexShaderId == null) {
  132. LAppPal.printMessage('failed to create vertexShader');
  133. return null;
  134. }
  135. const vertexShader: string =
  136. 'precision mediump float;' +
  137. 'attribute vec3 position;' +
  138. 'attribute vec2 uv;' +
  139. 'varying vec2 vuv;' +
  140. 'void main(void)' +
  141. '{' +
  142. ' gl_Position = vec4(position, 1.0);' +
  143. ' vuv = uv;' +
  144. '}';
  145. gl.shaderSource(vertexShaderId, vertexShader);
  146. gl.compileShader(vertexShaderId);
  147. // フラグメントシェーダのコンパイル
  148. const fragmentShaderId = gl.createShader(gl.FRAGMENT_SHADER);
  149. if (fragmentShaderId == null) {
  150. LAppPal.printMessage('failed to create fragmentShader');
  151. return null;
  152. }
  153. const fragmentShader: string =
  154. 'precision mediump float;' +
  155. 'varying vec2 vuv;' +
  156. 'uniform sampler2D texture;' +
  157. 'void main(void)' +
  158. '{' +
  159. ' gl_FragColor = texture2D(texture, vuv);' +
  160. '}';
  161. gl.shaderSource(fragmentShaderId, fragmentShader);
  162. gl.compileShader(fragmentShaderId);
  163. // プログラムオブジェクトの作成
  164. const programId = gl.createProgram();
  165. gl.attachShader(programId, vertexShaderId);
  166. gl.attachShader(programId, fragmentShaderId);
  167. gl.deleteShader(vertexShaderId);
  168. gl.deleteShader(fragmentShaderId);
  169. // リンク
  170. gl.linkProgram(programId);
  171. gl.useProgram(programId);
  172. return programId;
  173. }
  174. public getTextureManager(): LAppTextureManager {
  175. return this._textureManager;
  176. }
  177. public getFrameBuffer(): WebGLFramebuffer {
  178. return this._frameBuffer;
  179. }
  180. public getCanvas(): HTMLCanvasElement {
  181. return this._canvas;
  182. }
  183. public getGlManager(): LAppGlManager {
  184. return this._glManager;
  185. }
  186. public getLive2DManager(): LAppLive2DManager {
  187. return this._live2dManager;
  188. }
  189. /**
  190. * Resize the canvas to fill the screen.
  191. */
  192. private resizeCanvas(): void {
  193. this._canvas.width = this._canvas.clientWidth * window.devicePixelRatio;
  194. this._canvas.height = this._canvas.clientHeight * window.devicePixelRatio;
  195. const gl = this._glManager.getGl();
  196. gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
  197. }
  198. /**
  199. * マウスダウン、タッチダウンしたときに呼ばれる。
  200. */
  201. public onPointBegan(pageX: number, pageY: number): void {
  202. if (!this._view) {
  203. LAppPal.printMessage('view notfound');
  204. return;
  205. }
  206. this._captured = true;
  207. const localX: number = pageX - this._canvas.offsetLeft;
  208. const localY: number = pageY - this._canvas.offsetTop;
  209. this._view.onTouchesBegan(localX, localY);
  210. }
  211. /**
  212. * マウスポインタが動いたら呼ばれる。
  213. */
  214. public onPointMoved(pageX: number, pageY: number): void {
  215. if (!this._captured) {
  216. return;
  217. }
  218. const localX: number = pageX - this._canvas.offsetLeft;
  219. const localY: number = pageY - this._canvas.offsetTop;
  220. this._view.onTouchesMoved(localX, localY);
  221. }
  222. /**
  223. * クリックが終了したら呼ばれる。
  224. */
  225. public onPointEnded(pageX: number, pageY: number): void {
  226. this._captured = false;
  227. if (!this._view) {
  228. LAppPal.printMessage('view notfound');
  229. return;
  230. }
  231. const localX: number = pageX - this._canvas.offsetLeft;
  232. const localY: number = pageY - this._canvas.offsetTop;
  233. this._view.onTouchesEnded(localX, localY);
  234. }
  235. /**
  236. * タッチがキャンセルされると呼ばれる。
  237. */
  238. public onTouchCancel(pageX: number, pageY: number): void {
  239. this._captured = false;
  240. if (!this._view) {
  241. LAppPal.printMessage('view notfound');
  242. return;
  243. }
  244. const localX: number = pageX - this._canvas.offsetLeft;
  245. const localY: number = pageY - this._canvas.offsetTop;
  246. this._view.onTouchesEnded(localX, localY);
  247. }
  248. public isContextLost(): boolean {
  249. return this._glManager.getGl().isContextLost();
  250. }
  251. public changeCharacter(character: ResourceModel | null) {
  252. // _subdelegates中只有一个画布, 所以设置第一个即可
  253. this._live2dManager.changeCharacter(character);
  254. }
  255. private _canvas: HTMLCanvasElement;
  256. /**
  257. * View情報
  258. */
  259. private _view: LAppView;
  260. /**
  261. * テクスチャマネージャー
  262. */
  263. private _textureManager: LAppTextureManager;
  264. private _frameBuffer: WebGLFramebuffer;
  265. private _glManager: LAppGlManager;
  266. private _live2dManager: LAppLive2DManager;
  267. /**
  268. * ResizeObserver
  269. */
  270. private _resizeObserver: ResizeObserver;
  271. /**
  272. * クリックしているか
  273. */
  274. private _captured: boolean;
  275. private _needResize: boolean;
  276. }