Prechádzať zdrojové kódy

new color space interface

sunag 6 rokov pred
rodič
commit
3cc2cb487c

+ 17 - 5
examples/js/nodes/inputs/CubeTextureNode.js

@@ -5,6 +5,7 @@
 import { InputNode } from '../core/InputNode.js';
 import { ReflectNode } from '../accessors/ReflectNode.js';
 import { ColorSpaceNode } from '../utils/ColorSpaceNode.js';
+import { ExpressionNode } from '../core/ExpressionNode.js';
 
 function CubeTextureNode( value, uv, bias ) {
 
@@ -49,16 +50,27 @@ CubeTextureNode.prototype.generate = function ( builder, output ) {
 	if ( bias ) code = 'texCubeBias( ' + cubetex + ', ' + uv + ', ' + bias + ' )';
 	else code = 'texCube( ' + cubetex + ', ' + uv + ' )';
 
-	// add this context to replace ColorSpaceNode.input to code
+	// add a custom context for fix incompatibility with the core
+	// include ColorSpace function only for vertex shader (in fragment shader color space functions is added automatically by core)
+	// this should be removed in the future
+	// context.include =: is used to include or not functions if used FunctionNode
+	// context.ignoreCache =: not create variables nodeT0..9 to optimize the code
+	var context = { include: builder.isShader( 'vertex' ), ignoreCache: true };
+	var outputType = this.getType( builder );
 
-	builder.addContext( { input: code, encoding: builder.getTextureEncodingFromMap( this.value ), include: builder.isShader( 'vertex' ) } );
+	builder.addContext( context );
 
-	this.colorSpace = this.colorSpace || new ColorSpaceNode( this );
-	code = this.colorSpace.build( builder, this.type );
+	this.colorSpace = this.colorSpace || new ColorSpaceNode( new ExpressionNode('', outputType ) );
+	this.colorSpace.fromEncoding( builder.getTextureEncodingFromMap( this.value ) );
+	this.colorSpace.input.parse( code );
+
+	code = this.colorSpace.build( builder, outputType );
+
+	// end custom context
 
 	builder.removeContext();
 
-	return builder.format( code, this.type, output );
+	return builder.format( code, outputType, output );
 
 };
 

+ 18 - 6
examples/js/nodes/inputs/TextureNode.js

@@ -5,6 +5,7 @@
 import { InputNode } from '../core/InputNode.js';
 import { UVNode } from '../accessors/UVNode.js';
 import { ColorSpaceNode } from '../utils/ColorSpaceNode.js';
+import { ExpressionNode } from '../core/ExpressionNode.js';
 
 function TextureNode( value, uv, bias, project ) {
 
@@ -39,7 +40,7 @@ TextureNode.prototype.generate = function ( builder, output ) {
 		uv = this.uv.build( builder, this.project ? 'v4' : 'v2' ),
 		bias = this.bias ? this.bias.build( builder, 'f' ) : undefined;
 
-	if ( bias == undefined && builder.context.bias ) {
+	if ( bias === undefined && builder.context.bias ) {
 
 		bias = new builder.context.bias( this ).build( builder, 'f' );
 
@@ -53,16 +54,27 @@ TextureNode.prototype.generate = function ( builder, output ) {
 	if ( bias ) code = method + '( ' + tex + ', ' + uv + ', ' + bias + ' )';
 	else code = method + '( ' + tex + ', ' + uv + ' )';
 
-	// add this context to replace ColorSpaceNode.input to code
+	// add a custom context for fix incompatibility with the core
+	// include ColorSpace function only for vertex shader (in fragment shader color space functions is added automatically by core)
+	// this should be removed in the future
+	// context.include is used to include or not functions if used FunctionNode
+	// context.ignoreCache =: not create variables nodeT0..9 to optimize the code
+	var context = { include: builder.isShader( 'vertex' ), ignoreCache: true };
+	var outputType = this.getType( builder );
 
-	builder.addContext( { input: code, encoding: builder.getTextureEncodingFromMap( this.value ), include: builder.isShader( 'vertex' ) } );
+	builder.addContext( context );
 
-	this.colorSpace = this.colorSpace || new ColorSpaceNode( this );
-	code = this.colorSpace.build( builder, this.type );
+	this.colorSpace = this.colorSpace || new ColorSpaceNode( new ExpressionNode('', outputType ) );
+	this.colorSpace.fromEncoding( builder.getTextureEncodingFromMap( this.value ) );
+	this.colorSpace.input.parse( code );
+
+	code = this.colorSpace.build( builder, outputType );
+
+	// end custom context
 
 	builder.removeContext();
 
-	return builder.format( code, this.type, output );
+	return builder.format( code, outputType, output );
 
 };
 

+ 16 - 11
examples/js/nodes/misc/TextureCubeNode.js

@@ -24,7 +24,7 @@ TextureCubeNode.prototype = Object.create( TempNode.prototype );
 TextureCubeNode.prototype.constructor = TextureCubeNode;
 TextureCubeNode.prototype.nodeType = "TextureCube";
 
-TextureCubeNode.prototype.generateTextureCubeUV = function ( builder, cache, t ) {
+TextureCubeNode.prototype.generateTextureCubeUV = function ( builder, cache ) {
 
 	var uv_10 = cache.uv.build( builder ) + '.uv_10',
 		uv_20 = cache.uv.build( builder ) + '.uv_20',
@@ -34,26 +34,31 @@ TextureCubeNode.prototype.generateTextureCubeUV = function ( builder, cache, t )
 		color20 = 'texture2D( ' + this.value.build( builder, 'sampler2D' ) + ', ' + uv_20 + ' )';
 
 	// add a custom context for fix incompatibility with the core
-	// include ColorSpace function only if is a vertex shader
-	// for optimization this should be removed in the future
-	var context = { include: builder.isShader( 'vertex' ) };
+	// include ColorSpace function only for vertex shader (in fragment shader color space functions is added automatically by core)
+	// this should be removed in the future
+	// context.include =: is used to include or not functions if used FunctionNode
+	// context.ignoreCache =: not create variables nodeT0..9 to optimize the code
+	var context = { include: builder.isShader( 'vertex' ), ignoreCache: true };
+	var outputType = this.getType( builder );
 
 	builder.addContext( context );
 
-	cache.colorSpace10 = cache.colorSpace10 || new ColorSpaceNode( new ExpressionNode('', this.type ) );
-	cache.colorSpace10.input.eval( color10 );
+	cache.colorSpace10 = cache.colorSpace10 || new ColorSpaceNode( new ExpressionNode('', outputType ) );
 	cache.colorSpace10.fromDecoding( builder.getTextureEncodingFromMap( this.value.value ) );
-	color10 = cache.colorSpace10.build( builder, this.type );
+	cache.colorSpace10.input.parse( color10 );
 
-	cache.colorSpace20 = cache.colorSpace20 || new ColorSpaceNode( new ExpressionNode('', this.type ) );
-	cache.colorSpace20.input.eval( color20 );
+	color10 = cache.colorSpace10.build( builder, outputType );
+
+	cache.colorSpace20 = cache.colorSpace20 || new ColorSpaceNode( new ExpressionNode('', outputType ) );
 	cache.colorSpace20.fromDecoding( builder.getTextureEncodingFromMap( this.value.value ) );
-	color20 = cache.colorSpace20.build( builder, this.type );
+	cache.colorSpace20.input.parse( color20 );
 
-	builder.removeContext();
+	color20 = cache.colorSpace20.build( builder, outputType );
 
 	// end custom context
 
+	builder.removeContext();
+
 	return 'mix( ' + color10 + ', ' + color20 + ', ' + t + ' ).rgb';
 
 };

+ 40 - 34
examples/js/nodes/utils/ColorSpaceNode.js

@@ -203,6 +203,29 @@ ColorSpaceNode.LINEAR_TO_RGBD = 'LinearToRGBD';
 ColorSpaceNode.LINEAR_TO_LOG_LUV = 'LinearToLogLuv';
 ColorSpaceNode.LOG_LUV_TO_LINEAR = 'LogLuvToLinear';
 
+ColorSpaceNode.getEncodingComponents = function ( encoding ) {
+
+	switch ( encoding ) {
+
+		case THREE.LinearEncoding:
+			return [ 'Linear' ];
+		case THREE.sRGBEncoding:
+			return [ 'sRGB' ];
+		case THREE.RGBEEncoding:
+			return [ 'RGBE' ];
+		case THREE.RGBM7Encoding:
+			return [ 'RGBM', new FloatNode( 7.0 ).setReadonly( true ) ];
+		case THREE.RGBM16Encoding:
+			return [ 'RGBM', new FloatNode( 16.0 ).setReadonly( true ) ];
+		case THREE.RGBDEncoding:
+			return [ 'RGBD', new FloatNode( 256.0 ).setReadonly( true ) ];
+		case THREE.GammaEncoding:
+			return [ 'Gamma', new ExpressionNode( 'float( GAMMA_FACTOR )' ) ];
+
+	}
+
+};
+
 ColorSpaceNode.prototype = Object.create( TempNode.prototype );
 ColorSpaceNode.prototype.constructor = ColorSpaceNode;
 ColorSpaceNode.prototype.nodeType = "ColorSpace";
@@ -210,53 +233,38 @@ ColorSpaceNode.prototype.nodeType = "ColorSpace";
 ColorSpaceNode.prototype.generate = function ( builder, output ) {
 
 	var input = this.input.build( builder, 'v4' );
-	var method = [ this.method ];
-	var factor = this.factor ? this.factor.build( builder, 'f' ) : method[ 1 ];
-
-	method = builder.include( ColorSpaceNode.Nodes[ method[ 0 ] ] );
 
-	if ( factor ) {
+	if ( this.method === ColorSpaceNode.LINEAR_TO_LINEAR ) {
 
-		return builder.format( method + '( ' + input + ', ' + factor + ' )', this.getType( builder ), output );
+		return builder.format( input, this.getType( builder ), output );
 
 	} else {
 
-		return builder.format( method + '( ' + input + ' )', this.getType( builder ), output );
+		var method = [ this.method ];
+		var factor = this.factor ? this.factor.build( builder, 'f' ) : method[ 1 ];
 
-	}
+		method = builder.include( ColorSpaceNode.Nodes[ method[ 0 ] ] );
 
-};
+		if ( factor ) {
 
-ColorSpaceNode.getEncodingMethodFromEncoding = function ( encoding ) {
+			return builder.format( method + '( ' + input + ', ' + factor + ' )', this.getType( builder ), output );
 
-	var components = this.getEncodingComponents( encoding );
+		} else {
 
-	components[ 0 ] = 'LinearTo' + components[ 0 ];
+			return builder.format( method + '( ' + input + ' )', this.getType( builder ), output );
 
-	return components;
+		}
 
-};
+	}
 
-ColorSpaceNode.getEncodingComponents = function ( encoding ) {
+};
 
-	switch ( encoding ) {
+ColorSpaceNode.prototype.fromEncoding = function ( encoding ) {
 
-		case THREE.LinearEncoding:
-			return [ 'Linear' ];
-		case THREE.sRGBEncoding:
-			return [ 'sRGB' ];
-		case THREE.RGBEEncoding:
-			return [ 'RGBE' ];
-		case THREE.RGBM7Encoding:
-			return [ 'RGBM', new FloatNode( 7.0 ).setReadonly( true ) ];
-		case THREE.RGBM16Encoding:
-			return [ 'RGBM', new FloatNode( 16.0 ).setReadonly( true ) ];
-		case THREE.RGBDEncoding:
-			return [ 'RGBD', new FloatNode( 256.0 ).setReadonly( true ) ];
-		case THREE.GammaEncoding:
-			return [ 'Gamma', new ExpressionNode( 'float( GAMMA_FACTOR )' ) ];
+	var components = ColorSpaceNode.getEncodingComponents( encoding );
 
-	}
+	this.method = 'LinearTo' + components[ 0 ];
+	this.factor = components[ 1 ];
 
 };
 
@@ -264,9 +272,7 @@ ColorSpaceNode.prototype.fromDecoding = function ( encoding ) {
 
 	var components = ColorSpaceNode.getEncodingComponents( encoding );
 
-	components[ 0 ] += 'ToLinear';
-
-	this.method = components[ 0 ];
+	this.method = components[ 0 ] + 'ToLinear';
 	this.factor = components[ 1 ];
 
 };