NoiseNode.js 1.3 KB
Newer Older
S
sunag 已提交
1 2 3 4 5 6 7 8
/**
 * @author sunag / http://www.sunag.com.br/
 */

import { TempNode } from '../core/TempNode.js';
import { FunctionNode } from '../core/FunctionNode.js';
import { UVNode } from '../accessors/UVNode.js';

S
sunag 已提交
9
function NoiseNode( uv ) {
S
sunag 已提交
10

S
sunag 已提交
11
	TempNode.call( this, 'f' );
S
sunag 已提交
12

S
sunag 已提交
13
	this.uv = uv || new UVNode();
S
sunag 已提交
14

S
sunag 已提交
15
}
S
sunag 已提交
16 17 18 19 20

NoiseNode.prototype = Object.create( TempNode.prototype );
NoiseNode.prototype.constructor = NoiseNode;
NoiseNode.prototype.nodeType = "Noise";

S
sunag 已提交
21 22
NoiseNode.Nodes = ( function () {

S
sunag 已提交
23 24
	var snoise = new FunctionNode( [
		"float snoise(vec2 co) {",
S
sunag 已提交
25

S
sunag 已提交
26
		"	return fract( sin( dot( co.xy, vec2( 12.9898, 78.233 ) ) ) * 43758.5453 );",
S
sunag 已提交
27

S
sunag 已提交
28 29 30 31 32 33
		"}"
	].join( "\n" ) );

	return {
		snoise: snoise
	};
S
sunag 已提交
34 35

} )();
S
sunag 已提交
36 37 38 39 40

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

	var snoise = builder.include( NoiseNode.Nodes.snoise );

S
sunag 已提交
41
	return builder.format( snoise + '( ' + this.uv.build( builder, 'v2' ) + ' )', this.getType( builder ), output );
S
sunag 已提交
42 43 44 45

};

NoiseNode.prototype.copy = function ( source ) {
S
sunag 已提交
46

S
sunag 已提交
47
	TempNode.prototype.copy.call( this, source );
S
sunag 已提交
48

S
sunag 已提交
49
	this.uv = source.uv;
S
sunag 已提交
50

S
sunag 已提交
51 52 53 54 55 56 57 58 59 60
};

NoiseNode.prototype.toJSON = function ( meta ) {

	var data = this.getJSONNode( meta );

	if ( ! data ) {

		data = this.createJSONNode( meta );

S
sunag 已提交
61
		data.uv = this.uv.toJSON( meta ).uuid;
S
sunag 已提交
62 63 64 65 66 67 68

	}

	return data;

};

S
sunag 已提交
69
export { NoiseNode };