提交 f92f3607 编写于 作者: A alteredq

Added normal maps support to Blender scene exporter and SceneLoader.

Blender normal map support is work-in-progress:
 - for individual models normal map shader will need to be refactored to use standard scene lights
 - would be nice to handle also bump maps (a lot of Blender assets do not use "use_normal_map" flag on tangent space normal texture but instead use "use_map_normal" flag on texture slot of grayscale heightmap texture)

Also some whitespace cleanup.
上级 3f2c37af
因为 它太大了无法显示 source diff 。你可以改为 查看blob
此差异已折叠。
......@@ -165,12 +165,12 @@ default_vertex:"#ifndef USE_MORPHTARGETS\n#ifndef USE_SKINNING\ngl_Position = pr
THREE.UniformsLib={common:{diffuse:{type:"c",value:new THREE.Color(15658734)},opacity:{type:"f",value:1},map:{type:"t",value:0,texture:null},offsetRepeat:{type:"v4",value:new THREE.Vector4(0,0,1,1)},lightMap:{type:"t",value:2,texture:null},envMap:{type:"t",value:1,texture:null},useRefract:{type:"i",value:0},reflectivity:{type:"f",value:1},refractionRatio:{type:"f",value:0.98},combine:{type:"i",value:0},fogDensity:{type:"f",value:2.5E-4},fogNear:{type:"f",value:1},fogFar:{type:"f",value:2E3},fogColor:{type:"c",
value:new THREE.Color(16777215)},morphTargetInfluences:{type:"f",value:0}},lights:{enableLighting:{type:"i",value:1},ambientLightColor:{type:"fv",value:[]},directionalLightDirection:{type:"fv",value:[]},directionalLightColor:{type:"fv",value:[]},pointLightColor:{type:"fv",value:[]},pointLightPosition:{type:"fv",value:[]},pointLightDistance:{type:"fv1",value:[]}},particle:{psColor:{type:"c",value:new THREE.Color(15658734)},opacity:{type:"f",value:1},size:{type:"f",value:1},scale:{type:"f",value:1},
map:{type:"t",value:0,texture:null},fogDensity:{type:"f",value:2.5E-4},fogNear:{type:"f",value:1},fogFar:{type:"f",value:2E3},fogColor:{type:"c",value:new THREE.Color(16777215)}}};
THREE.ShaderLib={lensFlareVertexTexture:{vertexShader:"uniform \tvec3 \tscreenPosition;\nuniform\tvec2\tscale;\nuniform\tfloat\trotation;\nuniform int renderType;\nuniform\tsampler2D\tocclusionMap;\nattribute \tvec2 \tposition;\nattribute vec2\tUV;\nvarying\tvec2\tvUV;\nvarying\tfloat\tvVisibility;\nvoid main(void)\n{\nvUV = UV;\nvec2 pos = position;\nif( renderType == 2 ) {\nvec4 visibility = texture2D( occlusionMap, vec2( 0.1, 0.1 )) +\ntexture2D( occlusionMap, vec2( 0.5, 0.1 )) +\ntexture2D( occlusionMap, vec2( 0.9, 0.1 )) +\ntexture2D( occlusionMap, vec2( 0.9, 0.5 )) +\ntexture2D( occlusionMap, vec2( 0.9, 0.9 )) +\ntexture2D( occlusionMap, vec2( 0.5, 0.9 )) +\ntexture2D( occlusionMap, vec2( 0.1, 0.9 )) +\ntexture2D( occlusionMap, vec2( 0.1, 0.5 )) +\ntexture2D( occlusionMap, vec2( 0.5, 0.5 ));\nvVisibility = ( visibility.r / 9.0 ) *\n( 1.0 - visibility.g / 9.0 ) *\n( visibility.b / 9.0 ) *\n( 1.0 - visibility.a / 9.0 );\npos.x = cos( rotation ) * position.x - sin( rotation ) * position.y;\npos.y = sin( rotation ) * position.x + cos( rotation ) * position.y;\n}\ngl_Position = vec4(( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );\n}",fragmentShader:"#ifdef GL_ES\nprecision highp float;\n#endif\nuniform\tsampler2D\tmap;\nuniform\tfloat\t\topacity;\nuniform int renderType;\nvarying\tvec2\t\tvUV;\nvarying\tfloat\t\tvVisibility;\nvoid main( void )\n{\nif( renderType == 0 ) {\ngl_FragColor = vec4( 1.0, 0.0, 1.0, 0.0 );\n} else if( renderType == 1 ) {\ngl_FragColor = texture2D( map, vUV );\n} else {\nvec4 color = texture2D( map, vUV );\ncolor.a *= opacity * vVisibility;\ngl_FragColor = color;\n}\n}"},
lensFlare:{vertexShader:"uniform \tvec3 \tscreenPosition;\nuniform\tvec2\tscale;\nuniform\tfloat\trotation;\nuniform int renderType;\nattribute \tvec2 \tposition;\nattribute vec2\tUV;\nvarying\tvec2\tvUV;\nvoid main(void)\n{\nvUV = UV;\nvec2 pos = position;\nif( renderType == 2 ) {\npos.x = cos( rotation ) * position.x - sin( rotation ) * position.y;\npos.y = sin( rotation ) * position.x + cos( rotation ) * position.y;\n}\ngl_Position = vec4(( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );\n}",
fragmentShader:"#ifdef GL_ES\nprecision highp float;\n#endif\nuniform\tsampler2D\tmap;\nuniform\tsampler2D\tocclusionMap;\nuniform\tfloat\t\topacity;\nuniform int renderType;\nvarying\tvec2\t\tvUV;\nvoid main( void )\n{\nif( renderType == 0 ) {\ngl_FragColor = vec4( texture2D( map, vUV ).rgb, 0.0 );\n} else if( renderType == 1 ) {\ngl_FragColor = texture2D( map, vUV );\n} else {\nfloat visibility = texture2D( occlusionMap, vec2( 0.5, 0.1 )).a +\ntexture2D( occlusionMap, vec2( 0.9, 0.5 )).a +\ntexture2D( occlusionMap, vec2( 0.5, 0.9 )).a +\ntexture2D( occlusionMap, vec2( 0.1, 0.5 )).a;\nvisibility = ( 1.0 - visibility / 4.0 );\nvec4 color = texture2D( map, vUV );\ncolor.a *= opacity * visibility;\ngl_FragColor = color;\n}\n}"},
sprite:{vertexShader:"uniform\tint\t\tuseScreenCoordinates;\nuniform int affectedByDistance;\nuniform\tvec3\tscreenPosition;\nuniform \tmat4 \tmodelViewMatrix;\nuniform \tmat4 \tprojectionMatrix;\nuniform float rotation;\nuniform vec2 scale;\nuniform vec2 alignment;\nuniform vec2 uvOffset;\nuniform\tvec2 uvScale;\nattribute \tvec2 \tposition;\nattribute vec2\tuv;\nvarying\tvec2\tvUV;\nvoid main(void)\n{\nvUV = uvOffset + uv * uvScale;\nvec2 alignedPosition = position + alignment;\nvec2 rotatedPosition;\nrotatedPosition.x = ( cos( rotation ) * alignedPosition.x - sin( rotation ) * alignedPosition.y ) * scale.x;\nrotatedPosition.y = ( sin( rotation ) * alignedPosition.x + cos( rotation ) * alignedPosition.y ) * scale.y;\nvec4 finalPosition;\nif( useScreenCoordinates != 0 ) {\nfinalPosition = vec4( screenPosition.xy + rotatedPosition, screenPosition.z, 1.0 );\n} else {\nfinalPosition = projectionMatrix * modelViewMatrix * vec4( 0.0, 0.0, 0.0, 1.0 );\nfinalPosition.xy += rotatedPosition * ( affectedByDistance == 1 ? 1.0 : finalPosition.z );\n}\ngl_Position = finalPosition;\n}",
fragmentShader:"#ifdef GL_ES\nprecision highp float;\n#endif\nuniform\tsampler2D\tmap;\nuniform\tfloat\t\topacity;\nvarying\tvec2\t\tvUV;\nvoid main( void )\n{\nvec4 color = texture2D( map, vUV );\ncolor.a *= opacity;\ngl_FragColor = color;\n}"},shadowPost:{vertexShader:"uniform \tmat4 \tprojectionMatrix;\nattribute \tvec3 \tposition;\nvoid main(void)\n{\ngl_Position = projectionMatrix * vec4( position, 1.0 );\n}",fragmentShader:"#ifdef GL_ES\nprecision highp float;\n#endif\nuniform \tfloat \tdarkness;\nvoid main( void )\n{\ngl_FragColor = vec4( 0, 0, 0, darkness );\n}"},
shadowVolumeDynamic:{uniforms:{directionalLightDirection:{type:"fv",value:[]}},vertexShader:"uniform \tvec3 \tdirectionalLightDirection;\nvoid main() {\nvec4 pos = objectMatrix * vec4( position, 1.0 );\nvec3 norm = mat3( objectMatrix[0].xyz, objectMatrix[1].xyz, objectMatrix[2].xyz ) * normal;\nvec4 extruded = vec4( directionalLightDirection * 5000.0 * step( 0.0, dot( directionalLightDirection, norm )), 0.0 );\ngl_Position = projectionMatrix * viewMatrix * ( pos + extruded );\n}",fragmentShader:"void main() {\ngl_FragColor = vec4( 1.0 );\n}"},
THREE.ShaderLib={lensFlareVertexTexture:{vertexShader:"uniform \tvec3 \tscreenPosition;\nuniform\tvec2\tscale;\nuniform\tfloat\trotation;\nuniform int renderType;\nuniform\tsampler2D\tocclusionMap;\nattribute \tvec2 \tposition;\nattribute vec2\tUV;\nvarying\tvec2\tvUV;\nvarying\tfloat\tvVisibility;\nvoid main() {\nvUV = UV;\nvec2 pos = position;\nif( renderType == 2 ) {\nvec4 visibility = texture2D( occlusionMap, vec2( 0.1, 0.1 ) ) +\ntexture2D( occlusionMap, vec2( 0.5, 0.1 ) ) +\ntexture2D( occlusionMap, vec2( 0.9, 0.1 ) ) +\ntexture2D( occlusionMap, vec2( 0.9, 0.5 ) ) +\ntexture2D( occlusionMap, vec2( 0.9, 0.9 ) ) +\ntexture2D( occlusionMap, vec2( 0.5, 0.9 ) ) +\ntexture2D( occlusionMap, vec2( 0.1, 0.9 ) ) +\ntexture2D( occlusionMap, vec2( 0.1, 0.5 ) ) +\ntexture2D( occlusionMap, vec2( 0.5, 0.5 ) );\nvVisibility = ( visibility.r / 9.0 ) *\n( 1.0 - visibility.g / 9.0 ) *\n( visibility.b / 9.0 ) *\n( 1.0 - visibility.a / 9.0 );\npos.x = cos( rotation ) * position.x - sin( rotation ) * position.y;\npos.y = sin( rotation ) * position.x + cos( rotation ) * position.y;\n}\ngl_Position = vec4( ( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );\n}",fragmentShader:"#ifdef GL_ES\nprecision highp float;\n#endif\nuniform\tsampler2D\tmap;\nuniform\tfloat\t\topacity;\nuniform int renderType;\nvarying\tvec2\t\tvUV;\nvarying\tfloat\t\tvVisibility;\nvoid main() {\nif( renderType == 0 ) {\ngl_FragColor = vec4( 1.0, 0.0, 1.0, 0.0 );\n} else if( renderType == 1 ) {\ngl_FragColor = texture2D( map, vUV );\n} else {\nvec4 color = texture2D( map, vUV );\ncolor.a *= opacity * vVisibility;\ngl_FragColor = color;\n}\n}"},
lensFlare:{vertexShader:"uniform \tvec3 \tscreenPosition;\nuniform\tvec2\tscale;\nuniform\tfloat\trotation;\nuniform int renderType;\nattribute \tvec2 \tposition;\nattribute vec2\tUV;\nvarying\tvec2\tvUV;\nvoid main() {\nvUV = UV;\nvec2 pos = position;\nif( renderType == 2 ) {\npos.x = cos( rotation ) * position.x - sin( rotation ) * position.y;\npos.y = sin( rotation ) * position.x + cos( rotation ) * position.y;\n}\ngl_Position = vec4( ( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );\n}",
fragmentShader:"#ifdef GL_ES\nprecision highp float;\n#endif\nuniform\tsampler2D\tmap;\nuniform\tsampler2D\tocclusionMap;\nuniform\tfloat\t\topacity;\nuniform int renderType;\nvarying\tvec2\t\tvUV;\nvoid main() {\nif( renderType == 0 ) {\ngl_FragColor = vec4( texture2D( map, vUV ).rgb, 0.0 );\n} else if( renderType == 1 ) {\ngl_FragColor = texture2D( map, vUV );\n} else {\nfloat visibility = texture2D( occlusionMap, vec2( 0.5, 0.1 ) ).a +\ntexture2D( occlusionMap, vec2( 0.9, 0.5 ) ).a +\ntexture2D( occlusionMap, vec2( 0.5, 0.9 ) ).a +\ntexture2D( occlusionMap, vec2( 0.1, 0.5 ) ).a;\nvisibility = ( 1.0 - visibility / 4.0 );\nvec4 color = texture2D( map, vUV );\ncolor.a *= opacity * visibility;\ngl_FragColor = color;\n}\n}"},
sprite:{vertexShader:"uniform\tint\t\tuseScreenCoordinates;\nuniform int affectedByDistance;\nuniform\tvec3\tscreenPosition;\nuniform \tmat4 \tmodelViewMatrix;\nuniform \tmat4 \tprojectionMatrix;\nuniform float rotation;\nuniform vec2 scale;\nuniform vec2 alignment;\nuniform vec2 uvOffset;\nuniform\tvec2 uvScale;\nattribute \tvec2 \tposition;\nattribute vec2\tuv;\nvarying\tvec2\tvUV;\nvoid main() {\nvUV = uvOffset + uv * uvScale;\nvec2 alignedPosition = position + alignment;\nvec2 rotatedPosition;\nrotatedPosition.x = ( cos( rotation ) * alignedPosition.x - sin( rotation ) * alignedPosition.y ) * scale.x;\nrotatedPosition.y = ( sin( rotation ) * alignedPosition.x + cos( rotation ) * alignedPosition.y ) * scale.y;\nvec4 finalPosition;\nif( useScreenCoordinates != 0 ) {\nfinalPosition = vec4( screenPosition.xy + rotatedPosition, screenPosition.z, 1.0 );\n} else {\nfinalPosition = projectionMatrix * modelViewMatrix * vec4( 0.0, 0.0, 0.0, 1.0 );\nfinalPosition.xy += rotatedPosition * ( affectedByDistance == 1 ? 1.0 : finalPosition.z );\n}\ngl_Position = finalPosition;\n}",
fragmentShader:"#ifdef GL_ES\nprecision highp float;\n#endif\nuniform\tsampler2D\tmap;\nuniform\tfloat\t\topacity;\nvarying\tvec2\t\tvUV;\nvoid main() {\nvec4 color = texture2D( map, vUV );\ncolor.a *= opacity;\ngl_FragColor = color;\n}"},shadowPost:{vertexShader:"uniform \tmat4 \tprojectionMatrix;\nattribute \tvec3 \tposition;\nvoid main() {\ngl_Position = projectionMatrix * vec4( position, 1.0 );\n}",fragmentShader:"#ifdef GL_ES\nprecision highp float;\n#endif\nuniform \tfloat \tdarkness;\nvoid main() {\ngl_FragColor = vec4( 0, 0, 0, darkness );\n}"},
shadowVolumeDynamic:{uniforms:{directionalLightDirection:{type:"fv",value:[]}},vertexShader:"uniform \tvec3 \tdirectionalLightDirection;\nvoid main() {\nvec4 pos = objectMatrix * vec4( position, 1.0 );\nvec3 norm = mat3( objectMatrix[0].xyz, objectMatrix[1].xyz, objectMatrix[2].xyz ) * normal;\nvec4 extruded = vec4( directionalLightDirection * 5000.0 * step( 0.0, dot( directionalLightDirection, norm ) ), 0.0 );\ngl_Position = projectionMatrix * viewMatrix * ( pos + extruded );\n}",fragmentShader:"void main() {\ngl_FragColor = vec4( 1.0 );\n}"},
depth:{uniforms:{mNear:{type:"f",value:1},mFar:{type:"f",value:2E3},opacity:{type:"f",value:1}},fragmentShader:"uniform float mNear;\nuniform float mFar;\nuniform float opacity;\nvoid main() {\nfloat depth = gl_FragCoord.z / gl_FragCoord.w;\nfloat color = 1.0 - smoothstep( mNear, mFar, depth );\ngl_FragColor = vec4( vec3( color ), opacity );\n}",vertexShader:"void main() {\ngl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n}"},normal:{uniforms:{opacity:{type:"f",value:1}},
fragmentShader:"uniform float opacity;\nvarying vec3 vNormal;\nvoid main() {\ngl_FragColor = vec4( 0.5 * normalize( vNormal ) + 0.5, opacity );\n}",vertexShader:"varying vec3 vNormal;\nvoid main() {\nvec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );\nvNormal = normalize( normalMatrix * normal );\ngl_Position = projectionMatrix * mvPosition;\n}"},basic:{uniforms:THREE.UniformsLib.common,fragmentShader:["uniform vec3 diffuse;\nuniform float opacity;",THREE.ShaderChunk.color_pars_fragment,THREE.ShaderChunk.map_pars_fragment,
THREE.ShaderChunk.lightmap_pars_fragment,THREE.ShaderChunk.envmap_pars_fragment,THREE.ShaderChunk.fog_pars_fragment,"void main() {\ngl_FragColor = vec4( diffuse, opacity );",THREE.ShaderChunk.map_fragment,THREE.ShaderChunk.lightmap_fragment,THREE.ShaderChunk.color_fragment,THREE.ShaderChunk.envmap_fragment,THREE.ShaderChunk.fog_fragment,"}"].join("\n"),vertexShader:[THREE.ShaderChunk.map_pars_vertex,THREE.ShaderChunk.lightmap_pars_vertex,THREE.ShaderChunk.envmap_pars_vertex,THREE.ShaderChunk.color_pars_vertex,
......
......@@ -30,7 +30,7 @@ THREE.SceneLoader.prototype = {
g, o, m, l, p, c, t, f, tt, pp,
geometry, material, camera, fog,
texture, images,
materials,
materials, light,
data, binLoader, jsonLoader,
counter_models, counter_textures,
total_models, total_textures,
......@@ -132,11 +132,21 @@ THREE.SceneLoader.prototype = {
if ( geometry ) {
var hasNormals = false;
materials = [];
for( i = 0; i < o.materials.length; i++ ) {
materials[ i ] = result.materials[ o.materials[i] ];
hasNormals = materials[ i ] instanceof THREE.MeshShaderMaterial;
}
if ( hasNormals ) {
geometry.computeTangents();
}
p = o.position;
......@@ -193,26 +203,26 @@ THREE.SceneLoader.prototype = {
}
if ( o.castsShadow ) {
//object.visible = true;
//object.materials = [ new THREE.MeshBasicMaterial( { color: 0xff0000 } ) ];
var shadow = new THREE.ShadowVolume( geometry )
result.scene.addChild( shadow );
shadow.position = object.position;
shadow.rotation = object.rotation;
shadow.scale = object.scale;
}
if ( o.trigger && o.trigger.toLowerCase() != "none" ) {
var trigger = {
"type" : o.trigger,
"object" : o
};
result.triggers[ object.name ] = trigger;
}
......@@ -255,14 +265,13 @@ THREE.SceneLoader.prototype = {
result.objects[ dd ] = object;
result.empties[ dd ] = object;
if ( o.trigger && o.trigger.toLowerCase() != "none" ) {
var trigger = {
"type" : o.trigger,
"object" : o
};
result.triggers[ object.name ] = trigger;
}
......@@ -289,7 +298,7 @@ THREE.SceneLoader.prototype = {
handle_mesh( geo, id );
counter_models -= 1;
scope.onLoadComplete();
async_callback_gate();
......@@ -459,7 +468,7 @@ THREE.SceneLoader.prototype = {
if ( g.type == "bin_mesh" || g.type == "ascii_mesh" ) {
counter_models += 1;
scope.onLoadStart();
}
......@@ -540,9 +549,9 @@ THREE.SceneLoader.prototype = {
if( tt.url instanceof Array ) {
counter_textures += tt.url.length;
for( var n = 0; n < tt.url.length; n ++ ) {
scope.onLoadStart();
}
......@@ -670,12 +679,101 @@ THREE.SceneLoader.prototype = {
}
if ( m.parameters.opacity !== undefined && m.parameters.opacity < 1.0 ) {
m.parameters.transparent = true;
}
material = new THREE[ m.type ]( m.parameters );
if ( m.parameters.normalMap ) {
var shader = THREE.ShaderUtils.lib[ "normal" ];
var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
var diffuse = m.parameters.color;
var specular = m.parameters.specular;
var ambient = m.parameters.ambient;
var shininess = m.parameters.shininess;
var pointLight;
var directionalLight;
var ambientLight;
// take first of each light type (normal map shader supports just one light of each type)
for ( var i = 0; i < result.scene.lights.length; i ++ ) {
light = result.scene.lights[ i ];
if( ! ambientLight && ( light instanceof THREE.AmbientLight ) ) {
ambientLight = light;
} else if( ! pointLight && ( light instanceof THREE.PointLight ) ) {
pointLight = light;
} else if( ! directionalLight && ( light instanceof THREE.DirectionalLight ) ) {
directionalLight = light;
}
}
uniforms[ "tNormal" ].texture = result.textures[ m.parameters.normalMap ];
if ( m.parameters.normalMapFactor ) {
uniforms[ "uNormalScale" ].value = m.parameters.normalMapFactor;
}
if ( m.parameters.map ) {
uniforms[ "tDiffuse" ].texture = m.parameters.map;
uniforms[ "enableDiffuse" ].value = true;
}
uniforms[ "enableAO" ].value = false;
uniforms[ "enableSpecular" ].value = false;
if ( pointLight ) {
uniforms[ "uPointLightPos" ].value = pointLight.position;
uniforms[ "uPointLightColor" ].value = pointLight.color;
}
if ( directionalLight ) {
uniforms[ "uDirLightPos" ].value = directionalLight.position;
uniforms[ "uDirLightColor" ].value = directionalLight.color;
}
if ( ambientLight ) {
uniforms[ "uAmbientLightColor" ].value = ambientLight.color;
}
uniforms[ "uDiffuseColor" ].value.setHex( diffuse );
uniforms[ "uSpecularColor" ].value.setHex( specular );
uniforms[ "uAmbientColor" ].value.setHex( ambient );
uniforms[ "uShininess" ].value = shininess;
var parameters = { fragmentShader: shader.fragmentShader, vertexShader: shader.vertexShader, uniforms: uniforms };
material = new THREE.MeshShaderMaterial( parameters );
} else {
material = new THREE[ m.type ]( m.parameters );
}
result.materials[ dm ] = material;
}
......
......@@ -176,6 +176,7 @@ THREE.WebGLRenderer = function ( parameters ) {
_stencilShadow.vertexLocation = _gl.getAttribLocation ( _stencilShadow.program, "position" );
_stencilShadow.projectionLocation = _gl.getUniformLocation( _stencilShadow.program, "projectionMatrix" );
_stencilShadow.darknessLocation = _gl.getUniformLocation( _stencilShadow.program, "darkness" );
}
......
......@@ -629,7 +629,7 @@ THREE.UniformsLib = {
THREE.ShaderLib = {
'lensFlareVertexTexture': {
vertexShader: [
"uniform vec3 screenPosition;",
......@@ -643,74 +643,78 @@ THREE.ShaderLib = {
"attribute vec2 UV;",
"varying vec2 vUV;",
"varying float vVisibility;",
"void main(void)",
"{",
"void main() {",
"vUV = UV;",
"vec2 pos = position;",
"if( renderType == 2 ) {",
"vec4 visibility = texture2D( occlusionMap, vec2( 0.1, 0.1 )) +",
"texture2D( occlusionMap, vec2( 0.5, 0.1 )) +",
"texture2D( occlusionMap, vec2( 0.9, 0.1 )) +",
"texture2D( occlusionMap, vec2( 0.9, 0.5 )) +",
"texture2D( occlusionMap, vec2( 0.9, 0.9 )) +",
"texture2D( occlusionMap, vec2( 0.5, 0.9 )) +",
"texture2D( occlusionMap, vec2( 0.1, 0.9 )) +",
"texture2D( occlusionMap, vec2( 0.1, 0.5 )) +",
"texture2D( occlusionMap, vec2( 0.5, 0.5 ));",
"vec4 visibility = texture2D( occlusionMap, vec2( 0.1, 0.1 ) ) +",
"texture2D( occlusionMap, vec2( 0.5, 0.1 ) ) +",
"texture2D( occlusionMap, vec2( 0.9, 0.1 ) ) +",
"texture2D( occlusionMap, vec2( 0.9, 0.5 ) ) +",
"texture2D( occlusionMap, vec2( 0.9, 0.9 ) ) +",
"texture2D( occlusionMap, vec2( 0.5, 0.9 ) ) +",
"texture2D( occlusionMap, vec2( 0.1, 0.9 ) ) +",
"texture2D( occlusionMap, vec2( 0.1, 0.5 ) ) +",
"texture2D( occlusionMap, vec2( 0.5, 0.5 ) );",
"vVisibility = ( visibility.r / 9.0 ) *",
"( 1.0 - visibility.g / 9.0 ) *",
"( visibility.b / 9.0 ) *",
"( 1.0 - visibility.a / 9.0 );",
"( 1.0 - visibility.g / 9.0 ) *",
"( visibility.b / 9.0 ) *",
"( 1.0 - visibility.a / 9.0 );",
"pos.x = cos( rotation ) * position.x - sin( rotation ) * position.y;",
"pos.y = sin( rotation ) * position.x + cos( rotation ) * position.y;",
"}",
"gl_Position = vec4(( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );",
"gl_Position = vec4( ( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );",
"}"
].join( "\n" ),
fragmentShader: [
"#ifdef GL_ES",
"precision highp float;",
"#endif",
"#endif",
"uniform sampler2D map;",
"uniform float opacity;",
"uniform int renderType;",
"varying vec2 vUV;",
"varying float vVisibility;",
"void main( void )",
"{",
"void main() {",
// pink square
"if( renderType == 0 ) {",
"gl_FragColor = vec4( 1.0, 0.0, 1.0, 0.0 );",
// restore
"} else if( renderType == 1 ) {",
"gl_FragColor = texture2D( map, vUV );",
// flare
"} else {",
"vec4 color = texture2D( map, vUV );",
"color.a *= opacity * vVisibility;",
"gl_FragColor = color;",
"}",
"}"
].join( "\n" )
......@@ -718,7 +722,7 @@ THREE.ShaderLib = {
'lensFlare': {
vertexShader: [
"uniform vec3 screenPosition;",
......@@ -730,74 +734,80 @@ THREE.ShaderLib = {
"attribute vec2 UV;",
"varying vec2 vUV;",
"void main(void)",
"{",
"void main() {",
"vUV = UV;",
"vec2 pos = position;",
"if( renderType == 2 ) {",
"pos.x = cos( rotation ) * position.x - sin( rotation ) * position.y;",
"pos.y = sin( rotation ) * position.x + cos( rotation ) * position.y;",
"}",
"gl_Position = vec4(( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );",
"gl_Position = vec4( ( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );",
"}"
].join( "\n" ),
fragmentShader: [
"#ifdef GL_ES",
"precision highp float;",
"#endif",
"#endif",
"uniform sampler2D map;",
"uniform sampler2D occlusionMap;",
"uniform float opacity;",
"uniform int renderType;",
"varying vec2 vUV;",
"void main( void )",
"{",
"void main() {",
// pink square
"if( renderType == 0 ) {",
"gl_FragColor = vec4( texture2D( map, vUV ).rgb, 0.0 );",
// restore
"} else if( renderType == 1 ) {",
"gl_FragColor = texture2D( map, vUV );",
// flare
"} else {",
"float visibility = texture2D( occlusionMap, vec2( 0.5, 0.1 )).a +",
"texture2D( occlusionMap, vec2( 0.9, 0.5 )).a +",
"texture2D( occlusionMap, vec2( 0.5, 0.9 )).a +",
"texture2D( occlusionMap, vec2( 0.1, 0.5 )).a;",
"visibility = ( 1.0 - visibility / 4.0 );",
"float visibility = texture2D( occlusionMap, vec2( 0.5, 0.1 ) ).a +",
"texture2D( occlusionMap, vec2( 0.9, 0.5 ) ).a +",
"texture2D( occlusionMap, vec2( 0.5, 0.9 ) ).a +",
"texture2D( occlusionMap, vec2( 0.1, 0.5 ) ).a;",
"visibility = ( 1.0 - visibility / 4.0 );",
"vec4 color = texture2D( map, vUV );",
"color.a *= opacity * visibility;",
"gl_FragColor = color;",
"}",
"}"
].join( "\n" )
},
'sprite': {
vertexShader: [
"uniform int useScreenCoordinates;",
"uniform int affectedByDistance;",
"uniform vec3 screenPosition;",
......@@ -813,54 +823,56 @@ THREE.ShaderLib = {
"attribute vec2 uv;",
"varying vec2 vUV;",
"void main(void)",
"{",
"void main() {",
//"vUV = uvOffset + vec2( uv.x, 1.0 - uv.y ) * uvScale;",
"vUV = uvOffset + uv * uvScale;",
"vec2 alignedPosition = position + alignment;",
"vec2 rotatedPosition;",
"rotatedPosition.x = ( cos( rotation ) * alignedPosition.x - sin( rotation ) * alignedPosition.y ) * scale.x;",
"rotatedPosition.y = ( sin( rotation ) * alignedPosition.x + cos( rotation ) * alignedPosition.y ) * scale.y;",
"vec4 finalPosition;",
"if( useScreenCoordinates != 0 ) {",
"finalPosition = vec4( screenPosition.xy + rotatedPosition, screenPosition.z, 1.0 );",
"} else {",
"finalPosition = projectionMatrix * modelViewMatrix * vec4( 0.0, 0.0, 0.0, 1.0 );",
"finalPosition.xy += rotatedPosition * ( affectedByDistance == 1 ? 1.0 : finalPosition.z );",
"}",
"gl_Position = finalPosition;",
"}"
].join( "\n" ),
fragmentShader: [
"#ifdef GL_ES",
"precision highp float;",
"#endif",
"#endif",
"uniform sampler2D map;",
"uniform float opacity;",
"varying vec2 vUV;",
"void main( void )",
"{",
"void main() {",
"vec4 color = texture2D( map, vUV );",
"color.a *= opacity;",
"gl_FragColor = color;",
// "gl_FragColor = vec4( 1.0, 0.0, 1.0, 1.0 );",
"}"
].join( "\n" )
},
......@@ -874,9 +886,10 @@ THREE.ShaderLib = {
"uniform mat4 projectionMatrix;",
"attribute vec3 position;",
"void main(void)",
"{",
"void main() {",
"gl_Position = projectionMatrix * vec4( position, 1.0 );",
"}"
].join( "\n" ),
......@@ -885,13 +898,14 @@ THREE.ShaderLib = {
"#ifdef GL_ES",
"precision highp float;",
"#endif",
"#endif",
"uniform float darkness;",
"void main( void )",
"{",
"void main() {",
"gl_FragColor = vec4( 0, 0, 0, darkness );",
"}"
].join( "\n" )
......@@ -911,9 +925,10 @@ THREE.ShaderLib = {
"vec4 pos = objectMatrix * vec4( position, 1.0 );",
"vec3 norm = mat3( objectMatrix[0].xyz, objectMatrix[1].xyz, objectMatrix[2].xyz ) * normal;",
"vec4 extruded = vec4( directionalLightDirection * 5000.0 * step( 0.0, dot( directionalLightDirection, norm )), 0.0 );",
"vec4 extruded = vec4( directionalLightDirection * 5000.0 * step( 0.0, dot( directionalLightDirection, norm ) ), 0.0 );",
"gl_Position = projectionMatrix * viewMatrix * ( pos + extruded );",
"}"
].join( "\n" ),
......@@ -927,6 +942,7 @@ THREE.ShaderLib = {
"}"
].join( "\n" )
},
......
......@@ -808,7 +808,8 @@ def handle_texture(id, textures, material, filepath, option_copy_textures):
repeatName = 'map%sRepeat' % id.capitalize()
wrapName = 'map%sWrap' % id.capitalize()
texture = textures[id]
slot = textures[id]['slot']
texture = textures[id]['texture']
image = texture.image
fname = extract_texture_filename(image)
material[texName] = fname
......@@ -829,6 +830,11 @@ def handle_texture(id, textures, material, filepath, option_copy_textures):
wrap_y = "mirror"
material[wrapName] = [wrap_x, wrap_y]
if slot.use_map_normal:
if slot.normal_factor != 1.0:
material['mapNormalFactor'] = slot.normal_factor
# #####################################################
# ASCII model generator
......@@ -1319,17 +1325,20 @@ def extract_material_data(m, option_colors):
material['mapDiffuse'] = ""
material['mapLight'] = ""
material['mapNormal'] = ""
material['mapNormalFactor'] = 1.0
textures = guess_material_textures(m)
if textures['diffuse']:
material['mapDiffuse'] = textures['diffuse'].image.name
material['mapDiffuse'] = textures['diffuse']['texture'].image.name
if textures['light']:
material['mapLight'] = textures['light'].image.name
material['mapLight'] = textures['light']['texture'].image.name
if textures['normal']:
material['mapNormal'] = textures['normal'].image.name
material['mapNormal'] = textures['normal']['texture'].image.name
if textures['normal']['slot'].use_map_normal:
material['mapNormalFactor'] = textures['normal']['slot'].normal_factor
material['shading'] = m.THREE_materialType
......@@ -1352,14 +1361,14 @@ def guess_material_textures(material):
if slot.use and texture.type == 'IMAGE':
if texture.use_normal_map:
textures['normal'] = texture
textures['normal'] = { "texture": texture, "slot": slot }
else:
if not textures['diffuse']:
textures['diffuse'] = texture
textures['diffuse'] = { "texture": texture, "slot": slot }
else:
textures['light'] = texture
textures['light'] = { "texture": texture, "slot": slot }
if textures['diffuse'] and textures['normal'] and textures['light']:
break
......@@ -1387,6 +1396,7 @@ def generate_material_string(material):
colorMap = material['mapDiffuse']
lightMap = material['mapLight']
normalMap = material['mapNormal']
normalMapFactor = material['mapNormalFactor']
if colorMap:
parameters += ', "map": %s' % generate_string(colorMap)
......@@ -1394,6 +1404,8 @@ def generate_material_string(material):
parameters += ', "lightMap": %s' % generate_string(lightMap)
if normalMap:
parameters += ', "normalMap": %s' % generate_string(normalMap)
if normalMapFactor != 1.0:
parameters += ', "normalMapFactor": %f' % normalMapFactor
if material['vertexColors']:
parameters += ', "vertexColors": "vertex"'
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册