@shaderfrog/core 1.3.0 → 1.3.2

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/package.json +10 -7
  2. package/cjs/engine.js +0 -218
  3. package/cjs/graph/base-node.js +0 -13
  4. package/cjs/graph/code-nodes.js +0 -23
  5. package/cjs/graph/context.js +0 -253
  6. package/cjs/graph/data-nodes.js +0 -146
  7. package/cjs/graph/edge.js +0 -12
  8. package/cjs/graph/evaluate.js +0 -32
  9. package/cjs/graph/graph-node.js +0 -143
  10. package/cjs/graph/graph-types.js +0 -25
  11. package/cjs/graph/graph.js +0 -533
  12. package/cjs/graph/graph.test.js +0 -173
  13. package/cjs/graph/index.js +0 -27
  14. package/cjs/graph/parsers.js +0 -219
  15. package/cjs/graph/shader-sections.js +0 -268
  16. package/cjs/index.js +0 -19
  17. package/cjs/package.json +0 -1
  18. package/cjs/plugins/babylon/bablyengine.js +0 -597
  19. package/cjs/plugins/babylon/importers.js +0 -66
  20. package/cjs/plugins/babylon/index.js +0 -5
  21. package/cjs/plugins/playcanvas/importers.js +0 -30
  22. package/cjs/plugins/playcanvas/index.js +0 -5
  23. package/cjs/plugins/playcanvas/playengine.js +0 -549
  24. package/cjs/plugins/three/importers.js +0 -17
  25. package/cjs/plugins/three/index.js +0 -6
  26. package/cjs/plugins/three/threngine.js +0 -619
  27. package/cjs/strategy/assignemntTo.js +0 -31
  28. package/cjs/strategy/declarationOf.js +0 -28
  29. package/cjs/strategy/hardCode.js +0 -28
  30. package/cjs/strategy/index.js +0 -25
  31. package/cjs/strategy/inject.js +0 -127
  32. package/cjs/strategy/namedAttribute.js +0 -53
  33. package/cjs/strategy/strategy.js +0 -35
  34. package/cjs/strategy/stratgies.test.js +0 -192
  35. package/cjs/strategy/texture2D.js +0 -88
  36. package/cjs/strategy/uniform.js +0 -195
  37. package/cjs/strategy/variable.js +0 -85
  38. package/cjs/util/ast.js +0 -351
  39. package/cjs/util/ensure.js +0 -11
  40. package/cjs/util/id.js +0 -6
  41. package/esm/engine.d.ts +0 -69
  42. package/esm/graph/base-node.d.ts +0 -36
  43. package/esm/graph/code-nodes.d.ts +0 -44
  44. package/esm/graph/context.d.ts +0 -37
  45. package/esm/graph/data-nodes.d.ts +0 -83
  46. package/esm/graph/edge.d.ts +0 -13
  47. package/esm/graph/evaluate.d.ts +0 -9
  48. package/esm/graph/graph-node.d.ts +0 -8
  49. package/esm/graph/graph-types.d.ts +0 -38
  50. package/esm/graph/graph.d.ts +0 -89
  51. package/esm/graph/graph.test.d.ts +0 -1
  52. package/esm/graph/index.d.ts +0 -11
  53. package/esm/graph/parsers.d.ts +0 -39
  54. package/esm/graph/shader-sections.d.ts +0 -47
  55. package/esm/index.d.ts +0 -3
  56. package/esm/package.json +0 -1
  57. package/esm/plugins/babylon/bablyengine.d.ts +0 -28
  58. package/esm/plugins/babylon/importers.d.ts +0 -3
  59. package/esm/plugins/babylon/index.d.ts +0 -2
  60. package/esm/plugins/playcanvas/importers.d.ts +0 -3
  61. package/esm/plugins/playcanvas/index.d.ts +0 -2
  62. package/esm/plugins/playcanvas/playengine.d.ts +0 -39
  63. package/esm/plugins/three/importers.d.ts +0 -3
  64. package/esm/plugins/three/index.d.ts +0 -2
  65. package/esm/plugins/three/threngine.d.ts +0 -34
  66. package/esm/strategy/assignemntTo.d.ts +0 -9
  67. package/esm/strategy/declarationOf.d.ts +0 -9
  68. package/esm/strategy/hardCode.d.ts +0 -15
  69. package/esm/strategy/index.d.ts +0 -9
  70. package/esm/strategy/inject.d.ts +0 -15
  71. package/esm/strategy/namedAttribute.d.ts +0 -9
  72. package/esm/strategy/strategy.d.ts +0 -28
  73. package/esm/strategy/stratgies.test.d.ts +0 -1
  74. package/esm/strategy/texture2D.d.ts +0 -6
  75. package/esm/strategy/uniform.d.ts +0 -6
  76. package/esm/strategy/variable.d.ts +0 -6
  77. package/esm/util/ast.d.ts +0 -30
  78. package/esm/util/ensure.d.ts +0 -1
  79. package/esm/util/id.d.ts +0 -1
  80. /package/{cjs/engine.d.ts → engine.d.ts} +0 -0
  81. /package/{esm/engine.js → engine.js} +0 -0
  82. /package/{cjs/graph → graph}/base-node.d.ts +0 -0
  83. /package/{esm/graph → graph}/base-node.js +0 -0
  84. /package/{cjs/graph → graph}/code-nodes.d.ts +0 -0
  85. /package/{esm/graph → graph}/code-nodes.js +0 -0
  86. /package/{cjs/graph → graph}/context.d.ts +0 -0
  87. /package/{esm/graph → graph}/context.js +0 -0
  88. /package/{cjs/graph → graph}/data-nodes.d.ts +0 -0
  89. /package/{esm/graph → graph}/data-nodes.js +0 -0
  90. /package/{cjs/graph → graph}/edge.d.ts +0 -0
  91. /package/{esm/graph → graph}/edge.js +0 -0
  92. /package/{cjs/graph → graph}/evaluate.d.ts +0 -0
  93. /package/{esm/graph → graph}/evaluate.js +0 -0
  94. /package/{cjs/graph → graph}/graph-node.d.ts +0 -0
  95. /package/{esm/graph → graph}/graph-node.js +0 -0
  96. /package/{cjs/graph → graph}/graph-types.d.ts +0 -0
  97. /package/{esm/graph → graph}/graph-types.js +0 -0
  98. /package/{cjs/graph → graph}/graph.d.ts +0 -0
  99. /package/{esm/graph → graph}/graph.js +0 -0
  100. /package/{cjs/graph → graph}/graph.test.d.ts +0 -0
  101. /package/{esm/graph → graph}/graph.test.js +0 -0
  102. /package/{cjs/graph → graph}/index.d.ts +0 -0
  103. /package/{esm/graph → graph}/index.js +0 -0
  104. /package/{cjs/graph → graph}/parsers.d.ts +0 -0
  105. /package/{esm/graph → graph}/parsers.js +0 -0
  106. /package/{cjs/graph → graph}/shader-sections.d.ts +0 -0
  107. /package/{esm/graph → graph}/shader-sections.js +0 -0
  108. /package/{cjs/index.d.ts → index.d.ts} +0 -0
  109. /package/{esm/index.js → index.js} +0 -0
  110. /package/{cjs/plugins → plugins}/babylon/bablyengine.d.ts +0 -0
  111. /package/{esm/plugins → plugins}/babylon/bablyengine.js +0 -0
  112. /package/{cjs/plugins → plugins}/babylon/importers.d.ts +0 -0
  113. /package/{esm/plugins → plugins}/babylon/importers.js +0 -0
  114. /package/{cjs/plugins → plugins}/babylon/index.d.ts +0 -0
  115. /package/{esm/plugins → plugins}/babylon/index.js +0 -0
  116. /package/{cjs/plugins → plugins}/playcanvas/importers.d.ts +0 -0
  117. /package/{esm/plugins → plugins}/playcanvas/importers.js +0 -0
  118. /package/{cjs/plugins → plugins}/playcanvas/index.d.ts +0 -0
  119. /package/{esm/plugins → plugins}/playcanvas/index.js +0 -0
  120. /package/{cjs/plugins → plugins}/playcanvas/playengine.d.ts +0 -0
  121. /package/{esm/plugins → plugins}/playcanvas/playengine.js +0 -0
  122. /package/{cjs/plugins → plugins}/three/importers.d.ts +0 -0
  123. /package/{esm/plugins → plugins}/three/importers.js +0 -0
  124. /package/{cjs/plugins → plugins}/three/index.d.ts +0 -0
  125. /package/{esm/plugins → plugins}/three/index.js +0 -0
  126. /package/{cjs/plugins → plugins}/three/threngine.d.ts +0 -0
  127. /package/{esm/plugins → plugins}/three/threngine.js +0 -0
  128. /package/{cjs/strategy → strategy}/assignemntTo.d.ts +0 -0
  129. /package/{esm/strategy → strategy}/assignemntTo.js +0 -0
  130. /package/{cjs/strategy → strategy}/declarationOf.d.ts +0 -0
  131. /package/{esm/strategy → strategy}/declarationOf.js +0 -0
  132. /package/{cjs/strategy → strategy}/hardCode.d.ts +0 -0
  133. /package/{esm/strategy → strategy}/hardCode.js +0 -0
  134. /package/{cjs/strategy → strategy}/index.d.ts +0 -0
  135. /package/{esm/strategy → strategy}/index.js +0 -0
  136. /package/{cjs/strategy → strategy}/inject.d.ts +0 -0
  137. /package/{esm/strategy → strategy}/inject.js +0 -0
  138. /package/{cjs/strategy → strategy}/namedAttribute.d.ts +0 -0
  139. /package/{esm/strategy → strategy}/namedAttribute.js +0 -0
  140. /package/{cjs/strategy → strategy}/strategy.d.ts +0 -0
  141. /package/{esm/strategy → strategy}/strategy.js +0 -0
  142. /package/{cjs/strategy → strategy}/stratgies.test.d.ts +0 -0
  143. /package/{esm/strategy → strategy}/stratgies.test.js +0 -0
  144. /package/{cjs/strategy → strategy}/texture2D.d.ts +0 -0
  145. /package/{esm/strategy → strategy}/texture2D.js +0 -0
  146. /package/{cjs/strategy → strategy}/uniform.d.ts +0 -0
  147. /package/{esm/strategy → strategy}/uniform.js +0 -0
  148. /package/{cjs/strategy → strategy}/variable.d.ts +0 -0
  149. /package/{esm/strategy → strategy}/variable.js +0 -0
  150. /package/{cjs/util → util}/ast.d.ts +0 -0
  151. /package/{esm/util → util}/ast.js +0 -0
  152. /package/{cjs/util → util}/ensure.d.ts +0 -0
  153. /package/{esm/util → util}/ensure.js +0 -0
  154. /package/{cjs/util → util}/id.d.ts +0 -0
  155. /package/{esm/util → util}/id.js +0 -0
@@ -1,619 +0,0 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
- return new (P || (P = Promise))(function (resolve, reject) {
16
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
- step((generator = generator.apply(thisArg, _arguments || [])).next());
20
- });
21
- };
22
- var __generator = (this && this.__generator) || function (thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
- function verb(n) { return function (v) { return step([n, v]); }; }
26
- function step(op) {
27
- if (f) throw new TypeError("Generator is already executing.");
28
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
- 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;
30
- if (y = 0, t) op = [op[0] & 2, t.value];
31
- switch (op[0]) {
32
- case 0: case 1: t = op; break;
33
- case 4: _.label++; return { value: op[1], done: false };
34
- case 5: _.label++; y = op[1]; op = [0]; continue;
35
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
- default:
37
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
- if (t[2]) _.ops.pop();
42
- _.trys.pop(); continue;
43
- }
44
- op = body.call(thisArg, _);
45
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
- }
48
- };
49
- var __read = (this && this.__read) || function (o, n) {
50
- var m = typeof Symbol === "function" && o[Symbol.iterator];
51
- if (!m) return o;
52
- var i = m.call(o), r, ar = [], e;
53
- try {
54
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
55
- }
56
- catch (error) { e = { error: error }; }
57
- finally {
58
- try {
59
- if (r && !r.done && (m = i["return"])) m.call(i);
60
- }
61
- finally { if (e) throw e.error; }
62
- }
63
- return ar;
64
- };
65
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
66
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
67
- if (ar || !(i in from)) {
68
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
69
- ar[i] = from[i];
70
- }
71
- }
72
- return to.concat(ar || Array.prototype.slice.call(from));
73
- };
74
- var __importDefault = (this && this.__importDefault) || function (mod) {
75
- return (mod && mod.__esModule) ? mod : { "default": mod };
76
- };
77
- var _a, _b;
78
- Object.defineProperty(exports, "__esModule", { value: true });
79
- exports.createMaterial = exports.threngine = exports.toonNode = exports.defaultPropertySetting = exports.physicalNode = exports.phongNode = void 0;
80
- var three_1 = require("three");
81
- var graph_types_1 = require("../../graph/graph-types");
82
- var graph_1 = require("../../graph/graph");
83
- var importers_1 = __importDefault(require("./importers"));
84
- var engine_1 = require("../../engine");
85
- var graph_2 = require("../../graph/graph");
86
- var ast_1 = require("../../util/ast");
87
- var code_nodes_1 = require("../../graph/code-nodes");
88
- var strategy_1 = require("../../strategy");
89
- var log = function () {
90
- var _a;
91
- var args = [];
92
- for (var _i = 0; _i < arguments.length; _i++) {
93
- args[_i] = arguments[_i];
94
- }
95
- return (_a = console.log).call.apply(_a, __spreadArray([console, '\x1b[35m(three)\x1b[0m'], __read(args), false));
96
- };
97
- var phongNode = function (id, name, position, uniforms, stage) {
98
- return (0, graph_1.prepopulatePropertyInputs)({
99
- id: id,
100
- name: 'MeshPhongMaterial',
101
- position: position,
102
- engine: true,
103
- type: engine_1.EngineNodeType.phong,
104
- config: {
105
- version: 3,
106
- uniforms: uniforms,
107
- preprocess: true,
108
- mangle: false,
109
- properties: [
110
- (0, code_nodes_1.property)('Color', 'color', 'rgb', 'uniform_diffuse'),
111
- (0, code_nodes_1.property)('Emissive', 'emissive', 'rgb', 'uniform_emissive'),
112
- (0, code_nodes_1.property)('Emissive Map', 'emissiveMap', 'texture', 'filler_emissiveMap'),
113
- (0, code_nodes_1.property)('Emissive Intensity', 'emissiveIntensity', 'number', 'uniform_emissive'),
114
- (0, code_nodes_1.property)('Texture', 'map', 'texture', 'filler_map'),
115
- (0, code_nodes_1.property)('Normal Map', 'normalMap', 'texture', 'filler_normalMap'),
116
- (0, code_nodes_1.property)('Normal Scale', 'normalScale', 'vector2'),
117
- (0, code_nodes_1.property)('AO Map', 'aoMap', 'texture', 'filler_aoMap'),
118
- (0, code_nodes_1.property)('AO Intensity', 'aoMapIntensity', 'number', 'filler_aoMapIntensity'),
119
- (0, code_nodes_1.property)('Shininess', 'shininess', 'number'),
120
- (0, code_nodes_1.property)('Reflectivity', 'reflectivity', 'number'),
121
- (0, code_nodes_1.property)('Refraction Ratio', 'refractionRatio', 'number'),
122
- (0, code_nodes_1.property)('Specular', 'specular', 'rgb', 'uniform_specular'),
123
- (0, code_nodes_1.property)('Specular Map', 'specularMap', 'texture', 'filler_specularMap'),
124
- (0, code_nodes_1.property)('Displacement Map', 'displacementMap', 'texture', 'filler_displacementMap'),
125
- (0, code_nodes_1.property)('Displacement Scale', 'displacementScale', 'number'),
126
- (0, code_nodes_1.property)('Env Map', 'envMap', 'samplerCube'),
127
- ],
128
- strategies: [
129
- (0, strategy_1.uniformStrategy)(),
130
- stage === 'fragment'
131
- ? (0, strategy_1.texture2DStrategy)()
132
- : (0, strategy_1.namedAttributeStrategy)('position'),
133
- ],
134
- },
135
- inputs: [],
136
- outputs: [
137
- {
138
- name: 'vector4',
139
- dataType: 'vector4',
140
- category: 'data',
141
- id: '1',
142
- },
143
- ],
144
- source: '',
145
- stage: stage,
146
- });
147
- };
148
- exports.phongNode = phongNode;
149
- var physicalNode = function (id, name, position, uniforms, stage) {
150
- return (0, graph_1.prepopulatePropertyInputs)({
151
- id: id,
152
- name: 'MeshPhysicalMaterial',
153
- position: position,
154
- engine: true,
155
- type: engine_1.EngineNodeType.physical,
156
- config: {
157
- uniforms: uniforms,
158
- version: 3,
159
- mangle: false,
160
- preprocess: true,
161
- properties: [
162
- (0, code_nodes_1.property)('Color', 'color', 'rgb', 'uniform_diffuse'),
163
- (0, code_nodes_1.property)('Texture', 'map', 'texture', 'filler_map'),
164
- (0, code_nodes_1.property)('Opacity', 'opacity', 'number'),
165
- (0, code_nodes_1.property)('Normal Map', 'normalMap', 'texture', 'filler_normalMap'),
166
- (0, code_nodes_1.property)('Normal Scale', 'normalScale', 'vector2'),
167
- (0, code_nodes_1.property)('AO Map', 'aoMap', 'texture', 'filler_aoMap'),
168
- (0, code_nodes_1.property)('AO Intensity', 'aoMapIntensity', 'number', 'filler_aoMapIntensity'),
169
- (0, code_nodes_1.property)('Metalness', 'metalness', 'number', 'uniform_metalness'),
170
- (0, code_nodes_1.property)('Roughness', 'roughness', 'number', 'uniform_roughness'),
171
- (0, code_nodes_1.property)('Roughness Map', 'roughnessMap', 'texture', 'filler_roughnessMap'),
172
- (0, code_nodes_1.property)('Displacement Map', 'displacementMap', 'texture', 'filler_displacementMap'),
173
- (0, code_nodes_1.property)('Displacement Scale', 'displacementScale', 'number'),
174
- // MeshPhysicalMaterial gets envMap from the scene. MeshStandardMaterial
175
- // gets it from the material
176
- // property('Env Map', 'envMap', 'samplerCube'),
177
- (0, code_nodes_1.property)('Transmission', 'transmission', 'number'),
178
- (0, code_nodes_1.property)('Transmission Map', 'transmissionMap', 'texture', 'filler_transmissionMap'),
179
- (0, code_nodes_1.property)('Thickness', 'thickness', 'number'),
180
- (0, code_nodes_1.property)('Index of Refraction', 'ior', 'number'),
181
- // Sheen only works with directional lights?
182
- // https://discourse.threejs.org/t/meshphysicalmaterial-s-sheen/31901/6
183
- // property('Sheen', 'sheen', 'number'),
184
- (0, code_nodes_1.property)('Reflectivity', 'reflectivity', 'number'),
185
- (0, code_nodes_1.property)('Clearcoat', 'clearcoat', 'number'),
186
- (0, code_nodes_1.property)('Iridescence', 'iridescence', 'number'),
187
- (0, code_nodes_1.property)('Iridescence IOR', 'iridescenceIOR', 'number'),
188
- (0, code_nodes_1.property)('Iridescence Thickness Range', 'iridescenceThicknessRange', 'array', undefined, ['100', '400']),
189
- ],
190
- hardCodedProperties: {
191
- isMeshPhysicalMaterial: true,
192
- isMeshStandardMaterial: true,
193
- },
194
- strategies: [
195
- (0, strategy_1.uniformStrategy)(),
196
- stage === 'fragment'
197
- ? (0, strategy_1.texture2DStrategy)()
198
- : (0, strategy_1.namedAttributeStrategy)('position'),
199
- ],
200
- },
201
- inputs: [],
202
- outputs: [
203
- {
204
- name: 'vector4',
205
- dataType: 'vector4',
206
- category: 'data',
207
- id: '1',
208
- },
209
- ],
210
- source: '',
211
- stage: stage,
212
- });
213
- };
214
- exports.physicalNode = physicalNode;
215
- var cacher = function (engineContext, graph, node, sibling, newValue) {
216
- var cacheKey = programCacheKey(engineContext, graph, node, sibling);
217
- if (engineContext.runtime.cache.data[cacheKey]) {
218
- log('Cache hit', cacheKey);
219
- }
220
- else {
221
- log('Cache miss', cacheKey);
222
- }
223
- var materialData = engineContext.runtime.cache.data[cacheKey] || newValue();
224
- engineContext.runtime.cache.data[cacheKey] = materialData;
225
- engineContext.runtime.engineMaterial = materialData.material;
226
- // TODO: We mutate the nodes here, can we avoid that later?
227
- node.source =
228
- node.stage === 'fragment' ? materialData.fragment : materialData.vertex;
229
- if (sibling) {
230
- sibling.source =
231
- sibling.stage === 'fragment'
232
- ? materialData.fragment
233
- : materialData.vertex;
234
- }
235
- };
236
- var onBeforeCompileMegaShader = function (engineContext, newMat) {
237
- log('compiling three megashader!');
238
- var _a = engineContext.runtime, renderer = _a.renderer, sceneData = _a.sceneData, scene = _a.scene, camera = _a.camera;
239
- var mesh = sceneData.mesh;
240
- // Temporarily swap the mesh material to the new one, since materials can
241
- // be mesh specific, render, then get its source code
242
- var originalMaterial = mesh.material;
243
- mesh.material = newMat;
244
- renderer.compile(scene, camera);
245
- // The references to the compiled shaders in WebGL
246
- var fragmentRef = renderer.properties
247
- .get(mesh.material)
248
- .programs.values()
249
- .next().value.fragmentShader;
250
- var vertexRef = renderer.properties
251
- .get(mesh.material)
252
- .programs.values()
253
- .next().value.vertexShader;
254
- var gl = renderer.getContext();
255
- var fragment = gl.getShaderSource(fragmentRef);
256
- var vertex = gl.getShaderSource(vertexRef);
257
- // Reset the material on the mesh, since the shader we're computing context
258
- // for might not be the one actually want on the mesh - like if a toon node
259
- // was added to the graph but not connected
260
- mesh.material = originalMaterial;
261
- // Do we even need to do this? This is just for debugging right? Using the
262
- // source on the node is the important thing.
263
- return {
264
- material: newMat,
265
- fragmentRef: fragmentRef,
266
- vertexRef: vertexRef,
267
- fragment: fragment,
268
- vertex: vertex,
269
- };
270
- };
271
- var megaShaderMainpulateAst = function (engineContext, engine, graph, ast, inputEdges, node, sibling) {
272
- var programAst = ast;
273
- var mainName = 'main' || (0, graph_2.nodeName)(node);
274
- if (node.stage === 'vertex') {
275
- if ((0, graph_2.doesLinkThruShader)(graph, node)) {
276
- (0, ast_1.returnGlPositionHardCoded)(mainName, programAst, 'vec3', 'transformed');
277
- }
278
- else {
279
- (0, ast_1.returnGlPosition)(mainName, programAst);
280
- }
281
- }
282
- // We specify engine nodes are mangle: false, which is the graph step that
283
- // handles renaming the main fn, so we have to do it ourselves
284
- (0, graph_1.mangleMainFn)(programAst, node, sibling);
285
- return programAst;
286
- };
287
- var nodeCacheKey = function (graph, node) {
288
- return ('[ID:' +
289
- node.id +
290
- 'Edges:' +
291
- graph.edges
292
- .filter(function (edge) { return edge.to === node.id; })
293
- .map(function (edge) { return "(".concat(edge.to, "->").concat(edge.input, ")"); })
294
- .sort()
295
- .join(',') +
296
- ']'
297
- // Currently excluding node inputs because these are calculated *after*
298
- // the onbeforecompile, so the next compile, they'll all change!
299
- // node.inputs.map((i) => `${i.id}${i.bakeable}`)
300
- );
301
- };
302
- var programCacheKey = function (engineContext, graph, node, sibling) {
303
- // The megashader source is dependent on scene information, like the number
304
- // and type of lights in the scene. This kinda sucks - it's duplicating
305
- // three's material cache key, and is coupled to how three builds shaders
306
- var scene = engineContext.runtime.scene;
307
- var lights = [];
308
- scene.traverse(function (obj) {
309
- if (obj instanceof three_1.Light) {
310
- lights.push(obj.type);
311
- }
312
- });
313
- return ([node, sibling]
314
- .filter(function (n) { return !!n; })
315
- .sort(function (a, b) { return a.id.localeCompare(b.id); })
316
- .map(function (n) { return nodeCacheKey(graph, n); })
317
- .join('-') +
318
- '|Lights:' +
319
- lights.join(',') +
320
- '|Envtex:' +
321
- scene.environmentTexture);
322
- };
323
- var defaultPropertySetting = function (property) {
324
- if (property.type === 'texture') {
325
- return new three_1.Texture();
326
- }
327
- else if (property.type === 'number') {
328
- return 0.5;
329
- }
330
- else if (property.type === 'rgb') {
331
- return new three_1.Color(1, 1, 1);
332
- }
333
- else if (property.type === 'rgba') {
334
- return new three_1.Vector4(1, 1, 1, 1);
335
- }
336
- };
337
- exports.defaultPropertySetting = defaultPropertySetting;
338
- var threeMaterialProperties = function (graph, node, sibling) {
339
- // Find inputs to this node that are dependent on a property of the material
340
- var propertyInputs = node.inputs
341
- .filter(function (i) { return i.property; })
342
- .reduce(function (acc, input) {
343
- var _a;
344
- return (__assign(__assign({}, acc), (_a = {}, _a[input.id] = input, _a)));
345
- }, {});
346
- // Then look for any edges into those inputs and set the material property
347
- return graph.edges
348
- .filter(function (edge) { return edge.to === node.id || edge.to === (sibling === null || sibling === void 0 ? void 0 : sibling.id); })
349
- .reduce(function (acc, edge) {
350
- // Check if we've plugged into an input for a property
351
- var propertyInput = propertyInputs[edge.input];
352
- if (propertyInput) {
353
- // Find the property itself
354
- var property_1 = (node.config.properties || []).find(function (p) { return p.property === propertyInput.property; });
355
- // Initialize the property on the material
356
- acc[property_1.property] = (0, exports.defaultPropertySetting)(property_1);
357
- }
358
- return acc;
359
- }, {});
360
- };
361
- var evaluateNode = function (node) {
362
- if (node.type === 'number') {
363
- return parseFloat(node.value);
364
- }
365
- if (node.type === 'vector2') {
366
- return new three_1.Vector2(parseFloat(node.value[0]), parseFloat(node.value[1]));
367
- }
368
- else if (node.type === 'vector3') {
369
- return new three_1.Vector3(parseFloat(node.value[0]), parseFloat(node.value[1]), parseFloat(node.value[2]));
370
- }
371
- else if (node.type === 'vector4') {
372
- return new three_1.Vector4(parseFloat(node.value[0]), parseFloat(node.value[1]), parseFloat(node.value[2]), parseFloat(node.value[3]));
373
- }
374
- else if (node.type === 'rgb') {
375
- return new three_1.Color(parseFloat(node.value[0]), parseFloat(node.value[1]), parseFloat(node.value[2]));
376
- }
377
- else if (node.type === 'rgba') {
378
- return new three_1.Vector4(parseFloat(node.value[0]), parseFloat(node.value[1]), parseFloat(node.value[2]), parseFloat(node.value[3]));
379
- }
380
- else {
381
- return node.value;
382
- }
383
- };
384
- var toonNode = function (id, name, position, uniforms, stage) {
385
- return (0, graph_1.prepopulatePropertyInputs)({
386
- id: id,
387
- name: 'MeshToonMaterial',
388
- position: position,
389
- engine: true,
390
- type: engine_1.EngineNodeType.toon,
391
- config: {
392
- uniforms: uniforms,
393
- version: 3,
394
- preprocess: true,
395
- mangle: false,
396
- properties: [
397
- (0, code_nodes_1.property)('Color', 'color', 'rgb', 'uniform_diffuse'),
398
- (0, code_nodes_1.property)('Texture', 'map', 'texture', 'filler_map'),
399
- (0, code_nodes_1.property)('Gradient Map', 'gradientMap', 'texture', 'filler_gradientMap'),
400
- (0, code_nodes_1.property)('Normal Map', 'normalMap', 'texture', 'filler_normalMap'),
401
- (0, code_nodes_1.property)('Normal Scale', 'normalScale', 'vector2'),
402
- (0, code_nodes_1.property)('AO Map', 'aoMap', 'texture', 'filler_aoMap'),
403
- (0, code_nodes_1.property)('AO Intensity', 'aoMapIntensity', 'number', 'filler_aoMapIntensity'),
404
- (0, code_nodes_1.property)('Displacement Map', 'displacementMap', 'texture', 'filler_displacementMap'),
405
- (0, code_nodes_1.property)('Displacement Scale', 'displacementScale', 'number'),
406
- (0, code_nodes_1.property)('Env Map', 'envMap', 'samplerCube'),
407
- ],
408
- strategies: [
409
- (0, strategy_1.uniformStrategy)(),
410
- stage === 'fragment'
411
- ? (0, strategy_1.texture2DStrategy)()
412
- : (0, strategy_1.namedAttributeStrategy)('position'),
413
- ],
414
- },
415
- inputs: [],
416
- outputs: [
417
- {
418
- name: 'vector4',
419
- dataType: 'vector4',
420
- category: 'data',
421
- id: '1',
422
- },
423
- ],
424
- source: '',
425
- stage: stage,
426
- });
427
- };
428
- exports.toonNode = toonNode;
429
- exports.threngine = {
430
- name: 'three',
431
- displayName: 'Three.js',
432
- importers: importers_1.default,
433
- mergeOptions: {
434
- includePrecisions: true,
435
- includeVersion: true,
436
- },
437
- evaluateNode: evaluateNode,
438
- constructors: (_a = {},
439
- _a[engine_1.EngineNodeType.phong] = exports.phongNode,
440
- _a[engine_1.EngineNodeType.physical] = exports.physicalNode,
441
- _a[engine_1.EngineNodeType.toon] = exports.toonNode,
442
- _a),
443
- // TODO: Get from uniform lib?
444
- preserve: new Set([
445
- 'viewMatrix',
446
- 'modelMatrix',
447
- 'modelViewMatrix',
448
- 'projectionMatrix',
449
- 'normalMatrix',
450
- 'uvTransform',
451
- // Attributes
452
- 'position',
453
- 'normal',
454
- 'uv',
455
- 'uv2',
456
- 'tangent',
457
- // Varyings
458
- 'vUv',
459
- 'vUv2',
460
- 'vViewPosition',
461
- 'vNormal',
462
- 'vPosition',
463
- // Uniforms
464
- 'cameraPosition',
465
- 'isOrthographic',
466
- 'diffuse',
467
- 'emissive',
468
- 'specular',
469
- 'shininess',
470
- 'opacity',
471
- 'map',
472
- 'specularTint',
473
- 'time',
474
- 'normalScale',
475
- 'normalMap',
476
- 'envMap',
477
- 'envMapIntensity',
478
- 'flipEnvMap',
479
- 'maxMipLevel',
480
- 'roughnessMap',
481
- // Uniforms for lighting
482
- 'receiveShadow',
483
- 'ambientLightColor',
484
- 'lightProbe',
485
- // Light uniform arrays
486
- 'spotLights',
487
- 'pointLights',
488
- // This isn't three wtf
489
- 'resolution',
490
- 'color',
491
- 'image',
492
- 'gradientMap',
493
- // TODO: This isn't specific to threejs as an engine, it's specific to the
494
- // phong shader. If a *shader* node has brightness, it should be unique, not
495
- // use the threejs one!
496
- 'brightness',
497
- // TODO: These depend on the shaderlib, this might need to be a runtime
498
- // concern
499
- // Metalness
500
- 'roughness',
501
- 'metalness',
502
- 'ior',
503
- 'specularIntensity',
504
- 'clearcoat',
505
- 'clearcoatRoughness',
506
- 'transmission',
507
- 'thickness',
508
- 'attenuationDistance',
509
- 'attenuationTint',
510
- 'transmissionSamplerMap',
511
- 'transmissionSamplerSize',
512
- 'displacementMap',
513
- 'displacementScale',
514
- 'displacementBias',
515
- ]),
516
- parsers: (_b = {},
517
- _b[graph_types_1.NodeType.SOURCE] = {
518
- manipulateAst: function (engineContext, engine, graph, ast, inputEdges, node, sibling) {
519
- var programAst = ast;
520
- var mainName = 'main' || (0, graph_2.nodeName)(node);
521
- // This hinges on the vertex shader calling vec3(p)
522
- if (node.stage === 'vertex') {
523
- if ((0, graph_2.doesLinkThruShader)(graph, node)) {
524
- (0, ast_1.returnGlPositionVec3Right)(mainName, programAst);
525
- }
526
- else {
527
- (0, ast_1.returnGlPosition)(mainName, programAst);
528
- }
529
- }
530
- return ast;
531
- },
532
- },
533
- _b[engine_1.EngineNodeType.phong] = {
534
- onBeforeCompile: function (graph, engineContext, node, sibling) { return __awaiter(void 0, void 0, void 0, function () {
535
- return __generator(this, function (_a) {
536
- cacher(engineContext, graph, node, sibling, function () {
537
- return onBeforeCompileMegaShader(engineContext, new three_1.MeshPhongMaterial(__assign({
538
- // @ts-ignore
539
- isMeshPhongMaterial: true }, threeMaterialProperties(graph, node, sibling))));
540
- });
541
- return [2 /*return*/];
542
- });
543
- }); },
544
- manipulateAst: megaShaderMainpulateAst,
545
- },
546
- _b[engine_1.EngineNodeType.physical] = {
547
- onBeforeCompile: function (graph, engineContext, node, sibling) { return __awaiter(void 0, void 0, void 0, function () {
548
- return __generator(this, function (_a) {
549
- cacher(engineContext, graph, node, sibling, function () {
550
- return onBeforeCompileMegaShader(engineContext, new three_1.MeshPhysicalMaterial(__assign(__assign({}, node.config.hardCodedProperties), threeMaterialProperties(graph, node, sibling))));
551
- });
552
- return [2 /*return*/];
553
- });
554
- }); },
555
- manipulateAst: megaShaderMainpulateAst,
556
- },
557
- _b[engine_1.EngineNodeType.toon] = {
558
- onBeforeCompile: function (graph, engineContext, node, sibling) { return __awaiter(void 0, void 0, void 0, function () {
559
- return __generator(this, function (_a) {
560
- cacher(engineContext, graph, node, sibling, function () {
561
- return onBeforeCompileMegaShader(engineContext, new three_1.MeshToonMaterial(__assign({ gradientMap: new three_1.Texture(),
562
- // @ts-ignore
563
- isMeshToonMaterial: true }, threeMaterialProperties(graph, node, sibling))));
564
- });
565
- return [2 /*return*/];
566
- });
567
- }); },
568
- manipulateAst: megaShaderMainpulateAst,
569
- },
570
- _b),
571
- };
572
- var createMaterial = function (compileResult, ctx) {
573
- var engineMaterial = ctx.runtime.engineMaterial;
574
- var finalUniforms = __assign(__assign(__assign(__assign({}, three_1.ShaderLib.phong.uniforms), three_1.ShaderLib.toon.uniforms), three_1.ShaderLib.physical.uniforms), { time: { value: 0 }, cameraPosition: { value: new three_1.Vector3(1.0) } });
575
- // Also the ThreeComponent's sceneConfig properties modify the material
576
- var initialProperties = {
577
- name: 'ShaderFrog Material',
578
- lights: true,
579
- uniforms: __assign({}, finalUniforms),
580
- // See https://github.com/mrdoob/three.js/pull/26809
581
- glslVersion: three_1.GLSL3,
582
- vertexShader: compileResult === null || compileResult === void 0 ? void 0 : compileResult.vertexResult.replace('#version 300 es', ''),
583
- fragmentShader: compileResult === null || compileResult === void 0 ? void 0 : compileResult.fragmentResult.replace('#version 300 es', ''),
584
- };
585
- var additionalProperties = Object.entries(__assign({}, engineMaterial))
586
- .filter(function (_a) {
587
- var _b = __read(_a, 1), property = _b[0];
588
- // Ignore three material "hidden" properties
589
- return property.charAt(0) !== '_' &&
590
- // Ignore uuid since it should probably be unique?
591
- property !== 'uuid' &&
592
- // I'm not sure what three does with type under the hood, ignore it
593
- property !== 'type' &&
594
- // "precision" adds a precision preprocessor line
595
- property !== 'precision' &&
596
- // Ignore existing properties
597
- !(property in initialProperties) &&
598
- // Ignore STANDARD and PHYSICAL defines to the top of the shader in
599
- // WebGLProgram
600
- // https://github.com/mrdoob/three.js/blob/e7042de7c1a2c70e38654a04b6fd97d9c978e781/src/renderers/webgl/WebGLProgram.js#L392
601
- // which occurs if we set isMeshPhysicalMaterial/isMeshStandardMaterial
602
- property !== 'defines';
603
- })
604
- .reduce(function (acc, _a) {
605
- var _b;
606
- var _c = __read(_a, 2), key = _c[0], value = _c[1];
607
- return (__assign(__assign({}, acc), (_b = {}, _b[key] = value, _b)));
608
- }, {});
609
- var material = new three_1.RawShaderMaterial(initialProperties);
610
- // This prevents a deluge of warnings from three on the constructor saying
611
- // that each of these properties is not a property of the material
612
- Object.entries(additionalProperties).forEach(function (_a) {
613
- var _b = __read(_a, 2), key = _b[0], value = _b[1];
614
- // @ts-ignore
615
- material[key] = value;
616
- });
617
- return material;
618
- };
619
- exports.createMaterial = createMaterial;
@@ -1,31 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.applyAssignmentToStrategy = exports.assignemntToStrategy = void 0;
4
- var ast_1 = require("../util/ast");
5
- var base_node_1 = require("../graph/base-node");
6
- var _1 = require(".");
7
- // Constructor
8
- var assignemntToStrategy = function (assignTo) { return ({
9
- type: _1.StrategyType.ASSIGNMENT_TO,
10
- config: { assignTo: assignTo },
11
- }); };
12
- exports.assignemntToStrategy = assignemntToStrategy;
13
- // Apply the strategy
14
- var applyAssignmentToStrategy = function (strategy, ast, graphNode, siblingNode) {
15
- var assignNode = (0, ast_1.findAssignmentTo)(ast, strategy.config.assignTo);
16
- var name = strategy.config.assignTo;
17
- return assignNode
18
- ? [
19
- [
20
- (0, base_node_1.nodeInput)(name, "filler_".concat(name), 'filler', undefined, // Data type for what plugs into this filler
21
- ['code', 'data'], false),
22
- function (fillerAst) {
23
- assignNode.expression.right =
24
- fillerAst;
25
- return ast;
26
- },
27
- ],
28
- ]
29
- : [];
30
- };
31
- exports.applyAssignmentToStrategy = applyAssignmentToStrategy;
@@ -1,28 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.constApplyDeclarationOf = exports.declarationOfStrategy = void 0;
4
- var ast_1 = require("../util/ast");
5
- var base_node_1 = require("../graph/base-node");
6
- var _1 = require(".");
7
- var declarationOfStrategy = function (declarationOf) { return ({
8
- type: _1.StrategyType.DECLARATION_OF,
9
- config: { declarationOf: declarationOf },
10
- }); };
11
- exports.declarationOfStrategy = declarationOfStrategy;
12
- var constApplyDeclarationOf = function (strategy, ast, graphNode, siblingNode) {
13
- var declaration = (0, ast_1.findDeclarationOf)(ast, strategy.config.declarationOf);
14
- var name = strategy.config.declarationOf;
15
- return declaration
16
- ? [
17
- [
18
- (0, base_node_1.nodeInput)(name, "filler_".concat(name), 'filler', undefined, // Data type for what plugs into this filler
19
- ['code', 'data'], false),
20
- function (fillerAst) {
21
- declaration.initializer = fillerAst;
22
- return ast;
23
- },
24
- ],
25
- ]
26
- : [];
27
- };
28
- exports.constApplyDeclarationOf = constApplyDeclarationOf;