vislite 0.3.1 → 0.4.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 (128) hide show
  1. package/AUTHORS.txt +6 -6
  2. package/CHANGELOG +7 -1
  3. package/LICENSE +20 -20
  4. package/README.md +53 -53
  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 -2
  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/OralCanvas/index.es.js +9 -2
  20. package/lib/OralCanvas/index.es.min.js +3 -3
  21. package/lib/OralWebGL/index.es.js +2 -2
  22. package/lib/OralWebGL/index.es.min.js +2 -2
  23. package/lib/SVG/index.umd.js +2 -2
  24. package/lib/SVG/index.umd.min.js +2 -2
  25. package/lib/Shader/index.umd.js +2 -2
  26. package/lib/Shader/index.umd.min.js +2 -2
  27. package/lib/Texture/index.umd.js +2 -2
  28. package/lib/Texture/index.umd.min.js +2 -2
  29. package/lib/WebGL/index.umd.js +2 -2
  30. package/lib/WebGL/index.umd.min.js +2 -2
  31. package/lib/animation/index.umd.js +2 -2
  32. package/lib/animation/index.umd.min.js +2 -2
  33. package/lib/getLoopColors/index.umd.js +2 -2
  34. package/lib/getLoopColors/index.umd.min.js +2 -2
  35. package/lib/getWebGLContext/index.umd.js +2 -2
  36. package/lib/getWebGLContext/index.umd.min.js +2 -2
  37. package/lib/index.umd.js +9 -2
  38. package/lib/index.umd.min.js +3 -3
  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/viewHandler/index.umd.js +2 -2
  44. package/lib/viewHandler/index.umd.min.js +2 -2
  45. package/miniprogram/ui-canvas/index.js +99 -99
  46. package/miniprogram/ui-canvas/index.json +4 -4
  47. package/miniprogram/ui-canvas/index.wxml +4 -4
  48. package/miniprogram/ui-canvas/index.wxss +5 -5
  49. package/package/Buffer/index.ts +1 -1
  50. package/package/Canvas/index.ts +1 -1
  51. package/package/Cardinal/index.ts +2 -2
  52. package/package/Eoap/index.ts +2 -2
  53. package/package/Hermite/index.ts +2 -2
  54. package/package/Matrix4/index.ts +2 -2
  55. package/package/Mercator/index.ts +2 -2
  56. package/package/OralCanvas/index.ts +1 -1
  57. package/package/OralWebGL/index.ts +1 -1
  58. package/package/SVG/index.ts +2 -2
  59. package/package/Shader/index.ts +1 -1
  60. package/package/Texture/index.ts +1 -1
  61. package/package/WebGL/index.ts +1 -1
  62. package/package/animation/index.ts +2 -2
  63. package/package/getLoopColors/index.ts +2 -2
  64. package/package/getWebGLContext/index.ts +39 -39
  65. package/package/index.ts +56 -56
  66. package/package/rotate/index.ts +2 -2
  67. package/package/ruler/index.ts +2 -2
  68. package/package/viewHandler/index.ts +2 -2
  69. package/package.json +2 -2
  70. package/src/Canvas.ts +56 -56
  71. package/src/Eoap.ts +66 -66
  72. package/src/Matrix4/index.ts +76 -76
  73. package/src/Matrix4/move.ts +12 -12
  74. package/src/Matrix4/rotate.ts +15 -15
  75. package/src/Matrix4/scale.ts +11 -11
  76. package/src/Matrix4/transform.ts +64 -64
  77. package/src/Mercator.ts +26 -26
  78. package/src/SVG.ts +23 -23
  79. package/src/WebGL.ts +52 -52
  80. package/src/animation.ts +99 -99
  81. package/src/common/canvas/arc.ts +50 -50
  82. package/src/common/canvas/config.ts +157 -157
  83. package/src/common/canvas/gradient.ts +29 -29
  84. package/src/common/canvas/index.ts +14 -0
  85. package/src/common/canvas/painter.ts +355 -355
  86. package/src/common/canvas/texts.ts +18 -18
  87. package/src/common/setStyle.ts +5 -5
  88. package/src/common/svg/config.ts +173 -173
  89. package/src/common/svg/index.ts +334 -334
  90. package/src/common/svg/tool.ts +43 -43
  91. package/src/common/webgl/buffer.ts +79 -79
  92. package/src/common/webgl/doDraw.ts +106 -106
  93. package/src/common/webgl/getColorFactory.ts +26 -26
  94. package/src/common/webgl/getShader.ts +75 -75
  95. package/src/common/webgl/getTexture.ts +11 -11
  96. package/src/common/webgl/getWebGLContext.ts +27 -27
  97. package/src/common/webgl/index.ts +216 -216
  98. package/src/common/webgl/shader.ts +75 -75
  99. package/src/common/webgl/texture.ts +99 -99
  100. package/src/getLoopColors.ts +42 -42
  101. package/src/interpolation/Cardinal.ts +109 -109
  102. package/src/interpolation/Hermite.ts +65 -65
  103. package/src/rotate.ts +7 -7
  104. package/src/ruler.ts +99 -99
  105. package/src/viewHandler.ts +139 -139
  106. package/types/Buffer.d.ts +23 -23
  107. package/types/Canvas.d.ts +288 -282
  108. package/types/CanvasConfig.d.ts +80 -80
  109. package/types/Cardinal.d.ts +13 -13
  110. package/types/Gradient.d.ts +14 -14
  111. package/types/Hermite.d.ts +18 -18
  112. package/types/Map.d.ts +9 -9
  113. package/types/Matrix4.d.ts +42 -42
  114. package/types/Object3D.d.ts +113 -113
  115. package/types/SVG.d.ts +210 -210
  116. package/types/SVGConfig.d.ts +58 -58
  117. package/types/Scene3D.d.ts +6 -6
  118. package/types/Shader.d.ts +15 -15
  119. package/types/Texture.d.ts +16 -16
  120. package/types/WebGL.d.ts +38 -38
  121. package/types/animation.d.ts +6 -6
  122. package/types/getLoopColors.d.ts +2 -2
  123. package/types/getWebGLContext.d.ts +2 -2
  124. package/types/index.d.ts +129 -129
  125. package/types/rotate.d.ts +2 -2
  126. package/types/ruler.d.ts +2 -2
  127. package/types/viewHandler.d.ts +12 -12
  128. package/uni-app/ui-canvas.vue +212 -212
@@ -1,76 +1,76 @@
1
- import ShaderObject from './shader'
2
-
3
- import { meshType } from '../../../types/Object3D'
4
-
5
- let shaders = {}
6
- export default function (type: string, painter: WebGLRenderingContext, mesh: meshType) {
7
-
8
- let uniqueName = type +
9
- (mesh.geometry.attributes.normal ? "1" : "0") +
10
- (mesh.material.image ? "1" : "0") +
11
- (mesh.material.color ? "1" : "0") +
12
- (mesh.material.colors ? "1" : "0") +
13
- (mesh.material.cube ? "1" : "0")
14
- if (shaders[uniqueName]) return shaders[uniqueName].use()
15
-
16
- // 顶点着色器
17
- let vertexShader = `
18
- attribute vec4 a_position;
19
-
20
- ${mesh.geometry.attributes.normal ? 'attribute vec4 a_normal;varying vec3 v_normal;' : ''}
21
-
22
- uniform mat4 u_matrix_world;
23
- uniform mat4 u_matrix_mesh;
24
- uniform mat4 u_matrix_proporion;
25
-
26
- ${mesh.material.image ? 'attribute vec2 a_textcoord;varying vec2 v_textcoord;' : ''}
27
-
28
- ${mesh.material.colors ? 'attribute vec4 a_color;varying vec4 v_color;' : ''}
29
-
30
- void main(){
31
- vec4 temp = a_position;
32
-
33
- // 应用变换矩阵:屏幕比调平矩阵、世界矩阵、物体矩阵
34
- temp = u_matrix_proporion * u_matrix_world * u_matrix_mesh * temp;
35
-
36
- // 把原生的WebGL使用的左手坐标系变成了右手坐标系
37
- temp.z = -1.0 * temp.z;
38
-
39
- // 表示眼睛距离vec4(0.0,0.0,1.0)的距离
40
- float dist = 4.0;
41
-
42
- // 使用投影直接计算
43
- // 为保证纹理和相对位置正确
44
- // x、y、z的改变满足线性变换
45
- 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));
46
-
47
- ${mesh.geometry.attributes.normal ? 'v_normal = normalize(vec3(a_normal));' : ''}
48
- ${mesh.material.image ? 'v_textcoord = a_textcoord;' : ''}
49
- ${mesh.material.colors ? 'v_color=a_color;' : ''}
50
- }
51
- `
52
-
53
- // 片段(元)着色器
54
- let fragmentShader = `
55
- precision mediump float;
56
-
57
- ${mesh.geometry.attributes.normal ? 'varying vec3 v_normal;' : ''}
58
- ${mesh.material.color ? 'uniform vec4 u_color;' : ''}
59
- ${mesh.material.cube ? 'uniform samplerCube u_texture;' : ''}
60
- ${mesh.material.image ? 'uniform sampler2D u_sampler;varying vec2 v_textcoord;' : ''}
61
- ${mesh.material.colors ? 'varying vec4 v_color;' : ''}
62
-
63
- void main(){
64
- ${mesh.material.color ? 'gl_FragColor = u_color;' : ''}
65
- ${mesh.material.cube ? 'gl_FragColor = textureCube(u_texture,normalize(v_normal));' : ''}
66
- ${mesh.material.image ? 'gl_FragColor = texture2D(u_sampler,v_textcoord);' : ''}
67
- ${mesh.material.colors ? 'gl_FragColor=v_color;' : ''}
68
- }
69
- `
70
-
71
- // 启用着色器
72
- let shader = new ShaderObject(painter).compile(vertexShader, fragmentShader).use()
73
-
74
- shaders[uniqueName] = shader
75
- return shader
1
+ import ShaderObject from './shader'
2
+
3
+ import { meshType } from '../../../types/Object3D'
4
+
5
+ let shaders = {}
6
+ export default function (type: string, painter: WebGLRenderingContext, mesh: meshType) {
7
+
8
+ let uniqueName = type +
9
+ (mesh.geometry.attributes.normal ? "1" : "0") +
10
+ (mesh.material.image ? "1" : "0") +
11
+ (mesh.material.color ? "1" : "0") +
12
+ (mesh.material.colors ? "1" : "0") +
13
+ (mesh.material.cube ? "1" : "0")
14
+ if (shaders[uniqueName]) return shaders[uniqueName].use()
15
+
16
+ // 顶点着色器
17
+ let vertexShader = `
18
+ attribute vec4 a_position;
19
+
20
+ ${mesh.geometry.attributes.normal ? 'attribute vec4 a_normal;varying vec3 v_normal;' : ''}
21
+
22
+ uniform mat4 u_matrix_world;
23
+ uniform mat4 u_matrix_mesh;
24
+ uniform mat4 u_matrix_proporion;
25
+
26
+ ${mesh.material.image ? 'attribute vec2 a_textcoord;varying vec2 v_textcoord;' : ''}
27
+
28
+ ${mesh.material.colors ? 'attribute vec4 a_color;varying vec4 v_color;' : ''}
29
+
30
+ void main(){
31
+ vec4 temp = a_position;
32
+
33
+ // 应用变换矩阵:屏幕比调平矩阵、世界矩阵、物体矩阵
34
+ temp = u_matrix_proporion * u_matrix_world * u_matrix_mesh * temp;
35
+
36
+ // 把原生的WebGL使用的左手坐标系变成了右手坐标系
37
+ temp.z = -1.0 * temp.z;
38
+
39
+ // 表示眼睛距离vec4(0.0,0.0,1.0)的距离
40
+ float dist = 4.0;
41
+
42
+ // 使用投影直接计算
43
+ // 为保证纹理和相对位置正确
44
+ // x、y、z的改变满足线性变换
45
+ 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));
46
+
47
+ ${mesh.geometry.attributes.normal ? 'v_normal = normalize(vec3(a_normal));' : ''}
48
+ ${mesh.material.image ? 'v_textcoord = a_textcoord;' : ''}
49
+ ${mesh.material.colors ? 'v_color=a_color;' : ''}
50
+ }
51
+ `
52
+
53
+ // 片段(元)着色器
54
+ let fragmentShader = `
55
+ precision mediump float;
56
+
57
+ ${mesh.geometry.attributes.normal ? 'varying vec3 v_normal;' : ''}
58
+ ${mesh.material.color ? 'uniform vec4 u_color;' : ''}
59
+ ${mesh.material.cube ? 'uniform samplerCube u_texture;' : ''}
60
+ ${mesh.material.image ? 'uniform sampler2D u_sampler;varying vec2 v_textcoord;' : ''}
61
+ ${mesh.material.colors ? 'varying vec4 v_color;' : ''}
62
+
63
+ void main(){
64
+ ${mesh.material.color ? 'gl_FragColor = u_color;' : ''}
65
+ ${mesh.material.cube ? 'gl_FragColor = textureCube(u_texture,normalize(v_normal));' : ''}
66
+ ${mesh.material.image ? 'gl_FragColor = texture2D(u_sampler,v_textcoord);' : ''}
67
+ ${mesh.material.colors ? 'gl_FragColor=v_color;' : ''}
68
+ }
69
+ `
70
+
71
+ // 启用着色器
72
+ let shader = new ShaderObject(painter).compile(vertexShader, fragmentShader).use()
73
+
74
+ shaders[uniqueName] = shader
75
+ return shader
76
76
  }
@@ -1,12 +1,12 @@
1
- import TextureObject from './texture'
2
-
3
- let textures = {}
4
- export default function (type: string, painter: WebGLRenderingContext, kind: string) {
5
- let uniqueName = type + "-" + kind
6
-
7
- if (!textures[uniqueName]) {
8
- textures[uniqueName] = new TextureObject(painter, kind)
9
- }
10
-
11
- return textures[uniqueName]
1
+ import TextureObject from './texture'
2
+
3
+ let textures = {}
4
+ export default function (type: string, painter: WebGLRenderingContext, kind: string) {
5
+ let uniqueName = type + "-" + kind
6
+
7
+ if (!textures[uniqueName]) {
8
+ textures[uniqueName] = new TextureObject(painter, kind)
9
+ }
10
+
11
+ return textures[uniqueName]
12
12
  }
@@ -1,28 +1,28 @@
1
- interface OptsType {
2
- preserveDrawingBuffer?: boolean
3
- }
4
- export default function (canvas: HTMLCanvasElement, scale: number, opts: OptsType = {}) {
5
- let names = ["webgl", "experimental-webgl", "webkit-3d", "moz-webgl"], painter = null
6
- for (let i = 0; i < names.length; i++) {
7
- try {
8
- painter = canvas.getContext(names[i], opts)
9
- } catch (e) { }
10
- if (painter) break
11
- }
12
- if (!painter) throw new Error('Non canvas or browser does not support webgl.')
13
-
14
- let width = painter.canvas.width, height = painter.canvas.height
15
-
16
- let viewWidth = width * scale
17
- let viewHeight = height * scale
18
-
19
- painter.viewport((width - viewWidth) * 0.5, (height - viewHeight) * 0.5, viewWidth, viewHeight)
20
-
21
- // https://developer.mozilla.org/zh-CN/docs/Web/API/WebGLRenderingContext/depthFunc
22
- painter.depthFunc(painter.LEQUAL)
23
-
24
- // 开启深度计算
25
- painter.enable(painter.DEPTH_TEST);
26
-
27
- return painter
1
+ interface OptsType {
2
+ preserveDrawingBuffer?: boolean
3
+ }
4
+ export default function (canvas: HTMLCanvasElement, scale: number, opts: OptsType = {}) {
5
+ let names = ["webgl", "experimental-webgl", "webkit-3d", "moz-webgl"], painter = null
6
+ for (let i = 0; i < names.length; i++) {
7
+ try {
8
+ painter = canvas.getContext(names[i], opts)
9
+ } catch (e) { }
10
+ if (painter) break
11
+ }
12
+ if (!painter) throw new Error('Non canvas or browser does not support webgl.')
13
+
14
+ let width = painter.canvas.width, height = painter.canvas.height
15
+
16
+ let viewWidth = width * scale
17
+ let viewHeight = height * scale
18
+
19
+ painter.viewport((width - viewWidth) * 0.5, (height - viewHeight) * 0.5, viewWidth, viewHeight)
20
+
21
+ // https://developer.mozilla.org/zh-CN/docs/Web/API/WebGLRenderingContext/depthFunc
22
+ painter.depthFunc(painter.LEQUAL)
23
+
24
+ // 开启深度计算
25
+ painter.enable(painter.DEPTH_TEST);
26
+
27
+ return painter
28
28
  }
@@ -1,217 +1,217 @@
1
- import getWebGLContext from './getWebGLContext'
2
- import Object3DType from '../../../types/Object3D'
3
- import Scene3DType from '../../../types/Scene3D'
4
- import getColorFactory from './getColorFactory'
5
- import Matrix4 from '..//../Matrix4/index'
6
- import doDraw from './doDraw'
7
- import assemble from '../assemble'
8
-
9
- // 获取区域信息前是否需要刷新区域
10
- let needUpdate = false
11
-
12
- let scale = 10
13
- class WebGL {
14
-
15
- readonly name: string = "WebGL"
16
-
17
- painter: WebGLRenderingContext
18
- private __regionPainter: WebGLRenderingContext
19
-
20
- private __getColor = (x: number, y: number) => "rgba(0,0,0,1)"
21
-
22
- private __regionList = {} //区域映射表
23
- private __regionAssemble = assemble(0, 1, 0.2, 3)
24
-
25
- private __regionName = "" // 当前绘制的内容名称
26
- private __regionColor = [0, 0, 0, 1] // 当前记录区域应该使用的颜色
27
-
28
- private __scene: Scene3DType = { // 记录着3D世界
29
- children: [],
30
-
31
- // 右手坐标系
32
- matrix: new Matrix4([
33
- 1, 0, 0, 0,
34
- 0, 1, 0, 0,
35
- 0, 0, 1, 0,
36
- 0, 0, 0, scale
37
- ])
38
- }
39
-
40
- private __proporion: number[]
41
-
42
- private __unique: number
43
-
44
- constructor(ViewCanvas: HTMLCanvasElement, RegionCanvas: HTMLCanvasElement) {
45
- this.__unique = new Date().valueOf()
46
-
47
- this.painter = getWebGLContext(ViewCanvas, scale)
48
- this.__regionPainter = getWebGLContext(RegionCanvas, scale, {
49
-
50
- // 如果不设置,涉及到缓冲区的情况可能无法获取颜色
51
- // preserveDrawingBuffer: true
52
- })
53
-
54
- let proportion = this.painter.canvas.width / this.painter.canvas.height
55
-
56
- let xProportion = 1
57
- let yProportion = 1
58
-
59
- if (proportion > 1) {
60
- xProportion = 1 / proportion
61
- } else {
62
- yProportion = proportion
63
- }
64
-
65
- let zProportion = Math.min(xProportion, yProportion)
66
-
67
- this.__proporion = [
68
- xProportion, 0, 0, 0,
69
- 0, yProportion, 0, 0,
70
- 0, 0, zProportion, 0,
71
- 0, 0, 0, 1
72
- ]
73
-
74
- }
75
-
76
- matrix(initMatrix4?: number[]) {
77
- return new Matrix4(initMatrix4)
78
- }
79
-
80
- getMatrix() {
81
- return this.__scene.matrix
82
- }
83
-
84
- getObject3D() {
85
- return this.__scene.children
86
- }
87
-
88
- review(isUpdateRegion = false) {
89
-
90
- if (isUpdateRegion) {
91
- this.__regionPainter.clearColor(1, 1, 1, 1)
92
- this.__regionPainter.clear(this.__regionPainter.COLOR_BUFFER_BIT)
93
- } else {
94
- this.painter.clearColor(1, 1, 1, 1)
95
- this.painter.clear(this.painter.COLOR_BUFFER_BIT)
96
- }
97
-
98
- for (let index = 0; index < this.__scene.children.length; index++) {
99
- this.draw(this.__scene.children[index], isUpdateRegion)
100
- }
101
- return this
102
- }
103
-
104
- private draw(object3D: Object3DType, isUpdateRegion = false) {
105
- if (!isUpdateRegion) needUpdate = true
106
-
107
- let meshWorld = {
108
- matrix: object3D.matrix.value()
109
- }
110
-
111
- let globalWorld = {
112
- matrix: this.__scene.matrix.value(),
113
- proporion: this.__proporion
114
- }
115
-
116
- // 设置好当前区域
117
- this.setRegion(object3D.region)
118
-
119
- for (let index = 0; index < object3D.mesh.length; index++) {
120
-
121
- if (isUpdateRegion) {
122
- // 区域上
123
- doDraw("painter" + this.__unique, this.__regionPainter, {
124
- geometry: object3D.mesh[index].geometry,
125
- material: {
126
- color: {
127
- r: this.__regionColor[0],
128
- g: this.__regionColor[1],
129
- b: this.__regionColor[2],
130
- alpha: this.__regionColor[3]
131
- }
132
- }
133
- }, meshWorld, globalWorld)
134
-
135
- this.__getColor = getColorFactory(this.__regionPainter)
136
- } else {
137
- // 画布上
138
- doDraw("region" + this.__unique, this.painter, object3D.mesh[index], meshWorld, globalWorld)
139
- }
140
- }
141
-
142
- }
143
-
144
- render(object3D: Object3DType) {
145
- if (!('matrix' in object3D)) object3D.matrix = new Matrix4()
146
- if (!('region' in object3D)) object3D.region = ""
147
-
148
- // 对数据进行校对
149
- for (let index = 0; index < object3D.mesh.length; index++) {
150
- let mesh = object3D.mesh[index]
151
-
152
- if (mesh.geometry.attributes.normal && Array.isArray(mesh.geometry.attributes.normal.array)) {
153
- mesh.geometry.attributes.normal.array = new Float32Array(mesh.geometry.attributes.normal.array)
154
- }
155
-
156
- if (mesh.geometry.attributes.position && Array.isArray(mesh.geometry.attributes.position.array)) {
157
- mesh.geometry.attributes.position.array = new Float32Array(mesh.geometry.attributes.position.array)
158
- }
159
-
160
- if (mesh.geometry.attributes.uv && Array.isArray(mesh.geometry.attributes.uv.array)) {
161
- mesh.geometry.attributes.uv.array = new Float32Array(mesh.geometry.attributes.uv.array)
162
- }
163
-
164
- if (mesh.geometry.index && Array.isArray(mesh.geometry.index.array)) {
165
- mesh.geometry.index.array = new Uint8Array(mesh.geometry.index.array)
166
- }
167
-
168
- if (mesh.material.colors && Array.isArray(mesh.material.colors.array)) {
169
- mesh.material.colors.array = new Float32Array(mesh.material.colors.array)
170
- }
171
-
172
- }
173
-
174
- this.__scene.children.push(object3D)
175
- this.draw(object3D)
176
- }
177
-
178
- // 设置当前绘制区域名称
179
- setRegion(regionName: string | number) {
180
- this.__regionName = regionName + ""
181
-
182
- if (regionName) {
183
- if (this.__regionList[regionName] == undefined) {
184
- this.__regionList[regionName] = [... this.__regionAssemble(), 1]
185
- }
186
- this.__regionColor = this.__regionList[regionName]
187
- } else {
188
- this.__regionColor = [0, 0, 0, 1]
189
- }
190
- return this
191
- }
192
-
193
- // 获取当前事件触发的区域名称
194
- getRegion(x: number, y: number): Promise<string> {
195
- if (needUpdate) this.review(true)
196
-
197
- needUpdate = false
198
- return new Promise((resolve, reject) => {
199
-
200
- let rgba = this.__getColor(x, y)
201
-
202
- // 查找当前点击的区域
203
- for (let key in this.__regionList) {
204
- let currentRGBA = this.__regionList[key]
205
- if ("rgba(" + currentRGBA[0] * 255 + "," + currentRGBA[1] * 255 + "," + currentRGBA[2] * 255 + "," + currentRGBA[3] * 255 + ")" == rgba) {
206
- resolve(key)
207
- break
208
- }
209
- }
210
- resolve("")
211
-
212
- })
213
- }
214
-
215
- }
216
-
1
+ import getWebGLContext from './getWebGLContext'
2
+ import Object3DType from '../../../types/Object3D'
3
+ import Scene3DType from '../../../types/Scene3D'
4
+ import getColorFactory from './getColorFactory'
5
+ import Matrix4 from '..//../Matrix4/index'
6
+ import doDraw from './doDraw'
7
+ import assemble from '../assemble'
8
+
9
+ // 获取区域信息前是否需要刷新区域
10
+ let needUpdate = false
11
+
12
+ let scale = 10
13
+ class WebGL {
14
+
15
+ readonly name: string = "WebGL"
16
+
17
+ painter: WebGLRenderingContext
18
+ private __regionPainter: WebGLRenderingContext
19
+
20
+ private __getColor = (x: number, y: number) => "rgba(0,0,0,1)"
21
+
22
+ private __regionList = {} //区域映射表
23
+ private __regionAssemble = assemble(0, 1, 0.2, 3)
24
+
25
+ private __regionName = "" // 当前绘制的内容名称
26
+ private __regionColor = [0, 0, 0, 1] // 当前记录区域应该使用的颜色
27
+
28
+ private __scene: Scene3DType = { // 记录着3D世界
29
+ children: [],
30
+
31
+ // 右手坐标系
32
+ matrix: new Matrix4([
33
+ 1, 0, 0, 0,
34
+ 0, 1, 0, 0,
35
+ 0, 0, 1, 0,
36
+ 0, 0, 0, scale
37
+ ])
38
+ }
39
+
40
+ private __proporion: number[]
41
+
42
+ private __unique: number
43
+
44
+ constructor(ViewCanvas: HTMLCanvasElement, RegionCanvas: HTMLCanvasElement) {
45
+ this.__unique = new Date().valueOf()
46
+
47
+ this.painter = getWebGLContext(ViewCanvas, scale)
48
+ this.__regionPainter = getWebGLContext(RegionCanvas, scale, {
49
+
50
+ // 如果不设置,涉及到缓冲区的情况可能无法获取颜色
51
+ // preserveDrawingBuffer: true
52
+ })
53
+
54
+ let proportion = this.painter.canvas.width / this.painter.canvas.height
55
+
56
+ let xProportion = 1
57
+ let yProportion = 1
58
+
59
+ if (proportion > 1) {
60
+ xProportion = 1 / proportion
61
+ } else {
62
+ yProportion = proportion
63
+ }
64
+
65
+ let zProportion = Math.min(xProportion, yProportion)
66
+
67
+ this.__proporion = [
68
+ xProportion, 0, 0, 0,
69
+ 0, yProportion, 0, 0,
70
+ 0, 0, zProportion, 0,
71
+ 0, 0, 0, 1
72
+ ]
73
+
74
+ }
75
+
76
+ matrix(initMatrix4?: number[]) {
77
+ return new Matrix4(initMatrix4)
78
+ }
79
+
80
+ getMatrix() {
81
+ return this.__scene.matrix
82
+ }
83
+
84
+ getObject3D() {
85
+ return this.__scene.children
86
+ }
87
+
88
+ review(isUpdateRegion = false) {
89
+
90
+ if (isUpdateRegion) {
91
+ this.__regionPainter.clearColor(1, 1, 1, 1)
92
+ this.__regionPainter.clear(this.__regionPainter.COLOR_BUFFER_BIT)
93
+ } else {
94
+ this.painter.clearColor(1, 1, 1, 1)
95
+ this.painter.clear(this.painter.COLOR_BUFFER_BIT)
96
+ }
97
+
98
+ for (let index = 0; index < this.__scene.children.length; index++) {
99
+ this.draw(this.__scene.children[index], isUpdateRegion)
100
+ }
101
+ return this
102
+ }
103
+
104
+ private draw(object3D: Object3DType, isUpdateRegion = false) {
105
+ if (!isUpdateRegion) needUpdate = true
106
+
107
+ let meshWorld = {
108
+ matrix: object3D.matrix.value()
109
+ }
110
+
111
+ let globalWorld = {
112
+ matrix: this.__scene.matrix.value(),
113
+ proporion: this.__proporion
114
+ }
115
+
116
+ // 设置好当前区域
117
+ this.setRegion(object3D.region)
118
+
119
+ for (let index = 0; index < object3D.mesh.length; index++) {
120
+
121
+ if (isUpdateRegion) {
122
+ // 区域上
123
+ doDraw("painter" + this.__unique, this.__regionPainter, {
124
+ geometry: object3D.mesh[index].geometry,
125
+ material: {
126
+ color: {
127
+ r: this.__regionColor[0],
128
+ g: this.__regionColor[1],
129
+ b: this.__regionColor[2],
130
+ alpha: this.__regionColor[3]
131
+ }
132
+ }
133
+ }, meshWorld, globalWorld)
134
+
135
+ this.__getColor = getColorFactory(this.__regionPainter)
136
+ } else {
137
+ // 画布上
138
+ doDraw("region" + this.__unique, this.painter, object3D.mesh[index], meshWorld, globalWorld)
139
+ }
140
+ }
141
+
142
+ }
143
+
144
+ render(object3D: Object3DType) {
145
+ if (!('matrix' in object3D)) object3D.matrix = new Matrix4()
146
+ if (!('region' in object3D)) object3D.region = ""
147
+
148
+ // 对数据进行校对
149
+ for (let index = 0; index < object3D.mesh.length; index++) {
150
+ let mesh = object3D.mesh[index]
151
+
152
+ if (mesh.geometry.attributes.normal && Array.isArray(mesh.geometry.attributes.normal.array)) {
153
+ mesh.geometry.attributes.normal.array = new Float32Array(mesh.geometry.attributes.normal.array)
154
+ }
155
+
156
+ if (mesh.geometry.attributes.position && Array.isArray(mesh.geometry.attributes.position.array)) {
157
+ mesh.geometry.attributes.position.array = new Float32Array(mesh.geometry.attributes.position.array)
158
+ }
159
+
160
+ if (mesh.geometry.attributes.uv && Array.isArray(mesh.geometry.attributes.uv.array)) {
161
+ mesh.geometry.attributes.uv.array = new Float32Array(mesh.geometry.attributes.uv.array)
162
+ }
163
+
164
+ if (mesh.geometry.index && Array.isArray(mesh.geometry.index.array)) {
165
+ mesh.geometry.index.array = new Uint8Array(mesh.geometry.index.array)
166
+ }
167
+
168
+ if (mesh.material.colors && Array.isArray(mesh.material.colors.array)) {
169
+ mesh.material.colors.array = new Float32Array(mesh.material.colors.array)
170
+ }
171
+
172
+ }
173
+
174
+ this.__scene.children.push(object3D)
175
+ this.draw(object3D)
176
+ }
177
+
178
+ // 设置当前绘制区域名称
179
+ setRegion(regionName: string | number) {
180
+ this.__regionName = regionName + ""
181
+
182
+ if (regionName) {
183
+ if (this.__regionList[regionName] == undefined) {
184
+ this.__regionList[regionName] = [... this.__regionAssemble(), 1]
185
+ }
186
+ this.__regionColor = this.__regionList[regionName]
187
+ } else {
188
+ this.__regionColor = [0, 0, 0, 1]
189
+ }
190
+ return this
191
+ }
192
+
193
+ // 获取当前事件触发的区域名称
194
+ getRegion(x: number, y: number): Promise<string> {
195
+ if (needUpdate) this.review(true)
196
+
197
+ needUpdate = false
198
+ return new Promise((resolve, reject) => {
199
+
200
+ let rgba = this.__getColor(x, y)
201
+
202
+ // 查找当前点击的区域
203
+ for (let key in this.__regionList) {
204
+ let currentRGBA = this.__regionList[key]
205
+ if ("rgba(" + currentRGBA[0] * 255 + "," + currentRGBA[1] * 255 + "," + currentRGBA[2] * 255 + "," + currentRGBA[3] * 255 + ")" == rgba) {
206
+ resolve(key)
207
+ break
208
+ }
209
+ }
210
+ resolve("")
211
+
212
+ })
213
+ }
214
+
215
+ }
216
+
217
217
  export default WebGL