Skip to content

Commit

Permalink
InputNode: Added .setPrecision() (#25561)
Browse files Browse the repository at this point in the history
* InputNode: Added .setPrecision()

* InputNode: Default precision is null.
  • Loading branch information
sunag authored Feb 24, 2023
1 parent 05f6df7 commit 6a9e961
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
13 changes: 13 additions & 0 deletions examples/jsm/nodes/core/InputNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class InputNode extends Node {
this.isInputNode = true;

this.value = value;
this.precision = null;

}

Expand All @@ -31,6 +32,14 @@ class InputNode extends Node {

}

setPrecision( precision ) {

this.precision = precision;

return this;

}

serialize( data ) {

super.serialize( data );
Expand All @@ -42,6 +51,8 @@ class InputNode extends Node {
data.valueType = getValueType( this.value );
data.nodeType = this.nodeType;

data.precision = this.precision;

}

deserialize( data ) {
Expand All @@ -51,6 +62,8 @@ class InputNode extends Node {
this.nodeType = data.nodeType;
this.value = Array.isArray( data.value ) ? getValueFromType( data.valueType, ...data.value ) : data.value;

this.precision = data.precision || null;

if ( this.value && this.value.fromArray ) this.value = this.value.fromArray( data.value );

}
Expand Down
32 changes: 27 additions & 5 deletions examples/jsm/renderers/webgl/nodes/WebGLNodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ const glslMethods = {
[ MathNode.ATAN2 ]: 'atan'
};

const precisionLib = {
low: 'lowp',
medium: 'mediump',
high: 'highp'
};

function getIncludeSnippet( name ) {

return `#include <${name}>`;
Expand Down Expand Up @@ -460,29 +466,45 @@ class WebGLNodeBuilder extends NodeBuilder {

const uniforms = this.uniforms[ shaderStage ];

let snippet = '';
let output = '';

for ( const uniform of uniforms ) {

let snippet = null;

if ( uniform.type === 'texture' ) {

snippet += `uniform sampler2D ${uniform.name}; `;
snippet = `sampler2D ${uniform.name}; `;

} else if ( uniform.type === 'cubeTexture' ) {

snippet += `uniform samplerCube ${uniform.name}; `;
snippet = `samplerCube ${uniform.name}; `;

} else {

const vectorType = this.getVectorType( uniform.type );

snippet += `uniform ${vectorType} ${uniform.name}; `;
snippet = `${vectorType} ${uniform.name}; `;

}

const precision = uniform.node.precision;

if ( precision !== null ) {

snippet = 'uniform ' + precisionLib[ precision ] + ' ' + snippet;

} else {

snippet = 'uniform ' + snippet;

}

output += snippet;

}

return snippet;
return output;

}

Expand Down

0 comments on commit 6a9e961

Please sign in to comment.