File size: 2,491 Bytes
6cd9596
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
 * @author sunag / http://www.sunag.com.br/
 */

import { TempNode } from '../core/TempNode.js';

function Math2Node( a, b, method ) {

	TempNode.call( this );

	this.a = a;
	this.b = b;

	this.method = method;

}

Math2Node.MIN = 'min';
Math2Node.MAX = 'max';
Math2Node.MOD = 'mod';
Math2Node.STEP = 'step';
Math2Node.REFLECT = 'reflect';
Math2Node.DISTANCE = 'distance';
Math2Node.DOT = 'dot';
Math2Node.CROSS = 'cross';
Math2Node.POW = 'pow';

Math2Node.prototype = Object.create( TempNode.prototype );
Math2Node.prototype.constructor = Math2Node;
Math2Node.prototype.nodeType = "Math2";

Math2Node.prototype.getInputType = function ( builder ) {

	// use the greater length vector

	if ( builder.getTypeLength( this.b.getType( builder ) ) > builder.getTypeLength( this.a.getType( builder ) ) ) {

		return this.b.getType( builder );

	}

	return this.a.getType( builder );

};

Math2Node.prototype.getType = function ( builder ) {

	switch ( this.method ) {

		case Math2Node.DISTANCE:
		case Math2Node.DOT:

			return 'f';

		case Math2Node.CROSS:

			return 'v3';

	}

	return this.getInputType( builder );

};

Math2Node.prototype.generate = function ( builder, output ) {

	var a, b,
		type = this.getInputType( builder ),
		al = builder.getTypeLength( this.a.getType( builder ) ),
		bl = builder.getTypeLength( this.b.getType( builder ) );

	// optimzer

	switch ( this.method ) {

		case Math2Node.CROSS:

			a = this.a.build( builder, 'v3' );
			b = this.b.build( builder, 'v3' );

			break;

		case Math2Node.STEP:

			a = this.a.build( builder, al === 1 ? 'f' : type );
			b = this.b.build( builder, type );

			break;

		case Math2Node.MIN:
		case Math2Node.MAX:
		case Math2Node.MOD:

			a = this.a.build( builder, type );
			b = this.b.build( builder, bl === 1 ? 'f' : type );

			break;

		default:

			a = this.a.build( builder, type );
			b = this.b.build( builder, type );

			break;

	}

	return builder.format( this.method + '( ' + a + ', ' + b + ' )', this.getType( builder ), output );

};

Math2Node.prototype.copy = function ( source ) {

	TempNode.prototype.copy.call( this, source );

	this.a = source.a;
	this.b = source.b;
	this.method = source.method;

};

Math2Node.prototype.toJSON = function ( meta ) {

	var data = this.getJSONNode( meta );

	if ( ! data ) {

		data = this.createJSONNode( meta );

		data.a = this.a.toJSON( meta ).uuid;
		data.b = this.b.toJSON( meta ).uuid;
		data.method = this.method;

	}

	return data;

};

export { Math2Node };