Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NodeMaterial: Add RemapNode. #21793

Merged
merged 1 commit into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/jsm/nodes/Nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export { BumpMapNode } from './misc/BumpMapNode.js';
export { BypassNode } from './utils/BypassNode.js';
export { JoinNode } from './utils/JoinNode.js';
export { SwitchNode } from './utils/SwitchNode.js';
export { RemapNode } from './utils/RemapNode.js';
export { TimerNode } from './utils/TimerNode.js';
export { VelocityNode } from './utils/VelocityNode.js';
export { UVTransformNode } from './utils/UVTransformNode.js';
Expand Down
153 changes: 153 additions & 0 deletions examples/jsm/nodes/utils/RemapNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import { TempNode } from '../core/TempNode.js';
import { FunctionNode } from '../core/FunctionNode.js';

const REMAP_SRC = `
float remap( float value, float inLow, float inHigh, float outLow, float outHigh ) {

float x = ( value - inLow ) / ( inHigh - inLow );
return outLow + ( outHigh - outLow ) * x;

}

vec2 remap( vec2 value, vec2 inLow, vec2 inHigh, vec2 outLow, vec2 outHigh ) {

return vec2(
remap( value.x, inLow.x, inHigh.x, outLow.x, outHigh.x ),
remap( value.y, inLow.y, inHigh.y, outLow.y, outHigh.y )
);

}

vec2 remap( vec2 value, float inLow, float inHigh, float outLow, float outHigh ) {

return vec2(
remap( value.x, inLow, inHigh, outLow, outHigh ),
remap( value.y, inLow, inHigh, outLow, outHigh )
);

}

vec3 remap( vec3 value, vec3 inLow, vec3 inHigh, vec3 outLow, vec3 outHigh ) {

return vec3(
remap( value.x, inLow.x, inHigh.x, outLow.x, outHigh.x ),
remap( value.y, inLow.y, inHigh.y, outLow.y, outHigh.y ),
remap( value.z, inLow.z, inHigh.z, outLow.z, outHigh.z )
);

}

vec3 remap( vec3 value, float inLow, float inHigh, float outLow, float outHigh ) {

return vec3(
remap( value.x, inLow, inHigh, outLow, outHigh ),
remap( value.y, inLow, inHigh, outLow, outHigh ),
remap( value.z, inLow, inHigh, outLow, outHigh )
);

}

vec4 remap( vec4 value, vec4 inLow, vec4 inHigh, vec4 outLow, vec4 outHigh ) {

return vec4(
remap( value.x, inLow.x, inHigh.x, outLow.x, outHigh.x ),
remap( value.y, inLow.y, inHigh.y, outLow.y, outHigh.y ),
remap( value.z, inLow.z, inHigh.z, outLow.z, outHigh.z ),
remap( value.w, inLow.w, inHigh.w, outLow.w, outHigh.w )
);

}

vec4 remap( vec4 value, float inLow, float inHigh, float outLow, float outHigh ) {

return vec4(
remap( value.x, inLow, inHigh, outLow, outHigh ),
remap( value.y, inLow, inHigh, outLow, outHigh ),
remap( value.z, inLow, inHigh, outLow, outHigh ),
remap( value.w, inLow, inHigh, outLow, outHigh )
);

}
`.trim();

function RemapNode( value, inLow, inHigh, outLow, outHigh ) {

TempNode.call( this, 'f' );

this.value = value;
this.inLow = inLow;
this.inHigh = inHigh;
this.outLow = outLow;
this.outHigh = outHigh;

}

RemapNode.prototype = Object.create( TempNode.prototype );
RemapNode.prototype.constructor = RemapNode;
RemapNode.prototype.nodeType = 'Remap';

RemapNode.Nodes = (function () {

return {

remap: new FunctionNode( REMAP_SRC )

};

})();

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

const remap = builder.include( RemapNode.Nodes.remap );

return builder.format( remap + '( ' + [

this.value.build( builder ),
this.inLow.build( builder ),
this.inHigh.build( builder ),
this.outLow.build( builder ),
this.outHigh.build( builder ),

].join( ', ' ) + ' )', this.getType( builder ), output );

};

RemapNode.prototype.getType = function ( builder ) {

return this.value.getType( builder );

};

RemapNode.prototype.copy = function ( source ) {

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

this.value = source.value;
this.inLow = source.inLow;
this.inHigh = source.inHigh;
this.outLow = source.outLow;
this.outHigh = source.outHigh;

};

RemapNode.prototype.toJSON = function ( meta ) {

const data = this.getJSONNode( meta );

if ( ! data ) {

data = this.createJSONNode( meta );

data.value = this.value.toJSON( meta ).uuid;
data.inLow = this.inLow.toJSON( meta ).uuid;
data.inHigh = this.inHigh.toJSON( meta ).uuid;
data.outLow = this.outLow.toJSON( meta ).uuid;
data.outHigh = this.outHigh.toJSON( meta ).uuid;

}

return data;

};

export { RemapNode };