@shaderfrog/core 0.0.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.
@@ -0,0 +1,266 @@
1
+ import { EngineNodeType } from '../engine';
2
+ import { NodeType, ShaderStage, prepopulatePropertyInputs } from '../graph';
3
+ import {
4
+ assignemntToStrategy,
5
+ hardCodeStrategy,
6
+ namedAttributeStrategy,
7
+ texture2DStrategy,
8
+ uniformStrategy,
9
+ variableStrategy,
10
+ } from '../strategy';
11
+ import { BinaryNode, CodeNode, NodeConfig, property } from './code-nodes';
12
+ import { NodePosition } from './core-node';
13
+ import { UniformDataType } from './data-nodes';
14
+
15
+ /**
16
+ * TODO: These definitions should live outside of core since I'm trying to
17
+ * refactor out this core folder to only know about nodes with config config,
18
+ * where nodes like output/phong/physical are all configured at the
19
+ * implementation level. "phong" shouldn't be in the core
20
+ */
21
+
22
+ export const sourceNode = (
23
+ id: string,
24
+ name: string,
25
+ position: NodePosition,
26
+ config: NodeConfig,
27
+ source: string,
28
+ stage: ShaderStage,
29
+ originalEngine?: string,
30
+ nextStageNodeId?: string
31
+ ): CodeNode => ({
32
+ id,
33
+ name,
34
+ groupId: undefined,
35
+ type: NodeType.SOURCE,
36
+ config,
37
+ position,
38
+ inputs: [],
39
+ outputs: [
40
+ {
41
+ name: 'vector4',
42
+ category: 'data',
43
+ id: '1',
44
+ },
45
+ ],
46
+ source,
47
+ stage,
48
+ originalEngine,
49
+ nextStageNodeId,
50
+ });
51
+
52
+ export const outputNode = (
53
+ id: string,
54
+ name: string,
55
+ position: NodePosition,
56
+ stage: ShaderStage
57
+ ): CodeNode => ({
58
+ id,
59
+ name,
60
+ position,
61
+ groupId: undefined,
62
+ type: NodeType.OUTPUT,
63
+ config: {
64
+ version: 3,
65
+ mangle: false,
66
+ preprocess: false,
67
+ uniforms: [],
68
+ inputMapping:
69
+ stage === 'fragment'
70
+ ? {
71
+ filler_frogFragOut: 'Color',
72
+ }
73
+ : {
74
+ filler_gl_Position: 'Position',
75
+ },
76
+ strategies: [
77
+ assignemntToStrategy(
78
+ stage === 'fragment' ? 'frogFragOut' : 'gl_Position'
79
+ ),
80
+ ],
81
+ },
82
+ inputs: [],
83
+ outputs: [],
84
+ // Consumed by findVec4Constructo4
85
+ source:
86
+ stage === 'fragment'
87
+ ? `
88
+ #version 300 es
89
+ precision highp float;
90
+
91
+ out vec4 frogFragOut;
92
+ void main() {
93
+ frogFragOut = vec4(1.0);
94
+ }
95
+ `
96
+ : // gl_Position isn't "out"-able apparently https://stackoverflow.com/a/24425436/743464
97
+ `
98
+ #version 300 es
99
+ precision highp float;
100
+
101
+ void main() {
102
+ gl_Position = vec4(1.0);
103
+ }
104
+ `,
105
+ stage,
106
+ });
107
+
108
+ export const expressionNode = (
109
+ id: string,
110
+ name: string,
111
+ position: NodePosition,
112
+ source: string
113
+ ): CodeNode => ({
114
+ id,
115
+ name,
116
+ position,
117
+ type: NodeType.SOURCE,
118
+ expressionOnly: true,
119
+ groupId: undefined,
120
+ stage: undefined,
121
+ config: {
122
+ uniforms: [],
123
+ version: 3,
124
+ preprocess: false,
125
+ inputMapping: {},
126
+ strategies: [variableStrategy()],
127
+ },
128
+ inputs: [],
129
+ outputs: [
130
+ {
131
+ name: 'expression',
132
+ category: 'data',
133
+ id: '1',
134
+ },
135
+ ],
136
+ source,
137
+ });
138
+
139
+ export const phongNode = (
140
+ id: string,
141
+ name: string,
142
+ groupId: string,
143
+ position: NodePosition,
144
+ stage: ShaderStage,
145
+ nextStageNodeId?: string
146
+ ): CodeNode =>
147
+ prepopulatePropertyInputs({
148
+ id,
149
+ name,
150
+ groupId,
151
+ position,
152
+ type: EngineNodeType.phong,
153
+ config: {
154
+ version: 3,
155
+ uniforms: [],
156
+ preprocess: true,
157
+ mangle: false,
158
+ properties: [
159
+ property('Color', 'color', 'rgb', 'uniform_diffuse'),
160
+ property('Emissive', 'emissive', 'rgb', 'uniform_emissive'),
161
+ property(
162
+ 'Emissive Map',
163
+ 'emissiveMap',
164
+ 'texture',
165
+ 'filler_emissiveMap'
166
+ ),
167
+ property(
168
+ 'Emissive Intensity',
169
+ 'emissiveIntensity',
170
+ 'number',
171
+ 'uniform_emissive'
172
+ ),
173
+ property('Texture', 'map', 'texture', 'filler_map'),
174
+ property('Normal Map', 'normalMap', 'texture', 'filler_normalMap'),
175
+ property('Normal Scale', 'normalScale', 'vector2'),
176
+ property('Shininess', 'shininess', 'number'),
177
+ property('Reflectivity', 'reflectivity', 'number'),
178
+ property('Refraction Ratio', 'refractionRatio', 'number'),
179
+ property('Specular', 'specular', 'rgb', 'uniform_specular'),
180
+ property(
181
+ 'Specular Map',
182
+ 'specularMap',
183
+ 'texture',
184
+ 'filler_specularMap'
185
+ ),
186
+ property('Displacement Map', 'displacementMap', 'texture'),
187
+ property('Env Map', 'envMap', 'samplerCube'),
188
+ ],
189
+ strategies: [
190
+ uniformStrategy(),
191
+ stage === 'fragment'
192
+ ? texture2DStrategy()
193
+ : namedAttributeStrategy('position'),
194
+ ],
195
+ },
196
+ inputs: [],
197
+ outputs: [
198
+ {
199
+ name: 'vector4',
200
+ category: 'data',
201
+ id: '1',
202
+ },
203
+ ],
204
+ source: '',
205
+ stage,
206
+ nextStageNodeId,
207
+ });
208
+
209
+ export const addNode = (id: string, position: NodePosition): BinaryNode => ({
210
+ id,
211
+ name: 'add',
212
+ position,
213
+ type: NodeType.BINARY,
214
+ groupId: undefined,
215
+ stage: undefined,
216
+ config: {
217
+ mangle: false,
218
+ version: 3,
219
+ preprocess: true,
220
+ strategies: [],
221
+ uniforms: [],
222
+ },
223
+ inputs: [],
224
+ outputs: [
225
+ {
226
+ name: 'sum',
227
+ category: 'data',
228
+ id: '1',
229
+ },
230
+ ],
231
+ source: `a + b`,
232
+ operator: '+',
233
+ expressionOnly: true,
234
+ biStage: true,
235
+ });
236
+
237
+ export const multiplyNode = (
238
+ id: string,
239
+ position: NodePosition
240
+ ): BinaryNode => ({
241
+ id,
242
+ name: 'multiply',
243
+ type: NodeType.BINARY,
244
+ groupId: undefined,
245
+ stage: undefined,
246
+ position,
247
+ config: {
248
+ version: 3,
249
+ uniforms: [],
250
+ mangle: false,
251
+ preprocess: true,
252
+ strategies: [],
253
+ },
254
+ inputs: [],
255
+ outputs: [
256
+ {
257
+ name: 'product',
258
+ category: 'data',
259
+ id: '1',
260
+ },
261
+ ],
262
+ source: `a * b`,
263
+ operator: '*',
264
+ expressionOnly: true,
265
+ biStage: true,
266
+ });