@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.
- package/README.md +184 -1
- package/dist/ast/manipulate.js +328 -0
- package/dist/ast/shader-sections.js +256 -0
- package/dist/context.js +230 -0
- package/dist/engine.js +209 -0
- package/dist/evaluate.js +27 -0
- package/dist/graph-types.js +7 -0
- package/dist/graph.js +381 -0
- package/dist/graph.test.js +168 -0
- package/dist/nodes/code-nodes.js +18 -0
- package/dist/nodes/core-node.js +9 -0
- package/dist/nodes/data-nodes.js +123 -0
- package/dist/nodes/edge.js +1 -0
- package/dist/nodes/engine-node.js +189 -0
- package/dist/parsers.js +213 -0
- package/dist/plugins/babylon/bablyengine.js +582 -0
- package/dist/plugins/babylon/importers.js +64 -0
- package/{src/plugins/babylon/index.ts → dist/plugins/babylon/index.js} +0 -1
- package/dist/plugins/playcanvas/importers.js +28 -0
- package/dist/plugins/playcanvas/index.js +2 -0
- package/dist/plugins/playcanvas/playengine.js +510 -0
- package/dist/plugins/three/importers.js +15 -0
- package/{src/plugins/three/index.ts → dist/plugins/three/index.js} +0 -1
- package/dist/plugins/three/threngine.js +495 -0
- package/dist/strategy/assignemntTo.js +26 -0
- package/dist/strategy/declarationOf.js +23 -0
- package/dist/strategy/hardCode.js +23 -0
- package/dist/strategy/index.js +38 -0
- package/dist/strategy/inject.js +122 -0
- package/dist/strategy/namedAttribute.js +48 -0
- package/dist/strategy/texture2D.js +83 -0
- package/dist/strategy/uniform.js +190 -0
- package/dist/strategy/variable.js +80 -0
- package/dist/stratgies.test.js +164 -0
- package/dist/util/ast.js +9 -0
- package/dist/util/ensure.js +7 -0
- package/dist/util/id.js +2 -0
- package/package.json +12 -4
- package/src/ast/manipulate.ts +0 -392
- package/src/ast/shader-sections.ts +0 -323
- package/src/core/engine.ts +0 -214
- package/src/core/file.js +0 -53
- package/src/core/graph.ts +0 -1007
- package/src/core/nodes/code-nodes.ts +0 -66
- package/src/core/nodes/core-node.ts +0 -48
- package/src/core/nodes/data-nodes.ts +0 -344
- package/src/core/nodes/edge.ts +0 -23
- package/src/core/nodes/engine-node.ts +0 -266
- package/src/core/strategy.ts +0 -520
- package/src/core.test.ts +0 -312
- package/src/plugins/babylon/bablyengine.ts +0 -670
- package/src/plugins/babylon/importers.ts +0 -69
- package/src/plugins/three/importers.ts +0 -18
- package/src/plugins/three/threngine.tsx +0 -571
- package/src/util/ensure.ts +0 -10
- package/src/util/id.ts +0 -2
|
@@ -0,0 +1,495 @@
|
|
|
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 { Vector2, Vector3, Vector4, Color } from 'three';
|
|
75
|
+
import { NodeType } from '../../graph-types';
|
|
76
|
+
import { prepopulatePropertyInputs, mangleMainFn } from '../../graph';
|
|
77
|
+
import importers from './importers';
|
|
78
|
+
import { EngineNodeType } from '../../engine';
|
|
79
|
+
import { doesLinkThruShader, nodeName } from '../../graph';
|
|
80
|
+
import { returnGlPosition, returnGlPositionHardCoded, returnGlPositionVec3Right, } from '../../ast/manipulate';
|
|
81
|
+
import { property, } from '../../nodes/code-nodes';
|
|
82
|
+
import { namedAttributeStrategy, texture2DStrategy, uniformStrategy, } from '../../strategy';
|
|
83
|
+
var log = function () {
|
|
84
|
+
var _a;
|
|
85
|
+
var args = [];
|
|
86
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
87
|
+
args[_i] = arguments[_i];
|
|
88
|
+
}
|
|
89
|
+
return (_a = console.log).call.apply(_a, __spreadArray([console, '\x1b[35m(three)\x1b[0m'], __read(args), false));
|
|
90
|
+
};
|
|
91
|
+
export var physicalNode = function (id, name, groupId, position, uniforms, stage, nextStageNodeId) {
|
|
92
|
+
return prepopulatePropertyInputs({
|
|
93
|
+
id: id,
|
|
94
|
+
name: name,
|
|
95
|
+
groupId: groupId,
|
|
96
|
+
position: position,
|
|
97
|
+
type: EngineNodeType.physical,
|
|
98
|
+
config: {
|
|
99
|
+
uniforms: uniforms,
|
|
100
|
+
version: 3,
|
|
101
|
+
mangle: false,
|
|
102
|
+
preprocess: true,
|
|
103
|
+
properties: [
|
|
104
|
+
property('Color', 'color', 'rgb', 'uniform_diffuse'),
|
|
105
|
+
property('Texture', 'map', 'texture', 'filler_map'),
|
|
106
|
+
property('Normal Map', 'normalMap', 'texture', 'filler_normalMap'),
|
|
107
|
+
property('Normal Scale', 'normalScale', 'vector2'),
|
|
108
|
+
property('Metalness', 'metalness', 'number', 'uniform_metalness'),
|
|
109
|
+
property('Roughness', 'roughness', 'number', 'uniform_roughness'),
|
|
110
|
+
property('Roughness Map', 'roughnessMap', 'texture', 'filler_roughnessMap'),
|
|
111
|
+
property('Displacement Map', 'displacementMap', 'texture'),
|
|
112
|
+
// MeshPhysicalMaterial gets envMap from the scene. MeshStandardMaterial
|
|
113
|
+
// gets it from the material
|
|
114
|
+
// property('Env Map', 'envMap', 'samplerCube'),
|
|
115
|
+
property('Transmission', 'transmission', 'number'),
|
|
116
|
+
property('Transmission Map', 'transmissionMap', 'texture', 'filler_transmissionMap'),
|
|
117
|
+
property('Thickness', 'thickness', 'number'),
|
|
118
|
+
property('Index of Refraction', 'ior', 'number'),
|
|
119
|
+
property('Sheen', 'sheen', 'number'),
|
|
120
|
+
property('Reflectivity', 'reflectivity', 'number'),
|
|
121
|
+
property('Clearcoat', 'clearcoat', 'number'),
|
|
122
|
+
property('Iridescence', 'iridescence', 'number'),
|
|
123
|
+
property('Iridescence IOR', 'iridescenceIOR', 'number'),
|
|
124
|
+
property('Iridescence Thickness Range', 'iridescenceThicknessRange', 'array', undefined, ['100', '400']),
|
|
125
|
+
],
|
|
126
|
+
hardCodedProperties: {
|
|
127
|
+
isMeshPhysicalMaterial: true,
|
|
128
|
+
isMeshStandardMaterial: true,
|
|
129
|
+
},
|
|
130
|
+
strategies: [
|
|
131
|
+
uniformStrategy(),
|
|
132
|
+
stage === 'fragment'
|
|
133
|
+
? texture2DStrategy()
|
|
134
|
+
: namedAttributeStrategy('position'),
|
|
135
|
+
],
|
|
136
|
+
},
|
|
137
|
+
inputs: [],
|
|
138
|
+
outputs: [
|
|
139
|
+
{
|
|
140
|
+
name: 'vector4',
|
|
141
|
+
category: 'data',
|
|
142
|
+
id: '1',
|
|
143
|
+
},
|
|
144
|
+
],
|
|
145
|
+
source: '',
|
|
146
|
+
stage: stage,
|
|
147
|
+
nextStageNodeId: nextStageNodeId,
|
|
148
|
+
});
|
|
149
|
+
};
|
|
150
|
+
var cacher = function (engineContext, graph, node, sibling, newValue) {
|
|
151
|
+
var cacheKey = programCacheKey(engineContext, graph, node, sibling);
|
|
152
|
+
if (engineContext.runtime.cache.data[cacheKey]) {
|
|
153
|
+
log('Cache hit', cacheKey);
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
log('Cache miss', cacheKey);
|
|
157
|
+
}
|
|
158
|
+
var materialData = engineContext.runtime.cache.data[cacheKey] || newValue();
|
|
159
|
+
engineContext.runtime.cache.data[cacheKey] = materialData;
|
|
160
|
+
engineContext.runtime.engineMaterial = materialData.material;
|
|
161
|
+
// TODO: We mutate the nodes here, can we avoid that later?
|
|
162
|
+
node.source =
|
|
163
|
+
node.stage === 'fragment' ? materialData.fragment : materialData.vertex;
|
|
164
|
+
sibling.source =
|
|
165
|
+
sibling.stage === 'fragment' ? materialData.fragment : materialData.vertex;
|
|
166
|
+
};
|
|
167
|
+
var onBeforeCompileMegaShader = function (engineContext, newMat) {
|
|
168
|
+
log('compiling three megashader!');
|
|
169
|
+
var _a = engineContext.runtime, renderer = _a.renderer, sceneData = _a.sceneData, scene = _a.scene, camera = _a.camera;
|
|
170
|
+
var mesh = sceneData.mesh;
|
|
171
|
+
// Temporarily swap the mesh material to the new one, since materials can
|
|
172
|
+
// be mesh specific, render, then get its source code
|
|
173
|
+
var originalMaterial = mesh.material;
|
|
174
|
+
mesh.material = newMat;
|
|
175
|
+
renderer.compile(scene, camera);
|
|
176
|
+
// The references to the compiled shaders in WebGL
|
|
177
|
+
var fragmentRef = renderer.properties
|
|
178
|
+
.get(mesh.material)
|
|
179
|
+
.programs.values()
|
|
180
|
+
.next().value.fragmentShader;
|
|
181
|
+
var vertexRef = renderer.properties
|
|
182
|
+
.get(mesh.material)
|
|
183
|
+
.programs.values()
|
|
184
|
+
.next().value.vertexShader;
|
|
185
|
+
var gl = renderer.getContext();
|
|
186
|
+
var fragment = gl.getShaderSource(fragmentRef);
|
|
187
|
+
var vertex = gl.getShaderSource(vertexRef);
|
|
188
|
+
// Reset the material on the mesh, since the shader we're computing context
|
|
189
|
+
// for might not be the one actually want on the mesh - like if a toon node
|
|
190
|
+
// was added to the graph but not connected
|
|
191
|
+
mesh.material = originalMaterial;
|
|
192
|
+
// Do we even need to do this? This is just for debugging right? Using the
|
|
193
|
+
// source on the node is the important thing.
|
|
194
|
+
return {
|
|
195
|
+
material: newMat,
|
|
196
|
+
fragmentRef: fragmentRef,
|
|
197
|
+
vertexRef: vertexRef,
|
|
198
|
+
fragment: fragment,
|
|
199
|
+
vertex: vertex,
|
|
200
|
+
};
|
|
201
|
+
};
|
|
202
|
+
var megaShaderMainpulateAst = function (engineContext, engine, graph, node, ast, inputEdges) {
|
|
203
|
+
var programAst = ast;
|
|
204
|
+
var mainName = 'main' || nodeName(node);
|
|
205
|
+
if (node.stage === 'vertex') {
|
|
206
|
+
if (doesLinkThruShader(graph, node)) {
|
|
207
|
+
returnGlPositionHardCoded(mainName, programAst, 'vec3', 'transformed');
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
returnGlPosition(mainName, programAst);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
// We specify engine nodes are mangle: false, which is the graph step that
|
|
214
|
+
// handles renaming the main fn, so we have to do it ourselves
|
|
215
|
+
mangleMainFn(programAst, node);
|
|
216
|
+
return programAst;
|
|
217
|
+
};
|
|
218
|
+
var nodeCacheKey = function (graph, node) {
|
|
219
|
+
return ('[ID:' +
|
|
220
|
+
node.id +
|
|
221
|
+
'Edges:' +
|
|
222
|
+
graph.edges
|
|
223
|
+
.filter(function (edge) { return edge.to === node.id; })
|
|
224
|
+
.map(function (edge) { return "(".concat(edge.to, "->").concat(edge.input, ")"); })
|
|
225
|
+
.sort()
|
|
226
|
+
.join(',') +
|
|
227
|
+
']'
|
|
228
|
+
// Currently excluding node inputs because these are calculated *after*
|
|
229
|
+
// the onbeforecompile, so the next compile, they'll all change!
|
|
230
|
+
// node.inputs.map((i) => `${i.id}${i.bakeable}`)
|
|
231
|
+
);
|
|
232
|
+
};
|
|
233
|
+
var programCacheKey = function (engineContext, graph, node, sibling) {
|
|
234
|
+
// The megashader source is dependent on scene information, like the number
|
|
235
|
+
// and type of lights in the scene. This kinda sucks - it's duplicating
|
|
236
|
+
// three's material cache key, and is coupled to how three builds shaders
|
|
237
|
+
var _a = engineContext.runtime, three = _a.three, scene = _a.scene;
|
|
238
|
+
var lights = [];
|
|
239
|
+
scene.traverse(function (obj) {
|
|
240
|
+
if (obj instanceof three.Light) {
|
|
241
|
+
lights.push(obj.type);
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
return ([node, sibling]
|
|
245
|
+
.sort(function (a, b) { return a.id.localeCompare(b.id); })
|
|
246
|
+
.map(function (n) { return nodeCacheKey(graph, n); })
|
|
247
|
+
.join('-') +
|
|
248
|
+
'|Lights:' +
|
|
249
|
+
lights.join(',') +
|
|
250
|
+
'|Envtex:' +
|
|
251
|
+
scene.environmentTexture);
|
|
252
|
+
};
|
|
253
|
+
export var defaultPropertySetting = function (three, property) {
|
|
254
|
+
if (property.type === 'texture') {
|
|
255
|
+
return new three.Texture();
|
|
256
|
+
}
|
|
257
|
+
else if (property.type === 'number') {
|
|
258
|
+
return 0.5;
|
|
259
|
+
}
|
|
260
|
+
else if (property.type === 'rgb') {
|
|
261
|
+
return new three.Color(1, 1, 1);
|
|
262
|
+
}
|
|
263
|
+
else if (property.type === 'rgba') {
|
|
264
|
+
return new three.Color(1, 1, 1, 1);
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
var threeMaterialProperties = function (three, graph, node, sibling) {
|
|
268
|
+
// Find inputs to this node that are dependent on a property of the material
|
|
269
|
+
var propertyInputs = node.inputs
|
|
270
|
+
.filter(function (i) { return i.property; })
|
|
271
|
+
.reduce(function (acc, input) {
|
|
272
|
+
var _a;
|
|
273
|
+
return (__assign(__assign({}, acc), (_a = {}, _a[input.id] = input, _a)));
|
|
274
|
+
}, {});
|
|
275
|
+
// Then look for any edges into those inputs and set the material property
|
|
276
|
+
return graph.edges
|
|
277
|
+
.filter(function (edge) { return edge.to === node.id || edge.to === (sibling === null || sibling === void 0 ? void 0 : sibling.id); })
|
|
278
|
+
.reduce(function (acc, edge) {
|
|
279
|
+
// Check if we've plugged into an input for a property
|
|
280
|
+
var propertyInput = propertyInputs[edge.input];
|
|
281
|
+
if (propertyInput) {
|
|
282
|
+
// Find the property itself
|
|
283
|
+
var property_1 = (node.config.properties || []).find(function (p) { return p.property === propertyInput.property; });
|
|
284
|
+
// Initialize the property on the material
|
|
285
|
+
acc[property_1.property] = defaultPropertySetting(three, property_1);
|
|
286
|
+
}
|
|
287
|
+
return acc;
|
|
288
|
+
}, {});
|
|
289
|
+
};
|
|
290
|
+
var evaluateNode = function (node) {
|
|
291
|
+
if (node.type === 'number') {
|
|
292
|
+
return parseFloat(node.value);
|
|
293
|
+
}
|
|
294
|
+
if (node.type === 'vector2') {
|
|
295
|
+
return new Vector2(parseFloat(node.value[0]), parseFloat(node.value[1]));
|
|
296
|
+
}
|
|
297
|
+
else if (node.type === 'vector3') {
|
|
298
|
+
return new Vector3(parseFloat(node.value[0]), parseFloat(node.value[1]), parseFloat(node.value[2]));
|
|
299
|
+
}
|
|
300
|
+
else if (node.type === 'vector4') {
|
|
301
|
+
return new Vector4(parseFloat(node.value[0]), parseFloat(node.value[1]), parseFloat(node.value[2]), parseFloat(node.value[3]));
|
|
302
|
+
}
|
|
303
|
+
else if (node.type === 'rgb') {
|
|
304
|
+
return new Color(parseFloat(node.value[0]), parseFloat(node.value[1]), parseFloat(node.value[2]));
|
|
305
|
+
}
|
|
306
|
+
else if (node.type === 'rgba') {
|
|
307
|
+
return new Vector4(parseFloat(node.value[0]), parseFloat(node.value[1]), parseFloat(node.value[2]), parseFloat(node.value[3]));
|
|
308
|
+
}
|
|
309
|
+
else {
|
|
310
|
+
return node.value;
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
export var toonNode = function (id, name, groupId, position, uniforms, stage, nextStageNodeId) {
|
|
314
|
+
return prepopulatePropertyInputs({
|
|
315
|
+
id: id,
|
|
316
|
+
name: name,
|
|
317
|
+
groupId: groupId,
|
|
318
|
+
position: position,
|
|
319
|
+
type: EngineNodeType.toon,
|
|
320
|
+
config: {
|
|
321
|
+
uniforms: uniforms,
|
|
322
|
+
version: 3,
|
|
323
|
+
preprocess: true,
|
|
324
|
+
mangle: false,
|
|
325
|
+
properties: [
|
|
326
|
+
property('Color', 'color', 'rgb', 'uniform_diffuse'),
|
|
327
|
+
property('Texture', 'map', 'texture', 'filler_map'),
|
|
328
|
+
property('Gradient Map', 'gradientMap', 'texture', 'filler_gradientMap'),
|
|
329
|
+
property('Normal Map', 'normalMap', 'texture', 'filler_normalMap'),
|
|
330
|
+
property('Normal Scale', 'normalScale', 'vector2'),
|
|
331
|
+
property('Displacement Map', 'displacementMap', 'texture'),
|
|
332
|
+
property('Env Map', 'envMap', 'samplerCube'),
|
|
333
|
+
],
|
|
334
|
+
strategies: [
|
|
335
|
+
uniformStrategy(),
|
|
336
|
+
stage === 'fragment'
|
|
337
|
+
? texture2DStrategy()
|
|
338
|
+
: namedAttributeStrategy('position'),
|
|
339
|
+
],
|
|
340
|
+
},
|
|
341
|
+
inputs: [],
|
|
342
|
+
outputs: [
|
|
343
|
+
{
|
|
344
|
+
name: 'vector4',
|
|
345
|
+
category: 'data',
|
|
346
|
+
id: '1',
|
|
347
|
+
},
|
|
348
|
+
],
|
|
349
|
+
source: '',
|
|
350
|
+
stage: stage,
|
|
351
|
+
nextStageNodeId: nextStageNodeId,
|
|
352
|
+
});
|
|
353
|
+
};
|
|
354
|
+
export var threngine = {
|
|
355
|
+
name: 'three',
|
|
356
|
+
importers: importers,
|
|
357
|
+
mergeOptions: {
|
|
358
|
+
includePrecisions: true,
|
|
359
|
+
includeVersion: true,
|
|
360
|
+
},
|
|
361
|
+
evaluateNode: evaluateNode,
|
|
362
|
+
constructors: (_a = {},
|
|
363
|
+
_a[EngineNodeType.physical] = physicalNode,
|
|
364
|
+
_a[EngineNodeType.toon] = toonNode,
|
|
365
|
+
_a),
|
|
366
|
+
// TODO: Get from uniform lib?
|
|
367
|
+
preserve: new Set([
|
|
368
|
+
'viewMatrix',
|
|
369
|
+
'modelMatrix',
|
|
370
|
+
'modelViewMatrix',
|
|
371
|
+
'projectionMatrix',
|
|
372
|
+
'normalMatrix',
|
|
373
|
+
'uvTransform',
|
|
374
|
+
// Attributes
|
|
375
|
+
'position',
|
|
376
|
+
'normal',
|
|
377
|
+
'uv',
|
|
378
|
+
'uv2',
|
|
379
|
+
// Varyings
|
|
380
|
+
'vUv',
|
|
381
|
+
'vUv2',
|
|
382
|
+
'vViewPosition',
|
|
383
|
+
'vNormal',
|
|
384
|
+
'vPosition',
|
|
385
|
+
// Uniforms
|
|
386
|
+
'cameraPosition',
|
|
387
|
+
'isOrthographic',
|
|
388
|
+
'diffuse',
|
|
389
|
+
'emissive',
|
|
390
|
+
'specular',
|
|
391
|
+
'shininess',
|
|
392
|
+
'opacity',
|
|
393
|
+
'map',
|
|
394
|
+
'specularTint',
|
|
395
|
+
'time',
|
|
396
|
+
'normalScale',
|
|
397
|
+
'normalMap',
|
|
398
|
+
'envMap',
|
|
399
|
+
'envMapIntensity',
|
|
400
|
+
'flipEnvMap',
|
|
401
|
+
'maxMipLevel',
|
|
402
|
+
'roughnessMap',
|
|
403
|
+
// Uniforms for lighting
|
|
404
|
+
'receiveShadow',
|
|
405
|
+
'ambientLightColor',
|
|
406
|
+
'lightProbe',
|
|
407
|
+
// Light uniform arrays
|
|
408
|
+
'spotLights',
|
|
409
|
+
'pointLights',
|
|
410
|
+
// This isn't three wtf
|
|
411
|
+
'resolution',
|
|
412
|
+
'color',
|
|
413
|
+
'image',
|
|
414
|
+
'gradientMap',
|
|
415
|
+
// TODO: This isn't specific to threejs as an engine, it's specific to the
|
|
416
|
+
// phong shader. If a *shader* node has brightness, it should be unique, not
|
|
417
|
+
// use the threejs one!
|
|
418
|
+
'brightness',
|
|
419
|
+
// TODO: These depend on the shaderlib, this might need to be a runtime
|
|
420
|
+
// concern
|
|
421
|
+
// Metalness
|
|
422
|
+
'roughness',
|
|
423
|
+
'metalness',
|
|
424
|
+
'ior',
|
|
425
|
+
'specularIntensity',
|
|
426
|
+
'clearcoat',
|
|
427
|
+
'clearcoatRoughness',
|
|
428
|
+
'transmission',
|
|
429
|
+
'thickness',
|
|
430
|
+
'attenuationDistance',
|
|
431
|
+
'attenuationTint',
|
|
432
|
+
'transmissionSamplerMap',
|
|
433
|
+
'transmissionSamplerSize',
|
|
434
|
+
'displacementMap',
|
|
435
|
+
'displacementScale',
|
|
436
|
+
'displacementBias',
|
|
437
|
+
]),
|
|
438
|
+
parsers: (_b = {},
|
|
439
|
+
_b[NodeType.SOURCE] = {
|
|
440
|
+
manipulateAst: function (engineContext, engine, graph, node, ast, inputEdges) {
|
|
441
|
+
var programAst = ast;
|
|
442
|
+
var mainName = 'main' || nodeName(node);
|
|
443
|
+
// This hinges on the vertex shader calling vec3(p)
|
|
444
|
+
if (node.stage === 'vertex') {
|
|
445
|
+
if (doesLinkThruShader(graph, node)) {
|
|
446
|
+
returnGlPositionVec3Right(mainName, programAst);
|
|
447
|
+
}
|
|
448
|
+
else {
|
|
449
|
+
returnGlPosition(mainName, programAst);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
return ast;
|
|
453
|
+
},
|
|
454
|
+
},
|
|
455
|
+
_b[EngineNodeType.phong] = {
|
|
456
|
+
onBeforeCompile: function (graph, engineContext, node, sibling) { return __awaiter(void 0, void 0, void 0, function () {
|
|
457
|
+
var three;
|
|
458
|
+
return __generator(this, function (_a) {
|
|
459
|
+
three = engineContext.runtime.three;
|
|
460
|
+
cacher(engineContext, graph, node, sibling, function () {
|
|
461
|
+
return onBeforeCompileMegaShader(engineContext, new three.MeshPhongMaterial(__assign({ isMeshPhongMaterial: true }, threeMaterialProperties(three, graph, node, sibling))));
|
|
462
|
+
});
|
|
463
|
+
return [2 /*return*/];
|
|
464
|
+
});
|
|
465
|
+
}); },
|
|
466
|
+
manipulateAst: megaShaderMainpulateAst,
|
|
467
|
+
},
|
|
468
|
+
_b[EngineNodeType.physical] = {
|
|
469
|
+
onBeforeCompile: function (graph, engineContext, node, sibling) { return __awaiter(void 0, void 0, void 0, function () {
|
|
470
|
+
var three;
|
|
471
|
+
return __generator(this, function (_a) {
|
|
472
|
+
three = engineContext.runtime.three;
|
|
473
|
+
cacher(engineContext, graph, node, sibling, function () {
|
|
474
|
+
return onBeforeCompileMegaShader(engineContext, new three.MeshPhysicalMaterial(__assign(__assign(__assign({}, node.config.hardCodedProperties), threeMaterialProperties(three, graph, node, sibling)), { iridescence: 1.0, iridescenceIOR: 2.0 })));
|
|
475
|
+
});
|
|
476
|
+
return [2 /*return*/];
|
|
477
|
+
});
|
|
478
|
+
}); },
|
|
479
|
+
manipulateAst: megaShaderMainpulateAst,
|
|
480
|
+
},
|
|
481
|
+
_b[EngineNodeType.toon] = {
|
|
482
|
+
onBeforeCompile: function (graph, engineContext, node, sibling) { return __awaiter(void 0, void 0, void 0, function () {
|
|
483
|
+
var three;
|
|
484
|
+
return __generator(this, function (_a) {
|
|
485
|
+
three = engineContext.runtime.three;
|
|
486
|
+
cacher(engineContext, graph, node, sibling, function () {
|
|
487
|
+
return onBeforeCompileMegaShader(engineContext, new three.MeshToonMaterial(__assign({ gradientMap: new three.Texture(), isMeshToonMaterial: true }, threeMaterialProperties(three, graph, node, sibling))));
|
|
488
|
+
});
|
|
489
|
+
return [2 /*return*/];
|
|
490
|
+
});
|
|
491
|
+
}); },
|
|
492
|
+
manipulateAst: megaShaderMainpulateAst,
|
|
493
|
+
},
|
|
494
|
+
_b),
|
|
495
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { findAssignmentTo } from '../ast/manipulate';
|
|
2
|
+
import { nodeInput } from '../nodes/core-node';
|
|
3
|
+
import { StrategyType } from '.';
|
|
4
|
+
// Constructor
|
|
5
|
+
export var assignemntToStrategy = function (assignTo) { return ({
|
|
6
|
+
type: StrategyType.ASSIGNMENT_TO,
|
|
7
|
+
config: { assignTo: assignTo },
|
|
8
|
+
}); };
|
|
9
|
+
// Apply the strategy
|
|
10
|
+
export var applyAssignmentToStrategy = function (node, ast, strategy) {
|
|
11
|
+
var assignNode = findAssignmentTo(ast, strategy.config.assignTo);
|
|
12
|
+
var name = strategy.config.assignTo;
|
|
13
|
+
return assignNode
|
|
14
|
+
? [
|
|
15
|
+
[
|
|
16
|
+
nodeInput(name, "filler_".concat(name), 'filler', undefined, // Data type for what plugs into this filler
|
|
17
|
+
new Set(['code', 'data']), false),
|
|
18
|
+
function (fillerAst) {
|
|
19
|
+
assignNode.expression.right =
|
|
20
|
+
fillerAst;
|
|
21
|
+
return ast;
|
|
22
|
+
},
|
|
23
|
+
],
|
|
24
|
+
]
|
|
25
|
+
: [];
|
|
26
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { findDeclarationOf } from '../ast/manipulate';
|
|
2
|
+
import { nodeInput } from '../nodes/core-node';
|
|
3
|
+
import { StrategyType } from '.';
|
|
4
|
+
export var declarationOfStrategy = function (declarationOf) { return ({
|
|
5
|
+
type: StrategyType.DECLARATION_OF,
|
|
6
|
+
config: { declarationOf: declarationOf },
|
|
7
|
+
}); };
|
|
8
|
+
export var constApplyDeclarationOf = function (node, ast, strategy) {
|
|
9
|
+
var declaration = findDeclarationOf(ast, strategy.config.declarationOf);
|
|
10
|
+
var name = strategy.config.declarationOf;
|
|
11
|
+
return declaration
|
|
12
|
+
? [
|
|
13
|
+
[
|
|
14
|
+
nodeInput(name, "filler_".concat(name), 'filler', undefined, // Data type for what plugs into this filler
|
|
15
|
+
new Set(['code', 'data']), false),
|
|
16
|
+
function (fillerAst) {
|
|
17
|
+
declaration.initializer = fillerAst;
|
|
18
|
+
return ast;
|
|
19
|
+
},
|
|
20
|
+
],
|
|
21
|
+
]
|
|
22
|
+
: [];
|
|
23
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { StrategyType } from '.';
|
|
2
|
+
export var hardCodeStrategy = function (inputs) { return ({
|
|
3
|
+
type: StrategyType.HARD_CODE_INPUTS,
|
|
4
|
+
config: { inputs: inputs },
|
|
5
|
+
}); };
|
|
6
|
+
export var applyHardCodeStrategy = function (graphNode, ast, strategy) {
|
|
7
|
+
return strategy.config.inputs.map(function (input) {
|
|
8
|
+
// Broken out into two lines to enforce type checking on the array.
|
|
9
|
+
var ci = [
|
|
10
|
+
input,
|
|
11
|
+
// Fillers have to return AstNode | Program, and filler is type Filler,
|
|
12
|
+
// so this awkward branch is to make types line up. But I don't think
|
|
13
|
+
// this code path would ever get called
|
|
14
|
+
function (filler) {
|
|
15
|
+
if (!filler || Array.isArray(filler)) {
|
|
16
|
+
throw new Error('Cannot use void filler');
|
|
17
|
+
}
|
|
18
|
+
return filler;
|
|
19
|
+
},
|
|
20
|
+
];
|
|
21
|
+
return ci;
|
|
22
|
+
});
|
|
23
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
var _a;
|
|
2
|
+
import { applyHardCodeStrategy } from './hardCode';
|
|
3
|
+
import { applyUniformStrategy } from './uniform';
|
|
4
|
+
import { applyAssignmentToStrategy, } from './assignemntTo';
|
|
5
|
+
import { constApplyDeclarationOf as constApplyDeclarationOfStrategy, } from './declarationOf';
|
|
6
|
+
import { applyTexture2DStrategy } from './texture2D';
|
|
7
|
+
import { applyNamedAttributeStrategy, } from './namedAttribute';
|
|
8
|
+
import { applyVariableStrategy } from './variable';
|
|
9
|
+
import { applyInjectStrategy } from './inject';
|
|
10
|
+
export * from './hardCode';
|
|
11
|
+
export * from './uniform';
|
|
12
|
+
export * from './assignemntTo';
|
|
13
|
+
export * from './declarationOf';
|
|
14
|
+
export * from './texture2D';
|
|
15
|
+
export * from './namedAttribute';
|
|
16
|
+
export * from './variable';
|
|
17
|
+
export var StrategyType;
|
|
18
|
+
(function (StrategyType) {
|
|
19
|
+
StrategyType["VARIABLE"] = "Variable Names";
|
|
20
|
+
StrategyType["ASSIGNMENT_TO"] = "Assignment To";
|
|
21
|
+
StrategyType["DECLARATION_OF"] = "Variable Declaration";
|
|
22
|
+
StrategyType["TEXTURE_2D"] = "Texture2D";
|
|
23
|
+
StrategyType["NAMED_ATTRIBUTE"] = "Named Attribute";
|
|
24
|
+
StrategyType["UNIFORM"] = "Uniform";
|
|
25
|
+
StrategyType["INJECT"] = "Inject";
|
|
26
|
+
StrategyType["HARD_CODE_INPUTS"] = "Hard Code Inputs";
|
|
27
|
+
})(StrategyType || (StrategyType = {}));
|
|
28
|
+
var strategyRunners = (_a = {},
|
|
29
|
+
_a[StrategyType.INJECT] = applyInjectStrategy,
|
|
30
|
+
_a[StrategyType.HARD_CODE_INPUTS] = applyHardCodeStrategy,
|
|
31
|
+
_a[StrategyType.UNIFORM] = applyUniformStrategy,
|
|
32
|
+
_a[StrategyType.ASSIGNMENT_TO] = applyAssignmentToStrategy,
|
|
33
|
+
_a[StrategyType.DECLARATION_OF] = constApplyDeclarationOfStrategy,
|
|
34
|
+
_a[StrategyType.TEXTURE_2D] = applyTexture2DStrategy,
|
|
35
|
+
_a[StrategyType.NAMED_ATTRIBUTE] = applyNamedAttributeStrategy,
|
|
36
|
+
_a[StrategyType.VARIABLE] = applyVariableStrategy,
|
|
37
|
+
_a);
|
|
38
|
+
export var applyStrategy = function (strategy, node, ast) { return strategyRunners[strategy.type](node, ast, strategy); };
|