File size: 2,363 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
/**
 * @author sunag / http://www.sunag.com.br/
 */

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

function Math3Node( a, b, c, method ) {

	TempNode.call( this );

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

	this.method = method;

}

Math3Node.MIX = 'mix';
Math3Node.CLAMP = 'clamp';
Math3Node.REFRACT = 'refract';
Math3Node.SMOOTHSTEP = 'smoothstep';
Math3Node.FACEFORWARD = 'faceforward';

Math3Node.prototype = Object.create( TempNode.prototype );
Math3Node.prototype.constructor = Math3Node;
Math3Node.prototype.nodeType = "Math3";

Math3Node.prototype.getType = function ( builder ) {

	var a = builder.getTypeLength( this.a.getType( builder ) );
	var b = builder.getTypeLength( this.b.getType( builder ) );
	var c = builder.getTypeLength( this.c.getType( builder ) );

	if ( a > b && a > c ) {

		return this.a.getType( builder );

	} else if ( b > c ) {

		return this.b.getType( builder );

	}

	return this.c.getType( builder );

};

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

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

	// optimzer

	switch ( this.method ) {

		case Math3Node.REFRACT:

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

			break;

		case Math3Node.MIX:

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

			break;

		default:

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

			break;

	}

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

};

Math3Node.prototype.copy = function ( source ) {

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

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

};

Math3Node.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.c = this.c.toJSON( meta ).uuid;
		data.method = this.method;

	}

	return data;

};

export { Math3Node };