Skip to content

Instantly share code, notes, and snippets.

@watab0shi
Last active February 5, 2021 12:13
Show Gist options
  • Select an option

  • Save watab0shi/d2116e0b395470ee5f920dcbe601bce8 to your computer and use it in GitHub Desktop.

Select an option

Save watab0shi/d2116e0b395470ee5f920dcbe601bce8 to your computer and use it in GitHub Desktop.
three.js resolve shader includes
import { PlaneGeometry } from 'three/src/geometries/PlaneGeometry';
import { MeshStandardMaterial } from 'three/src/materials/MeshStandardMaterial';
import { Mesh } from 'three/src/objects/Mesh';
import { resolveShaderIncludes } from './resolveShaderIncludes';
const geo = new PlaneGeometry();
const mat = new MeshStandardMaterial();
mat.onBeforeCompile = (shader) => {
const { vertexShader, fragmentShader } = shader;
const includedVertexShader = resolveShaderIncludes(vertexShader);
const includedFragmentShader = resolveShaderIncludes(fragmentShader);
console.log({ vertexShader, fragmentShader, includedVertexShader, includedFragmentShader });
};
const mesh = new Mesh(geo, mat);
import { ShaderChunk } from 'three/src/renderers/shaders/ShaderChunk.js';
const includePattern = /^[ \t]*#include +<([\w\d./]+)>/gm;
let includeReplacer = () => {};
const resolveShaderIncludes = ( string ) => {
return string.replace( includePattern, includeReplacer );
};
includeReplacer = ( match, include ) => {
const string = ShaderChunk[ include ];
if ( string === undefined ) {
throw new Error( 'Can not resolve #include <' + include + '>' );
}
return resolveShaderIncludes( string );
};
export { resolveShaderIncludes };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment