NodeBuilder.js 4.1 KB
Newer Older
S
sunag 已提交
1 2 3 4
/**
 * @author sunag / http://www.sunag.com.br/
 */

5
THREE.NodeBuilder = function ( material, renderer ) {
S
sunag 已提交
6 7

	this.material = material;
8
	this.renderer = renderer;
S
sunag 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22

	this.caches = [];
	this.slots = [];

	this.keywords = {};

	this.parsing = false;
	this.optimize = true;

	this.update();

};

THREE.NodeBuilder.type = {
S
sunag 已提交
23 24 25 26 27 28
	float: 'fv1',
	vec2: 'v2',
	vec3: 'v3',
	vec4: 'v4',
	mat4: 'v4',
	int: 'iv1'
S
sunag 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
};

THREE.NodeBuilder.constructors = [
	'float',
	'vec2',
	'vec3',
	'vec4'
];

THREE.NodeBuilder.elements = [
	'x',
	'y',
	'z',
	'w'
];

THREE.NodeBuilder.prototype = {

	constructor: THREE.NodeBuilder,

S
sunag 已提交
49
	addCache: function ( name, requires ) {
S
sunag 已提交
50 51

		this.caches.push( {
S
sunag 已提交
52 53
			name: name || '',
			requires: requires || {}
S
sunag 已提交
54 55 56 57 58 59
		} );

		return this.update();

	},

S
sunag 已提交
60
	removeCache: function () {
S
sunag 已提交
61 62 63 64 65 66 67

		this.caches.pop();

		return this.update();

	},

S
sunag 已提交
68
	addSlot: function ( name ) {
S
sunag 已提交
69 70

		this.slots.push( {
S
sunag 已提交
71
			name: name || ''
S
sunag 已提交
72 73 74 75 76 77
		} );

		return this.update();

	},

S
sunag 已提交
78
	removeSlot: function () {
S
sunag 已提交
79 80 81 82 83 84 85

		this.slots.pop();

		return this.update();

	},

S
sunag 已提交
86
	isCache: function ( name ) {
S
sunag 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99

		var i = this.caches.length;

		while ( i -- ) {

			if ( this.caches[ i ].name == name ) return true;

		}

		return false;

	},

S
sunag 已提交
100
	isSlot: function ( name ) {
S
sunag 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113

		var i = this.slots.length;

		while ( i -- ) {

			if ( this.slots[ i ].name == name ) return true;

		}

		return false;

	},

S
sunag 已提交
114
	update: function () {
S
sunag 已提交
115 116 117 118 119 120 121 122 123 124 125 126

		var cache = this.caches[ this.caches.length - 1 ];
		var slot = this.slots[ this.slots.length - 1 ];

		this.slot = slot ? slot.name : '';
		this.cache = cache ? cache.name : '';
		this.requires = cache ? cache.requires : {};

		return this;

	},

S
sunag 已提交
127
	require: function ( name, node ) {
S
sunag 已提交
128 129 130 131 132 133 134

		this.requires[ name ] = node;

		return this;

	},

S
sunag 已提交
135
	include: function ( node, parent, source ) {
S
sunag 已提交
136 137 138 139 140 141 142

		this.material.include( this, node, parent, source );

		return this;

	},

S
sunag 已提交
143
	colorToVector: function ( color ) {
S
sunag 已提交
144 145 146 147 148

		return color.replace( 'r', 'x' ).replace( 'g', 'y' ).replace( 'b', 'z' ).replace( 'a', 'w' );

	},

S
sunag 已提交
149
	getConstructorFromLength: function ( len ) {
S
sunag 已提交
150 151 152 153 154

		return THREE.NodeBuilder.constructors[ len - 1 ];

	},

S
sunag 已提交
155
	getFormatName: function ( format ) {
S
sunag 已提交
156 157 158 159 160

		return format.replace( /c/g, 'v3' ).replace( /fv1/g, 'v1' ).replace( /iv1/g, 'i' );

	},

S
sunag 已提交
161
	isFormatMatrix: function ( format ) {
S
sunag 已提交
162 163 164 165 166

		return /^m/.test( format );

	},

S
sunag 已提交
167
	getFormatLength: function ( format ) {
S
sunag 已提交
168 169 170 171 172

		return parseInt( this.getFormatName( format ).substr( 1 ) );

	},

S
sunag 已提交
173
	getFormatFromLength: function ( len ) {
S
sunag 已提交
174 175 176 177 178 179 180

		if ( len == 1 ) return 'fv1';

		return 'v' + len;

	},

S
sunag 已提交
181
	format: function ( code, from, to ) {
S
sunag 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

		var format = this.getFormatName( to + '=' + from );

		switch ( format ) {

			case 'v1=v2': return code + '.x';
			case 'v1=v3': return code + '.x';
			case 'v1=v4': return code + '.x';
			case 'v1=i': return 'float(' + code + ')';

			case 'v2=v1': return 'vec2(' + code + ')';
			case 'v2=v3': return code + '.xy';
			case 'v2=v4': return code + '.xy';
			case 'v2=i': return 'vec2(float(' + code + '))';

			case 'v3=v1': return 'vec3(' + code + ')';
			case 'v3=v2': return 'vec3(' + code + ',0.0)';
			case 'v3=v4': return code + '.xyz';
			case 'v3=i': return 'vec2(float(' + code + '))';

			case 'v4=v1': return 'vec4(' + code + ')';
			case 'v4=v2': return 'vec4(' + code + ',0.0,1.0)';
			case 'v4=v3': return 'vec4(' + code + ',1.0)';
			case 'v4=i': return 'vec4(float(' + code + '))';

			case 'i=v1': return 'int(' + code + ')';
			case 'i=v2': return 'int(' + code + '.x)';
			case 'i=v3': return 'int(' + code + '.x)';
			case 'i=v4': return 'int(' + code + '.x)';

		}

		return code;

	},

S
sunag 已提交
218
	getTypeByFormat: function ( format ) {
S
sunag 已提交
219 220 221 222 223

		return THREE.NodeBuilder.type[ format ] || format;

	},

S
sunag 已提交
224
	getUuid: function ( uuid, useCache ) {
S
sunag 已提交
225 226 227 228 229 230 231 232 233

		useCache = useCache !== undefined ? useCache : true;

		if ( useCache && this.cache ) uuid = this.cache + '-' + uuid;

		return uuid;

	},

S
sunag 已提交
234
	getElementByIndex: function ( index ) {
S
sunag 已提交
235 236 237 238 239

		return THREE.NodeBuilder.elements[ index ];

	},

S
sunag 已提交
240
	getIndexByElement: function ( elm ) {
S
sunag 已提交
241 242 243 244 245

		return THREE.NodeBuilder.elements.indexOf( elm );

	},

S
sunag 已提交
246
	isShader: function ( shader ) {
S
sunag 已提交
247 248 249 250 251

		return this.shader == shader;

	},

S
sunag 已提交
252
	setShader: function ( shader ) {
S
sunag 已提交
253 254 255 256 257 258 259

		this.shader = shader;

		return this;

	}
};