@shaderfrog/core 1.0.5 → 1.0.6

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.
@@ -67,4 +67,13 @@ export declare const compileGraph: (engineContext: EngineContext, engine: Engine
67
67
  * which currently means it will find all active engine nodes.
68
68
  */
69
69
  export declare const collectNodeProperties: (graph: Graph) => SearchResult;
70
+ export type IndexedDataInputs = Record<string, NodeInput[]>;
71
+ export type CompileResult = {
72
+ fragmentResult: string;
73
+ vertexResult: string;
74
+ compileResult: CompileGraphResult;
75
+ dataNodes: Record<string, GraphNode>;
76
+ dataInputs: IndexedDataInputs;
77
+ };
78
+ export declare const compileSource: (graph: Graph, engine: Engine, ctx: EngineContext) => Promise<CompileResult>;
70
79
  export {};
@@ -9,6 +9,42 @@ var __assign = (this && this.__assign) || function () {
9
9
  };
10
10
  return __assign.apply(this, arguments);
11
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
+ };
12
48
  var __read = (this && this.__read) || function (o, n) {
13
49
  var m = typeof Symbol === "function" && o[Symbol.iterator];
14
50
  if (!m) return o;
@@ -35,7 +71,8 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
35
71
  return to.concat(ar || Array.prototype.slice.call(from));
36
72
  };
37
73
  import { renameBindings, renameFunctions, } from '@shaderfrog/glsl-parser/parser/utils';
38
- import { emptyShaderSections, findShaderSections, mergeShaderSections, } from './shader-sections';
74
+ import { computeGraphContext } from './context';
75
+ import { emptyShaderSections, findShaderSections, mergeShaderSections, shaderSectionsToProgram, } from './shader-sections';
39
76
  import { makeExpression } from '../util/ast';
40
77
  import { ensure } from '../util/ensure';
41
78
  import { SourceType } from './code-nodes';
@@ -44,6 +81,7 @@ import { makeId } from '../util/id';
44
81
  import { coreParsers } from './parsers';
45
82
  import { toGlsl } from './evaluate';
46
83
  import { MAGIC_OUTPUT_STMTS, NodeType } from './graph-types';
84
+ import { generate } from '@shaderfrog/glsl-parser';
47
85
  var log = function () {
48
86
  var _a;
49
87
  var args = [];
@@ -379,3 +417,33 @@ export var collectNodeProperties = function (graph) {
379
417
  inputs: __assign(__assign({}, fragProperties.inputs), vertProperties.inputs),
380
418
  };
381
419
  };
420
+ export var compileSource = function (graph, engine, ctx) { return __awaiter(void 0, void 0, void 0, function () {
421
+ var compileResult, fragmentResult, vertexResult, dataInputs, dataNodes;
422
+ return __generator(this, function (_a) {
423
+ switch (_a.label) {
424
+ case 0: return [4 /*yield*/, computeGraphContext(ctx, engine, graph)];
425
+ case 1:
426
+ _a.sent();
427
+ compileResult = compileGraph(ctx, engine, graph);
428
+ fragmentResult = generate(shaderSectionsToProgram(compileResult.fragment, engine.mergeOptions).program);
429
+ vertexResult = generate(shaderSectionsToProgram(compileResult.vertex, engine.mergeOptions).program);
430
+ dataInputs = filterGraphNodes(graph, [compileResult.outputFrag, compileResult.outputVert], { input: isDataInput }).inputs;
431
+ dataNodes = Object.entries(dataInputs).reduce(function (acc, _a) {
432
+ var _b = __read(_a, 2), nodeId = _b[0], inputs = _b[1];
433
+ return inputs.reduce(function (iAcc, input) {
434
+ var fromEdge = graph.edges.find(function (edge) { return edge.to === nodeId && edge.input === input.id; });
435
+ var fromNode = fromEdge && graph.nodes.find(function (node) { return node.id === fromEdge.from; });
436
+ return fromNode
437
+ ? __assign(__assign({}, iAcc), collectConnectedNodes(graph, fromNode)) : iAcc;
438
+ }, acc);
439
+ }, {});
440
+ return [2 /*return*/, {
441
+ compileResult: compileResult,
442
+ fragmentResult: fragmentResult,
443
+ vertexResult: vertexResult,
444
+ dataNodes: dataNodes,
445
+ dataInputs: dataInputs,
446
+ }];
447
+ }
448
+ });
449
+ }); };
@@ -3,6 +3,7 @@ export * from './data-nodes';
3
3
  export * from './engine-node';
4
4
  export * from './code-nodes';
5
5
  export * from './edge';
6
+ export * from './parsers';
6
7
  export * from './context';
7
8
  export * from './evaluate';
8
9
  export * from './graph-types';
@@ -3,6 +3,7 @@ export * from './data-nodes';
3
3
  export * from './engine-node';
4
4
  export * from './code-nodes';
5
5
  export * from './edge';
6
+ export * from './parsers';
6
7
  export * from './context';
7
8
  export * from './evaluate';
8
9
  export * from './graph-types';
@@ -1,5 +1,7 @@
1
+ import { RawShaderMaterial } from 'three';
1
2
  import { ShaderStage } from '../../graph/graph-types';
2
- import { Engine } from '../../engine';
3
+ import { Engine, EngineContext } from '../../engine';
4
+ import { CompileResult } from '../../graph/graph';
3
5
  import { CodeNode, NodeProperty } from '../../graph/code-nodes';
4
6
  import { NodePosition } from '../../graph/base-node';
5
7
  import { UniformDataType } from '../../graph/data-nodes';
@@ -29,3 +31,4 @@ export type ThreeRuntime = {
29
31
  };
30
32
  export declare const toonNode: (id: string, name: string, groupId: string | null | undefined, position: NodePosition, uniforms: UniformDataType[], stage: ShaderStage | undefined, nextStageNodeId?: string) => CodeNode;
31
33
  export declare const threngine: Engine;
34
+ export declare const createMaterial: (compileResult: CompileResult, ctx: EngineContext) => RawShaderMaterial;
@@ -71,7 +71,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
71
71
  return to.concat(ar || Array.prototype.slice.call(from));
72
72
  };
73
73
  var _a, _b;
74
- import { Vector2, Vector3, Vector4, Color } from 'three';
74
+ import { ShaderLib, RawShaderMaterial, Vector2, Vector3, Vector4, Color, } from 'three';
75
75
  import { NodeType } from '../../graph/graph-types';
76
76
  import { prepopulatePropertyInputs, mangleMainFn } from '../../graph/graph';
77
77
  import importers from './importers';
@@ -493,3 +493,49 @@ export var threngine = {
493
493
  },
494
494
  _b),
495
495
  };
496
+ export var createMaterial = function (compileResult, ctx) {
497
+ var engineMaterial = ctx.runtime.engineMaterial;
498
+ var finalUniforms = __assign(__assign(__assign(__assign({}, ShaderLib.phong.uniforms), ShaderLib.toon.uniforms), ShaderLib.physical.uniforms), { time: { value: 0 } });
499
+ var initialProperties = {
500
+ name: 'ShaderFrog Material',
501
+ lights: true,
502
+ uniforms: __assign({}, finalUniforms),
503
+ transparent: true,
504
+ opacity: 1.0,
505
+ vertexShader: compileResult === null || compileResult === void 0 ? void 0 : compileResult.vertexResult,
506
+ fragmentShader: compileResult === null || compileResult === void 0 ? void 0 : compileResult.fragmentResult,
507
+ };
508
+ var additionalProperties = Object.entries(__assign({}, engineMaterial))
509
+ .filter(function (_a) {
510
+ var _b = __read(_a, 1), property = _b[0];
511
+ // Ignore three material "hidden" properties
512
+ return property.charAt(0) !== '_' &&
513
+ // Ignore uuid since it should probably be unique?
514
+ property !== 'uuid' &&
515
+ // I'm not sure what three does with type under the hood, ignore it
516
+ property !== 'type' &&
517
+ // "precision" adds a precision preprocessor line
518
+ property !== 'precision' &&
519
+ // Ignore existing properties
520
+ !(property in initialProperties) &&
521
+ // Ignore STANDARD and PHYSICAL defines to the top of the shader in
522
+ // WebGLProgram
523
+ // https://github.com/mrdoob/three.js/blob/e7042de7c1a2c70e38654a04b6fd97d9c978e781/src/renderers/webgl/WebGLProgram.js#L392
524
+ // which occurs if we set isMeshPhysicalMaterial/isMeshStandardMaterial
525
+ property !== 'defines';
526
+ })
527
+ .reduce(function (acc, _a) {
528
+ var _b;
529
+ var _c = __read(_a, 2), key = _c[0], value = _c[1];
530
+ return (__assign(__assign({}, acc), (_b = {}, _b[key] = value, _b)));
531
+ }, {});
532
+ var material = new RawShaderMaterial(initialProperties);
533
+ // This prevents a deluge of warnings from three on the constructor saying
534
+ // that each of these properties is not a property of the material
535
+ Object.entries(additionalProperties).forEach(function (_a) {
536
+ var _b = __read(_a, 2), key = _b[0], value = _b[1];
537
+ // @ts-ignore
538
+ material[key] = value;
539
+ });
540
+ return material;
541
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shaderfrog/core",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "Shaderfrog core",
5
5
  "module": "./dist/index.js",
6
6
  "scripts": {