-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
56 lines (48 loc) · 2.82 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import _ from 'lodash';
import { splitPreset, prepareShaderNoDefine, createBasePresetFuns } from 'milkdrop-preset-utils';
import milkdropParser from 'milkdrop-eel-parser';
import parseHLSL from './hlslParser';
import optimizeGLSL from './glslOptimizer';
function convertShader (shader) {
if (shader.length === 0) {
return '';
}
const fullShader = prepareShaderNoDefine(shader);
let convertedShader = parseHLSL(fullShader, 'shader_body', 'fs');
convertedShader = _.replace(convertedShader, /#version 100\sprecision highp float;/, '');
convertedShader = _.join(_.filter(_.split(convertedShader, '\n'), (line) => !_.startsWith(line, '#line')), '\n');
convertedShader = _.replace(convertedShader, /vec2 uv;\s+uv = frag_TEXCOORD0;/, '');
convertedShader = _.replace(convertedShader, 'varying vec2 frag_TEXCOORD0;', 'varying vec2 uv;');
convertedShader = _.replace(convertedShader, 'gl_FragData[0]', 'gl_FragColor');
convertedShader = _.replace(convertedShader, 'uniform float M_INV_PI_2', 'const float M_INV_PI_2');
convertedShader = _.replace(convertedShader, 'uniform float M_PI_2', 'const float M_PI_2');
convertedShader = _.replace(convertedShader, 'uniform float M_PI', 'const float M_PI');
let optimizedShader = optimizeGLSL(convertedShader, 1, false);
optimizedShader = _.replace(optimizedShader, /void main \(\)\s*\{/, 'shader_body {');
optimizedShader = _.replace(optimizedShader, /gl_FragColor = (.+);/, (match, p1) => `ret = ${p1}.xyz;`);
let shaderLines = _.split(optimizedShader, '\n');
shaderLines = _.filter(shaderLines, (line) => !_.startsWith(line, 'varying') &&
!_.startsWith(line, 'uniform'));
return _.join(shaderLines, '\n');
}
// eslint-disable-next-line import/prefer-default-export
export function convertPreset (text) {
const mainPresetText = _.split(text, '[preset00]')[1];
const presetParts = splitPreset(mainPresetText);
const parsedPreset = milkdropParser.convert_preset_wave_and_shape(presetParts.presetVersion,
presetParts.presetInit,
presetParts.perFrame,
presetParts.perVertex,
presetParts.shapes,
presetParts.waves);
const presetMap = createBasePresetFuns(parsedPreset,
presetParts.shapes,
presetParts.waves);
const warpShader = convertShader(presetParts.warp);
const compShader = convertShader(presetParts.comp);
return _.assign({}, presetMap, {
baseVals: presetParts.baseVals,
warp: warpShader,
comp: compShader,
});
}