From ee7fd772d453b011e503f617a751926a057db18e Mon Sep 17 00:00:00 2001 From: Jack Caron Date: Thu, 29 Jun 2017 10:27:50 -0400 Subject: [PATCH 01/10] proper order to convert the opacity --- lib/VRMatToPhysical.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/VRMatToPhysical.js b/lib/VRMatToPhysical.js index bb032a7..0b2eda9 100644 --- a/lib/VRMatToPhysical.js +++ b/lib/VRMatToPhysical.js @@ -368,6 +368,7 @@ var getPhysicalPropertiesFromBRDFVRayMtl = function( node ) { } } + // try using 'refract' to determine the opacity var refractColor = clampColor(getColorFromInput( node.inputs['refract'], false )); var refractMono = ( refractColor.r + refractColor.g + refractColor.b ) * 0.3333333; var opacity = 1.0 - refractMono; @@ -378,16 +379,16 @@ var getPhysicalPropertiesFromBRDFVRayMtl = function( node ) { properties['opacityMap']['invert'] = ! properties['opacityMap']['invert']; } - if( properties['opacityFactor'] === 1 ) { + // if that doesn't work, use the 'opacity' + if( ! properties['opacityMap'] ) { + properties['opacityMap'] = getImageFromInput( node.inputs['opacity'], false ); + } + else if( properties['opacityFactor'] === 1 ) { var opacityColor = clampColor(getColorFromInput( node.inputs['opacity'], false )); var opacityMono = ( opacityColor.r + opacityColor.g + opacityColor.b ) * 0.3333333; properties['opacityFactor'] = opacityMono; } - if( ! properties['opacityMap'] ) { - properties['opacityMap'] = getImageFromInput( node.inputs['opacity'], false ); - } - properties['roughness'] = clampColor(getColorFromInput( node.inputs['reflect_glossiness'], false )).r; if( properties['roughness'] ) { properties['roughness'] = Math.sqrt( 1 - properties['roughness'] ); From b7f28a924f1565e88d9f2c98c3405913ea2aea87 Mon Sep 17 00:00:00 2001 From: Jack Caron Date: Wed, 5 Jul 2017 15:40:15 -0400 Subject: [PATCH 02/10] blend colors equally for TexMix and TexNoiseMax --- lib/VRMatToPhysical.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/lib/VRMatToPhysical.js b/lib/VRMatToPhysical.js index 0b2eda9..147311e 100644 --- a/lib/VRMatToPhysical.js +++ b/lib/VRMatToPhysical.js @@ -208,6 +208,31 @@ var findColorInNode = function( node, isFalloff, optionalNodePath ) { } break; + case "TexNoiseMax": + case "TexMix": + var input, color; + input = node.inputs.color1; + if( input && input.isConnected() ) { + color = getColorFromInput(input, false, optionalNodePath); + if( color ) { + //console.log( "TexNoiseMax/TexMix color1: ", color ); + resultColor.r += color.r * 0.5; + resultColor.g += color.g * 0.5; + resultColor.b += color.b * 0.5; + } + } + input = node.inputs.color2; + if( input && input.isConnected() ) { + color = getColorFromInput(input, false, optionalNodePath); + if( color ) { + //console.log( "TexNoiseMax/TexMix color2: ", color ); + resultColor.r += color.r * 0.5; + resultColor.g += color.g * 0.5; + resultColor.b += color.b * 0.5; + } + } + break; + case "TexFalloff": var input = node.inputs[ isFalloff ? 'color2' : 'color1']; if( input && input.isConnected() ) { @@ -418,6 +443,10 @@ var getPhysicalPropertiesFromBRDFVRayMtl = function( node ) { baseColor.r += (1.0 - baseColor.r)*fresnel; baseColor.g += (1.0 - baseColor.g)*fresnel; baseColor.b += (1.0 - baseColor.b)*fresnel; + // var epsilonColor = 1.0/255.0; // because full metallic, it should reflect something + // baseColor.r -= fresnel*(baseColor.r - epsilonColor); + // baseColor.g -= fresnel*(baseColor.g - epsilonColor); + // baseColor.b -= fresnel*(baseColor.b - epsilonColor); } //console.log( "physicalProperties", properties ); From c14c3aa88dbf8e2a3178f4c0b538aa1d583f3389 Mon Sep 17 00:00:00 2001 From: Jack Caron Date: Tue, 11 Jul 2017 10:42:34 -0400 Subject: [PATCH 03/10] map fresnel to clear coat and reflect with falloff to metallic --- lib/VRMatToPhysical.js | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/lib/VRMatToPhysical.js b/lib/VRMatToPhysical.js index 147311e..7eec2f7 100644 --- a/lib/VRMatToPhysical.js +++ b/lib/VRMatToPhysical.js @@ -431,24 +431,12 @@ var getPhysicalPropertiesFromBRDFVRayMtl = function( node ) { // base color / metallic interpolation using fresnel // map [1:12.1] --> [0:1] var fresnel = (getColorFromInput(node.inputs['fresnel_ior'], false).r - 1.0) / 11.1; - fresnel = Math.min(1.0, Math.max(0.0, fresnel)); + properties['clearCoat'] = Math.min(1.0, Math.max(0.0, fresnel)); // set the metallic value - properties['metallic'] = fresnel; // because 1.0 for metallic is too much + properties['metallic'] = clampColor(getColorFromInput(node.inputs['reflect'], true)).r; properties['metallicMap'] = getImageFromInput( node.inputs['reflect'], false ); - // adjust the base color (if it's set) - if (!properties['baseMap']) { - var baseColor = properties['baseColor']; - baseColor.r += (1.0 - baseColor.r)*fresnel; - baseColor.g += (1.0 - baseColor.g)*fresnel; - baseColor.b += (1.0 - baseColor.b)*fresnel; - // var epsilonColor = 1.0/255.0; // because full metallic, it should reflect something - // baseColor.r -= fresnel*(baseColor.r - epsilonColor); - // baseColor.g -= fresnel*(baseColor.g - epsilonColor); - // baseColor.b -= fresnel*(baseColor.b - epsilonColor); - } - //console.log( "physicalProperties", properties ); return properties; }; From 8733a085ad722e543b8fe21c4c6f948be1946105 Mon Sep 17 00:00:00 2001 From: Jack Caron Date: Wed, 12 Jul 2017 09:05:35 -0400 Subject: [PATCH 04/10] proper use of reflect color for metallic --- lib/VRMatToPhysical.js | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/lib/VRMatToPhysical.js b/lib/VRMatToPhysical.js index 7eec2f7..13b0138 100644 --- a/lib/VRMatToPhysical.js +++ b/lib/VRMatToPhysical.js @@ -414,9 +414,9 @@ var getPhysicalPropertiesFromBRDFVRayMtl = function( node ) { properties['opacityFactor'] = opacityMono; } - properties['roughness'] = clampColor(getColorFromInput( node.inputs['reflect_glossiness'], false )).r; - if( properties['roughness'] ) { - properties['roughness'] = Math.sqrt( 1 - properties['roughness'] ); + var roughness = clampColor(getColorFromInput( node.inputs['reflect_glossiness'], false )).r; + if (roughness !== undefined) { + properties['roughness'] = 1.0 - Math.sqrt(roughness); } properties['roughnessMap'] = getImageFromInput( node.inputs['reflect_glossiness'], false ); if( properties['roughnessMap'] ) { @@ -428,15 +428,38 @@ var getPhysicalPropertiesFromBRDFVRayMtl = function( node ) { properties = _.extend( properties, bumpProperties ); } + // var usingFresnel = node.inputs["fresnel"].value; + var reflectColor = getColorFromInput(node.inputs['reflect'], true); + var baseColor = properties['baseColor']; + var reflectIntensity = null; + if (reflectColor) { + if (baseColor.r === 0.0 && baseColor.g === 0.0 && baseColor.b === 0.0) { + reflectIntensity = 1.0; + } + else { + reflectIntensity = Math.max(reflectColor.r,reflectColor.g,reflectColor.b); + } + } + else { + reflectIntensity = 0.0; + } + // base color / metallic interpolation using fresnel // map [1:12.1] --> [0:1] - var fresnel = (getColorFromInput(node.inputs['fresnel_ior'], false).r - 1.0) / 11.1; - properties['clearCoat'] = Math.min(1.0, Math.max(0.0, fresnel)); + // var fresnel = (getColorFromInput(node.inputs['fresnel_ior'], false).r - 1.0) / 11.1; + // fresnel = Math.min(1.0, Math.max(0.0, fresnel)); // set the metallic value - properties['metallic'] = clampColor(getColorFromInput(node.inputs['reflect'], true)).r; + properties['metallic'] = reflectIntensity; properties['metallicMap'] = getImageFromInput( node.inputs['reflect'], false ); + // adjust the base color (if it's set) + if (reflectIntensity && !properties['baseMap']) { + baseColor.r += (reflectColor.r - baseColor.r)*reflectIntensity; + baseColor.g += (reflectColor.g - baseColor.g)*reflectIntensity; + baseColor.b += (reflectColor.b - baseColor.b)*reflectIntensity; + } + //console.log( "physicalProperties", properties ); return properties; }; From b716ea0d1c865b66a2315fccc08803ce072a1fd9 Mon Sep 17 00:00:00 2001 From: Jack Caron Date: Wed, 12 Jul 2017 09:44:33 -0400 Subject: [PATCH 05/10] group fresnel comment together for future use --- lib/VRMatToPhysical.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/VRMatToPhysical.js b/lib/VRMatToPhysical.js index 13b0138..a2ba7d3 100644 --- a/lib/VRMatToPhysical.js +++ b/lib/VRMatToPhysical.js @@ -428,7 +428,6 @@ var getPhysicalPropertiesFromBRDFVRayMtl = function( node ) { properties = _.extend( properties, bumpProperties ); } - // var usingFresnel = node.inputs["fresnel"].value; var reflectColor = getColorFromInput(node.inputs['reflect'], true); var baseColor = properties['baseColor']; var reflectIntensity = null; @@ -446,6 +445,7 @@ var getPhysicalPropertiesFromBRDFVRayMtl = function( node ) { // base color / metallic interpolation using fresnel // map [1:12.1] --> [0:1] + // var usingFresnel = node.inputs["fresnel"].value; // var fresnel = (getColorFromInput(node.inputs['fresnel_ior'], false).r - 1.0) / 11.1; // fresnel = Math.min(1.0, Math.max(0.0, fresnel)); From 74a968ae54c991dd4c629cd3956ab49581b2223b Mon Sep 17 00:00:00 2001 From: Jack Caron Date: Wed, 12 Jul 2017 14:12:48 -0400 Subject: [PATCH 06/10] include the baseMap into the metallic conversion --- lib/VRMatToPhysical.js | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/VRMatToPhysical.js b/lib/VRMatToPhysical.js index a2ba7d3..1ca8544 100644 --- a/lib/VRMatToPhysical.js +++ b/lib/VRMatToPhysical.js @@ -428,19 +428,26 @@ var getPhysicalPropertiesFromBRDFVRayMtl = function( node ) { properties = _.extend( properties, bumpProperties ); } - var reflectColor = getColorFromInput(node.inputs['reflect'], true); - var baseColor = properties['baseColor']; - var reflectIntensity = null; - if (reflectColor) { - if (baseColor.r === 0.0 && baseColor.g === 0.0 && baseColor.b === 0.0) { + var reflectColor = getColorFromInput(node.inputs['reflect'], false); + var reflectMap = getImageFromInput(node.inputs['reflect'], false); + if (reflectMap) { + reflectColor.r = 1.0; + reflectColor.g = 1.0; + reflectColor.b = 1.0; + } + var reflectIntensity = 0.0; + var baseColor = properties['baseColor']; + + if (baseColor.r === 0.0 && baseColor.g === 0.0 && baseColor.b === 0.0 && !properties['baseMap']) { + if (reflectMap || reflectColor.r > 0.0 || reflectColor.g > 0.0 || reflectColor.b > 0.0) { reflectIntensity = 1.0; - } - else { - reflectIntensity = Math.max(reflectColor.r,reflectColor.g,reflectColor.b); + properties['baseColor'] = reflectColor; + properties['baseMap'] = reflectMap; + reflectMap = null; } } else { - reflectIntensity = 0.0; + reflectIntensity = Math.max(reflectColor.r, reflectColor.g, reflectColor.b); } // base color / metallic interpolation using fresnel @@ -451,14 +458,7 @@ var getPhysicalPropertiesFromBRDFVRayMtl = function( node ) { // set the metallic value properties['metallic'] = reflectIntensity; - properties['metallicMap'] = getImageFromInput( node.inputs['reflect'], false ); - - // adjust the base color (if it's set) - if (reflectIntensity && !properties['baseMap']) { - baseColor.r += (reflectColor.r - baseColor.r)*reflectIntensity; - baseColor.g += (reflectColor.g - baseColor.g)*reflectIntensity; - baseColor.b += (reflectColor.b - baseColor.b)*reflectIntensity; - } + properties['metallicMap'] = reflectMap; //console.log( "physicalProperties", properties ); return properties; From 252657a2b1e5daa9eac572ed2abd262a1286eff3 Mon Sep 17 00:00:00 2001 From: Jack Caron Date: Wed, 12 Jul 2017 14:40:30 -0400 Subject: [PATCH 07/10] check the baseMap first --- lib/VRMatToPhysical.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/VRMatToPhysical.js b/lib/VRMatToPhysical.js index 1ca8544..9b528f0 100644 --- a/lib/VRMatToPhysical.js +++ b/lib/VRMatToPhysical.js @@ -438,7 +438,7 @@ var getPhysicalPropertiesFromBRDFVRayMtl = function( node ) { var reflectIntensity = 0.0; var baseColor = properties['baseColor']; - if (baseColor.r === 0.0 && baseColor.g === 0.0 && baseColor.b === 0.0 && !properties['baseMap']) { + if (!properties['baseMap'] && baseColor.r === 0.0 && baseColor.g === 0.0 && baseColor.b === 0.0) { if (reflectMap || reflectColor.r > 0.0 || reflectColor.g > 0.0 || reflectColor.b > 0.0) { reflectIntensity = 1.0; properties['baseColor'] = reflectColor; From 8b16d5c660ff681aff7bf9b45821d485a7e45b70 Mon Sep 17 00:00:00 2001 From: Jack Caron Date: Thu, 13 Jul 2017 14:03:32 -0400 Subject: [PATCH 08/10] go in specular if not metallic --- lib/VRMatToPhysical.js | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/VRMatToPhysical.js b/lib/VRMatToPhysical.js index 9b528f0..c3e5848 100644 --- a/lib/VRMatToPhysical.js +++ b/lib/VRMatToPhysical.js @@ -435,19 +435,20 @@ var getPhysicalPropertiesFromBRDFVRayMtl = function( node ) { reflectColor.g = 1.0; reflectColor.b = 1.0; } - var reflectIntensity = 0.0; + // var reflectIntensity = 0.0; var baseColor = properties['baseColor']; - - if (!properties['baseMap'] && baseColor.r === 0.0 && baseColor.g === 0.0 && baseColor.b === 0.0) { - if (reflectMap || reflectColor.r > 0.0 || reflectColor.g > 0.0 || reflectColor.b > 0.0) { - reflectIntensity = 1.0; - properties['baseColor'] = reflectColor; - properties['baseMap'] = reflectMap; - reflectMap = null; - } + if (!properties['baseMap'] && baseColor.r === 0.0 && baseColor.g === 0.0 && baseColor.b === 0.0 && + (reflectMap || reflectColor.r > 0.0 || reflectColor.g > 0.0 || reflectColor.b > 0.0)) + { + properties['metallic' ] = 1.0; + properties['baseColor'] = reflectColor; + properties['baseMap' ] = reflectMap; + reflectMap = null; } else { - reflectIntensity = Math.max(reflectColor.r, reflectColor.g, reflectColor.b); + var reflectIntensity = Math.max(reflectColor.r, reflectColor.g, reflectColor.b) * 0.5; + properties['specularColor'] = { r: reflectIntensity, g: reflectIntensity, b: reflectIntensity }; + properties['specularMap' ] = reflectMap; } // base color / metallic interpolation using fresnel @@ -457,8 +458,8 @@ var getPhysicalPropertiesFromBRDFVRayMtl = function( node ) { // fresnel = Math.min(1.0, Math.max(0.0, fresnel)); // set the metallic value - properties['metallic'] = reflectIntensity; - properties['metallicMap'] = reflectMap; + //properties['metallic'] = reflectIntensity; + //properties['metallicMap'] = reflectMap; //console.log( "physicalProperties", properties ); return properties; From 174a572372410aaa339560731cfad77a1be7e3e7 Mon Sep 17 00:00:00 2001 From: Jack Caron Date: Thu, 13 Jul 2017 16:15:48 -0400 Subject: [PATCH 09/10] revert and keep metallic --- lib/VRMatToPhysical.js | 212 +++++++++++++++++------------------------ 1 file changed, 88 insertions(+), 124 deletions(-) diff --git a/lib/VRMatToPhysical.js b/lib/VRMatToPhysical.js index c3e5848..ee51021 100644 --- a/lib/VRMatToPhysical.js +++ b/lib/VRMatToPhysical.js @@ -144,111 +144,86 @@ var clampColor = function( color ) { }; } -var getColorFromInput = function( input, isFalloff, optionalNodePath ) { - var result = null; - if( input ) { - //console.log( 'getBaseColorFromInput: input', input.path() ); - if( input.isConnected() ) { - result = findColorInNode(input.value.node, isFalloff, optionalNodePath); - } +var getColorFromInput = function( input, isFalloff ) { + if( input ) { + //console.log( 'getBaseColorFromInput: input', input.path() ); + if( input.isConnected() ) { + return findColor( input.value.node, isFalloff ); + } - var colorValue = input.value; - if(( colorValue !== null )&&( colorValue !== undefined )&&( colorValue.r !== undefined )&&( colorValue.g !== undefined )&&( colorValue.b !== undefined )) { - result = colorValue; + var colorValue = input.value; + if(( colorValue !== null )&&( colorValue !== undefined )&&( colorValue.r !== undefined )&&( colorValue.g !== undefined )&&( colorValue.b !== undefined )) { + return colorValue; + } } - } - if (!result) { - result = { r: 1, g: 1, b: 1 }; - } - return result; + + return { r: 1, g: 1, b: 1 }; } -var findColorInNode = function( node, isFalloff, optionalNodePath ) { + + +var findColor = function( node, isFalloff, optionalNodePath ) { //console.log( 'findBaseColor: node', node.path() ); var resultColor = { r: 0, g: 0, b: 0 }; + if( ! node ) return resultColor; + if( optionalNodePath ) { optionalNodePath.push( node ); } + //console.log( 'findPrimaryBRDFVRayMtl', node.label, node.spec.name ); + + //console.log( 'findColor', node.spec.name ); switch( node.spec.name ) { case "TexCombineColor": - var multiplier = node.inputs['texture_multiplier'].value; - if (multiplier === undefined) multiplier = 1.0; - - var color = node.inputs['color']; - var texture = node.inputs['texture']; - - if (texture && texture.isConnected()) { - // it's a valid texture, let get its color - texture = getColorFromInput(texture, isFalloff, optionalNodePath); - resultColor.r = texture.r * multiplier; - resultColor.g = texture.g * multiplier; - resultColor.b = texture.b * multiplier; - - if (color && multiplier < 1) { - // interpolate - color = color.value; - var inv = 1.0 - multiplier; - resultColor.r += color.r * inv; - resultColor.g += color.g * inv; - resultColor.b += color.b * inv; - } - } - else { - if (color) { - color = color.value; - resultColor.r = color.r; - resultColor.g = color.g; - resultColor.b = color.b; - } - //else resultColor stays black - } - break; - - case "TexNoiseMax": - case "TexMix": - var input, color; - input = node.inputs.color1; + var input = node.inputs['texture']; if( input && input.isConnected() ) { - color = getColorFromInput(input, false, optionalNodePath); + var color = findColor( input.value.node, isFalloff, optionalNodePath ); if( color ) { - //console.log( "TexNoiseMax/TexMix color1: ", color ); - resultColor.r += color.r * 0.5; - resultColor.g += color.g * 0.5; - resultColor.b += color.b * 0.5; + //console.log( "TexCombineColor texture: ", color ); + resultColor.r += color.r; + resultColor.g += color.g; + resultColor.b += color.b; } } - input = node.inputs.color2; - if( input && input.isConnected() ) { - color = getColorFromInput(input, false, optionalNodePath); - if( color ) { - //console.log( "TexNoiseMax/TexMix color2: ", color ); - resultColor.r += color.r * 0.5; - resultColor.g += color.g * 0.5; - resultColor.b += color.b * 0.5; - } + //console.log( 'texture_multiplier', node.inputs['texture_multiplier'] ? node.inputs['texture_multiplier'].value : null ); + //console.log( 'result_multiplier', node.inputs['result_multiplier'] ? node.inputs['result_multiplier'].value : null ); + + var colorInput = node.inputs['color']; + var color = colorInput.value; + if( color ) { + //console.log( "TexCombineColor color: ", color ); + resultColor.r += color.r; + resultColor.g += color.g; + resultColor.b += color.b; } - break; + + //console.log( "TexCombineColor resultColor: ", resultColor ); + return clampColor( resultColor ); case "TexFalloff": var input = node.inputs[ isFalloff ? 'color2' : 'color1']; if( input && input.isConnected() ) { - var color = getColorFromInput(input, false, optionalNodePath); + var color = findColor( input.value.node, false, optionalNodePath ); if( color ) { //console.log( "TexFalloff color: ", color ); - resultColor.r = color.r; - resultColor.g = color.g; - resultColor.b = color.b; + resultColor.r += color.r; + resultColor.g += color.g; + resultColor.b += color.b; } } - break; + return clampColor( resultColor ); } + //console.log( "ERROR, can not traverse node in findBaseColor: " + node.label + " <" + node.spec.name + ">"); + //nodeTreeToString( node, '' ); return resultColor; } + + var findTexBitmap = function( node, isFalloff, optionalNodePath ) { //console.log( 'findBaseTexBitmap: node', node.path() ); @@ -358,19 +333,6 @@ var getBumpPropertiesFromNode = function( node ) { return null; } -/** - * Multiple cases for 'map' and 'color' properties that need to be matched together - */ -function mapColorProperties(properties, propMap, propColor, propSrc, node, isFalloff) { - properties[propMap] = getImageFromInput(node.inputs[propSrc], isFalloff); - if (properties[propMap]) { - properties[propColor] = { r:1, g:1, b:1 }; - } - else { - properties[propColor] = clampColor(getColorFromInput(node.inputs[propSrc], isFalloff)); - } -}; - var getPhysicalPropertiesFromBRDFVRayMtl = function( node ) { if( ! node ) return { 'baseColor': { r:1, g:1, b:0} @@ -379,22 +341,23 @@ var getPhysicalPropertiesFromBRDFVRayMtl = function( node ) { if( node.spec.name !== "BRDFVRayMtl" ) throw new Error( "unknown node type: " + node.spec.name ); var properties = {}; + properties['baseMap'] = getImageFromInput( node.inputs['diffuse'], false ); + if( properties['baseMap'] ) { + properties['baseColor'] = { r:1, g:1, b:1 }; + } + else { + properties['baseColor'] = getColorFromInput( node.inputs['diffuse'], false ); + } - mapColorProperties(properties, 'baseMap', 'baseColor', 'diffuse', node, false); - - properties['baseFalloff'] = true; - mapColorProperties(properties, 'baseFalloffMap', 'baseFalloffColor', 'diffuse', node, true); - - if( _.isEqual( properties['baseMap'], properties['baseFalloffMap'] ) ) { - delete properties['baseFalloffMap']; - if( _.isEqual( properties['baseColor'], properties['baseFalloffColor'] ) ) { - delete properties['baseFalloffColor']; - properties['baseFalloff'] = false; - } + properties['baseFalloffMap'] = getImageFromInput( node.inputs['diffuse'], true ); + if( properties['baseFalloffMap'] ) { + properties['baseFalloffColor'] = { r:1, g:1, b:1 }; + } + else { + properties['baseFalloffColor'] = getColorFromInput( node.inputs['diffuse'], true ); } - // try using 'refract' to determine the opacity - var refractColor = clampColor(getColorFromInput( node.inputs['refract'], false )); + var refractColor = getColorFromInput( node.inputs['refract'], false ); var refractMono = ( refractColor.r + refractColor.g + refractColor.b ) * 0.3333333; var opacity = 1.0 - refractMono; properties['opacityFactor'] = opacity; @@ -404,30 +367,34 @@ var getPhysicalPropertiesFromBRDFVRayMtl = function( node ) { properties['opacityMap']['invert'] = ! properties['opacityMap']['invert']; } - // if that doesn't work, use the 'opacity' + if( properties['opacityFactor'] === 1 ) { + var opacityColor = getColorFromInput( node.inputs['opacity'], false ); + var opacityMono = ( opacityColor.r + opacityColor.g + opacityColor.b ) * 0.3333333; + properties['opacityFactor'] = opacityMono; + } + if( ! properties['opacityMap'] ) { properties['opacityMap'] = getImageFromInput( node.inputs['opacity'], false ); } - else if( properties['opacityFactor'] === 1 ) { - var opacityColor = clampColor(getColorFromInput( node.inputs['opacity'], false )); - var opacityMono = ( opacityColor.r + opacityColor.g + opacityColor.b ) * 0.3333333; - properties['opacityFactor'] = opacityMono; + + properties['baseFalloff'] = true; + if( _.isEqual( properties['baseMap'], properties['baseFalloffMap'] ) ) { + delete properties['baseFalloffMap']; + if( _.isEqual( properties['baseColor'], properties['baseFalloffColor'] ) ) { + delete properties['baseFalloffColor']; + properties['baseFalloff'] = false; + } } - var roughness = clampColor(getColorFromInput( node.inputs['reflect_glossiness'], false )).r; - if (roughness !== undefined) { - properties['roughness'] = 1.0 - Math.sqrt(roughness); + properties['roughness'] = getColorFromInput( node.inputs['reflect_glossiness'], false ).r; + if( properties['roughness'] ) { + properties['roughness'] = Math.sqrt( 1 - properties['roughness'] ); } properties['roughnessMap'] = getImageFromInput( node.inputs['reflect_glossiness'], false ); if( properties['roughnessMap'] ) { properties['roughnessMap']['invert'] = true; } - var bumpProperties = getBumpPropertiesFromNode( node ); - if( bumpProperties ) { - properties = _.extend( properties, bumpProperties ); - } - var reflectColor = getColorFromInput(node.inputs['reflect'], false); var reflectMap = getImageFromInput(node.inputs['reflect'], false); if (reflectMap) { @@ -446,22 +413,19 @@ var getPhysicalPropertiesFromBRDFVRayMtl = function( node ) { reflectMap = null; } else { - var reflectIntensity = Math.max(reflectColor.r, reflectColor.g, reflectColor.b) * 0.5; - properties['specularColor'] = { r: reflectIntensity, g: reflectIntensity, b: reflectIntensity }; - properties['specularMap' ] = reflectMap; + // var reflectIntensity = Math.max(reflectColor.r, reflectColor.g, reflectColor.b) * 0.5; + // properties['specularColor'] = { r: reflectIntensity, g: reflectIntensity, b: reflectIntensity }; + // properties['specularMap' ] = reflectMap; } +//*/ - // base color / metallic interpolation using fresnel - // map [1:12.1] --> [0:1] - // var usingFresnel = node.inputs["fresnel"].value; - // var fresnel = (getColorFromInput(node.inputs['fresnel_ior'], false).r - 1.0) / 11.1; - // fresnel = Math.min(1.0, Math.max(0.0, fresnel)); - - // set the metallic value - //properties['metallic'] = reflectIntensity; - //properties['metallicMap'] = reflectMap; + var bumpProperties = getBumpPropertiesFromNode( node ); + if( bumpProperties ) { + properties = _.extend( properties, bumpProperties ); + } //console.log( "physicalProperties", properties ); + return properties; }; From e3b70e39a3e7b4b3cec577472a4d82819d5e50ea Mon Sep 17 00:00:00 2001 From: Ziyang Wang Date: Tue, 16 Jan 2018 12:30:36 -0500 Subject: [PATCH 10/10] Merge remote-tracking branch 'origin/vrmat-plastic' --- lib/VRMatToPhysical.js | 502 ++++++++++++++++++++++------------------- 1 file changed, 272 insertions(+), 230 deletions(-) diff --git a/lib/VRMatToPhysical.js b/lib/VRMatToPhysical.js index ee51021..347be90 100644 --- a/lib/VRMatToPhysical.js +++ b/lib/VRMatToPhysical.js @@ -1,192 +1,201 @@ var _ = require('underscore'); -var extractPhysical = function( varMatGraph ) { +var extractPhysical = function(varMatGraph) {}; -}; - -var getRootNodes = function( graph ) { +var getRootNodes = function(graph) { var rootNodes = []; - var collectRootNodes = function( nodeName ) { + var collectRootNodes = function(nodeName) { var node = graph.nodes[nodeName]; var connectedCount = 0; - _.forEach( Object.keys( node.outputs ), function( outputName ) { + _.forEach(Object.keys(node.outputs), function(outputName) { var output = node.outputs[outputName]; - if( output.isConnected() ) { - connectedCount ++; + if (output.isConnected()) { + connectedCount++; } //console.log( "output.isConnected()", output.isConnected(), node.path() ); - } ); + }); - if( connectedCount === 0 ) { - rootNodes.push( node ); + if (connectedCount === 0) { + rootNodes.push(node); } }; - _.forEach( Object.keys( graph.nodes ), collectRootNodes ); + _.forEach(Object.keys(graph.nodes), collectRootNodes); return rootNodes; }; -var getDiffuseColor = function( rootNode ) { - return 0; -} +var getDiffuseColor = function(rootNode) { + return 0; +}; -var nodeTreeToString = function( node, prefix ) { +var nodeTreeToString = function(node, prefix) { //console.log( node, prefix ); - console.log( prefix + node.label + " [" + node.spec.name + "]" ); + console.log(prefix + node.label + ' [' + node.spec.name + ']'); var prefix2 = prefix + ' '; - for( var inputName in node.inputs ) { + for (var inputName in node.inputs) { var input = node.inputs[inputName]; //console.log( input ); - var text = prefix2 + '.' + inputName + " <" + input.spec.type + "> = "; + var text = prefix2 + '.' + inputName + ' <' + input.spec.type + '> = '; var nodeValue = input.value; - if( nodeValue && nodeValue.node ) { - console.log( text ); - nodeTreeToString( nodeValue.node, prefix2 + ' ' ); - } - else { - console.log( text + JSON.stringify( nodeValue ) ); + if (nodeValue && nodeValue.node) { + console.log(text); + nodeTreeToString(nodeValue.node, prefix2 + ' '); + } else { + console.log(text + JSON.stringify(nodeValue)); } } -} +}; -var treeToString = function( graph ) { - var rootNodes = getRootNodes( graph ); - for( var i in rootNodes ) { - console.log( nodeTreeToString( rootNodes[i], '' ) ); +var treeToString = function(graph) { + var rootNodes = getRootNodes(graph); + for (var i in rootNodes) { + console.log(nodeTreeToString(rootNodes[i], '')); } +}; -} - -var collectNodeInputVariations = function( nodes, nodeInputVariations ) { +var collectNodeInputVariations = function(nodes, nodeInputVariations) { nodeInputVariations = nodeInputVariations || {}; - _.each( nodes, function( node ) { - var inputVariations = nodeInputVariations[ node.spec.name ] || {}; - nodeInputVariations[ node.spec.name ] = inputVariations; + _.each(nodes, function(node) { + var inputVariations = nodeInputVariations[node.spec.name] || {}; + nodeInputVariations[node.spec.name] = inputVariations; - _.each( node.inputs, function( input ) { + _.each(node.inputs, function(input) { var inputValue = input.value; - if( inputValue === undefined || inputValue === null ) { - inputValue = "" - } - var variations = inputVariations[ input.spec.name ] || {}; - inputVariations[ input.spec.name ] = variations; - if( inputValue.spec ) { - inputValue = "<" + inputValue.node.spec.name + ">"; + if (inputValue === undefined || inputValue === null) { + inputValue = ''; } - else { - inputValue = JSON.stringify( inputValue ); + var variations = inputVariations[input.spec.name] || {}; + inputVariations[input.spec.name] = variations; + if (inputValue.spec) { + inputValue = '<' + inputValue.node.spec.name + '>'; + } else { + inputValue = JSON.stringify(inputValue); } - variations[ inputValue ] = ( variations[ inputValue ] || 0 ) + 1; + variations[inputValue] = (variations[inputValue] || 0) + 1; }); }); return nodeInputVariations; -} - -var identifyVaryingNodeInputs = function( nodeInputVariations ) { - var varyingNodeInputs = {}; - _.each( nodeInputVariations, function( inputVariations, nodeSpecName ) { - _.each( inputVariations, function( variations, inputSpecName ) { - var numVariations = Object.keys( variations ).length; - if( numVariations > 1 ) { - var fullName = nodeSpecName + '.' + inputSpecName; - varyingNodeInputs[ fullName ] = variations; - } - }); +}; + +var identifyVaryingNodeInputs = function(nodeInputVariations) { + var varyingNodeInputs = {}; + _.each(nodeInputVariations, function(inputVariations, nodeSpecName) { + _.each(inputVariations, function(variations, inputSpecName) { + var numVariations = Object.keys(variations).length; + if (numVariations > 1) { + var fullName = nodeSpecName + '.' + inputSpecName; + varyingNodeInputs[fullName] = variations; + } }); + }); - return varyingNodeInputs; -} + return varyingNodeInputs; +}; -var findBaseBRDFVRayMtl = function( node, optionalNodePath ) { - if( ! node ) return null; +var findBaseBRDFVRayMtl = function(node, optionalNodePath) { + if (!node) return null; - if( optionalNodePath ) { - optionalNodePath.push( node ); + if (optionalNodePath) { + optionalNodePath.push(node); } //console.log( 'findPrimaryBRDFVRayMtl', node.label, node.spec.name ); - switch( node.spec.name ) { - - case "BRDFVRayMtl": + switch (node.spec.name) { + case 'BRDFVRayMtl': return node; - case "MtlSingleBRDF": + case 'MtlSingleBRDF': var input = node.inputs['brdf']; - if( input && input.isConnected() ) { + if (input && input.isConnected()) { //console.log( 'connected input', input ); - return findBaseBRDFVRayMtl( input.value.node, optionalNodePath ); + return findBaseBRDFVRayMtl(input.value.node, optionalNodePath); } break; - case "MtlASGVIS": + case 'MtlASGVIS': case 'BRDFLayered': - // get last brdf - for( var i = 10; i >= 0; i -- ) { + // get last brdf + for (var i = 10; i >= 0; i--) { var inputName = 'brdfs' + i; - var input = node.inputs[ inputName ]; - if( input && input.isConnected() ) { - return findBaseBRDFVRayMtl( input.value.node, optionalNodePath ) + var input = node.inputs[inputName]; + if (input && input.isConnected()) { + return findBaseBRDFVRayMtl(input.value.node, optionalNodePath); } } break; - } - console.log( "ERROR, can not traverse node in findBaseBRDFVRayMtl: " + node.label + " <" + node.spec.name + ">"); + console.log( + 'ERROR, can not traverse node in findBaseBRDFVRayMtl: ' + + node.label + + ' <' + + node.spec.name + + '>' + ); return null; -} +}; -var clampColor = function( color ) { +var clampColor = function(color) { return { - r: Math.max( Math.min( 1.0, color.r ), 0.0 ), - g: Math.max( Math.min( 1.0, color.g ), 0.0 ), - b: Math.max( Math.min( 1.0, color.b ), 0.0 ) + r: Math.max(Math.min(1.0, color.r), 0.0), + g: Math.max(Math.min(1.0, color.g), 0.0), + b: Math.max(Math.min(1.0, color.b), 0.0), }; -} - -var getColorFromInput = function( input, isFalloff ) { - if( input ) { - //console.log( 'getBaseColorFromInput: input', input.path() ); - if( input.isConnected() ) { - return findColor( input.value.node, isFalloff ); - } +}; - var colorValue = input.value; - if(( colorValue !== null )&&( colorValue !== undefined )&&( colorValue.r !== undefined )&&( colorValue.g !== undefined )&&( colorValue.b !== undefined )) { - return colorValue; - } +var getColorFromInput = function(input, isFalloff, ignoreTexture) { + if (input) { + //console.log( 'getBaseColorFromInput: input', input.path() ); + if (input.isConnected()) { + return findColor(input.value.node, isFalloff, null, ignoreTexture); } - return { r: 1, g: 1, b: 1 }; -} - + var colorValue = input.value; + if ( + colorValue !== null && + colorValue !== undefined && + colorValue.r !== undefined && + colorValue.g !== undefined && + colorValue.b !== undefined + ) { + return colorValue; + } + } + return { r: 1, g: 1, b: 1 }; +}; -var findColor = function( node, isFalloff, optionalNodePath ) { +var findColor = function(node, isFalloff, optionalNodePath, ignoreTexture) { //console.log( 'findBaseColor: node', node.path() ); var resultColor = { r: 0, g: 0, b: 0 }; - if( ! node ) return resultColor; + if (!node) return resultColor; - if( optionalNodePath ) { - optionalNodePath.push( node ); + if (optionalNodePath) { + optionalNodePath.push(node); } //console.log( 'findPrimaryBRDFVRayMtl', node.label, node.spec.name ); //console.log( 'findColor', node.spec.name ); - switch( node.spec.name ) { - - case "TexCombineColor": + switch (node.spec.name) { + case 'TexCombineColor': + var gotTexture = false; var input = node.inputs['texture']; - if( input && input.isConnected() ) { - var color = findColor( input.value.node, isFalloff, optionalNodePath ); - if( color ) { + if (input && input.isConnected()) { + var color = findColor( + input.value.node, + isFalloff, + optionalNodePath, + ignoreTexture + ); + if (color) { //console.log( "TexCombineColor texture: ", color ); resultColor.r += color.r; resultColor.g += color.g; resultColor.b += color.b; + gotTexture = true; } } //console.log( 'texture_multiplier', node.inputs['texture_multiplier'] ? node.inputs['texture_multiplier'].value : null ); @@ -194,7 +203,7 @@ var findColor = function( node, isFalloff, optionalNodePath ) { var colorInput = node.inputs['color']; var color = colorInput.value; - if( color ) { + if (color && (ignoreTexture || !gotTexture)) { //console.log( "TexCombineColor color: ", color ); resultColor.r += color.r; resultColor.g += color.g; @@ -202,89 +211,92 @@ var findColor = function( node, isFalloff, optionalNodePath ) { } //console.log( "TexCombineColor resultColor: ", resultColor ); - return clampColor( resultColor ); - - case "TexFalloff": - var input = node.inputs[ isFalloff ? 'color2' : 'color1']; - if( input && input.isConnected() ) { - var color = findColor( input.value.node, false, optionalNodePath ); - if( color ) { - //console.log( "TexFalloff color: ", color ); + return clampColor(resultColor); + + case 'TexFalloff': + var input = node.inputs[isFalloff ? 'color2' : 'color1']; + if (input && input.isConnected()) { + var color = findColor( + input.value.node, + false, + optionalNodePath, + ignoreTexture + ); + if (color) { + //console.log( "TexFalloff color: ", color ); resultColor.r += color.r; resultColor.g += color.g; resultColor.b += color.b; } } - return clampColor( resultColor ); + return clampColor(resultColor); } //console.log( "ERROR, can not traverse node in findBaseColor: " + node.label + " <" + node.spec.name + ">"); //nodeTreeToString( node, '' ); return resultColor; -} - - +}; -var findTexBitmap = function( node, isFalloff, optionalNodePath ) { +var findTexBitmap = function(node, isFalloff, optionalNodePath) { //console.log( 'findBaseTexBitmap: node', node.path() ); - if( ! node ) return null; + if (!node) return null; - if( optionalNodePath ) { - optionalNodePath.push( node ); + if (optionalNodePath) { + optionalNodePath.push(node); } //console.log( 'findPrimaryBRDFVRayMtl', node.label, node.spec.name ); - switch( node.spec.name ) { - - case "TexBitmap": + switch (node.spec.name) { + case 'TexBitmap': return node; - case "TexCombineColor": + case 'TexCombineColor': var input = node.inputs['texture']; - if( input && input.isConnected() ) { + if (input && input.isConnected()) { //console.log( 'connected input', input ); - return findTexBitmap( input.value.node, isFalloff, optionalNodePath ); + return findTexBitmap(input.value.node, isFalloff, optionalNodePath); } break; - case "TexFalloff": - var input = node.inputs[ isFalloff ? 'color2' : 'color1']; - if( input && input.isConnected() ) { + case 'TexFalloff': + var input = node.inputs[isFalloff ? 'color2' : 'color1']; + if (input && input.isConnected()) { //console.log( 'connected input', input ); - return findTexBitmap( input.value.node, false, optionalNodePath ); + return findTexBitmap(input.value.node, false, optionalNodePath); } break; } //console.log( "ERROR, can not traverse node in findBaseTexBitmap: " + node.label + " <" + node.spec.name + ">"); //nodeTreeToString( node, '' ); return null; -} +}; -var getImagePropertiesFromTexBitmap = function( node ) { - if( ! node ) return null; - if( node.spec.name !== "TexBitmap" ) throw new Error( "unknown node type: " + node.spec.name ); +var getImagePropertiesFromTexBitmap = function(node) { + if (!node) return null; + if (node.spec.name !== 'TexBitmap') + throw new Error('unknown node type: ' + node.spec.name); var properties = {}; var uvwGen = node.inputs['uvwgen'].value.node; - if( uvwGen ) { + if (uvwGen) { properties['wrap'] = { - x: ( uvwGen.inputs['wrap_u'].value === 1 ), - y: ( uvwGen.inputs['wrap_v'].value === 1 ) + x: uvwGen.inputs['wrap_u'].value === 1, + y: uvwGen.inputs['wrap_v'].value === 1, }; - var safeInverse = function( a ) { - if( a ) return 1.0 / a; + var safeInverse = function(a) { + if (a) return 1.0 / a; return 1.0; - } + }; var transformComponents = uvwGen.inputs['uvw_transform'].value; - if( transformComponents ) { + if (transformComponents) { properties['repeat'] = { - x: parseFloat( transformComponents.scale.x ), - y: parseFloat( transformComponents.scale.y ) + x: parseFloat(transformComponents.scale.x), + y: parseFloat(transformComponents.scale.y), }; properties['offset'] = { - x: parseFloat( transformComponents.translation.x ), - y: parseFloat( transformComponents.translation.y ) + x: parseFloat(transformComponents.translation.x), + y: parseFloat(transformComponents.translation.y), }; } } @@ -292,111 +304,136 @@ var getImagePropertiesFromTexBitmap = function( node ) { properties['invert'] = false; var bitmapInput = node.inputs['bitmap']; - if( bitmapInput.isConnected() ) { + if (bitmapInput.isConnected()) { var bitmapBuffer = bitmapInput.value.node; - if( bitmapBuffer ) { + if (bitmapBuffer) { var fileName = bitmapBuffer.inputs['file'].value; - if( fileName ) { + if (fileName) { properties['fileName'] = fileName; } } } return properties; -} +}; -var getImageFromInput = function( input, isFalloff ) { +var getImageFromInput = function(input, isFalloff) { //console.log( 'getImageFromInput: input', input.path() ); - if( ! input || ! input.value ) return null; - if( ! input.isConnected() ) return null; - var bitmapTex = findTexBitmap( input.value.node, isFalloff ); - if( ! bitmapTex ) return null; - return getImagePropertiesFromTexBitmap( bitmapTex ); -} - -var getBumpPropertiesFromNode = function( node ) { + if (!input || !input.value) return null; + if (!input.isConnected()) return null; + var bitmapTex = findTexBitmap(input.value.node, isFalloff); + if (!bitmapTex) return null; + return getImagePropertiesFromTexBitmap(bitmapTex); +}; - var bumpOnInput = node.inputs[ 'bump_on' ]; - if( bumpOnInput && bumpOnInput.value ) { +var getBumpPropertiesFromNode = function(node) { + var bumpOnInput = node.inputs['bump_on']; + if (bumpOnInput && bumpOnInput.value) { var properties = {}; var bumpScale = node.inputs['bump_tex_mult']; properties['bumpScale'] = bumpScale.value; - properties['bumpMap'] = getImageFromInput( node.inputs['bump_tex'] ); + properties['bumpMap'] = getImageFromInput(node.inputs['bump_tex']); return properties; } var connectedInputs = node.outputs['default'].connectedInputs; - for( var uuid in connectedInputs ) { + for (var uuid in connectedInputs) { var input = connectedInputs[uuid]; - return getBumpPropertiesFromNode( input.node ); + return getBumpPropertiesFromNode(input.node); } return null; -} +}; -var getPhysicalPropertiesFromBRDFVRayMtl = function( node ) { - if( ! node ) return { - 'baseColor': { r:1, g:1, b:0} - } +var gamma = 1.0 / 2.2; +var gammaCorrection = function(color) { + return { + r: Math.pow(color.r, gamma), + g: Math.pow(color.g, gamma), + b: Math.pow(color.b, gamma), + }; +}; + +var getPhysicalPropertiesFromBRDFVRayMtl = function(node) { + if (!node) + return { + baseColor: { r: 1, g: 1, b: 0 }, + }; - if( node.spec.name !== "BRDFVRayMtl" ) throw new Error( "unknown node type: " + node.spec.name ); + if (node.spec.name !== 'BRDFVRayMtl') + throw new Error('unknown node type: ' + node.spec.name); var properties = {}; - properties['baseMap'] = getImageFromInput( node.inputs['diffuse'], false ); - if( properties['baseMap'] ) { - properties['baseColor'] = { r:1, g:1, b:1 }; - } - else { - properties['baseColor'] = getColorFromInput( node.inputs['diffuse'], false ); + properties['baseMap'] = getImageFromInput(node.inputs['diffuse'], false); + if (properties['baseMap']) { + properties['baseColor'] = { r: 1, g: 1, b: 1 }; + } else { + properties['baseColor'] = gammaCorrection( + getColorFromInput(node.inputs['diffuse'], false) + ); } - properties['baseFalloffMap'] = getImageFromInput( node.inputs['diffuse'], true ); - if( properties['baseFalloffMap'] ) { - properties['baseFalloffColor'] = { r:1, g:1, b:1 }; - } - else { - properties['baseFalloffColor'] = getColorFromInput( node.inputs['diffuse'], true ); + properties['baseFalloffMap'] = getImageFromInput( + node.inputs['diffuse'], + true + ); + if (properties['baseFalloffMap']) { + properties['baseFalloffColor'] = { r: 1, g: 1, b: 1 }; + } else { + properties['baseFalloffColor'] = gammaCorrection( + getColorFromInput(node.inputs['diffuse'], true) + ); } - var refractColor = getColorFromInput( node.inputs['refract'], false ); - var refractMono = ( refractColor.r + refractColor.g + refractColor.b ) * 0.3333333; + var refractColor = getColorFromInput(node.inputs['refract'], false); + var refractMono = + (refractColor.r + refractColor.g + refractColor.b) * 0.3333333; var opacity = 1.0 - refractMono; properties['opacityFactor'] = opacity; - properties['opacityMap'] = getImageFromInput( node.inputs['refract'], false ); - if( properties['opacityMap'] ) { - properties['opacityMap']['invert'] = ! properties['opacityMap']['invert']; + properties['opacityMap'] = getImageFromInput(node.inputs['refract'], false); + if (properties['opacityMap']) { + properties['opacityMap']['invert'] = !properties['opacityMap']['invert']; } - if( properties['opacityFactor'] === 1 ) { - var opacityColor = getColorFromInput( node.inputs['opacity'], false ); - var opacityMono = ( opacityColor.r + opacityColor.g + opacityColor.b ) * 0.3333333; + if (properties['opacityFactor'] === 1.0) { + var opacityColor = getColorFromInput(node.inputs['opacity'], false, true); // we want the real factor, even if there's a texture + var opacityMono = + (opacityColor.r + opacityColor.g + opacityColor.b) * 0.3333333; properties['opacityFactor'] = opacityMono; } - if( ! properties['opacityMap'] ) { - properties['opacityMap'] = getImageFromInput( node.inputs['opacity'], false ); + if (!properties['opacityMap']) { + properties['opacityMap'] = getImageFromInput(node.inputs['opacity'], false); } properties['baseFalloff'] = true; - if( _.isEqual( properties['baseMap'], properties['baseFalloffMap'] ) ) { + if (_.isEqual(properties['baseMap'], properties['baseFalloffMap'])) { delete properties['baseFalloffMap']; - if( _.isEqual( properties['baseColor'], properties['baseFalloffColor'] ) ) { + if (_.isEqual(properties['baseColor'], properties['baseFalloffColor'])) { delete properties['baseFalloffColor']; properties['baseFalloff'] = false; } } - properties['roughness'] = getColorFromInput( node.inputs['reflect_glossiness'], false ).r; - if( properties['roughness'] ) { - properties['roughness'] = Math.sqrt( 1 - properties['roughness'] ); + properties['roughness'] = getColorFromInput( + node.inputs['reflect_glossiness'], + false + ).r; + if (properties['roughness'] !== undefined) { + properties['roughness'] = Math.sqrt(1 - properties['roughness']); } - properties['roughnessMap'] = getImageFromInput( node.inputs['reflect_glossiness'], false ); - if( properties['roughnessMap'] ) { + properties['roughnessMap'] = getImageFromInput( + node.inputs['reflect_glossiness'], + false + ); + if (properties['roughnessMap']) { properties['roughnessMap']['invert'] = true; } - var reflectColor = getColorFromInput(node.inputs['reflect'], false); - var reflectMap = getImageFromInput(node.inputs['reflect'], false); + var reflectColor = gammaCorrection( + getColorFromInput(node.inputs['reflect'], false) + ); + var reflectMap = getImageFromInput(node.inputs['reflect'], false); if (reflectMap) { reflectColor.r = 1.0; reflectColor.g = 1.0; @@ -404,24 +441,30 @@ var getPhysicalPropertiesFromBRDFVRayMtl = function( node ) { } // var reflectIntensity = 0.0; var baseColor = properties['baseColor']; - if (!properties['baseMap'] && baseColor.r === 0.0 && baseColor.g === 0.0 && baseColor.b === 0.0 && - (reflectMap || reflectColor.r > 0.0 || reflectColor.g > 0.0 || reflectColor.b > 0.0)) - { - properties['metallic' ] = 1.0; + if ( + !properties['baseMap'] && + baseColor.r === 0.0 && + baseColor.g === 0.0 && + baseColor.b === 0.0 && + (reflectMap || + reflectColor.r > 0.0 || + reflectColor.g > 0.0 || + reflectColor.b > 0.0) + ) { + properties['metallic'] = 1.0; properties['baseColor'] = reflectColor; - properties['baseMap' ] = reflectMap; + properties['baseMap'] = reflectMap; reflectMap = null; - } - else { + } else { // var reflectIntensity = Math.max(reflectColor.r, reflectColor.g, reflectColor.b) * 0.5; // properties['specularColor'] = { r: reflectIntensity, g: reflectIntensity, b: reflectIntensity }; // properties['specularMap' ] = reflectMap; } -//*/ + //*/ - var bumpProperties = getBumpPropertiesFromNode( node ); - if( bumpProperties ) { - properties = _.extend( properties, bumpProperties ); + var bumpProperties = getBumpPropertiesFromNode(node); + if (bumpProperties) { + properties = _.extend(properties, bumpProperties); } //console.log( "physicalProperties", properties ); @@ -429,33 +472,32 @@ var getPhysicalPropertiesFromBRDFVRayMtl = function( node ) { return properties; }; -var getPhysicalPropertiesFromVRMat = function( vrMat ) { - var rootNodes = getRootNodes( vrMat ); - if( rootNodes.length !== 1) { +var getPhysicalPropertiesFromVRMat = function(vrMat) { + var rootNodes = getRootNodes(vrMat); + if (rootNodes.length !== 1) { var bestRootNode = null; //console.log( "num of rootNodes: ", rootNodes.length, " num of total nodes:", Object.keys( vrMat.nodes ).length ); - for( var i = 0; i < rootNodes.length; i ++ ) { + for (var i = 0; i < rootNodes.length; i++) { //console.log( 'rootNode', rootNodes[i].label, rootNodes[i].spec.type, rootNodes[i].spec.name, rootNodes[i].spec.outputs.default.type ); - if( rootNodes[i].spec.outputs.default.type === "material" ) { + if (rootNodes[i].spec.outputs.default.type === 'material') { bestRootNode = rootNodes[i]; } } - if( bestRootNode ) { - rootNodes = [ bestRootNode ]; - } - else { + if (bestRootNode) { + rootNodes = [bestRootNode]; + } else { return { - 'baseColor': { r:1, g:0, b:0} + baseColor: { r: 1, g: 0, b: 0 }, }; } } - var baseBRDFVRayMtl = findBaseBRDFVRayMtl( rootNodes[0] ); + var baseBRDFVRayMtl = findBaseBRDFVRayMtl(rootNodes[0]); //nodeTreeToString( baseBRDFVRayMtl, '' ); //console.log( 'primary BRDF VRay Mtl: ' + baseBRDFVRayMtl.path() + " <" + baseBRDFVRayMtl.spec.name + ">"); - return getPhysicalPropertiesFromBRDFVRayMtl( baseBRDFVRayMtl ); -} + return getPhysicalPropertiesFromBRDFVRayMtl(baseBRDFVRayMtl); +}; module.exports = { extractPhysical: extractPhysical,