Browse Source

InputNode: Added .setPrecision() (#25561)

* InputNode: Added .setPrecision()

* InputNode: Default precision is null.
sunag 2 years ago
parent
commit
6a9e96187e

+ 13 - 0
examples/jsm/nodes/core/InputNode.js

@@ -10,6 +10,7 @@ class InputNode extends Node {
 		this.isInputNode = true;
 
 		this.value = value;
+		this.precision = null;
 
 	}
 
@@ -31,6 +32,14 @@ class InputNode extends Node {
 
 	}
 
+	setPrecision( precision ) {
+
+		this.precision = precision;
+
+		return this;
+
+	}
+
 	serialize( data ) {
 
 		super.serialize( data );
@@ -42,6 +51,8 @@ class InputNode extends Node {
 		data.valueType = getValueType( this.value );
 		data.nodeType = this.nodeType;
 
+		data.precision = this.precision;
+
 	}
 
 	deserialize( data ) {
@@ -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 );
 
 	}

+ 27 - 5
examples/jsm/renderers/webgl/nodes/WebGLNodeBuilder.js

@@ -17,6 +17,12 @@ const glslMethods = {
 	[ MathNode.ATAN2 ]: 'atan'
 };
 
+const precisionLib = {
+	low: 'lowp',
+	medium: 'mediump',
+	high: 'highp'
+};
+
 function getIncludeSnippet( name ) {
 
 	return `#include <${name}>`;
@@ -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;
 
 	}