vislite 0.7.0 → 0.7.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 (90) hide show
  1. package/CHANGELOG +11 -1
  2. package/README.md +135 -135
  3. package/lib/Buffer/index.umd.js +2 -2
  4. package/lib/Buffer/index.umd.min.js +2 -2
  5. package/lib/Canvas/index.umd.js +19 -13
  6. package/lib/Canvas/index.umd.min.js +3 -3
  7. package/lib/Cardinal/index.umd.js +2 -2
  8. package/lib/Cardinal/index.umd.min.js +2 -2
  9. package/lib/Eoap/index.umd.js +2 -2
  10. package/lib/Eoap/index.umd.min.js +2 -2
  11. package/lib/Geometry/index.umd.js +2 -2
  12. package/lib/Geometry/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 +11 -7
  20. package/lib/OralCanvas/index.es.min.js +3 -3
  21. package/lib/OralWebGL/index.es.js +2 -3
  22. package/lib/OralWebGL/index.es.min.js +3 -3
  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 -3
  26. package/lib/Shader/index.umd.min.js +3 -3
  27. package/lib/Texture/index.umd.js +2 -2
  28. package/lib/Texture/index.umd.min.js +2 -2
  29. package/lib/TreeLayout/index.umd.js +2 -2
  30. package/lib/TreeLayout/index.umd.min.js +2 -2
  31. package/lib/WebGL/index.umd.js +2 -3
  32. package/lib/WebGL/index.umd.min.js +3 -3
  33. package/lib/animation/index.umd.js +2 -2
  34. package/lib/animation/index.umd.min.js +2 -2
  35. package/lib/formatColor/index.umd.js +2 -2
  36. package/lib/formatColor/index.umd.min.js +2 -2
  37. package/lib/getLoopColors/index.umd.js +2 -2
  38. package/lib/getLoopColors/index.umd.min.js +2 -2
  39. package/lib/getWebGLContext/index.umd.js +2 -2
  40. package/lib/getWebGLContext/index.umd.min.js +2 -2
  41. package/lib/index.umd.js +19 -14
  42. package/lib/index.umd.min.js +3 -3
  43. package/lib/move/index.umd.js +2 -2
  44. package/lib/move/index.umd.min.js +2 -2
  45. package/lib/resizeObserver/index.umd.js +2 -2
  46. package/lib/resizeObserver/index.umd.min.js +2 -2
  47. package/lib/rotate/index.umd.js +2 -2
  48. package/lib/rotate/index.umd.min.js +2 -2
  49. package/lib/ruler/index.umd.js +2 -2
  50. package/lib/ruler/index.umd.min.js +2 -2
  51. package/lib/scale/index.umd.js +2 -2
  52. package/lib/scale/index.umd.min.js +2 -2
  53. package/lib/throttle/index.umd.js +2 -2
  54. package/lib/throttle/index.umd.min.js +2 -2
  55. package/lib/viewHandler/index.umd.js +2 -2
  56. package/lib/viewHandler/index.umd.min.js +2 -2
  57. package/package.json +1 -1
  58. package/src/Canvas.ts +8 -6
  59. package/src/Geometry.ts +0 -1
  60. package/src/Matrix4/rotate.ts +15 -15
  61. package/src/Matrix4/transform.ts +64 -64
  62. package/src/WebGL.ts +66 -66
  63. package/src/common/assemble.ts +22 -22
  64. package/src/common/canvas/index.ts +10 -4
  65. package/src/common/canvas/painter.ts +1 -1
  66. package/src/common/geometry/prism-vertical.ts +40 -40
  67. package/src/common/geometry/tool/rotateLine.ts +35 -35
  68. package/src/common/setStyle.ts +5 -5
  69. package/src/common/svg/config.ts +190 -190
  70. package/src/common/svg/index.ts +356 -356
  71. package/src/common/svg/tool.ts +44 -44
  72. package/src/common/tree/toInnerTree.ts +59 -59
  73. package/src/common/tree/toPlainTree.ts +132 -132
  74. package/src/common/webgl/buffer.ts +79 -79
  75. package/src/common/webgl/doDraw.ts +108 -108
  76. package/src/common/webgl/getWebGLContext.ts +28 -28
  77. package/src/common/webgl/index.ts +226 -226
  78. package/src/common/webgl/shader.ts +1 -1
  79. package/src/common/webgl/texture.ts +97 -97
  80. package/src/layout/TreeLayout.ts +188 -188
  81. package/src/resizeObserver.ts +37 -37
  82. package/src/ruler.ts +209 -209
  83. package/src/viewHandler.ts +160 -160
  84. package/types/Canvas.d.ts +308 -308
  85. package/types/Geometry.d.ts +65 -65
  86. package/types/Object3D.d.ts +114 -114
  87. package/types/SVGConfig.d.ts +79 -79
  88. package/types/Shader.d.ts +23 -21
  89. package/types/TreeConfig.d.ts +36 -36
  90. package/uni-app/ui-canvas.vue +53 -9
@@ -1,227 +1,227 @@
1
- import type Object3DType from '../../../types/Object3D'
2
- import type Scene3DType from '../../../types/Scene3D'
3
-
4
- import getWebGLContext from './getWebGLContext'
5
- import getColorFactory from './getColorFactory'
6
- import Matrix4 from '../../Matrix4/index'
7
- import doDraw from './doDraw'
8
- import assemble from '../assemble'
9
-
10
- // 获取区域信息前是否需要刷新区域
11
- let needUpdate = false
12
-
13
- const scale = 10
14
- class WebGL {
15
-
16
- readonly name: string = "WebGL"
17
-
18
- painter: WebGLRenderingContext
19
- private __regionPainter: WebGLRenderingContext
20
-
21
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
22
- private __getColor = (x: number, y: number) => "rgba(0,0,0,1)"
23
-
24
- private __regionList: {
25
- [key: string | number]: Array<number>
26
- } = {} //区域映射表
27
- private __regionAssemble = assemble(0, 1, 0.2, 3)
28
-
29
- private __regionName = "" // 当前绘制的内容名称
30
- private __regionColor = [0, 0, 0, 1] // 当前记录区域应该使用的颜色
31
-
32
- private __scene: Scene3DType = { // 记录着3D世界
33
- children: [],
34
-
35
- // 右手坐标系
36
- matrix: new Matrix4([
37
- 1, 0, 0, 0,
38
- 0, 1, 0, 0,
39
- 0, 0, 1, 0,
40
- 0, 0, 0, scale
41
- ])
42
- }
43
-
44
- private __proporion: number[]
45
-
46
- private __unique: number
47
-
48
- constructor(ViewCanvas: HTMLCanvasElement, RegionCanvas: HTMLCanvasElement | null) {
49
- this.__unique = new Date().valueOf()
50
-
51
- this.painter = getWebGLContext(ViewCanvas, scale)
52
- if (RegionCanvas) {
53
- this.__regionPainter = getWebGLContext(RegionCanvas, scale, {
54
-
55
- // 如果不设置,涉及到缓冲区的情况可能无法获取颜色
56
- // preserveDrawingBuffer: true
57
- })
58
- }
59
-
60
- const proportion = this.painter.canvas.width / this.painter.canvas.height
61
-
62
- let xProportion = 1
63
- let yProportion = 1
64
-
65
- if (proportion > 1) {
66
- xProportion = 1 / proportion
67
- } else {
68
- yProportion = proportion
69
- }
70
-
71
- const zProportion = Math.min(xProportion, yProportion)
72
-
73
- this.__proporion = [
74
- xProportion, 0, 0, 0,
75
- 0, yProportion, 0, 0,
76
- 0, 0, zProportion, 0,
77
- 0, 0, 0, 1
78
- ]
79
-
80
- }
81
-
82
- matrix(initMatrix4?: number[]) {
83
- return new Matrix4(initMatrix4)
84
- }
85
-
86
- getMatrix() {
87
- return this.__scene.matrix
88
- }
89
-
90
- getObject3D() {
91
- return this.__scene.children
92
- }
93
-
94
- review(isUpdateRegion = false) {
95
-
96
- if (isUpdateRegion) {
97
- if (this.__regionPainter) {
98
- this.__regionPainter.clearColor(1, 1, 1, 1)
99
- this.__regionPainter.clear(this.__regionPainter.COLOR_BUFFER_BIT)
100
- }
101
- } else {
102
- this.painter.clearColor(1, 1, 1, 1)
103
- this.painter.clear(this.painter.COLOR_BUFFER_BIT)
104
- }
105
-
106
- for (let index = 0; index < this.__scene.children.length; index++) {
107
- this.draw(this.__scene.children[index], isUpdateRegion)
108
- }
109
- return this
110
- }
111
-
112
- private draw(object3D: Object3DType, isUpdateRegion = false) {
113
- if (!isUpdateRegion) needUpdate = true
114
-
115
- const meshWorld = {
116
- matrix: object3D.matrix?.value() as Array<number>
117
- }
118
-
119
- const globalWorld = {
120
- matrix: this.__scene.matrix.value(),
121
- proporion: this.__proporion
122
- }
123
-
124
- // 设置好当前区域
125
- this.setRegion(object3D.region)
126
-
127
- for (let index = 0; index < object3D.mesh.length; index++) {
128
-
129
- if (isUpdateRegion) {
130
- if (this.__regionPainter) {
131
- // 区域上
132
- doDraw("painter" + this.__unique, this.__regionPainter, {
133
- geometry: object3D.mesh[index].geometry,
134
- material: {
135
- color: {
136
- r: this.__regionColor[0],
137
- g: this.__regionColor[1],
138
- b: this.__regionColor[2],
139
- alpha: this.__regionColor[3]
140
- }
141
- }
142
- }, meshWorld, globalWorld)
143
-
144
- this.__getColor = getColorFactory(this.__regionPainter)
145
- }
146
- } else {
147
- // 画布上
148
- doDraw("region" + this.__unique, this.painter, object3D.mesh[index], meshWorld, globalWorld)
149
- }
150
- }
151
-
152
- }
153
-
154
- render(object3D: Object3DType) {
155
- if (!('matrix' in object3D)) object3D.matrix = new Matrix4()
156
- if (!('region' in object3D)) object3D.region = ""
157
-
158
- // 对数据进行校对
159
- for (let index = 0; index < object3D.mesh.length; index++) {
160
- const mesh = object3D.mesh[index]
161
-
162
- if (mesh.geometry.attributes.normal && Array.isArray(mesh.geometry.attributes.normal.array)) {
163
- mesh.geometry.attributes.normal.array = new Float32Array(mesh.geometry.attributes.normal.array)
164
- }
165
-
166
- if (mesh.geometry.attributes.position && Array.isArray(mesh.geometry.attributes.position.array)) {
167
- mesh.geometry.attributes.position.array = new Float32Array(mesh.geometry.attributes.position.array)
168
- }
169
-
170
- if (mesh.geometry.attributes.uv && Array.isArray(mesh.geometry.attributes.uv.array)) {
171
- mesh.geometry.attributes.uv.array = new Float32Array(mesh.geometry.attributes.uv.array)
172
- }
173
-
174
- if (mesh.geometry.index && Array.isArray(mesh.geometry.index.array)) {
175
- mesh.geometry.index.array = new Uint8Array(mesh.geometry.index.array)
176
- }
177
-
178
- if (mesh.material.colors && Array.isArray(mesh.material.colors.array)) {
179
- mesh.material.colors.array = new Float32Array(mesh.material.colors.array)
180
- }
181
-
182
- }
183
-
184
- this.__scene.children.push(object3D)
185
- this.draw(object3D)
186
- }
187
-
188
- // 设置当前绘制区域名称
189
- setRegion(regionName: string | number | undefined) {
190
- this.__regionName = regionName + ""
191
-
192
- if (regionName) {
193
- if (this.__regionList[regionName] == void 0) {
194
- this.__regionList[regionName] = [... this.__regionAssemble(), 1]
195
- }
196
- this.__regionColor = this.__regionList[regionName]
197
- } else {
198
- this.__regionColor = [0, 0, 0, 1]
199
- }
200
- return this
201
- }
202
-
203
- // 获取当前事件触发的区域名称
204
- getRegion(x: number, y: number): Promise<string> {
205
- if (needUpdate) this.review(true)
206
-
207
- needUpdate = false
208
- return new Promise((resolve) => {
209
-
210
- const rgba = this.__getColor(x, y)
211
-
212
- // 查找当前点击的区域
213
- for (const key in this.__regionList) {
214
- const currentRGBA = this.__regionList[key]
215
- if ("rgba(" + currentRGBA[0] * 255 + "," + currentRGBA[1] * 255 + "," + currentRGBA[2] * 255 + "," + currentRGBA[3] * 255 + ")" == rgba) {
216
- resolve(key)
217
- break
218
- }
219
- }
220
- resolve("")
221
-
222
- })
223
- }
224
-
225
- }
226
-
1
+ import type Object3DType from '../../../types/Object3D'
2
+ import type Scene3DType from '../../../types/Scene3D'
3
+
4
+ import getWebGLContext from './getWebGLContext'
5
+ import getColorFactory from './getColorFactory'
6
+ import Matrix4 from '../../Matrix4/index'
7
+ import doDraw from './doDraw'
8
+ import assemble from '../assemble'
9
+
10
+ // 获取区域信息前是否需要刷新区域
11
+ let needUpdate = false
12
+
13
+ const scale = 10
14
+ class WebGL {
15
+
16
+ readonly name: string = "WebGL"
17
+
18
+ painter: WebGLRenderingContext
19
+ private __regionPainter: WebGLRenderingContext
20
+
21
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
22
+ private __getColor = (x: number, y: number) => "rgba(0,0,0,1)"
23
+
24
+ private __regionList: {
25
+ [key: string | number]: Array<number>
26
+ } = {} //区域映射表
27
+ private __regionAssemble = assemble(0, 1, 0.2, 3)
28
+
29
+ private __regionName = "" // 当前绘制的内容名称
30
+ private __regionColor = [0, 0, 0, 1] // 当前记录区域应该使用的颜色
31
+
32
+ private __scene: Scene3DType = { // 记录着3D世界
33
+ children: [],
34
+
35
+ // 右手坐标系
36
+ matrix: new Matrix4([
37
+ 1, 0, 0, 0,
38
+ 0, 1, 0, 0,
39
+ 0, 0, 1, 0,
40
+ 0, 0, 0, scale
41
+ ])
42
+ }
43
+
44
+ private __proporion: number[]
45
+
46
+ private __unique: number
47
+
48
+ constructor(ViewCanvas: HTMLCanvasElement, RegionCanvas: HTMLCanvasElement | null) {
49
+ this.__unique = new Date().valueOf()
50
+
51
+ this.painter = getWebGLContext(ViewCanvas, scale)
52
+ if (RegionCanvas) {
53
+ this.__regionPainter = getWebGLContext(RegionCanvas, scale, {
54
+
55
+ // 如果不设置,涉及到缓冲区的情况可能无法获取颜色
56
+ // preserveDrawingBuffer: true
57
+ })
58
+ }
59
+
60
+ const proportion = this.painter.canvas.width / this.painter.canvas.height
61
+
62
+ let xProportion = 1
63
+ let yProportion = 1
64
+
65
+ if (proportion > 1) {
66
+ xProportion = 1 / proportion
67
+ } else {
68
+ yProportion = proportion
69
+ }
70
+
71
+ const zProportion = Math.min(xProportion, yProportion)
72
+
73
+ this.__proporion = [
74
+ xProportion, 0, 0, 0,
75
+ 0, yProportion, 0, 0,
76
+ 0, 0, zProportion, 0,
77
+ 0, 0, 0, 1
78
+ ]
79
+
80
+ }
81
+
82
+ matrix(initMatrix4?: number[]) {
83
+ return new Matrix4(initMatrix4)
84
+ }
85
+
86
+ getMatrix() {
87
+ return this.__scene.matrix
88
+ }
89
+
90
+ getObject3D() {
91
+ return this.__scene.children
92
+ }
93
+
94
+ review(isUpdateRegion = false) {
95
+
96
+ if (isUpdateRegion) {
97
+ if (this.__regionPainter) {
98
+ this.__regionPainter.clearColor(1, 1, 1, 1)
99
+ this.__regionPainter.clear(this.__regionPainter.COLOR_BUFFER_BIT)
100
+ }
101
+ } else {
102
+ this.painter.clearColor(1, 1, 1, 1)
103
+ this.painter.clear(this.painter.COLOR_BUFFER_BIT)
104
+ }
105
+
106
+ for (let index = 0; index < this.__scene.children.length; index++) {
107
+ this.draw(this.__scene.children[index], isUpdateRegion)
108
+ }
109
+ return this
110
+ }
111
+
112
+ private draw(object3D: Object3DType, isUpdateRegion = false) {
113
+ if (!isUpdateRegion) needUpdate = true
114
+
115
+ const meshWorld = {
116
+ matrix: object3D.matrix?.value() as Array<number>
117
+ }
118
+
119
+ const globalWorld = {
120
+ matrix: this.__scene.matrix.value(),
121
+ proporion: this.__proporion
122
+ }
123
+
124
+ // 设置好当前区域
125
+ this.setRegion(object3D.region)
126
+
127
+ for (let index = 0; index < object3D.mesh.length; index++) {
128
+
129
+ if (isUpdateRegion) {
130
+ if (this.__regionPainter) {
131
+ // 区域上
132
+ doDraw("painter" + this.__unique, this.__regionPainter, {
133
+ geometry: object3D.mesh[index].geometry,
134
+ material: {
135
+ color: {
136
+ r: this.__regionColor[0],
137
+ g: this.__regionColor[1],
138
+ b: this.__regionColor[2],
139
+ alpha: this.__regionColor[3]
140
+ }
141
+ }
142
+ }, meshWorld, globalWorld)
143
+
144
+ this.__getColor = getColorFactory(this.__regionPainter)
145
+ }
146
+ } else {
147
+ // 画布上
148
+ doDraw("region" + this.__unique, this.painter, object3D.mesh[index], meshWorld, globalWorld)
149
+ }
150
+ }
151
+
152
+ }
153
+
154
+ render(object3D: Object3DType) {
155
+ if (!('matrix' in object3D)) object3D.matrix = new Matrix4()
156
+ if (!('region' in object3D)) object3D.region = ""
157
+
158
+ // 对数据进行校对
159
+ for (let index = 0; index < object3D.mesh.length; index++) {
160
+ const mesh = object3D.mesh[index]
161
+
162
+ if (mesh.geometry.attributes.normal && Array.isArray(mesh.geometry.attributes.normal.array)) {
163
+ mesh.geometry.attributes.normal.array = new Float32Array(mesh.geometry.attributes.normal.array)
164
+ }
165
+
166
+ if (mesh.geometry.attributes.position && Array.isArray(mesh.geometry.attributes.position.array)) {
167
+ mesh.geometry.attributes.position.array = new Float32Array(mesh.geometry.attributes.position.array)
168
+ }
169
+
170
+ if (mesh.geometry.attributes.uv && Array.isArray(mesh.geometry.attributes.uv.array)) {
171
+ mesh.geometry.attributes.uv.array = new Float32Array(mesh.geometry.attributes.uv.array)
172
+ }
173
+
174
+ if (mesh.geometry.index && Array.isArray(mesh.geometry.index.array)) {
175
+ mesh.geometry.index.array = new Uint8Array(mesh.geometry.index.array)
176
+ }
177
+
178
+ if (mesh.material.colors && Array.isArray(mesh.material.colors.array)) {
179
+ mesh.material.colors.array = new Float32Array(mesh.material.colors.array)
180
+ }
181
+
182
+ }
183
+
184
+ this.__scene.children.push(object3D)
185
+ this.draw(object3D)
186
+ }
187
+
188
+ // 设置当前绘制区域名称
189
+ setRegion(regionName: string | number | undefined) {
190
+ this.__regionName = regionName + ""
191
+
192
+ if (regionName) {
193
+ if (this.__regionList[regionName] == void 0) {
194
+ this.__regionList[regionName] = [... this.__regionAssemble(), 1]
195
+ }
196
+ this.__regionColor = this.__regionList[regionName]
197
+ } else {
198
+ this.__regionColor = [0, 0, 0, 1]
199
+ }
200
+ return this
201
+ }
202
+
203
+ // 获取当前事件触发的区域名称
204
+ getRegion(x: number, y: number): Promise<string> {
205
+ if (needUpdate) this.review(true)
206
+
207
+ needUpdate = false
208
+ return new Promise((resolve) => {
209
+
210
+ const rgba = this.__getColor(x, y)
211
+
212
+ // 查找当前点击的区域
213
+ for (const key in this.__regionList) {
214
+ const currentRGBA = this.__regionList[key]
215
+ if ("rgba(" + currentRGBA[0] * 255 + "," + currentRGBA[1] * 255 + "," + currentRGBA[2] * 255 + "," + currentRGBA[3] * 255 + ")" == rgba) {
216
+ resolve(key)
217
+ break
218
+ }
219
+ }
220
+ resolve("")
221
+
222
+ })
223
+ }
224
+
225
+ }
226
+
227
227
  export default WebGL
@@ -52,7 +52,7 @@ class ShaderObject {
52
52
 
53
53
  private __painter: WebGLRenderingContext
54
54
 
55
- program: WebGLProgram | null = null
55
+ program: WebGLProgram
56
56
 
57
57
  constructor(painter: WebGLRenderingContext) {
58
58
  this.__painter = painter
@@ -1,98 +1,98 @@
1
- // 初始化一个纹理对象
2
- // type = gl.TEXTURE_2D 二维纹理
3
- // type = gl.TEXTURE_CUBE_MAP 立方体纹理
4
- // unit表示开启的纹理单元编号
5
- const initTexture = (painter: WebGLRenderingContext, type: number, unit: number) => {
6
-
7
- // 创建纹理对象
8
- const texture = painter.createTexture()
9
-
10
- // 开启纹理单元,unit表示开启的编号
11
- painter.activeTexture(painter['TEXTURE' + unit])
12
-
13
- // 绑定纹理对象到目标上
14
- painter.bindTexture(type, texture)
15
-
16
- return texture
17
- }
18
-
19
- // 链接资源图片
20
- // level默认传入0即可,和金字塔纹理有关
21
- // format表示图像的内部格式:
22
- // gl.RGB(红绿蓝)
23
- // gl.RGBA(红绿蓝透明度)
24
- // gl.ALPHA(0.0,0.0,0.0,透明度)
25
- // gl.LUMINANCE(L、L、L、1L:流明)
26
- // gl.LUMINANCE_ALPHA(L、L、L,透明度)
27
- // textureType表示纹理数据的格式:
28
- // gl.UNSIGNED_BYTE: 表示无符号整形,每一个颜色分量占据1字节
29
- // gl.UNSIGNED_SHORT_5_6_5: 表示RGB,每一个分量分别占据占据5, 6, 5比特
30
- // gl.UNSIGNED_SHORT_4_4_4_4: 表示RGBA,每一个分量分别占据占据4, 4, 4, 4比特
31
- // gl.UNSIGNED_SHORT_5_5_5_1: 表示RGBA,每一个分量分别占据占据5比特,A分量占据1比特
32
- const linkImage = (painter: WebGLRenderingContext, type: number, level: number, format: number, textureType: number, image: TexImageSource) => {
33
- painter.texImage2D(type, level, format, format, textureType, image)
34
- }
35
-
36
- const linkCube = (painter: WebGLRenderingContext, type: number, level: number, format: number, textureType: number, images: TexImageSource[], width: number, height: number, texture: WebGLTexture) => {
37
-
38
- const types = [
39
- painter.TEXTURE_CUBE_MAP_POSITIVE_X, // 右
40
- painter.TEXTURE_CUBE_MAP_NEGATIVE_X, // 左
41
- painter.TEXTURE_CUBE_MAP_POSITIVE_Y, // 上
42
- painter.TEXTURE_CUBE_MAP_NEGATIVE_Y, // 下
43
- painter.TEXTURE_CUBE_MAP_POSITIVE_Z, // 后
44
- painter.TEXTURE_CUBE_MAP_NEGATIVE_Z // 前
45
- ]
46
- let target: number
47
-
48
- for (let i = 0; i < types.length; i++) {
49
- if (images[i]) {
50
- target = types[i]
51
- painter.texImage2D(target, level, format, width, height, 0, format, textureType, null)
52
- painter.bindTexture(type, texture)
53
- painter.texImage2D(target, level, format, format, textureType, images[i])
54
- }
55
- }
56
-
57
- painter.generateMipmap(type)
58
- }
59
-
60
- class TextureObject {
61
-
62
- private __painter: WebGLRenderingContext
63
- private __type: number
64
-
65
- private __texture: WebGLTexture
66
-
67
- constructor(painter: WebGLRenderingContext, type: string, unit = 0) {
68
- this.__painter = painter
69
- this.__type = {
70
- "2d": painter.TEXTURE_2D,
71
- "cube": painter.TEXTURE_CUBE_MAP
72
- }[type] as number
73
-
74
- // 创建纹理
75
- this.__texture = initTexture(painter, this.__type, unit) as WebGLTexture
76
-
77
- // 配置纹理
78
- painter.texParameteri(this.__type, painter.TEXTURE_MIN_FILTER, painter.NEAREST)
79
- painter.texParameteri(this.__type, painter.TEXTURE_MAG_FILTER, painter.NEAREST)
80
- painter.texParameteri(this.__type, painter.TEXTURE_WRAP_S, painter.CLAMP_TO_EDGE)
81
- painter.texParameteri(this.__type, painter.TEXTURE_WRAP_T, painter.CLAMP_TO_EDGE)
82
- }
83
-
84
- // 链接图片资源(单一)
85
- useImage(image: TexImageSource) {
86
- linkImage(this.__painter, this.__type, 0, this.__painter.RGBA, this.__painter.UNSIGNED_BYTE, image)
87
- return this
88
- }
89
-
90
- // 链接图片资源(多张)
91
- useCube(images: TexImageSource[], width: number, height: number) {
92
- linkCube(this.__painter, this.__type, 0, this.__painter.RGBA, this.__painter.UNSIGNED_BYTE, images, width, height, this.__texture)
93
- return this
94
- }
95
-
96
- }
97
-
1
+ // 初始化一个纹理对象
2
+ // type = gl.TEXTURE_2D 二维纹理
3
+ // type = gl.TEXTURE_CUBE_MAP 立方体纹理
4
+ // unit表示开启的纹理单元编号
5
+ const initTexture = (painter: WebGLRenderingContext, type: number, unit: number) => {
6
+
7
+ // 创建纹理对象
8
+ const texture = painter.createTexture()
9
+
10
+ // 开启纹理单元,unit表示开启的编号
11
+ painter.activeTexture(painter['TEXTURE' + unit])
12
+
13
+ // 绑定纹理对象到目标上
14
+ painter.bindTexture(type, texture)
15
+
16
+ return texture
17
+ }
18
+
19
+ // 链接资源图片
20
+ // level默认传入0即可,和金字塔纹理有关
21
+ // format表示图像的内部格式:
22
+ // gl.RGB(红绿蓝)
23
+ // gl.RGBA(红绿蓝透明度)
24
+ // gl.ALPHA(0.0,0.0,0.0,透明度)
25
+ // gl.LUMINANCE(L、L、L、1L:流明)
26
+ // gl.LUMINANCE_ALPHA(L、L、L,透明度)
27
+ // textureType表示纹理数据的格式:
28
+ // gl.UNSIGNED_BYTE: 表示无符号整形,每一个颜色分量占据1字节
29
+ // gl.UNSIGNED_SHORT_5_6_5: 表示RGB,每一个分量分别占据占据5, 6, 5比特
30
+ // gl.UNSIGNED_SHORT_4_4_4_4: 表示RGBA,每一个分量分别占据占据4, 4, 4, 4比特
31
+ // gl.UNSIGNED_SHORT_5_5_5_1: 表示RGBA,每一个分量分别占据占据5比特,A分量占据1比特
32
+ const linkImage = (painter: WebGLRenderingContext, type: number, level: number, format: number, textureType: number, image: TexImageSource) => {
33
+ painter.texImage2D(type, level, format, format, textureType, image)
34
+ }
35
+
36
+ const linkCube = (painter: WebGLRenderingContext, type: number, level: number, format: number, textureType: number, images: TexImageSource[], width: number, height: number, texture: WebGLTexture) => {
37
+
38
+ const types = [
39
+ painter.TEXTURE_CUBE_MAP_POSITIVE_X, // 右
40
+ painter.TEXTURE_CUBE_MAP_NEGATIVE_X, // 左
41
+ painter.TEXTURE_CUBE_MAP_POSITIVE_Y, // 上
42
+ painter.TEXTURE_CUBE_MAP_NEGATIVE_Y, // 下
43
+ painter.TEXTURE_CUBE_MAP_POSITIVE_Z, // 后
44
+ painter.TEXTURE_CUBE_MAP_NEGATIVE_Z // 前
45
+ ]
46
+ let target: number
47
+
48
+ for (let i = 0; i < types.length; i++) {
49
+ if (images[i]) {
50
+ target = types[i]
51
+ painter.texImage2D(target, level, format, width, height, 0, format, textureType, null)
52
+ painter.bindTexture(type, texture)
53
+ painter.texImage2D(target, level, format, format, textureType, images[i])
54
+ }
55
+ }
56
+
57
+ painter.generateMipmap(type)
58
+ }
59
+
60
+ class TextureObject {
61
+
62
+ private __painter: WebGLRenderingContext
63
+ private __type: number
64
+
65
+ private __texture: WebGLTexture
66
+
67
+ constructor(painter: WebGLRenderingContext, type: string, unit = 0) {
68
+ this.__painter = painter
69
+ this.__type = {
70
+ "2d": painter.TEXTURE_2D,
71
+ "cube": painter.TEXTURE_CUBE_MAP
72
+ }[type] as number
73
+
74
+ // 创建纹理
75
+ this.__texture = initTexture(painter, this.__type, unit) as WebGLTexture
76
+
77
+ // 配置纹理
78
+ painter.texParameteri(this.__type, painter.TEXTURE_MIN_FILTER, painter.NEAREST)
79
+ painter.texParameteri(this.__type, painter.TEXTURE_MAG_FILTER, painter.NEAREST)
80
+ painter.texParameteri(this.__type, painter.TEXTURE_WRAP_S, painter.CLAMP_TO_EDGE)
81
+ painter.texParameteri(this.__type, painter.TEXTURE_WRAP_T, painter.CLAMP_TO_EDGE)
82
+ }
83
+
84
+ // 链接图片资源(单一)
85
+ useImage(image: TexImageSource) {
86
+ linkImage(this.__painter, this.__type, 0, this.__painter.RGBA, this.__painter.UNSIGNED_BYTE, image)
87
+ return this
88
+ }
89
+
90
+ // 链接图片资源(多张)
91
+ useCube(images: TexImageSource[], width: number, height: number) {
92
+ linkCube(this.__painter, this.__type, 0, this.__painter.RGBA, this.__painter.UNSIGNED_BYTE, images, width, height, this.__texture)
93
+ return this
94
+ }
95
+
96
+ }
97
+
98
98
  export default TextureObject