vislite 0.5.2 → 0.6.0-alpha.2
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.
- package/.editorconfig +18 -18
- package/.eslintrc.js +24 -0
- package/AUTHORS.txt +6 -6
- package/CHANGELOG +15 -1
- package/LICENSE +20 -20
- package/README.md +41 -2
- package/lib/Buffer/index.umd.js +2 -2
- package/lib/Buffer/index.umd.min.js +2 -2
- package/lib/Canvas/index.umd.js +19 -8
- package/lib/Canvas/index.umd.min.js +3 -3
- package/lib/Cardinal/index.umd.js +2 -2
- package/lib/Cardinal/index.umd.min.js +2 -2
- package/lib/Eoap/index.umd.js +2 -2
- package/lib/Eoap/index.umd.min.js +2 -2
- package/lib/Geometry/index.umd.js +7 -4
- package/lib/Geometry/index.umd.min.js +2 -2
- package/lib/Hermite/index.umd.js +2 -2
- package/lib/Hermite/index.umd.min.js +2 -2
- package/lib/Matrix4/index.umd.js +14 -8
- package/lib/Matrix4/index.umd.min.js +3 -3
- package/lib/Mercator/index.umd.js +2 -2
- package/lib/Mercator/index.umd.min.js +2 -2
- package/lib/OralCanvas/index.es.js +14 -6
- package/lib/OralCanvas/index.es.min.js +3 -3
- package/lib/OralWebGL/index.es.js +27 -19
- package/lib/OralWebGL/index.es.min.js +3 -3
- package/lib/SVG/index.umd.js +6 -4
- package/lib/SVG/index.umd.min.js +2 -2
- package/lib/Shader/index.umd.js +10 -10
- package/lib/Shader/index.umd.min.js +3 -3
- package/lib/Texture/index.umd.js +4 -3
- package/lib/Texture/index.umd.min.js +2 -2
- package/lib/TreeLayout/index.umd.js +8 -5
- package/lib/TreeLayout/index.umd.min.js +2 -2
- package/lib/WebGL/index.umd.js +27 -19
- package/lib/WebGL/index.umd.min.js +3 -3
- package/lib/animation/index.umd.js +4 -3
- package/lib/animation/index.umd.min.js +2 -2
- package/lib/formatColor/index.umd.js +2 -2
- package/lib/formatColor/index.umd.min.js +2 -2
- package/lib/getLoopColors/index.umd.js +2 -2
- package/lib/getLoopColors/index.umd.min.js +2 -2
- package/lib/getWebGLContext/index.umd.js +4 -3
- package/lib/getWebGLContext/index.umd.min.js +2 -2
- package/lib/index.umd.js +73 -34
- package/lib/index.umd.min.js +3 -3
- package/lib/move/index.umd.js +2 -2
- package/lib/move/index.umd.min.js +2 -2
- package/lib/resizeObserver/index.umd.js +4 -3
- package/lib/resizeObserver/index.umd.min.js +2 -2
- package/lib/rotate/index.umd.js +2 -2
- package/lib/rotate/index.umd.min.js +2 -2
- package/lib/ruler/index.umd.js +14 -2
- package/lib/ruler/index.umd.min.js +3 -3
- package/lib/scale/index.umd.js +2 -2
- package/lib/scale/index.umd.min.js +2 -2
- package/lib/throttle/index.umd.js +2 -2
- package/lib/throttle/index.umd.min.js +2 -2
- package/lib/viewHandler/index.umd.js +4 -4
- package/lib/viewHandler/index.umd.min.js +3 -3
- package/miniprogram/ui-canvas/index.js +107 -107
- package/miniprogram/ui-canvas/index.json +4 -4
- package/miniprogram/ui-canvas/index.wxml +4 -4
- package/miniprogram/ui-canvas/index.wxss +5 -5
- package/package/Buffer/index.ts +2 -2
- package/package/Canvas/index.ts +2 -2
- package/package/Cardinal/index.ts +2 -2
- package/package/Eoap/index.ts +2 -2
- package/package/Geometry/index.ts +2 -2
- package/package/Hermite/index.ts +2 -2
- package/package/Matrix4/index.ts +2 -2
- package/package/Mercator/index.ts +2 -2
- package/package/OralCanvas/index.ts +2 -2
- package/package/OralWebGL/index.ts +2 -2
- package/package/SVG/index.ts +2 -2
- package/package/Shader/index.ts +39 -39
- package/package/Texture/index.ts +2 -2
- package/package/TreeLayout/index.ts +1 -1
- package/package/WebGL/index.ts +2 -2
- package/package/animation/index.ts +2 -2
- package/package/formatColor/index.ts +2 -2
- package/package/getLoopColors/index.ts +2 -2
- package/package/getWebGLContext/index.ts +39 -39
- package/package/index.ts +76 -76
- package/package/move/index.ts +2 -2
- package/package/resizeObserver/index.ts +2 -2
- package/package/rotate/index.ts +2 -2
- package/package/ruler/index.ts +2 -2
- package/package/scale/index.ts +2 -2
- package/package/throttle/index.ts +2 -2
- package/package/viewHandler/index.ts +2 -2
- package/package.json +6 -2
- package/src/Canvas.ts +70 -67
- package/src/Eoap.ts +66 -66
- package/src/Geometry.ts +82 -82
- package/src/Matrix4/index.ts +17 -9
- package/src/Matrix4/move.ts +12 -12
- package/src/Matrix4/rotate.ts +15 -15
- package/src/Matrix4/scale.ts +11 -11
- package/src/Matrix4/transform.ts +64 -64
- package/src/Mercator.ts +27 -27
- package/src/SVG.ts +24 -24
- package/src/WebGL.ts +66 -66
- package/src/animation.ts +98 -98
- package/src/common/assemble.ts +22 -22
- package/src/common/canvas/arc.ts +51 -50
- package/src/common/canvas/config.ts +158 -158
- package/src/common/canvas/gradient.ts +29 -29
- package/src/common/canvas/index.ts +149 -140
- package/src/common/canvas/painter.ts +417 -426
- package/src/common/canvas/texts.ts +18 -18
- package/src/common/geometry/prism-horizontal.ts +35 -33
- package/src/common/geometry/prism-vertical.ts +40 -40
- package/src/common/geometry/sphere-fragment.ts +40 -39
- package/src/common/geometry/tool/circle.ts +11 -11
- package/src/common/mergeOption.ts +6 -6
- package/src/common/setStyle.ts +5 -5
- package/src/common/svg/config.ts +186 -186
- package/src/common/svg/dictionary.ts +1 -1
- package/src/common/svg/gradient.ts +63 -63
- package/src/common/svg/index.ts +356 -356
- package/src/common/svg/tool.ts +44 -44
- package/src/common/tree/index.ts +25 -25
- package/src/common/tree/toInnerTree.ts +57 -56
- package/src/common/tree/toPlainTree.ts +132 -131
- package/src/common/webgl/buffer.ts +79 -79
- package/src/common/webgl/doDraw.ts +108 -108
- package/src/common/webgl/getColorFactory.ts +26 -26
- package/src/common/webgl/getShader.ts +48 -48
- package/src/common/webgl/getTexture.ts +11 -11
- package/src/common/webgl/getWebGLContext.ts +28 -27
- package/src/common/webgl/index.ts +224 -223
- package/src/common/webgl/shader.ts +75 -75
- package/src/common/webgl/texture.ts +97 -96
- package/src/formatColor.ts +44 -44
- package/src/getLoopColors.ts +42 -42
- package/src/interpolation/Cardinal.ts +110 -110
- package/src/interpolation/Hermite.ts +65 -65
- package/src/{TreeLayout/index.ts → layout/TreeLayout.ts} +21 -21
- package/src/move.ts +7 -7
- package/src/resizeObserver.ts +37 -36
- package/src/rotate.ts +7 -7
- package/src/ruler.ts +41 -19
- package/src/scale.ts +6 -6
- package/src/shader/fsColor.c +7 -7
- package/src/shader/fsColors.c +7 -7
- package/src/shader/fsCube.c +8 -8
- package/src/shader/fsImage.c +8 -8
- package/src/shader/vsColor.c +17 -17
- package/src/shader/vsColors.c +13 -13
- package/src/shader/vsCube.c +13 -13
- package/src/shader/vsImage.c +13 -13
- package/src/throttle.ts +52 -51
- package/src/viewHandler.ts +158 -158
- package/types/Buffer.d.ts +23 -23
- package/types/Canvas.d.ts +8 -0
- package/types/CanvasConfig.d.ts +106 -106
- package/types/CanvasOption.d.ts +8 -7
- package/types/CanvasOpts.d.ts +10 -0
- package/types/Cardinal.d.ts +13 -13
- package/types/Geometry.d.ts +40 -40
- package/types/GeometryOption.d.ts +11 -11
- package/types/GeometryResult.d.ts +18 -18
- package/types/Gradient.d.ts +14 -14
- package/types/Hermite.d.ts +18 -18
- package/types/Map.d.ts +9 -9
- package/types/Matrix4.d.ts +5 -0
- package/types/Object3D.d.ts +114 -114
- package/types/SVG.d.ts +238 -238
- package/types/SVGConfig.d.ts +76 -76
- package/types/Scene3D.d.ts +6 -6
- package/types/Shader.d.ts +21 -21
- package/types/Texture.d.ts +16 -16
- package/types/Tree.d.ts +69 -69
- package/types/TreeConfig.d.ts +24 -24
- package/types/TreeLayout.d.ts +54 -54
- package/types/TreeOption.d.ts +48 -48
- package/types/WebGL.d.ts +38 -38
- package/types/WebGLOption.d.ts +7 -7
- package/types/animation.d.ts +6 -6
- package/types/formatColor.d.ts +2 -2
- package/types/getLoopColors.d.ts +2 -2
- package/types/getWebGLContext.d.ts +2 -2
- package/types/index.d.ts +175 -175
- package/types/move.d.ts +2 -2
- package/types/painterConfig.d.ts +2 -2
- package/types/resizeObserver.d.ts +2 -2
- package/types/rotate.d.ts +2 -2
- package/types/ruler.d.ts +7 -7
- package/types/scale.d.ts +2 -2
- package/types/throttle.d.ts +4 -4
- package/types/throttleOption.d.ts +21 -21
- package/types/viewHandler.d.ts +13 -13
- package/uni-app/ui-canvas.vue +239 -206
|
@@ -1,80 +1,80 @@
|
|
|
1
|
-
// 缓冲区分为两种:
|
|
2
|
-
// 1.缓冲区中保存了包含顶点的数据
|
|
3
|
-
// 2.缓冲区保存了包含顶点的索引值
|
|
4
|
-
|
|
5
|
-
// 获取一个新的缓冲区
|
|
6
|
-
// isElement默认false,创建第一种缓冲区,为true创建第二种
|
|
7
|
-
|
|
8
|
-
return painter.createBuffer()
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
// 数据写入缓冲区
|
|
12
|
-
// data是一个类型化数组,表示写入的数据
|
|
13
|
-
// usage表示程序如何使用存储在缓冲区的数据
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
painter.bufferData(TYPE, data, usage)
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// 使用缓冲区数据
|
|
20
|
-
// location指定待分配的attribute变量的存储位置
|
|
21
|
-
// size每个分量个数
|
|
22
|
-
// type数据类型,应该是以下的某个:
|
|
23
|
-
// gl.UNSIGNED_BYTE Uint8Array
|
|
24
|
-
// gl.SHORT Int16Array
|
|
25
|
-
// gl.UNSIGNED_SHORT Uint16Array
|
|
26
|
-
// gl.INT Int32Array
|
|
27
|
-
// gl.UNSIGNED_INT Uint32Array
|
|
28
|
-
// gl.FLOAT Float32Array
|
|
29
|
-
// stride相邻两个数据项的字节数
|
|
30
|
-
// offset数据的起点字节位置
|
|
31
|
-
// normalized是否把非浮点型的数据归一化到[0,1]或[-1,1]区间
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
// 把缓冲区对象分配给目标变量
|
|
35
|
-
painter.vertexAttribPointer(location, size, type, normalized, stride, offset)
|
|
36
|
-
|
|
37
|
-
// 连接目标对象和缓冲区对象
|
|
38
|
-
painter.enableVertexAttribArray(location)
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
class BufferObject {
|
|
42
|
-
|
|
43
|
-
private __painter: WebGLRenderingContext
|
|
44
|
-
private __isElement: boolean
|
|
45
|
-
|
|
46
|
-
private __buffer: WebGLBuffer
|
|
47
|
-
private __type: number
|
|
48
|
-
private __fsize: number
|
|
49
|
-
|
|
50
|
-
constructor(painter: WebGLRenderingContext, isElement: boolean = false) {
|
|
51
|
-
this.__painter = painter
|
|
52
|
-
this.__isElement = isElement
|
|
53
|
-
|
|
54
|
-
this.__buffer = newBuffer(painter)
|
|
55
|
-
this.__type = isElement ? painter.ELEMENT_ARRAY_BUFFER : painter.ARRAY_BUFFER
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// 使用这个缓冲区对象
|
|
59
|
-
// (先于写入和分配)
|
|
60
|
-
use() {
|
|
61
|
-
this.__painter.bindBuffer(this.__type, this.__buffer) // 绑定到目标
|
|
62
|
-
return this
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// 写入数据
|
|
66
|
-
write(data: Uint8Array | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array) {
|
|
67
|
-
writeBuffer(this.__painter, data, this.__isElement, this.__painter.STATIC_DRAW)
|
|
68
|
-
this.__fsize = data.BYTES_PER_ELEMENT
|
|
69
|
-
return this
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// 分配使用
|
|
73
|
-
// 第2~4位参数分别表示:一个完整数据的长度、一组数据的长度、读取位置偏移
|
|
74
|
-
divide(location: number, size: number, stride: number, offset: number = 0) {
|
|
75
|
-
useBuffer(this.__painter, location, size, this.__painter.FLOAT, stride * this.__fsize, offset * this.__fsize, false)
|
|
76
|
-
return this
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
1
|
+
// 缓冲区分为两种:
|
|
2
|
+
// 1.缓冲区中保存了包含顶点的数据
|
|
3
|
+
// 2.缓冲区保存了包含顶点的索引值
|
|
4
|
+
|
|
5
|
+
// 获取一个新的缓冲区
|
|
6
|
+
// isElement默认false,创建第一种缓冲区,为true创建第二种
|
|
7
|
+
const newBuffer = (painter: WebGLRenderingContext) => {
|
|
8
|
+
return painter.createBuffer()
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// 数据写入缓冲区
|
|
12
|
+
// data是一个类型化数组,表示写入的数据
|
|
13
|
+
// usage表示程序如何使用存储在缓冲区的数据
|
|
14
|
+
const writeBuffer = (painter: WebGLRenderingContext, data: ArrayBuffer, isElement: boolean, usage: number) => {
|
|
15
|
+
const TYPE = isElement ? painter.ELEMENT_ARRAY_BUFFER : painter.ARRAY_BUFFER
|
|
16
|
+
painter.bufferData(TYPE, data, usage)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// 使用缓冲区数据
|
|
20
|
+
// location指定待分配的attribute变量的存储位置
|
|
21
|
+
// size每个分量个数
|
|
22
|
+
// type数据类型,应该是以下的某个:
|
|
23
|
+
// gl.UNSIGNED_BYTE Uint8Array
|
|
24
|
+
// gl.SHORT Int16Array
|
|
25
|
+
// gl.UNSIGNED_SHORT Uint16Array
|
|
26
|
+
// gl.INT Int32Array
|
|
27
|
+
// gl.UNSIGNED_INT Uint32Array
|
|
28
|
+
// gl.FLOAT Float32Array
|
|
29
|
+
// stride相邻两个数据项的字节数
|
|
30
|
+
// offset数据的起点字节位置
|
|
31
|
+
// normalized是否把非浮点型的数据归一化到[0,1]或[-1,1]区间
|
|
32
|
+
const useBuffer = (painter: WebGLRenderingContext, location: number, size: number, type: number, stride: number, offset: number, normalized: boolean) => {
|
|
33
|
+
|
|
34
|
+
// 把缓冲区对象分配给目标变量
|
|
35
|
+
painter.vertexAttribPointer(location, size, type, normalized, stride, offset)
|
|
36
|
+
|
|
37
|
+
// 连接目标对象和缓冲区对象
|
|
38
|
+
painter.enableVertexAttribArray(location)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
class BufferObject {
|
|
42
|
+
|
|
43
|
+
private __painter: WebGLRenderingContext
|
|
44
|
+
private __isElement: boolean
|
|
45
|
+
|
|
46
|
+
private __buffer: WebGLBuffer
|
|
47
|
+
private __type: number
|
|
48
|
+
private __fsize: number
|
|
49
|
+
|
|
50
|
+
constructor(painter: WebGLRenderingContext, isElement: boolean = false) {
|
|
51
|
+
this.__painter = painter
|
|
52
|
+
this.__isElement = isElement
|
|
53
|
+
|
|
54
|
+
this.__buffer = newBuffer(painter)
|
|
55
|
+
this.__type = isElement ? painter.ELEMENT_ARRAY_BUFFER : painter.ARRAY_BUFFER
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// 使用这个缓冲区对象
|
|
59
|
+
// (先于写入和分配)
|
|
60
|
+
use() {
|
|
61
|
+
this.__painter.bindBuffer(this.__type, this.__buffer) // 绑定到目标
|
|
62
|
+
return this
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// 写入数据
|
|
66
|
+
write(data: Uint8Array | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array) {
|
|
67
|
+
writeBuffer(this.__painter, data, this.__isElement, this.__painter.STATIC_DRAW)
|
|
68
|
+
this.__fsize = data.BYTES_PER_ELEMENT
|
|
69
|
+
return this
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// 分配使用
|
|
73
|
+
// 第2~4位参数分别表示:一个完整数据的长度、一组数据的长度、读取位置偏移
|
|
74
|
+
divide(location: number, size: number, stride: number, offset: number = 0) {
|
|
75
|
+
useBuffer(this.__painter, location, size, this.__painter.FLOAT, stride * this.__fsize, offset * this.__fsize, false)
|
|
76
|
+
return this
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
80
|
export default BufferObject
|
|
@@ -1,109 +1,109 @@
|
|
|
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
|
-
|
|
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
|
|
43
|
-
, mesh.material.color.g
|
|
44
|
-
, mesh.material.color.b
|
|
45
|
-
, mesh.material.color.alpha)
|
|
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, mesh.material.colors.itemSize, 0)
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// 立方纹理
|
|
59
|
-
else if ("cube" in mesh.material) {
|
|
60
|
-
|
|
61
|
-
|
|
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, 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
|
-
|
|
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
|
|
43
|
+
, mesh.material.color.g
|
|
44
|
+
, mesh.material.color.b
|
|
45
|
+
, mesh.material.color.alpha)
|
|
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, mesh.material.colors.itemSize, 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, 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
109
|
}
|
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
export default function (painter: WebGLRenderingContext) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return "rgba(" + pixelR + "," + pixelG + "," + pixelB + "," + pixelA + ")"
|
|
26
|
-
}
|
|
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
27
|
}
|
|
@@ -1,49 +1,49 @@
|
|
|
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
|
-
|
|
16
|
-
export default function (type: string, painter: WebGLRenderingContext, mesh: meshType) {
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
|
|
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
|
-
|
|
38
|
-
[type + "1000"]: fsColor,
|
|
39
|
-
[type + "0100"]: fsColors,
|
|
40
|
-
[type + "0010"]: fsImage,
|
|
41
|
-
[type + "0001"]: fsCube
|
|
42
|
-
}[uniqueName]
|
|
43
|
-
|
|
44
|
-
// 启用着色器
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
shaders[uniqueName] = shader
|
|
48
|
-
return shader
|
|
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
49
|
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import TextureObject from './texture'
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export default function (type: string, painter: WebGLRenderingContext, kind: string) {
|
|
5
|
-
|
|
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
|
+
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
12
|
}
|
|
@@ -1,28 +1,29 @@
|
|
|
1
|
-
interface OptsType {
|
|
2
|
-
preserveDrawingBuffer?: boolean
|
|
3
|
-
}
|
|
4
|
-
export default function (canvas: HTMLCanvasElement, scale: number, opts: OptsType = {}) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
1
|
+
interface OptsType {
|
|
2
|
+
preserveDrawingBuffer?: boolean
|
|
3
|
+
}
|
|
4
|
+
export default function (canvas: HTMLCanvasElement, scale: number, opts: OptsType = {}) {
|
|
5
|
+
const names = ["webgl", "experimental-webgl", "webkit-3d", "moz-webgl"]
|
|
6
|
+
let painter = null
|
|
7
|
+
for (let i = 0; i < names.length; i++) {
|
|
8
|
+
try {
|
|
9
|
+
painter = canvas.getContext(names[i], opts)
|
|
10
|
+
} catch (e) { }
|
|
11
|
+
if (painter) break
|
|
12
|
+
}
|
|
13
|
+
if (!painter) throw new Error('Non canvas or browser does not support webgl.')
|
|
14
|
+
|
|
15
|
+
const width = painter.canvas.width, height = painter.canvas.height
|
|
16
|
+
|
|
17
|
+
const viewWidth = width * scale
|
|
18
|
+
const viewHeight = height * scale
|
|
19
|
+
|
|
20
|
+
painter.viewport((width - viewWidth) * 0.5, (height - viewHeight) * 0.5, viewWidth, viewHeight)
|
|
21
|
+
|
|
22
|
+
// https://developer.mozilla.org/zh-CN/docs/Web/API/WebGLRenderingContext/depthFunc
|
|
23
|
+
painter.depthFunc(painter.LEQUAL)
|
|
24
|
+
|
|
25
|
+
// 开启深度计算
|
|
26
|
+
painter.enable(painter.DEPTH_TEST);
|
|
27
|
+
|
|
28
|
+
return painter
|
|
28
29
|
}
|