UNPKG

8.85 kBJavaScriptView Raw
1import _typeof from "@babel/runtime/helpers/typeof";
2import _defineProperty from "@babel/runtime/helpers/defineProperty";
3import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
4import _createClass from "@babel/runtime/helpers/createClass";
5
6function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
7
8function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
9
10import { gl } from '@antv/g-webgpu-core';
11import { extractUniforms } from '../utils/uniform';
12import { blendEquationMap, blendFuncMap, cullFaceMap, depthFuncMap, primitiveMap, stencilFuncMap, stencilOpMap } from './constants';
13
14/**
15 * adaptor for regl.DrawCommand
16 */
17var ReglModel = /*#__PURE__*/function () {
18 function ReglModel(reGl, options) {
19 _classCallCheck(this, ReglModel);
20
21 this.reGl = void 0;
22 this.drawCommand = void 0;
23 this.uniforms = {};
24 this.reGl = reGl;
25 var vs = options.vs,
26 fs = options.fs,
27 defines = options.defines,
28 attributes = options.attributes,
29 uniforms = options.uniforms,
30 primitive = options.primitive,
31 count = options.count,
32 elements = options.elements,
33 depth = options.depth,
34 blend = options.blend,
35 stencil = options.stencil,
36 cull = options.cull,
37 instances = options.instances,
38 scissor = options.scissor,
39 viewport = options.viewport;
40 var reglUniforms = {};
41
42 if (uniforms) {
43 this.uniforms = extractUniforms(uniforms);
44 Object.keys(uniforms).forEach(function (uniformName) {
45 // use regl prop API
46 // @ts-ignore
47 reglUniforms[uniformName] = reGl.prop(uniformName);
48 });
49 }
50
51 var reglAttributes = {};
52 Object.keys(attributes).forEach(function (name) {
53 reglAttributes[name] = attributes[name].get();
54 });
55 var defineStmts = defines && this.generateDefines(defines) || '';
56 var drawParams = {
57 attributes: reglAttributes,
58 frag: "#ifdef GL_FRAGMENT_PRECISION_HIGH\n precision highp float;\n#else\n precision mediump float;\n#endif\n".concat(defineStmts, "\n").concat(fs),
59 uniforms: reglUniforms,
60 vert: "\n".concat(defineStmts, "\n").concat(vs),
61 primitive: primitiveMap[primitive === undefined ? gl.TRIANGLES : primitive]
62 };
63
64 if (instances) {
65 drawParams.instances = instances;
66 } // elements 中可能包含 count,此时不应传入
67
68
69 if (count) {
70 drawParams.count = count;
71 }
72
73 if (elements) {
74 drawParams.elements = elements.get();
75 }
76
77 if (scissor) {
78 drawParams.scissor = scissor;
79 }
80
81 if (viewport) {
82 drawParams.viewport = viewport;
83 }
84
85 this.initDepthDrawParams({
86 depth: depth
87 }, drawParams);
88 this.initBlendDrawParams({
89 blend: blend
90 }, drawParams);
91 this.initStencilDrawParams({
92 stencil: stencil
93 }, drawParams);
94 this.initCullDrawParams({
95 cull: cull
96 }, drawParams);
97 this.drawCommand = reGl(drawParams);
98 }
99
100 _createClass(ReglModel, [{
101 key: "addUniforms",
102 value: function addUniforms(uniforms) {
103 this.uniforms = _objectSpread(_objectSpread({}, this.uniforms), extractUniforms(uniforms));
104 }
105 }, {
106 key: "draw",
107 value: function draw(options) {
108 var uniforms = _objectSpread(_objectSpread({}, this.uniforms), extractUniforms(options.uniforms || {}));
109
110 var reglDrawProps = {};
111 Object.keys(uniforms).forEach(function (uniformName) {
112 var type = _typeof(uniforms[uniformName]);
113
114 if (type === 'boolean' || type === 'number' || Array.isArray(uniforms[uniformName]) || // @ts-ignore
115 uniforms[uniformName].BYTES_PER_ELEMENT) {
116 reglDrawProps[uniformName] = uniforms[uniformName];
117 } else if (type === 'string') {// TODO: image url
118 } else {
119 reglDrawProps[uniformName] = uniforms[uniformName].get();
120 }
121 });
122 this.drawCommand(reglDrawProps);
123 }
124 }, {
125 key: "destroy",
126 value: function destroy() {// don't need do anything since we will call `rendererService.cleanup()`
127 }
128 /**
129 * @see https://github.com/regl-project/regl/blob/gh-pages/API.md#depth-buffer
130 */
131
132 }, {
133 key: "initDepthDrawParams",
134 value: function initDepthDrawParams(_ref, drawParams) {
135 var depth = _ref.depth;
136
137 if (depth) {
138 drawParams.depth = {
139 enable: depth.enable === undefined ? true : !!depth.enable,
140 mask: depth.mask === undefined ? true : !!depth.mask,
141 func: depthFuncMap[depth.func || gl.LESS],
142 range: depth.range || [0, 1]
143 };
144 }
145 }
146 /**
147 * @see https://github.com/regl-project/regl/blob/gh-pages/API.md#blending
148 */
149
150 }, {
151 key: "initBlendDrawParams",
152 value: function initBlendDrawParams(_ref2, drawParams) {
153 var blend = _ref2.blend;
154
155 if (blend) {
156 var enable = blend.enable,
157 func = blend.func,
158 equation = blend.equation,
159 _blend$color = blend.color,
160 color = _blend$color === void 0 ? [0, 0, 0, 0] : _blend$color; // @ts-ignore
161
162 drawParams.blend = {
163 enable: !!enable,
164 func: {
165 srcRGB: blendFuncMap[func && func.srcRGB || gl.SRC_ALPHA],
166 srcAlpha: blendFuncMap[func && func.srcAlpha || gl.SRC_ALPHA],
167 dstRGB: blendFuncMap[func && func.dstRGB || gl.ONE_MINUS_SRC_ALPHA],
168 dstAlpha: blendFuncMap[func && func.dstAlpha || gl.ONE_MINUS_SRC_ALPHA]
169 },
170 equation: {
171 rgb: blendEquationMap[equation && equation.rgb || gl.FUNC_ADD],
172 alpha: blendEquationMap[equation && equation.alpha || gl.FUNC_ADD]
173 },
174 color: color
175 };
176 }
177 }
178 /**
179 * @see https://github.com/regl-project/regl/blob/gh-pages/API.md#stencil
180 */
181
182 }, {
183 key: "initStencilDrawParams",
184 value: function initStencilDrawParams(_ref3, drawParams) {
185 var stencil = _ref3.stencil;
186
187 if (stencil) {
188 var enable = stencil.enable,
189 _stencil$mask = stencil.mask,
190 mask = _stencil$mask === void 0 ? -1 : _stencil$mask,
191 _stencil$func = stencil.func,
192 func = _stencil$func === void 0 ? {
193 cmp: gl.ALWAYS,
194 ref: 0,
195 mask: -1
196 } : _stencil$func,
197 _stencil$opFront = stencil.opFront,
198 opFront = _stencil$opFront === void 0 ? {
199 fail: gl.KEEP,
200 zfail: gl.KEEP,
201 zpass: gl.KEEP
202 } : _stencil$opFront,
203 _stencil$opBack = stencil.opBack,
204 opBack = _stencil$opBack === void 0 ? {
205 fail: gl.KEEP,
206 zfail: gl.KEEP,
207 zpass: gl.KEEP
208 } : _stencil$opBack;
209 drawParams.stencil = {
210 enable: !!enable,
211 mask: mask,
212 func: _objectSpread(_objectSpread({}, func), {}, {
213 cmp: stencilFuncMap[func.cmp]
214 }),
215 opFront: {
216 fail: stencilOpMap[opFront.fail],
217 zfail: stencilOpMap[opFront.zfail],
218 zpass: stencilOpMap[opFront.zpass]
219 },
220 opBack: {
221 fail: stencilOpMap[opBack.fail],
222 zfail: stencilOpMap[opBack.zfail],
223 zpass: stencilOpMap[opBack.zpass]
224 }
225 };
226 }
227 }
228 /**
229 * @see https://github.com/regl-project/regl/blob/gh-pages/API.md#culling
230 */
231
232 }, {
233 key: "initCullDrawParams",
234 value: function initCullDrawParams(_ref4, drawParams) {
235 var cull = _ref4.cull;
236
237 if (cull) {
238 var enable = cull.enable,
239 _cull$face = cull.face,
240 face = _cull$face === void 0 ? gl.BACK : _cull$face;
241 drawParams.cull = {
242 enable: !!enable,
243 face: cullFaceMap[face]
244 };
245 }
246 }
247 }, {
248 key: "generateDefines",
249 value: function generateDefines(defines) {
250 return Object.keys(defines).map(function (name) {
251 return "#define ".concat(name, " ").concat(Number(defines[name]));
252 }).join('\n');
253 }
254 }]);
255
256 return ReglModel;
257}();
258
259export { ReglModel as default };
260//# sourceMappingURL=ReglModel.js.map
\No newline at end of file