Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NodeMaterial code simplification #11533

Merged
merged 1 commit into from
Jun 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 36 additions & 37 deletions examples/js/nodes/materials/PhongNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,28 @@ THREE.PhongNode.prototype.build = function ( builder ) {

"#endif",

THREE.ShaderChunk[ "common" ],
THREE.ShaderChunk[ "fog_pars_vertex" ],
THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
THREE.ShaderChunk[ "skinning_pars_vertex" ],
THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ]

"#include <common>",
"#include <fog_pars_vertex>",
"#include <morphtarget_pars_vertex>",
"#include <skinning_pars_vertex>",
"#include <shadowmap_pars_vertex>",
"#include <logdepthbuf_pars_vertex>"
].join( "\n" ) );

var output = [
THREE.ShaderChunk[ "beginnormal_vertex" ],
THREE.ShaderChunk[ "morphnormal_vertex" ],
THREE.ShaderChunk[ "skinbase_vertex" ],
THREE.ShaderChunk[ "skinnormal_vertex" ],
THREE.ShaderChunk[ "defaultnormal_vertex" ],
"#include <beginnormal_vertex>",
"#include <morphnormal_vertex>",
"#include <skinbase_vertex>",
"#include <skinnormal_vertex>",
"#include <defaultnormal_vertex>",

"#ifndef FLAT_SHADED", // Normal computed with derivatives when FLAT_SHADED

" vNormal = normalize( transformedNormal );",

"#endif",

THREE.ShaderChunk[ "begin_vertex" ]
"#include <begin_vertex>"
];

if ( transform ) {
Expand All @@ -80,16 +79,16 @@ THREE.PhongNode.prototype.build = function ( builder ) {
}

output.push(
THREE.ShaderChunk[ "morphtarget_vertex" ],
THREE.ShaderChunk[ "skinning_vertex" ],
THREE.ShaderChunk[ "project_vertex" ],
THREE.ShaderChunk[ "fog_vertex" ],
THREE.ShaderChunk[ "logdepthbuf_vertex" ],
" #include <morphtarget_vertex>",
" #include <skinning_vertex>",
" #include <project_vertex>",
" #include <fog_vertex>",
" #include <logdepthbuf_vertex>",

" vViewPosition = - mvPosition.xyz;",

THREE.ShaderChunk[ "worldpos_vertex" ],
THREE.ShaderChunk[ "shadowmap_vertex" ]
" #include <worldpos_vertex>",
" #include <shadowmap_vertex>"
);

code = output.join( "\n" );
Expand Down Expand Up @@ -141,28 +140,28 @@ THREE.PhongNode.prototype.build = function ( builder ) {
material.requestAttribs.transparent = alpha != undefined;

material.addFragmentPars( [
THREE.ShaderChunk[ "common" ],
THREE.ShaderChunk[ "fog_pars_fragment" ],
THREE.ShaderChunk[ "bsdfs" ],
THREE.ShaderChunk[ "lights_pars" ],
THREE.ShaderChunk[ "lights_phong_pars_fragment" ],
THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ]
"#include <common>",
"#include <fog_pars_fragment>",
"#include <bsdfs>",
"#include <lights_pars>",
"#include <lights_phong_pars_fragment>",
"#include <shadowmap_pars_fragment>",
"#include <logdepthbuf_pars_fragment>"
].join( "\n" ) );

var output = [
// prevent undeclared normal
THREE.ShaderChunk[ "normal_flip" ],
THREE.ShaderChunk[ "normal_fragment" ],
// prevent undeclared normal
"#include <normal_flip>",
"#include <normal_fragment>",

// prevent undeclared material
// prevent undeclared material
" BlinnPhongMaterial material;",

color.code,
" vec3 diffuseColor = " + color.result + ";",
" ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );",

THREE.ShaderChunk[ "logdepthbuf_fragment" ],
"#include <logdepthbuf_fragment>",

specular.code,
" vec3 specular = " + specular.result + ";",
Expand Down Expand Up @@ -209,7 +208,7 @@ THREE.PhongNode.prototype.build = function ( builder ) {
'material.specularShininess = shininess;',
'material.specularStrength = specularStrength;',

THREE.ShaderChunk[ "lights_template" ]
"#include <lights_template>"
);

if ( light ) {
Expand Down Expand Up @@ -297,10 +296,10 @@ THREE.PhongNode.prototype.build = function ( builder ) {
}

output.push(
THREE.ShaderChunk[ "premultiplied_alpha_fragment" ],
THREE.ShaderChunk[ "tonemapping_fragment" ],
THREE.ShaderChunk[ "encodings_fragment" ],
THREE.ShaderChunk[ "fog_fragment" ]
"#include <premultiplied_alpha_fragment>",
"#include <tonemapping_fragment>",
"#include <encodings_fragment>",
"#include <fog_fragment>"
);

code = output.join( "\n" );
Expand Down
18 changes: 7 additions & 11 deletions examples/js/nodes/materials/SpriteNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ THREE.SpriteNode.prototype.build = function ( builder ) {
] ) );

material.addVertexPars( [
THREE.ShaderChunk[ "fog_pars_vertex" ],
"#include <fog_pars_vertex>"
].join( "\n" ) );

output = [
THREE.ShaderChunk[ "begin_vertex" ]
"#include <begin_vertex>"
];

if ( transform ) {
Expand All @@ -51,8 +51,8 @@ THREE.SpriteNode.prototype.build = function ( builder ) {

output.push(

THREE.ShaderChunk[ "project_vertex" ],
THREE.ShaderChunk[ "fog_vertex" ],
"#include <project_vertex>",
"#include <fog_vertex>",

'mat4 modelViewMtx = modelViewMatrix;',
'mat4 modelMtx = modelMatrix;',
Expand Down Expand Up @@ -106,12 +106,10 @@ THREE.SpriteNode.prototype.build = function ( builder ) {
'gl_Position = projectionMatrix * modelViewMtx * modelMtx * vec4( position, 1.0 );'
);

code = output.join( "\n" );

} else {

material.addFragmentPars( [
THREE.ShaderChunk[ "fog_pars_fragment" ]
"#include <fog_pars_fragment>",
].join( "\n" ) );

// parse all nodes to reuse generate codes
Expand Down Expand Up @@ -139,12 +137,10 @@ THREE.SpriteNode.prototype.build = function ( builder ) {

}

output.push( THREE.ShaderChunk[ "fog_fragment" ] );

code = output.join( "\n" );
output.push( "#include <fog_fragment>" );

}

return code;
return output.join( "\n" );

};
72 changes: 38 additions & 34 deletions examples/js/nodes/materials/StandardNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,32 @@ THREE.StandardNode.prototype.build = function ( builder ) {

"#endif",

THREE.ShaderChunk[ "common" ],
THREE.ShaderChunk[ "fog_pars_vertex" ],
THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
THREE.ShaderChunk[ "skinning_pars_vertex" ],
THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ]
"#include <common>",
"#include <fog_pars_vertex>",
"#include <morphtarget_pars_vertex>",
"#include <skinning_pars_vertex>",
"#include <shadowmap_pars_vertex>",
"#include <logdepthbuf_pars_vertex>"

].join( "\n" ) );

var output = [
THREE.ShaderChunk[ "beginnormal_vertex" ],
THREE.ShaderChunk[ "morphnormal_vertex" ],
THREE.ShaderChunk[ "skinbase_vertex" ],
THREE.ShaderChunk[ "skinnormal_vertex" ],
THREE.ShaderChunk[ "defaultnormal_vertex" ],
"#include <beginnormal_vertex>",
"#include <morphnormal_vertex>",
"#include <skinbase_vertex>",
"#include <skinnormal_vertex>",
"#include <defaultnormal_vertex>",
"#include <logdepthbuf_pars_vertex>",
"#include <logdepthbuf_pars_vertex>",
"#include <logdepthbuf_pars_vertex>",

"#ifndef FLAT_SHADED", // Normal computed with derivatives when FLAT_SHADED

" vNormal = normalize( transformedNormal );",

"#endif",

THREE.ShaderChunk[ "begin_vertex" ]
"#include <begin_vertex>"
];

if ( transform ) {
Expand All @@ -85,16 +88,16 @@ THREE.StandardNode.prototype.build = function ( builder ) {
}

output.push(
THREE.ShaderChunk[ "morphtarget_vertex" ],
THREE.ShaderChunk[ "skinning_vertex" ],
THREE.ShaderChunk[ "project_vertex" ],
THREE.ShaderChunk[ "fog_vertex" ],
THREE.ShaderChunk[ "logdepthbuf_vertex" ],
"#include <morphtarget_vertex>",
"#include <skinning_vertex>",
"#include <project_vertex>",
"#include <fog_vertex>",
"#include <logdepthbuf_vertex>",

" vViewPosition = - mvPosition.xyz;",

THREE.ShaderChunk[ "worldpos_vertex" ],
THREE.ShaderChunk[ "shadowmap_vertex" ]
"#include <worldpos_vertex>",
"#include <shadowmap_vertex>"
);

code = output.join( "\n" );
Expand Down Expand Up @@ -175,19 +178,20 @@ THREE.StandardNode.prototype.build = function ( builder ) {

"#endif",

THREE.ShaderChunk[ "common" ],
THREE.ShaderChunk[ "fog_pars_fragment" ],
THREE.ShaderChunk[ "bsdfs" ],
THREE.ShaderChunk[ "lights_pars" ],
THREE.ShaderChunk[ "lights_physical_pars_fragment" ],
THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ]
"#include <common>",
"#include <fog_pars_fragment>",
"#include <bsdfs>",
"#include <lights_pars>",
"#include <lights_physical_pars_fragment>",
"#include <shadowmap_pars_fragment>",
"#include <logdepthbuf_pars_fragment>",
"#include <logdepthbuf_vertex>"
].join( "\n" ) );

var output = [
// prevent undeclared normal
THREE.ShaderChunk[ "normal_flip" ],
THREE.ShaderChunk[ "normal_fragment" ],
" #include <normal_flip>",
" #include <normal_fragment>",

// prevent undeclared material
" PhysicalMaterial material;",
Expand All @@ -197,7 +201,7 @@ THREE.StandardNode.prototype.build = function ( builder ) {
" vec3 diffuseColor = " + color.result + ";",
" ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );",

THREE.ShaderChunk[ "logdepthbuf_fragment" ],
"#include <logdepthbuf_fragment>",

roughness.code,
" float roughnessFactor = " + roughness.result + ";",
Expand Down Expand Up @@ -283,7 +287,7 @@ THREE.StandardNode.prototype.build = function ( builder ) {
}

output.push(
THREE.ShaderChunk[ "lights_template" ]
"#include <lights_template>"
);

if ( light ) {
Expand Down Expand Up @@ -377,10 +381,10 @@ THREE.StandardNode.prototype.build = function ( builder ) {
}

output.push(
THREE.ShaderChunk[ "premultiplied_alpha_fragment" ],
THREE.ShaderChunk[ "tonemapping_fragment" ],
THREE.ShaderChunk[ "encodings_fragment" ],
THREE.ShaderChunk[ "fog_fragment" ]
"#include <premultiplied_alpha_fragment>",
"#include <tonemapping_fragment>",
"#include <encodings_fragment>",
"#include <fog_fragment>"
);

code = output.join( "\n" );
Expand Down