UNPKG

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