UNPKG

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