@zenvor/view360 4.0.0-beta.7-fork.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (261) hide show
  1. package/LICENSE +19 -0
  2. package/README.md +88 -0
  3. package/css/base.css +19 -0
  4. package/css/base.min.css +1 -0
  5. package/css/control-bar.css +329 -0
  6. package/css/control-bar.min.css +1 -0
  7. package/css/helper.css +51 -0
  8. package/css/helper.min.css +1 -0
  9. package/css/hotspot.css +21 -0
  10. package/css/hotspot.min.css +1 -0
  11. package/css/loading-spinner.css +37 -0
  12. package/css/loading-spinner.min.css +1 -0
  13. package/css/variable.css +1 -0
  14. package/css/variable.min.css +0 -0
  15. package/css/view360.css +468 -0
  16. package/css/view360.min.css +1 -0
  17. package/css/vr.css +8 -0
  18. package/css/vr.min.css +1 -0
  19. package/declaration/View360.d.ts +150 -0
  20. package/declaration/cfc/const.d.ts +1 -0
  21. package/declaration/cfc/index.d.ts +4 -0
  22. package/declaration/cfc/utils.d.ts +1 -0
  23. package/declaration/cfc/withMethods.d.ts +2 -0
  24. package/declaration/const/browser.d.ts +71 -0
  25. package/declaration/const/error.d.ts +47 -0
  26. package/declaration/const/external.d.ts +33 -0
  27. package/declaration/const/internal.d.ts +34 -0
  28. package/declaration/control/CameraControl.d.ts +10 -0
  29. package/declaration/control/GyroControl.d.ts +30 -0
  30. package/declaration/control/PanoControl.d.ts +58 -0
  31. package/declaration/control/RotateControl.d.ts +75 -0
  32. package/declaration/control/ZoomControl.d.ts +45 -0
  33. package/declaration/control/index.d.ts +7 -0
  34. package/declaration/control/input/GyroInput.d.ts +46 -0
  35. package/declaration/control/input/KeyboardInput.d.ts +21 -0
  36. package/declaration/control/input/MouseInput.d.ts +16 -0
  37. package/declaration/control/input/PinchInput.d.ts +14 -0
  38. package/declaration/control/input/PointerInput.d.ts +18 -0
  39. package/declaration/control/input/TouchInput.d.ts +21 -0
  40. package/declaration/control/input/WheelInput.d.ts +16 -0
  41. package/declaration/core/AutoResizer.d.ts +13 -0
  42. package/declaration/core/Autoplay.d.ts +54 -0
  43. package/declaration/core/Camera.d.ts +88 -0
  44. package/declaration/core/CameraAnimation.d.ts +25 -0
  45. package/declaration/core/CameraPath.d.ts +55 -0
  46. package/declaration/core/CubeTexturePainter.d.ts +16 -0
  47. package/declaration/core/FrameAnimator.d.ts +13 -0
  48. package/declaration/core/Motion.d.ts +39 -0
  49. package/declaration/core/Object3D.d.ts +21 -0
  50. package/declaration/core/ShaderProgram.d.ts +10 -0
  51. package/declaration/core/TextureLoader.d.ts +26 -0
  52. package/declaration/core/TriangleMesh.d.ts +20 -0
  53. package/declaration/core/VertexArrayObject.d.ts +14 -0
  54. package/declaration/core/VertexData.d.ts +8 -0
  55. package/declaration/core/View360Error.d.ts +5 -0
  56. package/declaration/core/WebGLContext.d.ts +74 -0
  57. package/declaration/core/WebGLRenderer.d.ts +26 -0
  58. package/declaration/core/XRManager.d.ts +33 -0
  59. package/declaration/core/index.d.ts +12 -0
  60. package/declaration/geometry/CubeGeometry.d.ts +9 -0
  61. package/declaration/geometry/CylinderGeometry.d.ts +5 -0
  62. package/declaration/geometry/Geometry.d.ts +8 -0
  63. package/declaration/geometry/PlaneGeometry.d.ts +5 -0
  64. package/declaration/geometry/SphereGeometry.d.ts +5 -0
  65. package/declaration/hotspot/Hotspot.d.ts +7 -0
  66. package/declaration/hotspot/HotspotRenderer.d.ts +17 -0
  67. package/declaration/hotspot/index.d.ts +4 -0
  68. package/declaration/index.cjs.d.ts +3 -0
  69. package/declaration/index.d.ts +11 -0
  70. package/declaration/index.umd.d.ts +2 -0
  71. package/declaration/plugin/ControlBar/AutoHide.d.ts +42 -0
  72. package/declaration/plugin/ControlBar/ControlBar.d.ts +113 -0
  73. package/declaration/plugin/ControlBar/ControlBarItem.d.ts +15 -0
  74. package/declaration/plugin/ControlBar/FullscreenButton.d.ts +19 -0
  75. package/declaration/plugin/ControlBar/GyroButton.d.ts +14 -0
  76. package/declaration/plugin/ControlBar/PieView.d.ts +26 -0
  77. package/declaration/plugin/ControlBar/PlayButton.d.ts +16 -0
  78. package/declaration/plugin/ControlBar/ProgressBar.d.ts +23 -0
  79. package/declaration/plugin/ControlBar/RangeControl.d.ts +27 -0
  80. package/declaration/plugin/ControlBar/VRButton.d.ts +12 -0
  81. package/declaration/plugin/ControlBar/VideoControl.d.ts +11 -0
  82. package/declaration/plugin/ControlBar/VideoTime.d.ts +17 -0
  83. package/declaration/plugin/ControlBar/VolumeControl.d.ts +23 -0
  84. package/declaration/plugin/ControlBar/const.d.ts +41 -0
  85. package/declaration/plugin/ControlBar/index.d.ts +11 -0
  86. package/declaration/plugin/LoadingSpinner/LoadingSpinner.d.ts +22 -0
  87. package/declaration/plugin/View360Plugin.d.ts +6 -0
  88. package/declaration/plugin/index.d.ts +5 -0
  89. package/declaration/projection/CorrectionPass.d.ts +26 -0
  90. package/declaration/projection/CubemapProjection.d.ts +19 -0
  91. package/declaration/projection/CubestripProjection.d.ts +18 -0
  92. package/declaration/projection/CylindricalProjection.d.ts +22 -0
  93. package/declaration/projection/EquiangularProjection.d.ts +13 -0
  94. package/declaration/projection/EquirectProjection.d.ts +15 -0
  95. package/declaration/projection/LittlePlanetProjection.d.ts +21 -0
  96. package/declaration/projection/Projection.d.ts +19 -0
  97. package/declaration/projection/StereoEquiProjection.d.ts +25 -0
  98. package/declaration/projection/WideAngleCorrectionProjection.d.ts +27 -0
  99. package/declaration/projection/WideAngleRealtimeProjection.d.ts +50 -0
  100. package/declaration/projection/index.d.ts +12 -0
  101. package/declaration/texture/Texture.d.ts +18 -0
  102. package/declaration/texture/Texture2D.d.ts +11 -0
  103. package/declaration/texture/TextureCube.d.ts +12 -0
  104. package/declaration/texture/TextureRenderTarget.d.ts +8 -0
  105. package/declaration/texture/TextureVideo.d.ts +11 -0
  106. package/declaration/type/events.d.ts +74 -0
  107. package/declaration/type/external.d.ts +13 -0
  108. package/declaration/type/internal.d.ts +47 -0
  109. package/declaration/type/utils.d.ts +11 -0
  110. package/declaration/uniform/Uniform.d.ts +7 -0
  111. package/declaration/uniform/UniformBoolean.d.ts +7 -0
  112. package/declaration/uniform/UniformCanvasCube.d.ts +12 -0
  113. package/declaration/uniform/UniformFloat.d.ts +7 -0
  114. package/declaration/uniform/UniformInt.d.ts +7 -0
  115. package/declaration/uniform/UniformMatrix.d.ts +8 -0
  116. package/declaration/uniform/UniformTexture2D.d.ts +11 -0
  117. package/declaration/uniform/UniformTextureCube.d.ts +12 -0
  118. package/declaration/uniform/UniformTextureRenderTarget.d.ts +11 -0
  119. package/declaration/uniform/UniformVec2.d.ts +7 -0
  120. package/declaration/uniform/UniformVec3.d.ts +7 -0
  121. package/declaration/uniform/UniformVector4Array.d.ts +7 -0
  122. package/declaration/utils.d.ts +25 -0
  123. package/dist/view360.cjs.js +9392 -0
  124. package/dist/view360.cjs.js.map +1 -0
  125. package/dist/view360.esm.js +9274 -0
  126. package/dist/view360.esm.js.map +1 -0
  127. package/dist/view360.js +9350 -0
  128. package/dist/view360.js.map +1 -0
  129. package/dist/view360.min.js +2 -0
  130. package/dist/view360.min.js.map +1 -0
  131. package/dist/view360.pkgd.js +48537 -0
  132. package/dist/view360.pkgd.js.map +1 -0
  133. package/dist/view360.pkgd.min.js +16 -0
  134. package/dist/view360.pkgd.min.js.map +1 -0
  135. package/package.json +110 -0
  136. package/sass/base.sass +17 -0
  137. package/sass/control-bar.sass +284 -0
  138. package/sass/helper.sass +51 -0
  139. package/sass/hotspot.sass +20 -0
  140. package/sass/loading-spinner.sass +34 -0
  141. package/sass/variable.sass +2 -0
  142. package/sass/view360.sass +6 -0
  143. package/sass/vr.sass +10 -0
  144. package/src/View360.ts +1220 -0
  145. package/src/cfc/const.ts +15 -0
  146. package/src/cfc/index.ts +8 -0
  147. package/src/cfc/utils.ts +12 -0
  148. package/src/cfc/withMethods.ts +40 -0
  149. package/src/const/browser.ts +109 -0
  150. package/src/const/error.ts +83 -0
  151. package/src/const/external.ts +77 -0
  152. package/src/const/internal.ts +53 -0
  153. package/src/control/CameraControl.ts +53 -0
  154. package/src/control/GyroControl.ts +225 -0
  155. package/src/control/PanoControl.ts +434 -0
  156. package/src/control/RotateControl.ts +411 -0
  157. package/src/control/ZoomControl.ts +254 -0
  158. package/src/control/index.ts +24 -0
  159. package/src/control/input/GyroInput.ts +315 -0
  160. package/src/control/input/KeyboardInput.ts +151 -0
  161. package/src/control/input/MouseInput.ts +102 -0
  162. package/src/control/input/PinchInput.ts +101 -0
  163. package/src/control/input/PointerInput.ts +118 -0
  164. package/src/control/input/TouchInput.ts +138 -0
  165. package/src/control/input/WheelInput.ts +89 -0
  166. package/src/core/AutoResizer.ts +92 -0
  167. package/src/core/Autoplay.ts +326 -0
  168. package/src/core/Camera.ts +616 -0
  169. package/src/core/CameraAnimation.ts +110 -0
  170. package/src/core/CameraPath.ts +454 -0
  171. package/src/core/CubeTexturePainter.ts +95 -0
  172. package/src/core/FrameAnimator.ts +69 -0
  173. package/src/core/Motion.ts +205 -0
  174. package/src/core/Object3D.ts +81 -0
  175. package/src/core/ShaderProgram.ts +21 -0
  176. package/src/core/TextureLoader.ts +303 -0
  177. package/src/core/TriangleMesh.ts +54 -0
  178. package/src/core/VertexArrayObject.ts +29 -0
  179. package/src/core/VertexData.ts +23 -0
  180. package/src/core/View360Error.ts +35 -0
  181. package/src/core/WebGLContext.ts +558 -0
  182. package/src/core/WebGLRenderer.ts +205 -0
  183. package/src/core/XRManager.ts +193 -0
  184. package/src/core/index.ts +35 -0
  185. package/src/geometry/CubeGeometry.ts +121 -0
  186. package/src/geometry/CylinderGeometry.ts +49 -0
  187. package/src/geometry/Geometry.ts +23 -0
  188. package/src/geometry/PlaneGeometry.ts +36 -0
  189. package/src/geometry/SphereGeometry.ts +55 -0
  190. package/src/hotspot/Hotspot.ts +32 -0
  191. package/src/hotspot/HotspotRenderer.ts +155 -0
  192. package/src/hotspot/index.ts +11 -0
  193. package/src/index.cjs.ts +13 -0
  194. package/src/index.ts +21 -0
  195. package/src/index.umd.ts +10 -0
  196. package/src/plugin/ControlBar/AutoHide.ts +246 -0
  197. package/src/plugin/ControlBar/ControlBar.ts +564 -0
  198. package/src/plugin/ControlBar/ControlBarItem.ts +84 -0
  199. package/src/plugin/ControlBar/FullscreenButton.ts +145 -0
  200. package/src/plugin/ControlBar/GyroButton.ts +124 -0
  201. package/src/plugin/ControlBar/PieView.ts +195 -0
  202. package/src/plugin/ControlBar/PlayButton.ts +130 -0
  203. package/src/plugin/ControlBar/ProgressBar.ts +175 -0
  204. package/src/plugin/ControlBar/RangeControl.ts +165 -0
  205. package/src/plugin/ControlBar/VRButton.ts +78 -0
  206. package/src/plugin/ControlBar/VideoControl.ts +79 -0
  207. package/src/plugin/ControlBar/VideoTime.ts +120 -0
  208. package/src/plugin/ControlBar/VolumeControl.ts +220 -0
  209. package/src/plugin/ControlBar/const.ts +72 -0
  210. package/src/plugin/ControlBar/index.ts +31 -0
  211. package/src/plugin/LoadingSpinner/LoadingSpinner.ts +116 -0
  212. package/src/plugin/View360Plugin.ts +30 -0
  213. package/src/plugin/index.ts +16 -0
  214. package/src/projection/CorrectionPass.ts +161 -0
  215. package/src/projection/CubemapProjection.ts +92 -0
  216. package/src/projection/CubestripProjection.ts +85 -0
  217. package/src/projection/CylindricalProjection.ts +123 -0
  218. package/src/projection/EquiangularProjection.ts +52 -0
  219. package/src/projection/EquirectProjection.ts +56 -0
  220. package/src/projection/LittlePlanetProjection.ts +82 -0
  221. package/src/projection/Projection.ts +103 -0
  222. package/src/projection/StereoEquiProjection.ts +104 -0
  223. package/src/projection/WideAngleCorrectionProjection.ts +307 -0
  224. package/src/projection/WideAngleRealtimeProjection.ts +653 -0
  225. package/src/projection/index.ts +36 -0
  226. package/src/shader/common.frag +7 -0
  227. package/src/shader/common.vert +12 -0
  228. package/src/shader/correction.frag +110 -0
  229. package/src/shader/cube.frag +8 -0
  230. package/src/shader/cube.vert +12 -0
  231. package/src/shader/eac.frag +40 -0
  232. package/src/shader/fullscreen.vert +7 -0
  233. package/src/shader/little-planet.frag +47 -0
  234. package/src/shader/little-planet.vert +12 -0
  235. package/src/shader/stereoequi.vert +15 -0
  236. package/src/shader/wideangle_realtime.frag +109 -0
  237. package/src/shader/wideangle_realtime.vert +17 -0
  238. package/src/shim.d.ts +9 -0
  239. package/src/texture/Texture.ts +47 -0
  240. package/src/texture/Texture2D.ts +34 -0
  241. package/src/texture/TextureCube.ts +36 -0
  242. package/src/texture/TextureRenderTarget.ts +33 -0
  243. package/src/texture/TextureVideo.ts +56 -0
  244. package/src/type/events.ts +389 -0
  245. package/src/type/external.ts +78 -0
  246. package/src/type/internal.ts +54 -0
  247. package/src/type/utils.ts +27 -0
  248. package/src/types/hls.d.ts +78 -0
  249. package/src/uniform/Uniform.ts +20 -0
  250. package/src/uniform/UniformBoolean.ts +23 -0
  251. package/src/uniform/UniformCanvasCube.ts +44 -0
  252. package/src/uniform/UniformFloat.ts +23 -0
  253. package/src/uniform/UniformInt.ts +25 -0
  254. package/src/uniform/UniformMatrix.ts +24 -0
  255. package/src/uniform/UniformTexture2D.ts +46 -0
  256. package/src/uniform/UniformTextureCube.ts +51 -0
  257. package/src/uniform/UniformTextureRenderTarget.ts +55 -0
  258. package/src/uniform/UniformVec2.ts +25 -0
  259. package/src/uniform/UniformVec3.ts +25 -0
  260. package/src/uniform/UniformVector4Array.ts +23 -0
  261. package/src/utils.ts +217 -0
@@ -0,0 +1,23 @@
1
+ /*
2
+ * Copyright (c) 2023-present NAVER Corp.
3
+ * egjs projects are licensed under the MIT license
4
+ */
5
+ import { TypedArray } from "../type/utils";
6
+
7
+ /**
8
+ * @hidden
9
+ */
10
+ class VertexData<T extends TypedArray> {
11
+ public readonly data: T;
12
+ public itemSize: number;
13
+ public count: number;
14
+
15
+ /** */
16
+ public constructor(data: T, itemSize: number) {
17
+ this.data = data;
18
+ this.itemSize = itemSize;
19
+ this.count = data.length / itemSize;
20
+ }
21
+ }
22
+
23
+ export default VertexData;
@@ -0,0 +1,35 @@
1
+ /*
2
+ * Copyright (c) 2023-present NAVER Corp.
3
+ * egjs projects are licensed under the MIT license
4
+ */
5
+
6
+ /**
7
+ * Error thrown by {@link View360}
8
+ * @ko {@link View360}이 발생시킨 에러
9
+ * @since 4.0.0
10
+ */
11
+ class View360Error extends Error {
12
+ /**
13
+ * Error code
14
+ * @ko 에러 코드
15
+ * @see ERROR_CODES
16
+ */
17
+ public code: number;
18
+
19
+ /**
20
+ * Create new instance of View360Error
21
+ * @ko View360Error의 인스턴스를 생성합니다.
22
+ * @param message - Error message {@ko 에러 메시지}
23
+ * @param code - Error code {@ko 에러 코드}
24
+ */
25
+ public constructor(message: string, code: number) {
26
+ super(message);
27
+
28
+ Object.setPrototypeOf(this, View360Error.prototype);
29
+
30
+ this.name = "View360Error";
31
+ this.code = code;
32
+ }
33
+ }
34
+
35
+ export default View360Error;
@@ -0,0 +1,558 @@
1
+ /*
2
+ * Copyright (c) 2023-present NAVER Corp.
3
+ * egjs projects are licensed under the MIT license
4
+ */
5
+ import { mat4 } from "gl-matrix";
6
+ import Uniform from "../uniform/Uniform";
7
+ import Camera from "./Camera";
8
+ import Object3D from "./Object3D";
9
+ import ShaderProgram from "./ShaderProgram";
10
+ import View360Error from "./View360Error";
11
+ import VertexArrayObject from "./VertexArrayObject";
12
+ import VertexData from "./VertexData";
13
+ import Texture from "../texture/Texture";
14
+ import Geometry from "../geometry/Geometry";
15
+ import * as BROWSER from "../const/browser";
16
+ import ERROR from "../const/error";
17
+ import { DEFAULT_CLASS } from "../const/external";
18
+ import { UniformLocations } from "../type/internal";
19
+
20
+ /**
21
+ * @hidden
22
+ */
23
+ class WebGLContext {
24
+ private _canvas: HTMLCanvasElement;
25
+ private _gl: WebGLRenderingContext | WebGL2RenderingContext;
26
+ private _contextLost: boolean;
27
+ private _maxTextureSize: number;
28
+ private _isWebGL2: boolean;
29
+ private _debug: boolean;
30
+ private _extensions: {
31
+ vao: OES_vertex_array_object | null;
32
+ loseContext: WEBGL_lose_context | null;
33
+ };
34
+
35
+ public get canvas() { return this._canvas; }
36
+ public get gl() { return this._gl; }
37
+ public get maxTextureSize() { return this._maxTextureSize; }
38
+ public get isWebGL2() { return this._isWebGL2; }
39
+ public get supportVAO() { return this._isWebGL2 || !!this._extensions.vao; }
40
+ public get lost() { return this._contextLost; }
41
+ public get debug() { return this._debug; }
42
+
43
+ public constructor(canvas: HTMLCanvasElement, debug: boolean) {
44
+ this._canvas = canvas;
45
+ this._contextLost = false;
46
+ this._debug = debug;
47
+ this._extensions = {
48
+ vao: null,
49
+ loseContext: null
50
+ };
51
+ }
52
+
53
+ public init() {
54
+ const canvas = this._canvas;
55
+
56
+ const { gl, isWebGL2 } = this._getContext(canvas);
57
+
58
+ this._gl = gl;
59
+ this._maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE);
60
+ this._isWebGL2 = isWebGL2;
61
+
62
+ if (!this._isWebGL2) {
63
+ this._extensions.vao = gl.getExtension("OES_vertex_array_object");
64
+ }
65
+
66
+ this._extensions.loseContext = gl.getExtension("WEBGL_lose_context");
67
+
68
+ canvas.addEventListener(BROWSER.EVENTS.CONTEXT_LOST, this._onContextLost);
69
+ canvas.addEventListener(BROWSER.EVENTS.CONTEXT_RESTORED, this._onContextRestore);
70
+
71
+ // gl.enable(gl.DEPTH_TEST);
72
+ }
73
+
74
+ public destroy() {
75
+ const gl = this._gl;
76
+ const canvas = this._canvas;
77
+
78
+ if (gl) {
79
+ // gl is not defined when destroy is called before init
80
+ gl.bindBuffer(gl.ARRAY_BUFFER, null);
81
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
82
+ }
83
+
84
+ canvas.removeEventListener(BROWSER.EVENTS.CONTEXT_LOST, this._onContextLost);
85
+ canvas.removeEventListener(BROWSER.EVENTS.CONTEXT_RESTORED, this._onContextRestore);
86
+ }
87
+
88
+ public forceLoseContext() {
89
+ const extension = this._extensions.loseContext;
90
+
91
+ if (!extension) return;
92
+
93
+ extension.loseContext();
94
+ }
95
+
96
+ public forceRestoreContext() {
97
+ const extension = this._extensions.loseContext;
98
+
99
+ if (!extension) return;
100
+
101
+ extension.restoreContext();
102
+ }
103
+
104
+ public clear() {
105
+ const gl = this._gl;
106
+
107
+ gl.clear(gl.COLOR_BUFFER_BIT);
108
+ }
109
+
110
+ public resize() {
111
+ const gl = this._gl;
112
+
113
+ gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
114
+ }
115
+
116
+ public viewport(x: number, y: number, width: number, height: number) {
117
+ const gl = this._gl;
118
+
119
+ gl.viewport(x, y, width, height);
120
+ }
121
+
122
+ public createVAO(geometry: Geometry, shaderProgram: ShaderProgram) {
123
+ const nativeVAO = this._createNativeVAO();
124
+
125
+ const vao = new VertexArrayObject(nativeVAO, geometry, {
126
+ indicies: this._createBuffer(),
127
+ position: this._createBuffer(),
128
+ uv: this._createBuffer()
129
+ });
130
+
131
+ if (nativeVAO) {
132
+ this._bindNativeVAO(nativeVAO);
133
+ this._supplyGeometryData(vao, shaderProgram);
134
+ this._bindNativeVAO(null);
135
+ this._unbindBuffers();
136
+ }
137
+
138
+ return vao;
139
+ }
140
+
141
+ public draw(vao: VertexArrayObject, shaderProgram: ShaderProgram) {
142
+ const gl = this._gl;
143
+
144
+ if (vao.obj) {
145
+ this._bindNativeVAO(vao.obj);
146
+ } else {
147
+ this._supplyGeometryData(vao, shaderProgram);
148
+ }
149
+
150
+ gl.drawElements(gl.TRIANGLES, vao.count, gl.UNSIGNED_SHORT, 0);
151
+
152
+ if (vao.obj) {
153
+ this._bindNativeVAO(null);
154
+ } else {
155
+ this._unbindBuffers();
156
+ }
157
+ }
158
+
159
+ public releaseVAO(vao: VertexArrayObject) {
160
+ if (vao.obj) {
161
+ this._deleteNativeVAO(vao.obj);
162
+ }
163
+
164
+ this._deleteBuffer(vao.buffers.indicies);
165
+ this._deleteBuffer(vao.buffers.position);
166
+ this._deleteBuffer(vao.buffers.uv);
167
+ }
168
+
169
+ public getUniformLocations<T extends Record<string, Uniform>>(program: WebGLProgram, uniforms: T): UniformLocations<T> {
170
+ const gl = this._gl;
171
+
172
+ const uniformLocations = Object.keys(uniforms).reduce((locations, key) => {
173
+ locations[key as keyof T] = gl.getUniformLocation(program, key)!;
174
+
175
+ return locations;
176
+ }, {} as UniformLocations<T>);
177
+
178
+ return {
179
+ ...this._getCommonUniformLocations(program),
180
+ ...uniformLocations
181
+ };
182
+ }
183
+
184
+ public updateCommonUniforms(entity: Object3D, camera: Camera, shaderProgram: ShaderProgram) {
185
+ const gl = this._gl;
186
+
187
+ const uniformLocations = shaderProgram.uniformLocations;
188
+
189
+ // We're using "matrix"(=local matrix) here for efficiency
190
+ // As projection doesn't require world matrix, as it doesn't have any parent or child
191
+ const matrix = entity.matrix;
192
+ const mvMatrix = mat4.create();
193
+ mat4.multiply(mvMatrix, camera.viewMatrix, matrix);
194
+
195
+ gl.uniformMatrix4fv(uniformLocations.uMVMatrix, false, mvMatrix);
196
+ gl.uniformMatrix4fv(uniformLocations.uPMatrix, false, camera.projectionMatrix);
197
+ }
198
+
199
+ public updateVRUniforms(shaderProgram: ShaderProgram, mvMatrix: mat4, pMatrix: mat4, eyeIndex: number) {
200
+ const gl = this._gl;
201
+
202
+ const uniformLocations = shaderProgram.uniformLocations;
203
+
204
+ gl.uniformMatrix4fv(uniformLocations.uMVMatrix, false, mvMatrix);
205
+ gl.uniformMatrix4fv(uniformLocations.uPMatrix, false, pMatrix);
206
+
207
+ if (uniformLocations.uEye) {
208
+ gl.uniform1f(uniformLocations.uEye, eyeIndex);
209
+ }
210
+ }
211
+
212
+ public updateUniforms(shaderProgram: ShaderProgram) {
213
+ const gl = this._gl;
214
+
215
+ const uniforms = shaderProgram.uniforms;
216
+ const uniformLocations = shaderProgram.uniformLocations;
217
+
218
+ for (const key in uniforms) {
219
+ const uniform = uniforms[key];
220
+ const location = uniformLocations[key];
221
+
222
+ if (!uniform) continue;
223
+
224
+ if (uniform.needsUpdate) {
225
+ uniform.update(gl, location, this._isWebGL2);
226
+ }
227
+ }
228
+ }
229
+
230
+ public releaseShaderResources(shaderProgram: ShaderProgram) {
231
+ const gl = this._gl;
232
+
233
+ const uniforms = shaderProgram.uniforms;
234
+
235
+ for (const key in uniforms) {
236
+ const uniform = uniforms[key];
237
+
238
+ if (!uniform) continue;
239
+
240
+ if (uniform.needsUpdate) {
241
+ uniform.destroy(gl);
242
+ }
243
+ }
244
+
245
+ gl.deleteProgram(shaderProgram.program);
246
+ }
247
+
248
+ public useProgram(shaderProgram: ShaderProgram) {
249
+ const gl = this._gl;
250
+
251
+ gl.useProgram(shaderProgram.program);
252
+ }
253
+
254
+ public createProgram(vertexShader: string, fragmentShader: string) {
255
+ const gl = this._gl;
256
+ const program = gl.createProgram()!;
257
+
258
+ const vs = this._compileShader(gl.VERTEX_SHADER, vertexShader);
259
+ const fs = this._compileShader(gl.FRAGMENT_SHADER, fragmentShader);
260
+
261
+ gl.attachShader(program, vs);
262
+ gl.attachShader(program, fs);
263
+ gl.bindAttribLocation(program, 0, "position");
264
+ gl.bindAttribLocation(program, 1, "uv");
265
+ gl.linkProgram(program);
266
+
267
+ if (this._debug && !gl.getProgramParameter(program, gl.LINK_STATUS)) {
268
+ let shaderLog: string | null = null;
269
+
270
+ if (!gl.getShaderParameter(vs, gl.COMPILE_STATUS)) {
271
+ shaderLog = gl.getShaderInfoLog(vs);
272
+ } else if (!gl.getShaderParameter(fs, gl.COMPILE_STATUS)) {
273
+ shaderLog = gl.getShaderInfoLog(fs);
274
+ }
275
+
276
+ throw new View360Error(ERROR.MESSAGES.FAILED_LINKING_PROGRAM(gl.getProgramInfoLog(program), shaderLog), ERROR.CODES.FAILED_LINKING_PROGRAM);
277
+ }
278
+
279
+ gl.deleteShader(vs);
280
+ gl.deleteShader(fs);
281
+
282
+ return program;
283
+ }
284
+
285
+ public createWebGLTexture(texData: Texture): WebGLTexture {
286
+ const gl = this._gl;
287
+ const texture = gl.createTexture()!;
288
+
289
+ gl.bindTexture(gl.TEXTURE_2D, texture);
290
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
291
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
292
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, texData.wrapS);
293
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, texData.wrapT);
294
+
295
+ if (!texData.isVideo() && this._isWebGL2) {
296
+ const gl2 = gl as WebGL2RenderingContext;
297
+
298
+ gl2.texStorage2D(gl2.TEXTURE_2D, 1, gl2.RGBA8, texData.width, texData.height);
299
+ }
300
+
301
+ return texture;
302
+ }
303
+
304
+ public createWebGLCubeTexture(texData: Texture, size: number): WebGLTexture {
305
+ const gl = this._gl;
306
+ const texture = gl.createTexture()!;
307
+
308
+ gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture);
309
+ gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
310
+ gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
311
+ gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, texData.wrapS);
312
+ gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, texData.wrapT);
313
+
314
+ if (this._isWebGL2) {
315
+ const gl2 = gl as WebGL2RenderingContext;
316
+
317
+ gl2.texStorage2D(gl2.TEXTURE_CUBE_MAP, 1, gl2.RGBA8, size, size);
318
+ }
319
+
320
+ return texture;
321
+ }
322
+
323
+ public async makeXRCompatible() {
324
+ const gl = this._gl;
325
+ const attributes = gl.getContextAttributes();
326
+
327
+ if (attributes && attributes.xrCompatible !== true) {
328
+ await gl.makeXRCompatible();
329
+ }
330
+ }
331
+
332
+ public bindXRLayer(session: XRSession) {
333
+ const gl = this._gl;
334
+ const xrLayer = new XRWebGLLayer(session, gl);
335
+ session.updateRenderState({ baseLayer: xrLayer });
336
+ }
337
+
338
+ public bindXRFrame(frame: XRFrame) {
339
+ const gl = this._gl;
340
+ const session = frame.session;
341
+ const baseLayer = session.renderState.baseLayer!;
342
+
343
+ gl.bindFramebuffer(gl.FRAMEBUFFER, baseLayer.framebuffer);
344
+ }
345
+
346
+ public useDefaultFrameBuffer() {
347
+ const gl = this._gl;
348
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
349
+ }
350
+
351
+ /**
352
+ * 创建 Framebuffer 及其颜色附件纹理
353
+ * @param width - 纹理宽度
354
+ * @param height - 纹理高度
355
+ * @returns FBO 对象,包含 framebuffer 和 texture
356
+ */
357
+ public createFramebuffer(width: number, height: number): {
358
+ framebuffer: WebGLFramebuffer;
359
+ texture: WebGLTexture;
360
+ } {
361
+ const gl = this._gl;
362
+ const framebuffer = gl.createFramebuffer();
363
+ const texture = gl.createTexture();
364
+
365
+ if (!framebuffer || !texture) {
366
+ throw new View360Error("无法创建 Framebuffer", ERROR.CODES.FAILED_LINKING_PROGRAM);
367
+ }
368
+
369
+ gl.bindTexture(gl.TEXTURE_2D, texture);
370
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
371
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
372
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
373
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
374
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
375
+
376
+ gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
377
+ gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
378
+
379
+ // 检查 FBO 完整性
380
+ const status = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
381
+ if (status !== gl.FRAMEBUFFER_COMPLETE) {
382
+ throw new View360Error("Framebuffer 不完整: " + status, ERROR.CODES.FAILED_LINKING_PROGRAM);
383
+ }
384
+
385
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
386
+ gl.bindTexture(gl.TEXTURE_2D, null);
387
+
388
+ return { framebuffer, texture };
389
+ }
390
+
391
+ /**
392
+ * 绑定指定的 Framebuffer
393
+ * @param framebuffer - 要绑定的 FBO,传 null 恢复默认
394
+ */
395
+ public bindFramebuffer(framebuffer: WebGLFramebuffer | null) {
396
+ this._gl.bindFramebuffer(this._gl.FRAMEBUFFER, framebuffer);
397
+ }
398
+
399
+ /**
400
+ * 删除 Framebuffer 及其附件纹理
401
+ * @param fbo - 要删除的 FBO 对象
402
+ */
403
+ public deleteFramebuffer(fbo: { framebuffer: WebGLFramebuffer; texture: WebGLTexture }) {
404
+ const gl = this._gl;
405
+ gl.deleteFramebuffer(fbo.framebuffer);
406
+ gl.deleteTexture(fbo.texture);
407
+ }
408
+
409
+ private _createBuffer(): WebGLBuffer {
410
+ return this._gl.createBuffer()!;
411
+ }
412
+
413
+ private _deleteBuffer(buffer: WebGLBuffer) {
414
+ return this._gl.deleteBuffer(buffer);
415
+ }
416
+
417
+ private _createNativeVAO() {
418
+ const gl = this._gl;
419
+
420
+ if (this._isWebGL2) {
421
+ return (gl as WebGL2RenderingContext).createVertexArray()!;
422
+ } else {
423
+ const ext = this._extensions.vao;
424
+
425
+ return ext?.createVertexArrayOES() || null;
426
+ }
427
+ }
428
+
429
+ private _bindNativeVAO(vao: WebGLVertexArrayObject | null) {
430
+ const gl = this._gl;
431
+
432
+ if (this._isWebGL2) {
433
+ (gl as WebGL2RenderingContext).bindVertexArray(vao);
434
+ } else {
435
+ const ext = this._extensions.vao;
436
+
437
+ ext?.bindVertexArrayOES(vao);
438
+ }
439
+ }
440
+
441
+ private _deleteNativeVAO(vao: WebGLVertexArrayObject | null) {
442
+ const gl = this._gl;
443
+
444
+ if (this._isWebGL2) {
445
+ (gl as WebGL2RenderingContext).deleteVertexArray(vao);
446
+ } else {
447
+ const ext = this._extensions.vao;
448
+
449
+ ext?.deleteVertexArrayOES(vao);
450
+ }
451
+ }
452
+
453
+ private _supplyGeometryData(vao: VertexArrayObject, shaderProgram: ShaderProgram) {
454
+ const geometry = vao.geometry;
455
+
456
+ this._supplyIndiciesData(geometry.indicies, vao.buffers.indicies);
457
+ this._supplyAttributeData(geometry.vertices, shaderProgram.program, "position", vao.buffers.position);
458
+ this._supplyAttributeData(geometry.uvs, shaderProgram.program, "uv", vao.buffers.uv);
459
+ }
460
+
461
+ private _unbindBuffers() {
462
+ const gl = this._gl;
463
+
464
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
465
+ gl.bindBuffer(gl.ARRAY_BUFFER, null);
466
+ }
467
+
468
+ private _supplyIndiciesData(indicies: VertexData<Uint16Array>, buffer: WebGLBuffer) {
469
+ const gl = this._gl;
470
+
471
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer);
472
+ gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indicies.data, gl.STATIC_DRAW);
473
+ }
474
+
475
+ private _supplyAttributeData(attribute: VertexData<Float32Array>, program: WebGLProgram, name: string, buffer: WebGLBuffer) {
476
+ const gl = this._gl;
477
+ const attribLocation = gl.getAttribLocation(program, name);
478
+
479
+ // Attribute not used
480
+ if (attribLocation < 0) return;
481
+
482
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
483
+ gl.bufferData(gl.ARRAY_BUFFER, attribute.data, gl.STATIC_DRAW);
484
+ gl.vertexAttribPointer(attribLocation, attribute.itemSize, gl.FLOAT, false, 0, 0);
485
+ gl.enableVertexAttribArray(attribLocation);
486
+ }
487
+
488
+ private _compileShader(type: number, src: string) {
489
+ const gl = this._gl;
490
+ const shader = gl.createShader(type)!;
491
+
492
+ gl.shaderSource(shader, src);
493
+ gl.compileShader(shader);
494
+
495
+ return shader;
496
+ }
497
+
498
+ private _getCommonUniformLocations(program: WebGLProgram) {
499
+ const gl = this._gl;
500
+
501
+ return {
502
+ uMVMatrix: gl.getUniformLocation(program, "uMVMatrix")!,
503
+ uPMatrix: gl.getUniformLocation(program, "uPMatrix")!
504
+ };
505
+ }
506
+
507
+ private _getContext(canvas: HTMLCanvasElement): {
508
+ gl: WebGLRenderingContext | WebGL2RenderingContext;
509
+ isWebGL2: boolean;
510
+ } {
511
+ const webglIdentifiers = ["webgl2", "webgl", "experimental-webgl", "webkit-3d", "moz-webgl"];
512
+ let context: WebGLRenderingContext | null = null;
513
+ let isWebGL2 = false;
514
+ const contextAttributes = {
515
+ preserveDrawingBuffer: false,
516
+ antialias: false
517
+ };
518
+
519
+ const onWebglContextCreationError = e => e.statusMessage;
520
+
521
+ canvas.addEventListener(BROWSER.EVENTS.CONTEXT_CREATE_ERROR, onWebglContextCreationError);
522
+
523
+ for (const identifier of webglIdentifiers) {
524
+ try {
525
+ context = canvas.getContext(identifier, contextAttributes) as WebGLRenderingContext;
526
+ isWebGL2 = identifier === "webgl2";
527
+ } catch (t) {} // eslint-disable-line no-empty
528
+ if (context) {
529
+ break;
530
+ }
531
+ }
532
+
533
+ canvas.removeEventListener(BROWSER.EVENTS.CONTEXT_CREATE_ERROR, onWebglContextCreationError);
534
+
535
+ if (!context) {
536
+ throw new View360Error(ERROR.MESSAGES.WEBGL_NOT_SUPPORTED, ERROR.CODES.WEBGL_NOT_SUPPORTED);
537
+ }
538
+
539
+ return {
540
+ gl: context,
541
+ isWebGL2
542
+ };
543
+ }
544
+
545
+ private _onContextLost = () => {
546
+ const canvas = this._canvas;
547
+ canvas.classList.add(DEFAULT_CLASS.CTX_LOST);
548
+ this._contextLost = true;
549
+ };
550
+
551
+ private _onContextRestore = () => {
552
+ const canvas = this._canvas;
553
+ canvas.classList.remove(DEFAULT_CLASS.CTX_LOST);
554
+ this._contextLost = false;
555
+ };
556
+ }
557
+
558
+ export default WebGLContext;