vislite 0.8.0 → 1.0.0-next.0

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 (201) hide show
  1. package/AUTHORS.txt +6 -6
  2. package/CHANGELOG +17 -2
  3. package/LICENSE +20 -20
  4. package/README.md +2 -36
  5. package/lib/Buffer/index.umd.js +2 -2
  6. package/lib/Buffer/index.umd.min.js +2 -2
  7. package/lib/Canvas/index.umd.js +9 -24
  8. package/lib/Canvas/index.umd.min.js +3 -3
  9. package/lib/Cardinal/index.umd.js +2 -2
  10. package/lib/Cardinal/index.umd.min.js +2 -2
  11. package/lib/Eoap/index.umd.js +2 -2
  12. package/lib/Eoap/index.umd.min.js +2 -2
  13. package/lib/Hermite/index.umd.js +2 -2
  14. package/lib/Hermite/index.umd.min.js +2 -2
  15. package/lib/Matrix4/index.umd.js +2 -2
  16. package/lib/Matrix4/index.umd.min.js +2 -2
  17. package/lib/Mercator/index.umd.js +2 -2
  18. package/lib/Mercator/index.umd.min.js +2 -2
  19. package/lib/SVG/index.umd.js +19 -35
  20. package/lib/SVG/index.umd.min.js +3 -3
  21. package/lib/Shader/index.umd.js +3 -82
  22. package/lib/Shader/index.umd.min.js +3 -3
  23. package/lib/Texture/index.umd.js +2 -2
  24. package/lib/Texture/index.umd.min.js +2 -2
  25. package/lib/TreeLayout/index.umd.js +2 -2
  26. package/lib/TreeLayout/index.umd.min.js +2 -2
  27. package/lib/animation/index.umd.js +2 -2
  28. package/lib/animation/index.umd.min.js +2 -2
  29. package/lib/getLoopColors/index.umd.js +2 -2
  30. package/lib/getLoopColors/index.umd.min.js +2 -2
  31. package/lib/getWebGLContext/index.umd.js +2 -2
  32. package/lib/getWebGLContext/index.umd.min.js +2 -2
  33. package/lib/index.umd.js +104 -944
  34. package/lib/index.umd.min.js +3 -3
  35. package/lib/move/index.umd.js +2 -2
  36. package/lib/move/index.umd.min.js +2 -2
  37. package/lib/resizeObserver/index.umd.js +2 -2
  38. package/lib/resizeObserver/index.umd.min.js +2 -2
  39. package/lib/rotate/index.umd.js +2 -2
  40. package/lib/rotate/index.umd.min.js +2 -2
  41. package/lib/ruler/index.umd.js +2 -2
  42. package/lib/ruler/index.umd.min.js +2 -2
  43. package/lib/scale/index.umd.js +2 -2
  44. package/lib/scale/index.umd.min.js +2 -2
  45. package/lib/throttle/index.umd.js +2 -2
  46. package/lib/throttle/index.umd.min.js +2 -2
  47. package/miniprogram/ui-canvas/index.js +100 -107
  48. package/miniprogram/ui-canvas/index.json +4 -4
  49. package/miniprogram/ui-canvas/index.wxml +4 -4
  50. package/miniprogram/ui-canvas/index.wxss +5 -5
  51. package/package/Buffer/index.ts +2 -2
  52. package/package/Canvas/index.ts +2 -2
  53. package/package/Cardinal/index.ts +2 -2
  54. package/package/Eoap/index.ts +2 -2
  55. package/package/Hermite/index.ts +2 -2
  56. package/package/Matrix4/index.ts +2 -2
  57. package/package/Mercator/index.ts +2 -2
  58. package/package/SVG/index.ts +2 -2
  59. package/package/Shader/index.ts +2 -39
  60. package/package/Texture/index.ts +2 -2
  61. package/package/TreeLayout/index.ts +2 -2
  62. package/package/animation/index.ts +2 -2
  63. package/package/getLoopColors/index.ts +2 -2
  64. package/package/index.ts +67 -76
  65. package/package/move/index.ts +2 -2
  66. package/package/resizeObserver/index.ts +2 -2
  67. package/package/rotate/index.ts +2 -2
  68. package/package/ruler/index.ts +2 -2
  69. package/package/scale/index.ts +2 -2
  70. package/package/throttle/index.ts +2 -2
  71. package/package.json +1 -1
  72. package/src/Eoap.ts +66 -66
  73. package/src/Matrix4/index.ts +84 -84
  74. package/src/Matrix4/move.ts +12 -12
  75. package/src/Matrix4/rotate.ts +15 -15
  76. package/src/Matrix4/scale.ts +11 -11
  77. package/src/Matrix4/transform.ts +64 -64
  78. package/src/Mercator.ts +27 -27
  79. package/src/SVG.ts +24 -24
  80. package/src/animation.ts +98 -98
  81. package/src/common/assemble.ts +22 -22
  82. package/src/common/canvas/arc.ts +51 -51
  83. package/src/common/canvas/config.ts +158 -158
  84. package/src/common/canvas/gradient.ts +24 -29
  85. package/src/common/canvas/index.ts +1 -12
  86. package/src/common/canvas/texts.ts +18 -18
  87. package/src/common/mergeOption.ts +6 -6
  88. package/src/common/setStyle.ts +5 -5
  89. package/src/common/svg/config.ts +190 -190
  90. package/src/common/svg/gradient.ts +53 -63
  91. package/src/common/svg/index.ts +347 -356
  92. package/src/common/svg/tool.ts +44 -44
  93. package/src/common/tree/index.ts +25 -25
  94. package/src/common/tree/toInnerTree.ts +59 -59
  95. package/src/common/tree/toPlainTree.ts +132 -132
  96. package/src/common/webgl/buffer.ts +79 -79
  97. package/src/common/webgl/getWebGLContext.ts +1 -1
  98. package/src/common/webgl/shader.ts +75 -75
  99. package/src/common/webgl/texture.ts +97 -97
  100. package/src/formatColor.ts +44 -44
  101. package/src/getLoopColors.ts +42 -42
  102. package/src/interpolation/Cardinal.ts +110 -110
  103. package/src/interpolation/Hermite.ts +65 -65
  104. package/src/layout/TreeLayout.ts +188 -188
  105. package/src/move.ts +7 -7
  106. package/src/resizeObserver.ts +37 -37
  107. package/src/rotate.ts +7 -7
  108. package/src/ruler.ts +209 -209
  109. package/src/scale.ts +6 -6
  110. package/src/throttle.ts +52 -52
  111. package/types/Buffer.d.ts +23 -23
  112. package/types/CanvasConfig.d.ts +106 -106
  113. package/types/CanvasOption.d.ts +8 -8
  114. package/types/CanvasOpts.d.ts +9 -9
  115. package/types/Cardinal.d.ts +13 -13
  116. package/types/Gradient.d.ts +14 -14
  117. package/types/Hermite.d.ts +18 -18
  118. package/types/Map.d.ts +9 -9
  119. package/types/Matrix4.d.ts +47 -47
  120. package/types/SVG.d.ts +238 -238
  121. package/types/SVGConfig.d.ts +79 -79
  122. package/types/Shader.d.ts +16 -23
  123. package/types/Texture.d.ts +16 -16
  124. package/types/Tree.d.ts +69 -69
  125. package/types/TreeConfig.d.ts +36 -36
  126. package/types/TreeLayout.d.ts +54 -54
  127. package/types/TreeOption.d.ts +48 -48
  128. package/types/animation.d.ts +9 -6
  129. package/types/getLoopColors.d.ts +5 -2
  130. package/types/getWebGLContext.d.ts +3 -0
  131. package/types/index.d.ts +149 -176
  132. package/types/move.d.ts +5 -2
  133. package/types/painterConfig.d.ts +2 -2
  134. package/types/resizeObserver.d.ts +5 -2
  135. package/types/rotate.d.ts +5 -2
  136. package/types/ruler.d.ts +10 -7
  137. package/types/scale.d.ts +5 -2
  138. package/types/throttle.d.ts +7 -4
  139. package/types/throttleOption.d.ts +21 -21
  140. package/uni-app/ui-canvas.vue +4 -11
  141. package/lib/Geometry/index.umd.d.ts +0 -3
  142. package/lib/Geometry/index.umd.js +0 -353
  143. package/lib/Geometry/index.umd.min.d.ts +0 -3
  144. package/lib/Geometry/index.umd.min.js +0 -11
  145. package/lib/OralCanvas/index.es.d.ts +0 -3
  146. package/lib/OralCanvas/index.es.js +0 -693
  147. package/lib/OralCanvas/index.es.min.d.ts +0 -3
  148. package/lib/OralCanvas/index.es.min.js +0 -11
  149. package/lib/OralWebGL/index.es.d.ts +0 -3
  150. package/lib/OralWebGL/index.es.js +0 -639
  151. package/lib/OralWebGL/index.es.min.d.ts +0 -3
  152. package/lib/OralWebGL/index.es.min.js +0 -11
  153. package/lib/WebGL/index.umd.d.ts +0 -3
  154. package/lib/WebGL/index.umd.js +0 -712
  155. package/lib/WebGL/index.umd.min.d.ts +0 -3
  156. package/lib/WebGL/index.umd.min.js +0 -11
  157. package/lib/formatColor/index.umd.d.ts +0 -3
  158. package/lib/formatColor/index.umd.js +0 -59
  159. package/lib/formatColor/index.umd.min.d.ts +0 -3
  160. package/lib/formatColor/index.umd.min.js +0 -11
  161. package/lib/viewHandler/index.umd.d.ts +0 -3
  162. package/lib/viewHandler/index.umd.js +0 -139
  163. package/lib/viewHandler/index.umd.min.d.ts +0 -3
  164. package/lib/viewHandler/index.umd.min.js +0 -11
  165. package/package/Geometry/index.ts +0 -3
  166. package/package/OralCanvas/index.ts +0 -3
  167. package/package/OralWebGL/index.ts +0 -3
  168. package/package/WebGL/index.ts +0 -3
  169. package/package/formatColor/index.ts +0 -3
  170. package/package/viewHandler/index.ts +0 -3
  171. package/src/Geometry.ts +0 -211
  172. package/src/WebGL.ts +0 -67
  173. package/src/common/geometry/prism-horizontal.ts +0 -50
  174. package/src/common/geometry/prism-vertical.ts +0 -55
  175. package/src/common/geometry/sphere-fragment.ts +0 -40
  176. package/src/common/geometry/tool/circle.ts +0 -18
  177. package/src/common/geometry/tool/rotateLine.ts +0 -49
  178. package/src/common/geometry/tool/triangles.ts +0 -24
  179. package/src/common/webgl/doDraw.ts +0 -109
  180. package/src/common/webgl/getColorFactory.ts +0 -27
  181. package/src/common/webgl/getShader.ts +0 -49
  182. package/src/common/webgl/getTexture.ts +0 -12
  183. package/src/common/webgl/index.ts +0 -228
  184. package/src/shader/fsColor.c +0 -8
  185. package/src/shader/fsColors.c +0 -8
  186. package/src/shader/fsCube.c +0 -9
  187. package/src/shader/fsImage.c +0 -9
  188. package/src/shader/vsColor.c +0 -18
  189. package/src/shader/vsColors.c +0 -14
  190. package/src/shader/vsCube.c +0 -14
  191. package/src/shader/vsImage.c +0 -14
  192. package/src/viewHandler.ts +0 -161
  193. package/types/Geometry.d.ts +0 -133
  194. package/types/GeometryOption.d.ts +0 -12
  195. package/types/GeometryResult.d.ts +0 -19
  196. package/types/Object3D.d.ts +0 -115
  197. package/types/Scene3D.d.ts +0 -7
  198. package/types/WebGL.d.ts +0 -39
  199. package/types/WebGLOption.d.ts +0 -15
  200. package/types/formatColor.d.ts +0 -3
  201. package/types/viewHandler.d.ts +0 -14
@@ -1,55 +0,0 @@
1
- import rotate from '../../rotate'
2
-
3
- /**
4
- * 棱柱垂直部分
5
- * @param normal 是否需要法向量
6
- * @param x 中心
7
- * @param y
8
- * @param z
9
- * @param radius 半径
10
- * @param height 高
11
- * @param num 份数
12
- * @param beginDeg 开始弧度,默认0
13
- * @param deg 跨越弧度,默认2pi
14
- * @returns 点数组
15
- */
16
- export default function (normal: boolean, x: number, y: number, z: number, radius: number, height: number, num: number, beginDeg: number = 0, deg: number = Math.PI * 2) {
17
- const points = []
18
- let beginPosition: [number, number]
19
-
20
- if (num == 4) {
21
- beginPosition = rotate(x, z, Math.PI * 0.25, x - radius, z)
22
- } else {
23
- beginPosition = [x + radius, z]
24
- }
25
-
26
- if (beginDeg != 0) {
27
- beginPosition = rotate(x, z, beginDeg, ...beginPosition)
28
- }
29
-
30
- const perDeg = deg / num, degHalf = deg / (num * 2)
31
-
32
- let endPosition: [number, number], normalPosition: [number, number, number] | [] = []
33
- for (let i = 0; i < num; i++) {
34
-
35
- endPosition = rotate(x, z, perDeg, ...beginPosition)
36
-
37
- if (normal) {
38
- const halfPosition = rotate(x, z, degHalf, ...beginPosition)
39
- normalPosition = [halfPosition[0], 0, halfPosition[1]]
40
- }
41
-
42
- points.push(beginPosition[0], y, beginPosition[1], ...normalPosition)
43
- points.push(beginPosition[0], y + height, beginPosition[1], ...normalPosition)
44
- points.push(endPosition[0], y + height, endPosition[1], ...normalPosition)
45
-
46
- points.push(beginPosition[0], y, beginPosition[1], ...normalPosition)
47
- points.push(endPosition[0], y, endPosition[1], ...normalPosition)
48
- points.push(endPosition[0], y + height, endPosition[1], ...normalPosition)
49
-
50
-
51
- beginPosition = endPosition
52
- }
53
-
54
- return points
55
- }
@@ -1,40 +0,0 @@
1
- import rotate from '../../rotate'
2
-
3
- // 球体中的一瓣子
4
-
5
- export default function (normal: boolean, cx: number, cy: number, cz: number, radius: number, num: number, index: number) {
6
- const points = [cx, cy + radius, cz], deg = Math.PI * 2 / num
7
- let point: [number, number]
8
-
9
- if (normal) points.push(0, radius, 0)
10
-
11
- const copy2 = () => {
12
- points.push(...points.slice(points.length - (normal ? 12 : 6)))
13
- }
14
-
15
- for (let i = 1; i < num * 0.5; i++) {
16
- point = rotate(cx, cy, deg * i, cx, cy + radius)
17
-
18
- if (i > 1) copy2()
19
-
20
- // 第一个点
21
- const point1 = rotate(cx, cz, deg * index, point[0], cz)
22
- points.push(point1[0], point[1], point1[1])
23
-
24
- if (normal) points.push(point1[0] - cx, point[1] - cy, point1[1] - cz)
25
-
26
- if (i > 1) copy2()
27
-
28
- // 下一个点
29
- const point2 = rotate(cx, cz, deg * (index + 1), point[0], cz)
30
- points.push(point2[0], point[1], point2[1])
31
-
32
- if (normal) points.push(point2[0] - cx, point2[1] - cy, point2[1] - cz)
33
- }
34
- copy2()
35
- points.push(cx, cy - radius, cz)
36
-
37
- if (normal) points.push(0, - radius, 0)
38
-
39
- return points
40
- }
@@ -1,18 +0,0 @@
1
- /**
2
- * 计算切割份数
3
- * @param precision 精度
4
- * @param radius 半径
5
- * @param deg 需要跨越的弧度,默认圆(2pi)
6
- * @returns 切割份数
7
- */
8
- export const splitNum = function (precision: number, radius: number, deg: number = Math.PI * 2) {
9
-
10
- // 根据切割弧度得出切割块数目
11
- const num = Math.ceil(deg /
12
-
13
- // 为了满足最小精度而得出的切割弧度
14
- Math.asin(precision / radius) * 2)
15
-
16
- return (isNaN(num) || num < 12) ? 12 : num
17
-
18
- }
@@ -1,49 +0,0 @@
1
- /**
2
- * 物体方向转换
3
- * 由(x,y,z)转换成(x2-x,y2-y,z2-z)
4
- * @param x
5
- * @param y
6
- * @param z
7
- * @param x2
8
- * @param y2
9
- * @param z2
10
- * @returns 点转换函数
11
- */
12
- export default function (x: number, y: number, z: number, x2: number, y2: number, z2: number) {
13
- return (x0: number, y0: number, z0: number, isNormal: boolean) => {
14
- if (x == x2 && z == z2) return [x0, y0, z0]
15
-
16
- let sin: number, cos: number, temp: Array<number>
17
-
18
- // 第一步:归零化
19
- let _x0 = x0 - (isNormal ? 0 : x), _y0 = y0 - (isNormal ? 0 : y), _z0 = z0 - (isNormal ? 0 : z) // 法向量起点本来就是原点
20
- const _x2 = x2 - x, _y2 = y2 - y, _z2 = z2 - z
21
-
22
- // 第二步:围绕OZ轴旋转
23
-
24
- const __d = Math.sqrt(_x2 * _x2 + _z2 * _z2)
25
- cos = _x2 / __d
26
- sin = _z2 / __d
27
-
28
- const _x2N = _z2 * sin + _x2 * cos
29
-
30
- const d = Math.sqrt(_y2 * _y2 + _x2N * _x2N)
31
- cos = _y2 / d
32
- sin = _x2N / d
33
-
34
- temp = [_y0, _x0]
35
- _y0 = temp[0] * cos - temp[1] * sin
36
- _x0 = temp[0] * sin + temp[1] * cos
37
-
38
- // 第三步:围绕0Y轴旋转
39
- cos = _x2 / __d
40
- sin = _z2 / __d
41
-
42
- temp = [_x0, _z0]
43
- _x0 = temp[0] * cos - temp[1] * sin
44
- _z0 = temp[0] * sin + temp[1] * cos
45
-
46
- // 第四步:去零化(直接返回)
47
- return [_x0 + (isNormal ? 0 : x), _y0 + (isNormal ? 0 : y), _z0 + (isNormal ? 0 : z)]
48
- }
49
- }
@@ -1,24 +0,0 @@
1
- /**
2
- * 方向:ab x ac
3
- * @param normal 是否需要法向量
4
- * @param points [a1, b1, c1, a2, b2, c2, ...]
5
- * @returns 完整的点数据
6
- */
7
- export default function (normal: boolean, points: Array<number>) {
8
- if (normal) {
9
- const _points = []
10
- for (let i = 0; i < points.length; i += 9) {
11
- const ab = [points[i + 3] - points[i], points[i + 4] - points[i + 1], points[i + 5] - points[i + 2]]
12
- const ac = [points[i + 6] - points[i], points[i + 7] - points[i + 1], points[i + 8] - points[i + 2]]
13
- const normalVector = [ab[1] * ac[2] - ac[1] * ab[2], ab[2] * ac[0] - ab[0] * ac[2], ab[0] * ac[1] - ab[1] * ac[2]]
14
- _points.push(
15
- points[i], points[i + 1], points[i + 2], ...normalVector,
16
- points[i + 3], points[i + 4], points[i + 5], ...normalVector,
17
- points[i + 6], points[i + 7], points[i + 8], ...normalVector,
18
- )
19
- }
20
- return _points
21
- } else {
22
- return points
23
- }
24
- }
@@ -1,109 +0,0 @@
1
- import BufferObject from './buffer'
2
-
3
- import getTexture from './getTexture'
4
- import getShader from './getShader'
5
-
6
- interface MeshWorldType {
7
- matrix: number[]
8
- }
9
-
10
- interface GlobalWorldType {
11
- matrix: number[]
12
- proporion: number[]
13
- }
14
-
15
- import { meshType } from '../../../types/Object3D'
16
-
17
- export default function (type: string, painter: WebGLRenderingContext, mesh: meshType, meshWorld: MeshWorldType, globalWorld: GlobalWorldType) {
18
- const shader = getShader(type, painter, mesh)
19
-
20
- // 点数据写入缓冲区
21
- new BufferObject(painter).use()
22
- .write(mesh.geometry.attributes.position.array as Float32Array)
23
- .divide(painter.getAttribLocation(shader.program, "a_position"), mesh.geometry.attributes.position.itemSize, mesh.geometry.attributes.position.itemSize, 0)
24
-
25
- // 写入矩阵
26
- painter.uniformMatrix4fv(painter.getUniformLocation(shader.program, "u_matrix_world"), false, globalWorld.matrix)
27
- painter.uniformMatrix4fv(painter.getUniformLocation(shader.program, "u_matrix_proporion"), false, globalWorld.proporion)
28
- painter.uniformMatrix4fv(painter.getUniformLocation(shader.program, "u_matrix_mesh"), false, meshWorld.matrix)
29
-
30
- // 如果有法向量数据
31
- if (mesh.geometry.attributes.normal) {
32
- new BufferObject(painter).use()
33
- .write(mesh.geometry.attributes.normal.array as Float32Array)
34
- .divide(painter.getAttribLocation(shader.program, "a_normal"), mesh.geometry.attributes.normal.itemSize, mesh.geometry.attributes.normal.itemSize, 0)
35
- }
36
-
37
- // 颜色
38
- if ("color" in mesh.material) {
39
-
40
- // 写入颜色
41
- painter.uniform4f(painter.getUniformLocation(shader.program, "u_color")
42
- , mesh.material.color?.r as number
43
- , mesh.material.color?.g as number
44
- , mesh.material.color?.b as number
45
- , mesh.material.color?.alpha as number)
46
- }
47
-
48
- // 多颜色
49
- if ("colors" in mesh.material) {
50
-
51
- // 写入颜色序列
52
- new BufferObject(painter).use()
53
- .write(mesh.material.colors?.array as Float32Array)
54
- .divide(painter.getAttribLocation(shader.program, "a_color"), mesh.material.colors?.itemSize as number, mesh.material.colors?.itemSize as number, 0)
55
-
56
- }
57
-
58
- // 立方纹理
59
- else if ("cube" in mesh.material) {
60
-
61
- const size = (mesh.material.cube?.right?.image.value as any).width
62
-
63
- painter.uniform1i(painter.getUniformLocation(shader.program, "u_texture"), 0)
64
-
65
- getTexture(type, painter, 'cube').useCube([
66
- mesh.material.cube?.right?.image.value,
67
- mesh.material.cube?.left?.image.value,
68
- mesh.material.cube?.top?.image.value,
69
- mesh.material.cube?.bottom?.image.value,
70
- mesh.material.cube?.far?.image.value,
71
- mesh.material.cube?.near?.image.value
72
- ], size, size)
73
-
74
- }
75
-
76
- // 二维纹理
77
- else if ("image" in mesh.material) {
78
-
79
- painter.uniform1i(painter.getUniformLocation(shader.program, "u_sampler"), 0)
80
-
81
- new BufferObject(painter).use()
82
- .write(mesh.geometry.attributes.uv?.array as Float32Array)
83
- .divide(painter.getAttribLocation(shader.program, "a_textcoord"), mesh.geometry.attributes.uv?.itemSize as number, 2, 0)
84
-
85
- getTexture(type, painter, '2d').useImage(mesh.material.image?.value)
86
-
87
- }
88
-
89
- // 如果使用了索引
90
- if (mesh.geometry.index) {
91
-
92
- // 索引数据
93
- new BufferObject(painter, true).use()
94
- .write(mesh.geometry.index.array as Uint8Array)
95
-
96
- // 绘制
97
- painter.drawElements(painter[mesh.geometry.type], mesh.geometry.index.count, painter.UNSIGNED_BYTE, 0)
98
-
99
- }
100
-
101
- // 否则
102
- else {
103
-
104
- // 绘制
105
- painter.drawArrays(painter[mesh.geometry.type], 0, mesh.geometry.attributes.position.count)
106
-
107
- }
108
-
109
- }
@@ -1,27 +0,0 @@
1
- export default function (painter: WebGLRenderingContext) {
2
- const width = painter.drawingBufferWidth, height = painter.drawingBufferHeight
3
-
4
- const pixels = new Uint8Array(
5
- 4 * width * height
6
- )
7
- painter.readPixels(
8
- 0,
9
- 0,
10
- width,
11
- height,
12
- painter.RGBA,
13
- painter.UNSIGNED_BYTE,
14
- pixels
15
- )
16
-
17
- return function (x: number, y: number): string {
18
- y = height - y
19
-
20
- const pixelR = pixels[4 * (y * width + x)]
21
- const pixelG = pixels[4 * (y * width + x) + 1]
22
- const pixelB = pixels[4 * (y * width + x) + 2]
23
- const pixelA = pixels[4 * (y * width + x) + 3]
24
-
25
- return "rgba(" + pixelR + "," + pixelG + "," + pixelB + "," + pixelA + ")"
26
- }
27
- }
@@ -1,49 +0,0 @@
1
- import type { meshType } from '../../../types/Object3D'
2
-
3
- import ShaderObject from './shader'
4
-
5
- import vsColor from '../../shader/vsColor.c'
6
- import vsColors from '../../shader/vsColors.c'
7
- import vsImage from '../../shader/vsImage.c'
8
- import vsCube from '../../shader/vsCube.c'
9
-
10
- import fsColor from '../../shader/fsColor.c'
11
- import fsColors from '../../shader/fsColors.c'
12
- import fsImage from '../../shader/fsImage.c'
13
- import fsCube from '../../shader/fsCube.c'
14
-
15
- const shaders = {}
16
- export default function (type: string, painter: WebGLRenderingContext, mesh: meshType) {
17
-
18
- const uniqueName = type +
19
- (mesh.material.color ? "1" : "0") +
20
- (mesh.material.colors ? "1" : "0") +
21
- (mesh.material.image ? "1" : "0") +
22
- (mesh.material.cube ? "1" : "0")
23
- if (shaders[uniqueName]) return shaders[uniqueName].use()
24
-
25
- // 顶点着色器
26
- const vertexShader = {
27
- [type + "1000"]: vsColor,
28
- [type + "0100"]: vsColors,
29
- [type + "0010"]: vsImage,
30
- [type + "0001"]: vsCube
31
- }[uniqueName].replace(/uniform +mat4 +u_matrix;/, `
32
- uniform mat4 u_matrix_world;
33
- uniform mat4 u_matrix_mesh;
34
- uniform mat4 u_matrix_proporion;`).replace(/u_matrix +\* +a_position/, "u_matrix_proporion * u_matrix_world * u_matrix_mesh * a_position")
35
-
36
- // 片段(元)着色器
37
- const fragmentShader = {
38
- [type + "1000"]: fsColor,
39
- [type + "0100"]: fsColors,
40
- [type + "0010"]: fsImage,
41
- [type + "0001"]: fsCube
42
- }[uniqueName]
43
-
44
- // 启用着色器
45
- const shader = new ShaderObject(painter).compile(vertexShader, fragmentShader).use()
46
-
47
- shaders[uniqueName] = shader
48
- return shader
49
- }
@@ -1,12 +0,0 @@
1
- import TextureObject from './texture'
2
-
3
- const textures = {}
4
- export default function (type: string, painter: WebGLRenderingContext, kind: string) {
5
- const uniqueName = type + "-" + kind
6
-
7
- if (!textures[uniqueName]) {
8
- textures[uniqueName] = new TextureObject(painter, kind)
9
- }
10
-
11
- return textures[uniqueName]
12
- }
@@ -1,228 +0,0 @@
1
- import type Object3DType from '../../../types/Object3D'
2
- import type Scene3DType from '../../../types/Scene3D'
3
- import type { WebGLmodeType } from "../../../types/getWebGLContext"
4
-
5
- import getWebGLContext from './getWebGLContext'
6
- import getColorFactory from './getColorFactory'
7
- import Matrix4 from '../../Matrix4/index'
8
- import doDraw from './doDraw'
9
- import assemble from '../assemble'
10
-
11
- // 获取区域信息前是否需要刷新区域
12
- let needUpdate = false
13
-
14
- const scale = 10
15
- class WebGL {
16
-
17
- readonly name: string = "WebGL"
18
-
19
- painter: WebGLRenderingContext
20
- private __regionPainter: WebGLRenderingContext
21
-
22
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
23
- private __getColor = (x: number, y: number) => "rgba(0,0,0,1)"
24
-
25
- private __regionList: {
26
- [key: string | number]: Array<number>
27
- } = {} //区域映射表
28
- private __regionAssemble = assemble(0, 1, 0.2, 3)
29
-
30
- private __regionName = "" // 当前绘制的内容名称
31
- private __regionColor = [0, 0, 0, 1] // 当前记录区域应该使用的颜色
32
-
33
- private __scene: Scene3DType = { // 记录着3D世界
34
- children: [],
35
-
36
- // 右手坐标系
37
- matrix: new Matrix4([
38
- 1, 0, 0, 0,
39
- 0, 1, 0, 0,
40
- 0, 0, 1, 0,
41
- 0, 0, 0, scale
42
- ])
43
- }
44
-
45
- private __proporion: number[]
46
-
47
- private __unique: number
48
-
49
- constructor(ViewCanvas: HTMLCanvasElement, RegionCanvas: HTMLCanvasElement | null, mode?: WebGLmodeType) {
50
- this.__unique = new Date().valueOf()
51
-
52
- this.painter = getWebGLContext(ViewCanvas, scale, {}, mode)
53
- if (RegionCanvas) {
54
- this.__regionPainter = getWebGLContext(RegionCanvas, scale, {
55
-
56
- // 如果不设置,涉及到缓冲区的情况可能无法获取颜色
57
- // preserveDrawingBuffer: true
58
- }, mode)
59
- }
60
-
61
- const proportion = this.painter.canvas.width / this.painter.canvas.height
62
-
63
- let xProportion = 1
64
- let yProportion = 1
65
-
66
- if (proportion > 1) {
67
- xProportion = 1 / proportion
68
- } else {
69
- yProportion = proportion
70
- }
71
-
72
- const zProportion = Math.min(xProportion, yProportion)
73
-
74
- this.__proporion = [
75
- xProportion, 0, 0, 0,
76
- 0, yProportion, 0, 0,
77
- 0, 0, zProportion, 0,
78
- 0, 0, 0, 1
79
- ]
80
-
81
- }
82
-
83
- matrix(initMatrix4?: number[]) {
84
- return new Matrix4(initMatrix4)
85
- }
86
-
87
- getMatrix() {
88
- return this.__scene.matrix
89
- }
90
-
91
- getObject3D() {
92
- return this.__scene.children
93
- }
94
-
95
- review(isUpdateRegion = false) {
96
-
97
- if (isUpdateRegion) {
98
- if (this.__regionPainter) {
99
- this.__regionPainter.clearColor(1, 1, 1, 1)
100
- this.__regionPainter.clear(this.__regionPainter.COLOR_BUFFER_BIT)
101
- }
102
- } else {
103
- this.painter.clearColor(1, 1, 1, 1)
104
- this.painter.clear(this.painter.COLOR_BUFFER_BIT)
105
- }
106
-
107
- for (let index = 0; index < this.__scene.children.length; index++) {
108
- this.draw(this.__scene.children[index], isUpdateRegion)
109
- }
110
- return this
111
- }
112
-
113
- private draw(object3D: Object3DType, isUpdateRegion = false) {
114
- if (!isUpdateRegion) needUpdate = true
115
-
116
- const meshWorld = {
117
- matrix: object3D.matrix?.value() as Array<number>
118
- }
119
-
120
- const globalWorld = {
121
- matrix: this.__scene.matrix.value(),
122
- proporion: this.__proporion
123
- }
124
-
125
- // 设置好当前区域
126
- this.setRegion(object3D.region)
127
-
128
- for (let index = 0; index < object3D.mesh.length; index++) {
129
-
130
- if (isUpdateRegion) {
131
- if (this.__regionPainter) {
132
- // 区域上
133
- doDraw("painter" + this.__unique, this.__regionPainter, {
134
- geometry: object3D.mesh[index].geometry,
135
- material: {
136
- color: {
137
- r: this.__regionColor[0],
138
- g: this.__regionColor[1],
139
- b: this.__regionColor[2],
140
- alpha: this.__regionColor[3]
141
- }
142
- }
143
- }, meshWorld, globalWorld)
144
-
145
- this.__getColor = getColorFactory(this.__regionPainter)
146
- }
147
- } else {
148
- // 画布上
149
- doDraw("region" + this.__unique, this.painter, object3D.mesh[index], meshWorld, globalWorld)
150
- }
151
- }
152
-
153
- }
154
-
155
- render(object3D: Object3DType) {
156
- if (!('matrix' in object3D)) object3D.matrix = new Matrix4()
157
- if (!('region' in object3D)) object3D.region = ""
158
-
159
- // 对数据进行校对
160
- for (let index = 0; index < object3D.mesh.length; index++) {
161
- const mesh = object3D.mesh[index]
162
-
163
- if (mesh.geometry.attributes.normal && Array.isArray(mesh.geometry.attributes.normal.array)) {
164
- mesh.geometry.attributes.normal.array = new Float32Array(mesh.geometry.attributes.normal.array)
165
- }
166
-
167
- if (mesh.geometry.attributes.position && Array.isArray(mesh.geometry.attributes.position.array)) {
168
- mesh.geometry.attributes.position.array = new Float32Array(mesh.geometry.attributes.position.array)
169
- }
170
-
171
- if (mesh.geometry.attributes.uv && Array.isArray(mesh.geometry.attributes.uv.array)) {
172
- mesh.geometry.attributes.uv.array = new Float32Array(mesh.geometry.attributes.uv.array)
173
- }
174
-
175
- if (mesh.geometry.index && Array.isArray(mesh.geometry.index.array)) {
176
- mesh.geometry.index.array = new Uint8Array(mesh.geometry.index.array)
177
- }
178
-
179
- if (mesh.material.colors && Array.isArray(mesh.material.colors.array)) {
180
- mesh.material.colors.array = new Float32Array(mesh.material.colors.array)
181
- }
182
-
183
- }
184
-
185
- this.__scene.children.push(object3D)
186
- this.draw(object3D)
187
- }
188
-
189
- // 设置当前绘制区域名称
190
- setRegion(regionName: string | number | undefined) {
191
- this.__regionName = regionName + ""
192
-
193
- if (regionName) {
194
- if (this.__regionList[regionName] == void 0) {
195
- this.__regionList[regionName] = [... this.__regionAssemble(), 1]
196
- }
197
- this.__regionColor = this.__regionList[regionName]
198
- } else {
199
- this.__regionColor = [0, 0, 0, 1]
200
- }
201
- return this
202
- }
203
-
204
- // 获取当前事件触发的区域名称
205
- getRegion(x: number, y: number): Promise<string> {
206
- if (needUpdate) this.review(true)
207
-
208
- needUpdate = false
209
- return new Promise((resolve) => {
210
-
211
- const rgba = this.__getColor(x, y)
212
-
213
- // 查找当前点击的区域
214
- for (const key in this.__regionList) {
215
- const currentRGBA = this.__regionList[key]
216
- if ("rgba(" + currentRGBA[0] * 255 + "," + currentRGBA[1] * 255 + "," + currentRGBA[2] * 255 + "," + currentRGBA[3] * 255 + ")" == rgba) {
217
- resolve(key)
218
- break
219
- }
220
- }
221
- resolve("")
222
-
223
- })
224
- }
225
-
226
- }
227
-
228
- export default WebGL
@@ -1,8 +0,0 @@
1
- precision mediump float;
2
-
3
- uniform vec4 u_color;
4
-
5
- void main()
6
- {
7
- gl_FragColor = u_color;
8
- }
@@ -1,8 +0,0 @@
1
- precision mediump float;
2
-
3
- varying vec4 v_color;
4
-
5
- void main()
6
- {
7
- gl_FragColor = v_color;
8
- }
@@ -1,9 +0,0 @@
1
- precision mediump float;
2
-
3
- varying vec3 v_normal;
4
- uniform samplerCube u_texture;
5
-
6
- void main()
7
- {
8
- gl_FragColor = textureCube(u_texture, normalize(v_normal));
9
- }
@@ -1,9 +0,0 @@
1
- precision mediump float;
2
-
3
- uniform sampler2D u_sampler;
4
- varying vec2 v_textcoord;
5
-
6
- void main()
7
- {
8
- gl_FragColor = texture2D(u_sampler, v_textcoord);
9
- }
@@ -1,18 +0,0 @@
1
- attribute vec4 a_position;
2
- uniform mat4 u_matrix;
3
-
4
- void main()
5
- {
6
- vec4 temp = u_matrix * a_position;
7
-
8
- // 把原生的WebGL使用的左手坐标系变成了右手坐标系
9
- temp.z = -1.0 * temp.z;
10
-
11
- // 表示眼睛距离vec4(0.0,0.0,1.0)的距离
12
- float dist = 4.0;
13
-
14
- // 使用投影直接计算
15
- // 为保证纹理和相对位置正确
16
- // x、y、z的改变满足线性变换
17
- gl_Position = vec4((dist + 1.0) * temp.x, (dist + 1.0) * temp.y, dist * (dist + temp.z) + 1.0 - dist * dist, temp.w * 2.0 * (dist + temp.z));
18
- }