@shaderfrog/core 1.2.0 → 1.3.0

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 (155) hide show
  1. package/cjs/package.json +1 -0
  2. package/esm/engine.d.ts +69 -0
  3. package/esm/engine.js +209 -0
  4. package/esm/graph/base-node.d.ts +36 -0
  5. package/esm/graph/base-node.js +9 -0
  6. package/esm/graph/code-nodes.d.ts +44 -0
  7. package/esm/graph/code-nodes.js +18 -0
  8. package/esm/graph/context.d.ts +37 -0
  9. package/esm/graph/context.js +243 -0
  10. package/esm/graph/data-nodes.d.ts +83 -0
  11. package/esm/graph/data-nodes.js +131 -0
  12. package/esm/graph/edge.d.ts +13 -0
  13. package/esm/graph/edge.js +7 -0
  14. package/esm/graph/evaluate.d.ts +9 -0
  15. package/esm/graph/evaluate.js +27 -0
  16. package/esm/graph/graph-node.d.ts +8 -0
  17. package/esm/graph/graph-node.js +135 -0
  18. package/esm/graph/graph-types.d.ts +38 -0
  19. package/esm/graph/graph-types.js +22 -0
  20. package/esm/graph/graph.d.ts +89 -0
  21. package/esm/graph/graph.js +506 -0
  22. package/esm/graph/graph.test.d.ts +1 -0
  23. package/esm/graph/graph.test.js +168 -0
  24. package/esm/graph/index.d.ts +11 -0
  25. package/esm/graph/index.js +11 -0
  26. package/esm/graph/parsers.d.ts +39 -0
  27. package/esm/graph/parsers.js +213 -0
  28. package/esm/graph/shader-sections.d.ts +47 -0
  29. package/esm/graph/shader-sections.js +256 -0
  30. package/esm/index.d.ts +3 -0
  31. package/esm/index.js +3 -0
  32. package/esm/package.json +1 -0
  33. package/esm/plugins/babylon/bablyengine.d.ts +28 -0
  34. package/esm/plugins/babylon/bablyengine.js +589 -0
  35. package/esm/plugins/babylon/importers.d.ts +3 -0
  36. package/esm/plugins/babylon/importers.js +64 -0
  37. package/esm/plugins/babylon/index.d.ts +2 -0
  38. package/esm/plugins/babylon/index.js +2 -0
  39. package/esm/plugins/playcanvas/importers.d.ts +3 -0
  40. package/esm/plugins/playcanvas/importers.js +28 -0
  41. package/esm/plugins/playcanvas/index.d.ts +2 -0
  42. package/esm/plugins/playcanvas/index.js +2 -0
  43. package/esm/plugins/playcanvas/playengine.d.ts +39 -0
  44. package/esm/plugins/playcanvas/playengine.js +517 -0
  45. package/esm/plugins/three/importers.d.ts +3 -0
  46. package/esm/plugins/three/importers.js +15 -0
  47. package/esm/plugins/three/index.d.ts +2 -0
  48. package/esm/plugins/three/index.js +2 -0
  49. package/esm/plugins/three/threngine.d.ts +34 -0
  50. package/esm/plugins/three/threngine.js +608 -0
  51. package/esm/strategy/assignemntTo.d.ts +9 -0
  52. package/esm/strategy/assignemntTo.js +26 -0
  53. package/esm/strategy/declarationOf.d.ts +9 -0
  54. package/esm/strategy/declarationOf.js +23 -0
  55. package/esm/strategy/hardCode.d.ts +15 -0
  56. package/esm/strategy/hardCode.js +23 -0
  57. package/esm/strategy/index.d.ts +9 -0
  58. package/esm/strategy/index.js +9 -0
  59. package/esm/strategy/inject.d.ts +15 -0
  60. package/esm/strategy/inject.js +122 -0
  61. package/esm/strategy/namedAttribute.d.ts +9 -0
  62. package/esm/strategy/namedAttribute.js +48 -0
  63. package/esm/strategy/strategy.d.ts +28 -0
  64. package/esm/strategy/strategy.js +31 -0
  65. package/esm/strategy/stratgies.test.d.ts +1 -0
  66. package/esm/strategy/stratgies.test.js +164 -0
  67. package/esm/strategy/texture2D.d.ts +6 -0
  68. package/esm/strategy/texture2D.js +83 -0
  69. package/esm/strategy/uniform.d.ts +6 -0
  70. package/esm/strategy/uniform.js +190 -0
  71. package/esm/strategy/variable.d.ts +6 -0
  72. package/esm/strategy/variable.js +80 -0
  73. package/esm/util/ast.d.ts +30 -0
  74. package/esm/util/ast.js +332 -0
  75. package/esm/util/ensure.d.ts +1 -0
  76. package/esm/util/ensure.js +7 -0
  77. package/esm/util/id.d.ts +1 -0
  78. package/esm/util/id.js +2 -0
  79. package/package.json +9 -11
  80. /package/{engine.d.ts → cjs/engine.d.ts} +0 -0
  81. /package/{engine.js → cjs/engine.js} +0 -0
  82. /package/{graph → cjs/graph}/base-node.d.ts +0 -0
  83. /package/{graph → cjs/graph}/base-node.js +0 -0
  84. /package/{graph → cjs/graph}/code-nodes.d.ts +0 -0
  85. /package/{graph → cjs/graph}/code-nodes.js +0 -0
  86. /package/{graph → cjs/graph}/context.d.ts +0 -0
  87. /package/{graph → cjs/graph}/context.js +0 -0
  88. /package/{graph → cjs/graph}/data-nodes.d.ts +0 -0
  89. /package/{graph → cjs/graph}/data-nodes.js +0 -0
  90. /package/{graph → cjs/graph}/edge.d.ts +0 -0
  91. /package/{graph → cjs/graph}/edge.js +0 -0
  92. /package/{graph → cjs/graph}/evaluate.d.ts +0 -0
  93. /package/{graph → cjs/graph}/evaluate.js +0 -0
  94. /package/{graph → cjs/graph}/graph-node.d.ts +0 -0
  95. /package/{graph → cjs/graph}/graph-node.js +0 -0
  96. /package/{graph → cjs/graph}/graph-types.d.ts +0 -0
  97. /package/{graph → cjs/graph}/graph-types.js +0 -0
  98. /package/{graph → cjs/graph}/graph.d.ts +0 -0
  99. /package/{graph → cjs/graph}/graph.js +0 -0
  100. /package/{graph → cjs/graph}/graph.test.d.ts +0 -0
  101. /package/{graph → cjs/graph}/graph.test.js +0 -0
  102. /package/{graph → cjs/graph}/index.d.ts +0 -0
  103. /package/{graph → cjs/graph}/index.js +0 -0
  104. /package/{graph → cjs/graph}/parsers.d.ts +0 -0
  105. /package/{graph → cjs/graph}/parsers.js +0 -0
  106. /package/{graph → cjs/graph}/shader-sections.d.ts +0 -0
  107. /package/{graph → cjs/graph}/shader-sections.js +0 -0
  108. /package/{index.d.ts → cjs/index.d.ts} +0 -0
  109. /package/{index.js → cjs/index.js} +0 -0
  110. /package/{plugins → cjs/plugins}/babylon/bablyengine.d.ts +0 -0
  111. /package/{plugins → cjs/plugins}/babylon/bablyengine.js +0 -0
  112. /package/{plugins → cjs/plugins}/babylon/importers.d.ts +0 -0
  113. /package/{plugins → cjs/plugins}/babylon/importers.js +0 -0
  114. /package/{plugins → cjs/plugins}/babylon/index.d.ts +0 -0
  115. /package/{plugins → cjs/plugins}/babylon/index.js +0 -0
  116. /package/{plugins → cjs/plugins}/playcanvas/importers.d.ts +0 -0
  117. /package/{plugins → cjs/plugins}/playcanvas/importers.js +0 -0
  118. /package/{plugins → cjs/plugins}/playcanvas/index.d.ts +0 -0
  119. /package/{plugins → cjs/plugins}/playcanvas/index.js +0 -0
  120. /package/{plugins → cjs/plugins}/playcanvas/playengine.d.ts +0 -0
  121. /package/{plugins → cjs/plugins}/playcanvas/playengine.js +0 -0
  122. /package/{plugins → cjs/plugins}/three/importers.d.ts +0 -0
  123. /package/{plugins → cjs/plugins}/three/importers.js +0 -0
  124. /package/{plugins → cjs/plugins}/three/index.d.ts +0 -0
  125. /package/{plugins → cjs/plugins}/three/index.js +0 -0
  126. /package/{plugins → cjs/plugins}/three/threngine.d.ts +0 -0
  127. /package/{plugins → cjs/plugins}/three/threngine.js +0 -0
  128. /package/{strategy → cjs/strategy}/assignemntTo.d.ts +0 -0
  129. /package/{strategy → cjs/strategy}/assignemntTo.js +0 -0
  130. /package/{strategy → cjs/strategy}/declarationOf.d.ts +0 -0
  131. /package/{strategy → cjs/strategy}/declarationOf.js +0 -0
  132. /package/{strategy → cjs/strategy}/hardCode.d.ts +0 -0
  133. /package/{strategy → cjs/strategy}/hardCode.js +0 -0
  134. /package/{strategy → cjs/strategy}/index.d.ts +0 -0
  135. /package/{strategy → cjs/strategy}/index.js +0 -0
  136. /package/{strategy → cjs/strategy}/inject.d.ts +0 -0
  137. /package/{strategy → cjs/strategy}/inject.js +0 -0
  138. /package/{strategy → cjs/strategy}/namedAttribute.d.ts +0 -0
  139. /package/{strategy → cjs/strategy}/namedAttribute.js +0 -0
  140. /package/{strategy → cjs/strategy}/strategy.d.ts +0 -0
  141. /package/{strategy → cjs/strategy}/strategy.js +0 -0
  142. /package/{strategy → cjs/strategy}/stratgies.test.d.ts +0 -0
  143. /package/{strategy → cjs/strategy}/stratgies.test.js +0 -0
  144. /package/{strategy → cjs/strategy}/texture2D.d.ts +0 -0
  145. /package/{strategy → cjs/strategy}/texture2D.js +0 -0
  146. /package/{strategy → cjs/strategy}/uniform.d.ts +0 -0
  147. /package/{strategy → cjs/strategy}/uniform.js +0 -0
  148. /package/{strategy → cjs/strategy}/variable.d.ts +0 -0
  149. /package/{strategy → cjs/strategy}/variable.js +0 -0
  150. /package/{util → cjs/util}/ast.d.ts +0 -0
  151. /package/{util → cjs/util}/ast.js +0 -0
  152. /package/{util → cjs/util}/ensure.d.ts +0 -0
  153. /package/{util → cjs/util}/ensure.js +0 -0
  154. /package/{util → cjs/util}/id.d.ts +0 -0
  155. /package/{util → cjs/util}/id.js +0 -0
@@ -0,0 +1,28 @@
1
+ var _a;
2
+ import { EngineNodeType } from '../../engine';
3
+ var nodeInputMap = {
4
+ normalMap: 'bumpSampler',
5
+ property_map: 'property_albedoTexture',
6
+ property_normalMap: 'property_bumpTexture',
7
+ property_color: 'property_albedoColor',
8
+ property_metalness: 'property_metallic',
9
+ filler_position: 'filler_position',
10
+ };
11
+ var importers = {
12
+ three: {
13
+ convertAst: function (ast, type) {
14
+ throw new Error('Not implemented');
15
+ },
16
+ nodeInputMap: (_a = {},
17
+ _a[EngineNodeType.physical] = nodeInputMap,
18
+ _a),
19
+ edgeMap: {
20
+ normalMap: 'bumpSampler',
21
+ property_map: 'property_albedoTexture',
22
+ property_normalMap: 'property_bumpTexture',
23
+ property_color: 'property_albedoColor',
24
+ property_metalness: 'property_metallic',
25
+ },
26
+ },
27
+ };
28
+ export default importers;
@@ -0,0 +1,2 @@
1
+ import { playengine } from './playengine';
2
+ export { playengine as engine };
@@ -0,0 +1,2 @@
1
+ import { playengine } from './playengine';
2
+ export { playengine as engine };
@@ -0,0 +1,39 @@
1
+ import * as pc from 'playcanvas';
2
+ import { Engine } from '../../engine';
3
+ import { ShaderStage } from '../../graph/graph-types';
4
+ import { CodeNode, NodeProperty } from '../../graph/code-nodes';
5
+ import { NodePosition } from '../../graph/base-node';
6
+ import { UniformDataType } from '../../graph/data-nodes';
7
+ export declare const physicalDefaultProperties: {
8
+ blendType: number;
9
+ opacity: number;
10
+ useMetalness: boolean;
11
+ cull: number;
12
+ };
13
+ /**
14
+ * For the use of "0.5", apparently PlayCanvas optimizes uniform
15
+ * generation where if you set diffuse to white (1,1,1) then it doesn't
16
+ * add the diffuse uniform, because that's the default state.
17
+ */
18
+ export declare const defaultPropertySetting: (app: pc.Application, property: NodeProperty) => 0.5 | pc.Texture | pc.Color;
19
+ export declare const physicalNode: (id: string, name: string, position: NodePosition, uniforms: UniformDataType[], stage: ShaderStage | undefined) => CodeNode;
20
+ export type RuntimeContext = {
21
+ scene: pc.Scene;
22
+ camera: pc.Camera;
23
+ pc: any;
24
+ sceneData: any;
25
+ cache: {
26
+ data: {
27
+ [key: string]: any;
28
+ };
29
+ nodes: {
30
+ [id: string]: {
31
+ fragment: string;
32
+ vertex: string;
33
+ };
34
+ };
35
+ };
36
+ };
37
+ export declare const toonNode: (id: string, name: string, position: NodePosition, uniforms: UniformDataType[], stage: ShaderStage | undefined) => CodeNode;
38
+ export declare let mIdx: number;
39
+ export declare const playengine: Engine;
@@ -0,0 +1,517 @@
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/graph';
77
+ import { NodeType } from '../../graph/graph-types';
78
+ import importers from './importers';
79
+ import { returnGlPositionHardCoded, returnGlPosition, returnGlPositionVec3Right, } from '../../util/ast';
80
+ import { property, } from '../../graph/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, position, uniforms, stage) {
164
+ return prepopulatePropertyInputs({
165
+ id: id,
166
+ name: 'StandardMaterial',
167
+ position: position,
168
+ engine: true,
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('Glossiness', '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
+ dataType: 'vector4',
220
+ category: 'data',
221
+ id: '1',
222
+ },
223
+ ],
224
+ source: '',
225
+ stage: stage,
226
+ });
227
+ };
228
+ export var toonNode = function (id, name, position, uniforms, stage) {
229
+ return prepopulatePropertyInputs({
230
+ id: id,
231
+ name: name,
232
+ position: position,
233
+ engine: true,
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
+ dataType: 'vector4',
261
+ category: 'data',
262
+ id: '1',
263
+ },
264
+ ],
265
+ source: '',
266
+ stage: stage,
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
+ .filter(function (n) { return !!n; })
293
+ .sort(function (a, b) { return a.id.localeCompare(b.id); })
294
+ .map(function (n) { return nodeCacheKey(graph, n); })
295
+ .join('-') +
296
+ '|Lights:' +
297
+ lights.join(',') +
298
+ '|Envtex:' +
299
+ [app.scene.envAtlas, app.scene.prefilteredCubemaps].join(','));
300
+ };
301
+ var cacher = function (engineContext, graph, node, sibling, newValue) { return __awaiter(void 0, void 0, void 0, function () {
302
+ var cacheKey, materialData;
303
+ return __generator(this, function (_a) {
304
+ switch (_a.label) {
305
+ case 0:
306
+ cacheKey = programCacheKey(engineContext, graph, node, sibling);
307
+ if (engineContext.runtime.cache.data[cacheKey]) {
308
+ log("Cache hit \"".concat(cacheKey, "\""));
309
+ }
310
+ else {
311
+ log("Cache miss \"".concat(cacheKey, "\""));
312
+ }
313
+ return [4 /*yield*/, (engineContext.runtime.cache.data[cacheKey] ||
314
+ newValue())];
315
+ case 1:
316
+ materialData = _a.sent();
317
+ log("Material cache \"".concat(cacheKey, "\" is now"), materialData);
318
+ engineContext.runtime.cache.data[cacheKey] = materialData;
319
+ engineContext.runtime.engineMaterial = materialData.material;
320
+ // TODO: We mutate the nodes here, can we avoid that later?
321
+ node.source =
322
+ node.stage === 'fragment' ? materialData.fragment : materialData.vertex;
323
+ if (sibling) {
324
+ sibling.source =
325
+ sibling.stage === 'fragment'
326
+ ? materialData.fragment
327
+ : materialData.vertex;
328
+ }
329
+ return [2 /*return*/];
330
+ }
331
+ });
332
+ }); };
333
+ var onBeforeCompileMegaShader = function (engineContext, graph, node, sibling) { return __awaiter(void 0, void 0, void 0, function () {
334
+ var _a, appUn, sceneData, app, pbrName, shaderMaterial, newProperties, applied, vertexSource, fragmentSource, origMat;
335
+ var _b;
336
+ return __generator(this, function (_c) {
337
+ _a = engineContext.runtime, appUn = _a.app, sceneData = _a.sceneData;
338
+ app = appUn;
339
+ pbrName = "engine_pbr".concat(id());
340
+ shaderMaterial = new pc.StandardMaterial();
341
+ shaderMaterial.name = pbrName;
342
+ newProperties = __assign({}, (node.config.hardCodedProperties ||
343
+ ((_b = sibling === null || sibling === void 0 ? void 0 : sibling.config) === null || _b === void 0 ? void 0 : _b.hardCodedProperties) ||
344
+ {}));
345
+ Object.assign(shaderMaterial, newProperties);
346
+ applied = applyPlayMaterialProperties(engineContext, shaderMaterial, app, graph, node, sibling);
347
+ log('Engine megashader initial properties', __assign(__assign({}, newProperties), applied));
348
+ // This is a hack to force the material to regenerate. The chunks are used
349
+ // internally in playcanvas to generate the material cache key. If you use
350
+ // a real chunk, it messes up the GLSL. So this introduces a fake chunk that
351
+ // isn't used in the GLSL, but forces a new material to generate. The code
352
+ // path I'm hacking through is:
353
+ // render() -> renderForward() -> updatePassShader() -> getShaderVariant() ->
354
+ // library.getProgram() -> generateShaderDefinition()
355
+ // TODO: Try using the new hook https://github.com/playcanvas/engine/pull/5524
356
+ shaderMaterial.chunks.engineHackSource = "".concat(Math.random());
357
+ // I had this here before for testing, I commented it out trying to figure out
358
+ // the opacity issue while working on Playcanvas. Can remove if I ever figure
359
+ // out the opacity issue
360
+ // shaderMaterial.clearVariants();
361
+ shaderMaterial.update();
362
+ origMat = sceneData.mesh.render.meshInstances[0].material;
363
+ sceneData.mesh.render.meshInstances[0].material = shaderMaterial;
364
+ // Force shader compilation
365
+ app.render();
366
+ sceneData.mesh.render.meshInstances[0].material = origMat;
367
+ return [2 /*return*/, new Promise(function (resolve) {
368
+ var variants = shaderMaterial.variants;
369
+ if (variants.size === 1) {
370
+ var _a = __read(variants.entries().next().value, 2), untypedVariant = _a[1];
371
+ var variant = untypedVariant;
372
+ var _b = variant.definition, fshader = _b.fshader, vshader = _b.vshader;
373
+ fragmentSource = fshader;
374
+ vertexSource = vshader;
375
+ engineContext.runtime.cache.nodes[node.id] = {
376
+ fragment: fshader,
377
+ vertex: vshader,
378
+ };
379
+ }
380
+ else {
381
+ console.error('Bad variants!', variants);
382
+ }
383
+ resolve({
384
+ material: shaderMaterial,
385
+ fragment: fragmentSource,
386
+ vertex: vertexSource,
387
+ });
388
+ })];
389
+ });
390
+ }); };
391
+ // TODO: NEED TO DO SAME THREE MANGLIGN STEP HERE
392
+ var megaShaderMainpulateAst = function (engineContext, engine, graph, ast, inputEdges, node, sibling) {
393
+ var programAst = ast;
394
+ var mainName = 'main' || nodeName(node);
395
+ if (node.stage === 'vertex') {
396
+ if (doesLinkThruShader(graph, node)) {
397
+ returnGlPositionHardCoded(mainName, programAst, 'vec3', 'transformed');
398
+ }
399
+ else {
400
+ returnGlPosition(mainName, programAst);
401
+ }
402
+ }
403
+ // We specify engine nodes are mangle: false, which is the graph step that
404
+ // handles renaming the main fn, so we have to do it ourselves
405
+ mangleMainFn(programAst, node, sibling);
406
+ return programAst;
407
+ };
408
+ var evaluateNode = function (node) {
409
+ if (node.type === 'number') {
410
+ return parseFloat(node.value);
411
+ // Apparently PlayCanvas can't use new pc.Vec2(...), etc for vector uniforms,
412
+ // and instead requires arrays? But can (maybe) use a pc.Color() for other
413
+ // uniforms? I need to test the color assumption, but it's weird PC doesn't
414
+ // use its own types for uniforms
415
+ }
416
+ else if (node.type === 'texture') {
417
+ return node;
418
+ }
419
+ else if (node.type === 'vector2') {
420
+ return [parseFloat(node.value[0]), parseFloat(node.value[1])];
421
+ }
422
+ else if (node.type === 'vector3') {
423
+ return [
424
+ parseFloat(node.value[0]),
425
+ parseFloat(node.value[1]),
426
+ parseFloat(node.value[2]),
427
+ ];
428
+ }
429
+ else if (node.type === 'vector4') {
430
+ return [
431
+ parseFloat(node.value[0]),
432
+ parseFloat(node.value[1]),
433
+ parseFloat(node.value[2]),
434
+ parseFloat(node.value[3]),
435
+ ];
436
+ }
437
+ else if (node.type === 'rgb') {
438
+ return [
439
+ parseFloat(node.value[0]),
440
+ parseFloat(node.value[1]),
441
+ parseFloat(node.value[2]),
442
+ ];
443
+ }
444
+ else if (node.type === 'rgba') {
445
+ return [
446
+ parseFloat(node.value[0]),
447
+ parseFloat(node.value[1]),
448
+ parseFloat(node.value[2]),
449
+ parseFloat(node.value[3]),
450
+ ];
451
+ }
452
+ else {
453
+ return node.value;
454
+ }
455
+ };
456
+ export var playengine = {
457
+ name: 'playcanvas',
458
+ displayName: 'PlayCanvas',
459
+ importers: importers,
460
+ mergeOptions: {
461
+ includePrecisions: true,
462
+ includeVersion: false,
463
+ },
464
+ evaluateNode: evaluateNode,
465
+ constructors: (_a = {},
466
+ _a[EngineNodeType.physical] = physicalNode,
467
+ _a[EngineNodeType.toon] = toonNode,
468
+ _a),
469
+ // TODO: Move into core based on engine shader scrape
470
+ preserve: new Set([
471
+ 'matrix_model',
472
+ 'matrix_normal',
473
+ 'matrix_view',
474
+ 'matrix_viewProjection',
475
+ 'time',
476
+ 'vertex_color',
477
+ 'vertex_normal',
478
+ 'vertex_position',
479
+ 'vertex_tangent',
480
+ 'vertex_tangent',
481
+ 'vertex_texCoord0',
482
+ 'vertex_texCoord1',
483
+ 'vNormalW',
484
+ 'vPositionW',
485
+ 'vUv0',
486
+ ]),
487
+ parsers: (_b = {},
488
+ _b[NodeType.SOURCE] = {
489
+ manipulateAst: function (engineContext, engine, graph, ast, inputEdges, node, sibling) {
490
+ var programAst = ast;
491
+ var mainName = 'main' || nodeName(node);
492
+ // This hinges on the vertex shader calling vec3(p)
493
+ if (node.stage === 'vertex') {
494
+ if (doesLinkThruShader(graph, node)) {
495
+ returnGlPositionVec3Right(mainName, programAst);
496
+ }
497
+ else {
498
+ returnGlPosition(mainName, programAst);
499
+ }
500
+ }
501
+ return ast;
502
+ },
503
+ },
504
+ _b[EngineNodeType.physical] = {
505
+ onBeforeCompile: function (graph, engineContext, node, sibling) {
506
+ return cacher(engineContext, graph, node, sibling, function () {
507
+ return onBeforeCompileMegaShader(engineContext, graph, node, sibling);
508
+ });
509
+ },
510
+ manipulateAst: megaShaderMainpulateAst,
511
+ },
512
+ _b),
513
+ };
514
+ playengine.parsers[EngineNodeType.toon] =
515
+ playengine.parsers[EngineNodeType.physical];
516
+ playengine.parsers[EngineNodeType.phong] =
517
+ playengine.parsers[EngineNodeType.physical];
@@ -0,0 +1,3 @@
1
+ import { EngineImporters } from '../../engine';
2
+ declare const importers: EngineImporters;
3
+ export default importers;
@@ -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;
@@ -0,0 +1,2 @@
1
+ import { createMaterial, threngine } from './threngine';
2
+ export { createMaterial, threngine as engine };
@@ -0,0 +1,2 @@
1
+ import { createMaterial, threngine } from './threngine';
2
+ export { createMaterial, threngine as engine };
@@ -0,0 +1,34 @@
1
+ import { RawShaderMaterial, Vector4, Color, Texture } from 'three';
2
+ import { ShaderStage } from '../../graph/graph-types';
3
+ import { Engine, EngineContext } from '../../engine';
4
+ import { CompileResult } from '../../graph/graph';
5
+ import { CodeNode, NodeProperty } from '../../graph/code-nodes';
6
+ import { NodePosition } from '../../graph/base-node';
7
+ import { UniformDataType } from '../../graph/data-nodes';
8
+ export declare const phongNode: (id: string, name: string, position: NodePosition, uniforms: UniformDataType[], stage: ShaderStage | undefined) => CodeNode;
9
+ export declare const physicalNode: (id: string, name: string, position: NodePosition, uniforms: UniformDataType[], stage: ShaderStage | undefined) => CodeNode;
10
+ export declare const defaultPropertySetting: (property: NodeProperty) => 0.5 | Texture | Color | Vector4;
11
+ export type ThreeRuntime = {
12
+ scene: any;
13
+ camera: any;
14
+ renderer: any;
15
+ sceneData: any;
16
+ engineMaterial: any;
17
+ index: number;
18
+ cache: {
19
+ data: {
20
+ [key: string]: any;
21
+ };
22
+ nodes: {
23
+ [id: string]: {
24
+ fragmentRef: any;
25
+ vertexRef: any;
26
+ fragment: string;
27
+ vertex: string;
28
+ };
29
+ };
30
+ };
31
+ };
32
+ export declare const toonNode: (id: string, name: string, position: NodePosition, uniforms: UniformDataType[], stage: ShaderStage | undefined) => CodeNode;
33
+ export declare const threngine: Engine;
34
+ export declare const createMaterial: (compileResult: CompileResult, ctx: EngineContext) => RawShaderMaterial;