Skip to content

Instantly share code, notes, and snippets.

@Busata
Created February 2, 2018 09:16
Show Gist options
  • Save Busata/513126fd68eb755462c5531c8e4745d8 to your computer and use it in GitHub Desktop.
Save Busata/513126fd68eb755462c5531c8e4745d8 to your computer and use it in GitHub Desktop.
import {ShaderLib, ShaderMaterial, UniformsUtils, Vector4} from "three";
const ParallaxShader = ShaderLib.standard;
export class ParallaxMaterial extends ShaderMaterial {
constructor(parameters) {
super(parameters);
this.lights = true;
}
public static createFrom(meshStandardMaterial) {
let shaderParams = copyFrom(meshStandardMaterial);
return new ParallaxMaterial(shaderParams);
}
}
function copyFrom(source) {
let uniforms = UniformsUtils.clone(ParallaxShader.uniforms);
let vertexShader = ParallaxShader.vertexShader;
let fragmentShader = ParallaxShader.fragmentShader;
uniforms.diffuse.value = source.color;
uniforms.roughness.value = source.roughness;
uniforms.metalness.value = source.metalness;
uniforms.map.value = source.map;
uniforms.offsetRepeat = {value: new Vector4( source.map.offset.x, source.map.offset.y, source.map.repeat.x, source.map.repeat.y) };
uniforms.lightMap.value = source.lightMap;
uniforms.lightMapIntensity.value = source.lightMapIntensity;
uniforms.aoMap.value = source.aoMap;
uniforms.aoMapIntensity.value = source.aoMapIntensity;
uniforms.emissive.value = source.emissive.multiply(source.emissiveIntensity);
uniforms.emissiveMap.value = source.emissiveMap;
uniforms.bumpMap.value = source.bumpMap;
uniforms.bumpScale.value = source.bumpScale;
uniforms.normalMap.value = source.normalMap;
uniforms.normalScale.value = source.normalScale;
uniforms.displacementMap.value = source.displacementMap;
uniforms.displacementScale.value = source.displacementScale;
uniforms.displacementBias.value = source.displacementBias;
uniforms.roughnessMap.value = source.roughnessMap;
uniforms.metalnessMap.value = source.metalnessMap;
uniforms.alphaMap.value = source.alphaMap;
uniforms.envMap.value = source.envMap;
uniforms.envMapIntensity.value = source.envMapIntensity;
uniforms.refractionRatio.value = source.refractionRatio;
return {
name: 'ParallaxShader',
uniforms,
defines: {
"STANDARD": ''
},
vertexShader,
fragmentShader
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment