From 82780ac7ee821c1e0a62be25286177f72dbd00f7 Mon Sep 17 00:00:00 2001 From: Don McCurdy Date: Wed, 5 May 2021 22:49:44 -0700 Subject: [PATCH] RemapNode: Initial commit. --- examples/jsm/nodes/Nodes.js | 1 + examples/jsm/nodes/utils/RemapNode.js | 153 ++++++++++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 examples/jsm/nodes/utils/RemapNode.js diff --git a/examples/jsm/nodes/Nodes.js b/examples/jsm/nodes/Nodes.js index 6fbf47640982f3..cd531ddc080ea9 100644 --- a/examples/jsm/nodes/Nodes.js +++ b/examples/jsm/nodes/Nodes.js @@ -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'; diff --git a/examples/jsm/nodes/utils/RemapNode.js b/examples/jsm/nodes/utils/RemapNode.js new file mode 100644 index 00000000000000..1589e9d93b2191 --- /dev/null +++ b/examples/jsm/nodes/utils/RemapNode.js @@ -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 }; \ No newline at end of file