@shaderfrog/core 0.0.2 → 0.1.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 (56) hide show
  1. package/README.md +184 -1
  2. package/dist/ast/manipulate.js +328 -0
  3. package/dist/ast/shader-sections.js +256 -0
  4. package/dist/context.js +230 -0
  5. package/dist/engine.js +209 -0
  6. package/dist/evaluate.js +27 -0
  7. package/dist/graph-types.js +7 -0
  8. package/dist/graph.js +381 -0
  9. package/dist/graph.test.js +168 -0
  10. package/dist/nodes/code-nodes.js +18 -0
  11. package/dist/nodes/core-node.js +9 -0
  12. package/dist/nodes/data-nodes.js +123 -0
  13. package/dist/nodes/edge.js +1 -0
  14. package/dist/nodes/engine-node.js +189 -0
  15. package/dist/parsers.js +213 -0
  16. package/dist/plugins/babylon/bablyengine.js +582 -0
  17. package/dist/plugins/babylon/importers.js +64 -0
  18. package/{src/plugins/babylon/index.ts → dist/plugins/babylon/index.js} +0 -1
  19. package/dist/plugins/playcanvas/importers.js +28 -0
  20. package/dist/plugins/playcanvas/index.js +2 -0
  21. package/dist/plugins/playcanvas/playengine.js +510 -0
  22. package/dist/plugins/three/importers.js +15 -0
  23. package/{src/plugins/three/index.ts → dist/plugins/three/index.js} +0 -1
  24. package/dist/plugins/three/threngine.js +495 -0
  25. package/dist/strategy/assignemntTo.js +26 -0
  26. package/dist/strategy/declarationOf.js +23 -0
  27. package/dist/strategy/hardCode.js +23 -0
  28. package/dist/strategy/index.js +38 -0
  29. package/dist/strategy/inject.js +122 -0
  30. package/dist/strategy/namedAttribute.js +48 -0
  31. package/dist/strategy/texture2D.js +83 -0
  32. package/dist/strategy/uniform.js +190 -0
  33. package/dist/strategy/variable.js +80 -0
  34. package/dist/stratgies.test.js +164 -0
  35. package/dist/util/ast.js +9 -0
  36. package/dist/util/ensure.js +7 -0
  37. package/dist/util/id.js +2 -0
  38. package/package.json +12 -4
  39. package/src/ast/manipulate.ts +0 -392
  40. package/src/ast/shader-sections.ts +0 -323
  41. package/src/core/engine.ts +0 -214
  42. package/src/core/file.js +0 -53
  43. package/src/core/graph.ts +0 -1007
  44. package/src/core/nodes/code-nodes.ts +0 -66
  45. package/src/core/nodes/core-node.ts +0 -48
  46. package/src/core/nodes/data-nodes.ts +0 -344
  47. package/src/core/nodes/edge.ts +0 -23
  48. package/src/core/nodes/engine-node.ts +0 -266
  49. package/src/core/strategy.ts +0 -520
  50. package/src/core.test.ts +0 -312
  51. package/src/plugins/babylon/bablyengine.ts +0 -670
  52. package/src/plugins/babylon/importers.ts +0 -69
  53. package/src/plugins/three/importers.ts +0 -18
  54. package/src/plugins/three/threngine.tsx +0 -571
  55. package/src/util/ensure.ts +0 -10
  56. package/src/util/id.ts +0 -2
@@ -0,0 +1,510 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
13
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
14
+ return new (P || (P = Promise))(function (resolve, reject) {
15
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
16
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
17
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
18
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
19
+ });
20
+ };
21
+ var __generator = (this && this.__generator) || function (thisArg, body) {
22
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
23
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
24
+ function verb(n) { return function (v) { return step([n, v]); }; }
25
+ function step(op) {
26
+ if (f) throw new TypeError("Generator is already executing.");
27
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
28
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
29
+ if (y = 0, t) op = [op[0] & 2, t.value];
30
+ switch (op[0]) {
31
+ case 0: case 1: t = op; break;
32
+ case 4: _.label++; return { value: op[1], done: false };
33
+ case 5: _.label++; y = op[1]; op = [0]; continue;
34
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
35
+ default:
36
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
37
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
38
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
39
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
40
+ if (t[2]) _.ops.pop();
41
+ _.trys.pop(); continue;
42
+ }
43
+ op = body.call(thisArg, _);
44
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
45
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
46
+ }
47
+ };
48
+ var __read = (this && this.__read) || function (o, n) {
49
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
50
+ if (!m) return o;
51
+ var i = m.call(o), r, ar = [], e;
52
+ try {
53
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
54
+ }
55
+ catch (error) { e = { error: error }; }
56
+ finally {
57
+ try {
58
+ if (r && !r.done && (m = i["return"])) m.call(i);
59
+ }
60
+ finally { if (e) throw e.error; }
61
+ }
62
+ return ar;
63
+ };
64
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
65
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
66
+ if (ar || !(i in from)) {
67
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
68
+ ar[i] = from[i];
69
+ }
70
+ }
71
+ return to.concat(ar || Array.prototype.slice.call(from));
72
+ };
73
+ var _a, _b;
74
+ import * as pc from 'playcanvas';
75
+ import { EngineNodeType } from '../../engine';
76
+ import { nodeName, doesLinkThruShader, prepopulatePropertyInputs, mangleMainFn, } from '../../graph';
77
+ import { NodeType } from '../../graph-types';
78
+ import importers from './importers';
79
+ import { returnGlPositionHardCoded, returnGlPosition, returnGlPositionVec3Right, } from '../../ast/manipulate';
80
+ import { property, } from '../../nodes/code-nodes';
81
+ import { namedAttributeStrategy, texture2DStrategy, uniformStrategy, } from '../../strategy';
82
+ var log = function () {
83
+ var _a;
84
+ var args = [];
85
+ for (var _i = 0; _i < arguments.length; _i++) {
86
+ args[_i] = arguments[_i];
87
+ }
88
+ return (_a = console.log).call.apply(_a, __spreadArray([console, '\x1b[33m(playengine)\x1b[0m'], __read(args), false));
89
+ };
90
+ export var physicalDefaultProperties = {
91
+ // Required for objects with opacity
92
+ blendType: pc.BLEND_NORMAL,
93
+ // Hack (for now?). Playcanvas optimizes their materials to not calculate in
94
+ // the generated GLSL, if opacity is set to 1 or is unused. This forces the
95
+ // generated shader to include the opacity calculation. When we tween the
96
+ // opacity slider at runtime, playcanvas instantly generates a new shader +
97
+ // GLSL. The front-end code hijacks that call, and for now, for conveneinece,
98
+ // uses the GLSL from the core here that always includes opacity. Another
99
+ // option is to hijack both variations of the shader and generate two core
100
+ // variations. I might need to consider that with additional parameter
101
+ // variations.
102
+ opacity: 0.5,
103
+ // not sure if required for metalness https://developer.playcanvas.com/en/api/pc.StandardMaterial.html#useMetalness
104
+ useMetalness: true,
105
+ // Double sided
106
+ cull: pc.CULLFACE_NONE,
107
+ };
108
+ /**
109
+ * For the use of "0.5", apparently PlayCanvas optimizes uniform
110
+ * generation where if you set diffuse to white (1,1,1) then it doesn't
111
+ * add the diffuse uniform, because that's the default state.
112
+ */
113
+ export var defaultPropertySetting = function (app, property) {
114
+ if (property.type === 'texture') {
115
+ return new pc.Texture(app.graphicsDevice);
116
+ }
117
+ else if (property.type === 'number') {
118
+ return 0.5;
119
+ }
120
+ else if (property.type === 'rgb') {
121
+ return new pc.Color(0.5, 0.5, 0.5);
122
+ }
123
+ else if (property.type === 'rgba') {
124
+ return new pc.Color(0.5, 0.5, 0.5, 0.5);
125
+ }
126
+ };
127
+ var applyPlayMaterialProperties = function (engineContext, shaderMaterial, app, graph, node, sibling) {
128
+ // Find inputs to this node that are dependent on a property of the material
129
+ var propertyInputs = node.inputs
130
+ .filter(function (i) { return i.property; })
131
+ .reduce(function (acc, input) {
132
+ var _a;
133
+ return (__assign(__assign({}, acc), (_a = {}, _a[input.id] = input, _a)));
134
+ }, {});
135
+ // Then look for any edges into those inputs and set the material property
136
+ var props = graph.edges
137
+ .filter(function (edge) { return edge.to === node.id || edge.to === (sibling === null || sibling === void 0 ? void 0 : sibling.id); })
138
+ .reduce(function (acc, edge) {
139
+ // Check if we've plugged into an input for a property
140
+ var propertyInput = propertyInputs[edge.input];
141
+ if (propertyInput) {
142
+ // Find the property itself
143
+ var property_1 = (node.config.properties || []).find(function (p) { return p.property === propertyInput.property; });
144
+ /**
145
+ * For the mutation, you apparently need to explicitily call .set() on
146
+ * some properties, like diffuse:
147
+ * material.diffuse = new pc.Color(0.5, 0.5, 0.5)
148
+ * has no effect. You have to do
149
+ * material.diffuse.set(0.5, 0.5, 0.5)
150
+ * and note the API isn't great, you can't do
151
+ * material.diffuse.set(new pc.Color(...))
152
+ * This code will probably error on some properties because I don't know
153
+ * if all "rgb" properties have to be set in this painful way.
154
+ */
155
+ acc[property_1.property] = defaultPropertySetting(app, property_1);
156
+ // @ts-ignore
157
+ shaderMaterial[property_1.property] = acc[property_1.property];
158
+ }
159
+ return acc;
160
+ }, {});
161
+ return props;
162
+ };
163
+ export var physicalNode = function (id, name, groupId, position, uniforms, stage, nextStageNodeId) {
164
+ return prepopulatePropertyInputs({
165
+ id: id,
166
+ name: name,
167
+ groupId: groupId,
168
+ position: position,
169
+ type: EngineNodeType.physical,
170
+ config: {
171
+ uniforms: uniforms,
172
+ version: 3,
173
+ mangle: false,
174
+ preprocess: true,
175
+ properties: [
176
+ property('Color', 'diffuse', 'rgb'),
177
+ // Object is black while using these
178
+ // property('Emissive', 'emissive', 'rgb'),
179
+ // property('Emissive Intensity', 'emissiveIntensity', 'number'),
180
+ // property('Emissive Map', 'emissiveMap', 'texture'),
181
+ property('Diffuse Map', 'diffuseMap', 'texture', 'filler_texture_diffuseMap'),
182
+ property('Normal Map', 'normalMap', 'texture', 'filler_texture_normalMap'),
183
+ property('Bumpiness', 'bumpiness', 'number'),
184
+ property('Specular', 'specular', 'rgb'),
185
+ property('Glosiness', 'gloss', 'number'),
186
+ property('Opacity', 'opacity', 'number'),
187
+ property('Opacity Map', 'opacityMap', 'texture'),
188
+ property('Metalness', 'metalness', 'number'),
189
+ property('Reflectivity', 'reflectivity', 'number'),
190
+ property('Refraction', 'refraction', 'number'),
191
+ property('Refraction Index', 'refractionIndex', 'number'),
192
+ // property('Bump Map', 'bumpTexture', 'texture', 'filler_bumpSampler'),
193
+ // property('Metalness', 'metallic', 'number'),
194
+ // property('Roughness', 'roughness', 'number'),
195
+ // property('Env Map', 'environmentTexture', 'samplerCube'),
196
+ // property('Reflection Texture', 'reflectionTexture', 'samplerCube'),
197
+ // property('Refraction Texture', 'refractionTexture', 'samplerCube'),
198
+ // property('Index Of Refraction', 'indexOfRefraction', 'number'),
199
+ // property('Alpha', 'alpha', 'number'),
200
+ // property('Direct Intensity', 'directIntensity', 'number'),
201
+ // property('Environment Intensity', 'environmentIntensity', 'number'),
202
+ // property('Camera Exposure', 'cameraExposure', 'number'),
203
+ // property('Camera Contrast', 'cameraContrast', 'number'),
204
+ // property('Micro Surface', 'microSurface', 'number'),
205
+ // property('Reflectivity Color', 'reflectivityColor', 'rgb'),
206
+ ],
207
+ hardCodedProperties: physicalDefaultProperties,
208
+ strategies: [
209
+ uniformStrategy(),
210
+ stage === 'fragment'
211
+ ? texture2DStrategy()
212
+ : namedAttributeStrategy('vertex_position'),
213
+ ],
214
+ },
215
+ inputs: [],
216
+ outputs: [
217
+ {
218
+ name: 'vector4',
219
+ category: 'data',
220
+ id: '1',
221
+ },
222
+ ],
223
+ source: '',
224
+ stage: stage,
225
+ nextStageNodeId: nextStageNodeId,
226
+ });
227
+ };
228
+ export var toonNode = function (id, name, groupId, position, uniforms, stage, nextStageNodeId) {
229
+ return prepopulatePropertyInputs({
230
+ id: id,
231
+ name: name,
232
+ groupId: groupId,
233
+ position: position,
234
+ type: EngineNodeType.toon,
235
+ config: {
236
+ uniforms: uniforms,
237
+ version: 3,
238
+ preprocess: true,
239
+ mangle: false,
240
+ properties: [
241
+ property('Color', 'color', 'rgb', 'uniform_diffuse'),
242
+ property('Texture', 'map', 'texture', 'filler_map'),
243
+ property('Gradient Map', 'gradientMap', 'texture', 'filler_gradientMap'),
244
+ property('Normal Map', 'normalMap', 'texture', 'filler_normalMap'),
245
+ property('Normal Scale', 'normalScale', 'vector2'),
246
+ property('Displacement Map', 'displacementMap', 'texture'),
247
+ property('Env Map', 'envMap', 'samplerCube'),
248
+ ],
249
+ strategies: [
250
+ uniformStrategy(),
251
+ stage === 'fragment'
252
+ ? texture2DStrategy()
253
+ : namedAttributeStrategy('position'),
254
+ ],
255
+ },
256
+ inputs: [],
257
+ outputs: [
258
+ {
259
+ name: 'vector4',
260
+ category: 'data',
261
+ id: '1',
262
+ },
263
+ ],
264
+ source: '',
265
+ stage: stage,
266
+ nextStageNodeId: nextStageNodeId,
267
+ });
268
+ };
269
+ export var mIdx = 0;
270
+ var id = function () { return mIdx++; };
271
+ var nodeCacheKey = function (graph, node) {
272
+ return ('[ID:' +
273
+ node.id +
274
+ 'Edges:' +
275
+ graph.edges
276
+ .filter(function (edge) { return edge.to === node.id; })
277
+ .map(function (edge) { return "(".concat(edge.to, "->").concat(edge.input, ")"); })
278
+ .sort()
279
+ .join(',') +
280
+ ']'
281
+ // Currently excluding node inputs because these are calculated *after*
282
+ // the onbeforecompile, so the next compile, they'll all change!
283
+ // node.inputs.map((i) => `${i.id}${i.bakeable}`)
284
+ );
285
+ };
286
+ var programCacheKey = function (engineContext, graph, node, sibling) {
287
+ var app = engineContext.runtime.app;
288
+ var lights = app.root
289
+ .findComponents('light')
290
+ .map(function (l) { return l.type; });
291
+ return ([node, sibling]
292
+ .sort(function (a, b) { return a.id.localeCompare(b.id); })
293
+ .map(function (n) { return nodeCacheKey(graph, n); })
294
+ .join('-') +
295
+ '|Lights:' +
296
+ lights.join(',') +
297
+ '|Envtex:' +
298
+ [app.scene.envAtlas, app.scene.prefilteredCubemaps].join(','));
299
+ };
300
+ var cacher = function (engineContext, graph, node, sibling, newValue) { return __awaiter(void 0, void 0, void 0, function () {
301
+ var cacheKey, materialData;
302
+ return __generator(this, function (_a) {
303
+ switch (_a.label) {
304
+ case 0:
305
+ cacheKey = programCacheKey(engineContext, graph, node, sibling);
306
+ if (engineContext.runtime.cache.data[cacheKey]) {
307
+ log("Cache hit \"".concat(cacheKey, "\""));
308
+ }
309
+ else {
310
+ log("Cache miss \"".concat(cacheKey, "\""));
311
+ }
312
+ return [4 /*yield*/, (engineContext.runtime.cache.data[cacheKey] ||
313
+ newValue())];
314
+ case 1:
315
+ materialData = _a.sent();
316
+ log("Material cache \"".concat(cacheKey, "\" is now"), materialData);
317
+ engineContext.runtime.cache.data[cacheKey] = materialData;
318
+ engineContext.runtime.engineMaterial = materialData.material;
319
+ // TODO: We mutate the nodes here, can we avoid that later?
320
+ node.source =
321
+ node.stage === 'fragment' ? materialData.fragment : materialData.vertex;
322
+ sibling.source =
323
+ sibling.stage === 'fragment' ? materialData.fragment : materialData.vertex;
324
+ return [2 /*return*/];
325
+ }
326
+ });
327
+ }); };
328
+ var onBeforeCompileMegaShader = function (engineContext, graph, node, sibling) { return __awaiter(void 0, void 0, void 0, function () {
329
+ var _a, appUn, sceneData, app, pbrName, shaderMaterial, newProperties, applied, vertexSource, fragmentSource, origMat;
330
+ return __generator(this, function (_b) {
331
+ _a = engineContext.runtime, appUn = _a.app, sceneData = _a.sceneData;
332
+ app = appUn;
333
+ pbrName = "engine_pbr".concat(id());
334
+ shaderMaterial = new pc.StandardMaterial();
335
+ shaderMaterial.name = pbrName;
336
+ newProperties = __assign({}, (node.config.hardCodedProperties ||
337
+ sibling.config.hardCodedProperties ||
338
+ {}));
339
+ Object.assign(shaderMaterial, newProperties);
340
+ applied = applyPlayMaterialProperties(engineContext, shaderMaterial, app, graph, node, sibling);
341
+ log('Engine megashader initial properties', __assign(__assign({}, newProperties), applied));
342
+ // This is a hack to force the material to regenerate. The chunks are used
343
+ // internally in playcanvas to generate the material cache key. If you use
344
+ // a real chunk, it messes up the GLSL. So this introduces a fake chunk that
345
+ // isn't used in the GLSL, but forces a new material to generate. The code
346
+ // path I'm hacking through is:
347
+ // render() -> renderForward() -> updatePassShader() -> getShaderVariant() ->
348
+ // library.getProgram() -> generateShaderDefinition()
349
+ // TODO: Try using the new hook https://github.com/playcanvas/engine/pull/5524
350
+ shaderMaterial.chunks.engineHackSource = "".concat(Math.random());
351
+ // I had this here before for testing, I commented it out trying to figure out
352
+ // the opacity issue while working on Playcanvas. Can remove if I ever figure
353
+ // out the opacity issue
354
+ // shaderMaterial.clearVariants();
355
+ shaderMaterial.update();
356
+ origMat = sceneData.mesh.render.meshInstances[0].material;
357
+ sceneData.mesh.render.meshInstances[0].material = shaderMaterial;
358
+ // Force shader compilation
359
+ app.render();
360
+ sceneData.mesh.render.meshInstances[0].material = origMat;
361
+ return [2 /*return*/, new Promise(function (resolve) {
362
+ var variants = shaderMaterial.variants;
363
+ if (variants.size === 1) {
364
+ var _a = __read(variants.entries().next().value, 2), untypedVariant = _a[1];
365
+ var variant = untypedVariant;
366
+ var _b = variant.definition, fshader = _b.fshader, vshader = _b.vshader;
367
+ fragmentSource = fshader;
368
+ vertexSource = vshader;
369
+ engineContext.runtime.cache.nodes[node.id] = {
370
+ fragment: fshader,
371
+ vertex: vshader,
372
+ };
373
+ }
374
+ else {
375
+ console.error('Bad variants!', variants);
376
+ }
377
+ resolve({
378
+ material: shaderMaterial,
379
+ fragment: fragmentSource,
380
+ vertex: vertexSource,
381
+ });
382
+ })];
383
+ });
384
+ }); };
385
+ // TODO: NEED TO DO SAME THREE MANGLIGN STEP HERE
386
+ var megaShaderMainpulateAst = function (engineContext, engine, graph, node, ast, inputEdges) {
387
+ var programAst = ast;
388
+ var mainName = 'main' || nodeName(node);
389
+ if (node.stage === 'vertex') {
390
+ if (doesLinkThruShader(graph, node)) {
391
+ returnGlPositionHardCoded(mainName, programAst, 'vec3', 'transformed');
392
+ }
393
+ else {
394
+ returnGlPosition(mainName, programAst);
395
+ }
396
+ }
397
+ // We specify engine nodes are mangle: false, which is the graph step that
398
+ // handles renaming the main fn, so we have to do it ourselves
399
+ mangleMainFn(programAst, node);
400
+ return programAst;
401
+ };
402
+ var evaluateNode = function (node) {
403
+ if (node.type === 'number') {
404
+ return parseFloat(node.value);
405
+ // Apparently PlayCanvas can't use new pc.Vec2(...), etc for vector uniforms,
406
+ // and instead requires arrays? But can (maybe) use a pc.Color() for other
407
+ // uniforms? I need to test the color assumption, but it's weird PC doesn't
408
+ // use its own types for uniforms
409
+ }
410
+ else if (node.type === 'texture') {
411
+ return node;
412
+ }
413
+ else if (node.type === 'vector2') {
414
+ return [parseFloat(node.value[0]), parseFloat(node.value[1])];
415
+ }
416
+ else if (node.type === 'vector3') {
417
+ return [
418
+ parseFloat(node.value[0]),
419
+ parseFloat(node.value[1]),
420
+ parseFloat(node.value[2]),
421
+ ];
422
+ }
423
+ else if (node.type === 'vector4') {
424
+ return [
425
+ parseFloat(node.value[0]),
426
+ parseFloat(node.value[1]),
427
+ parseFloat(node.value[2]),
428
+ parseFloat(node.value[3]),
429
+ ];
430
+ }
431
+ else if (node.type === 'rgb') {
432
+ return [
433
+ parseFloat(node.value[0]),
434
+ parseFloat(node.value[1]),
435
+ parseFloat(node.value[2]),
436
+ ];
437
+ }
438
+ else if (node.type === 'rgba') {
439
+ return [
440
+ parseFloat(node.value[0]),
441
+ parseFloat(node.value[1]),
442
+ parseFloat(node.value[2]),
443
+ parseFloat(node.value[3]),
444
+ ];
445
+ }
446
+ else {
447
+ return node.value;
448
+ }
449
+ };
450
+ export var playengine = {
451
+ name: 'playcanvas',
452
+ importers: importers,
453
+ mergeOptions: {
454
+ includePrecisions: true,
455
+ includeVersion: false,
456
+ },
457
+ evaluateNode: evaluateNode,
458
+ constructors: (_a = {},
459
+ _a[EngineNodeType.physical] = physicalNode,
460
+ _a[EngineNodeType.toon] = toonNode,
461
+ _a),
462
+ // TODO: Move into core based on engine shader scrape
463
+ preserve: new Set([
464
+ // Ugh I gotta stop doing this
465
+ 'vPositionW',
466
+ 'vertex_normal',
467
+ 'vertex_position',
468
+ 'vertex_texCoord0',
469
+ 'vertex_texCoord1',
470
+ 'matrix_model',
471
+ 'matrix_viewProjection',
472
+ 'matrix_normal',
473
+ 'normal',
474
+ 'uv',
475
+ 'uv2',
476
+ // varyings
477
+ 'vUv0',
478
+ 'time',
479
+ ]),
480
+ parsers: (_b = {},
481
+ _b[NodeType.SOURCE] = {
482
+ manipulateAst: function (engineContext, engine, graph, node, ast, inputEdges) {
483
+ var programAst = ast;
484
+ var mainName = 'main' || nodeName(node);
485
+ // This hinges on the vertex shader calling vec3(p)
486
+ if (node.stage === 'vertex') {
487
+ if (doesLinkThruShader(graph, node)) {
488
+ returnGlPositionVec3Right(mainName, programAst);
489
+ }
490
+ else {
491
+ returnGlPosition(mainName, programAst);
492
+ }
493
+ }
494
+ return ast;
495
+ },
496
+ },
497
+ _b[EngineNodeType.physical] = {
498
+ onBeforeCompile: function (graph, engineContext, node, sibling) {
499
+ return cacher(engineContext, graph, node, sibling, function () {
500
+ return onBeforeCompileMegaShader(engineContext, graph, node, sibling);
501
+ });
502
+ },
503
+ manipulateAst: megaShaderMainpulateAst,
504
+ },
505
+ _b),
506
+ };
507
+ playengine.parsers[EngineNodeType.toon] =
508
+ playengine.parsers[EngineNodeType.physical];
509
+ playengine.parsers[EngineNodeType.phong] =
510
+ playengine.parsers[EngineNodeType.physical];
@@ -0,0 +1,15 @@
1
+ import { renameBindings } from '@shaderfrog/glsl-parser/parser/utils';
2
+ var importers = {
3
+ babylon: {
4
+ convertAst: function (ast, type) {
5
+ renameBindings(ast.scopes[0], function (name) {
6
+ return name === 'vMainUV1' ? 'vUv' : name === 'vNormalW' ? 'vNormal' : name;
7
+ });
8
+ },
9
+ nodeInputMap: {},
10
+ edgeMap: {
11
+ bumpSampler: 'normalMap',
12
+ },
13
+ },
14
+ };
15
+ export default importers;
@@ -1,3 +1,2 @@
1
1
  import { threngine } from './threngine';
2
-
3
2
  export { threngine as engine };