Skip to content

Instantly share code, notes, and snippets.

@Makio64
Last active July 17, 2017 13:18
Show Gist options
  • Save Makio64/eafee856d0ea5a6c038497f92faf89e8 to your computer and use it in GitHub Desktop.
Save Makio64/eafee856d0ea5a6c038497f92faf89e8 to your computer and use it in GitHub Desktop.
MeshCustomMaterial, use to your own risk ;)
export default class MeshCustomMaterial extends THREE.MeshStandardMaterial {
constructor(parameters, uniforms={}, fs=null , vs=null ){
fs = fs || `
#define PHYSICAL
uniform vec3 diffuse;
uniform vec3 emissive;
uniform float roughness;
uniform float metalness;
uniform float opacity;
#ifndef STANDARD
uniform float clearCoat;
uniform float clearCoatRoughness;
#endif
varying vec3 vViewPosition;
#ifndef FLAT_SHADED
varying vec3 vNormal;
#endif
#include <common>
#include <packing>
#include <color_pars_fragment>
#include <uv_pars_fragment>
#include <uv2_pars_fragment>
#include <map_pars_fragment>
#include <alphamap_pars_fragment>
#include <aomap_pars_fragment>
#include <lightmap_pars_fragment>
#include <emissivemap_pars_fragment>
#include <envmap_pars_fragment>
#include <fog_pars_fragment>
#include <bsdfs>
#include <cube_uv_reflection_fragment>
#include <lights_pars>
#include <lights_physical_pars_fragment>
#include <shadowmap_pars_fragment>
#include <bumpmap_pars_fragment>
#include <normalmap_pars_fragment>
#include <roughnessmap_pars_fragment>
#include <metalnessmap_pars_fragment>
#include <logdepthbuf_pars_fragment>
#include <clipping_planes_pars_fragment>
varying float vBrightness;
void main() {
#include <clipping_planes_fragment>
vec4 diffuseColor = vec4( diffuse, opacity );
ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );
vec3 totalEmissiveRadiance = emissive;
#include <logdepthbuf_fragment>
#ifdef USE_MAP
vec4 texelColor = texture2D( map, vUv*2. );
texelColor = mapTexelToLinear( texelColor );
diffuseColor *= texelColor;
#endif
#include <color_fragment>
#include <alphamap_fragment>
#include <alphatest_fragment>
#include <specularmap_fragment>
#include <roughnessmap_fragment>
#include <metalnessmap_fragment>
#include <normal_flip>
#include <normal_fragment>
#include <emissivemap_fragment>
// accumulation
#include <lights_physical_fragment>
#include <lights_template>
// modulation
#include <aomap_fragment>
vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;
gl_FragColor = vec4( outgoingLight*vBrightness, diffuseColor.a );
#include <dithering_fragment>
#include <premultiplied_alpha_fragment>
#include <tonemapping_fragment>
#include <encodings_fragment>
#include <fog_fragment>
}`
vs = vs || `
#define PHYSICAL
varying vec3 vViewPosition;
#ifndef FLAT_SHADED
varying vec3 vNormal;
#endif
#include <common>
#include <uv_pars_vertex>
#include <uv2_pars_vertex>
#include <displacementmap_pars_vertex>
#include <color_pars_vertex>
#include <fog_pars_vertex>
#include <morphtarget_pars_vertex>
#include <skinning_pars_vertex>
#include <shadowmap_pars_vertex>
#include <specularmap_pars_fragment>
#include <logdepthbuf_pars_vertex>
#include <clipping_planes_pars_vertex>
attribute vec3 aPosition;
attribute float aScale;
attribute float aBrightness;
varying float vBrightness;
void main() {
#include <uv_vertex>
#include <uv2_vertex>
#include <color_vertex>
#include <beginnormal_vertex>
#include <defaultnormal_vertex>
#ifndef FLAT_SHADED // Normal computed with derivatives when FLAT_SHADED
vNormal = normalize( transformedNormal );
#endif
#include <begin_vertex>
//Here you have a variable call transformed, you can modify it
//for exemple add a
//transformed = transformed + normal * sin(uv.x+uv.y);
#include <displacementmap_vertex>
#include <morphtarget_vertex>
#include <skinning_vertex>
#include <project_vertex>
#include <logdepthbuf_vertex>
#include <clipping_planes_vertex>
vBrightness = aBrightness;
vViewPosition = - mvPosition.xyz;
#include <worldpos_vertex>
#include <shadowmap_vertex>
#include <fog_vertex>
}
`
super(parameters)
this.uniforms = THREE.UniformsUtils.merge([
THREE.ShaderLib.standard.uniforms,
uniforms
])
this.isMeshStandardMaterial = true
this.setFlags(this,fs,vs)
this.setValues(parameters)
}
setFlags(material, fs, vs) {
material.vertexShader = vs;
material.fragmentShader = fs;
material.type = 'MeshStandardMaterial';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment