Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function upgradeShaders(vertexShader, fragmentShader, instanceUniforms=[]) {
// Pre-expand includes
vertexShader = expandShaderIncludes(vertexShader)
fragmentShader = expandShaderIncludes(fragmentShader)
// See what gets used
let usesModelMatrix = modelMatrixRefRE.test(vertexShader)
let usesModelViewMatrix = modelViewMatrixRefRE.test(vertexShader)
let usesNormalMatrix = normalMatrixRefRE.test(vertexShader)
// Find what uniforms are declared in which shader and their types
let vertexUniforms = getShaderUniformTypes(vertexShader)
let fragmentUniforms = getShaderUniformTypes(fragmentShader)
let vertexDeclarations = [modelMatrixRowAttrs]
let vertexAssignments = []
let fragmentDeclarations = []
// Add variable assignments for, and rewrite references to, builtin matrices
if (usesModelMatrix || usesModelViewMatrix || usesNormalMatrix) {
vertexShader = vertexShader.replace(modelMatrixRefRE, attrRefReplacer)
vertexAssignments.push(modelMatrixVarAssignment)
}
if (usesModelViewMatrix || usesNormalMatrix) {
vertexShader = vertexShader.replace(modelViewMatrixRefRE, attrRefReplacer)
vertexAssignments.push(modelViewMatrixVarAssignment)
}
if (usesNormalMatrix) {
export function upgradeShaders(vertexShader, fragmentShader, instanceUniforms=[]) {
// Pre-expand includes
vertexShader = expandShaderIncludes(vertexShader)
fragmentShader = expandShaderIncludes(fragmentShader)
// See what gets used
let usesModelMatrix = modelMatrixRefRE.test(vertexShader)
let usesModelViewMatrix = modelViewMatrixRefRE.test(vertexShader)
let usesNormalMatrix = normalMatrixRefRE.test(vertexShader)
// Find what uniforms are declared in which shader and their types
let vertexUniforms = getShaderUniformTypes(vertexShader)
let fragmentUniforms = getShaderUniformTypes(fragmentShader)
let vertexDeclarations = [modelMatrixRowAttrs]
let vertexAssignments = []
let fragmentDeclarations = []
// Add variable assignments for, and rewrite references to, builtin matrices
if (usesModelMatrix || usesModelViewMatrix || usesNormalMatrix) {
vertexShader = vertexShader.replace(modelMatrixRefRE, attrRefReplacer)
vertexAssignments.push(modelMatrixVarAssignment)
}
if (usesModelViewMatrix || usesNormalMatrix) {
vertexShader = vertexShader.replace(modelViewMatrixRefRE, attrRefReplacer)
vertexAssignments.push(modelViewMatrixVarAssignment)
}
if (usesNormalMatrix) {
vertexShader = vertexShader.replace(normalMatrixRefRE, attrRefReplacer)
_getInstanceUniformsTypes(material) {
let cache = this._uniformTypesCache || (this._uniformTypesCache = Object.create(null)) //cache results for duration of this frame
let result = cache[material.id]
if (!result) {
result = cache[material.id] = Object.create(null)
let {instanceUniforms} = material
if (instanceUniforms && instanceUniforms.length) {
let {vertexShader, fragmentShader} = getShadersForMaterial(material)
let allTypes = assign(
getShaderUniformTypes(expandShaderIncludes(vertexShader)),
getShaderUniformTypes(expandShaderIncludes(fragmentShader))
) //TODO handle type mismatches?
for (let i = instanceUniforms.length; i--;) {
let uniform = instanceUniforms[i]
if (allTypes[uniform]) {
result[uniform] = allTypes[uniform]
}
}
}
}
return result
}
_getInstanceUniformsTypes(material) {
let cache = this._uniformTypesCache || (this._uniformTypesCache = Object.create(null)) //cache results for duration of this frame
let result = cache[material.id]
if (!result) {
result = cache[material.id] = Object.create(null)
let {instanceUniforms} = material
if (instanceUniforms && instanceUniforms.length) {
let {vertexShader, fragmentShader} = getShadersForMaterial(material)
let allTypes = assign(
getShaderUniformTypes(expandShaderIncludes(vertexShader)),
getShaderUniformTypes(expandShaderIncludes(fragmentShader))
) //TODO handle type mismatches?
for (let i = instanceUniforms.length; i--;) {
let uniform = instanceUniforms[i]
if (allTypes[uniform]) {
result[uniform] = allTypes[uniform]
}
}
}
}
return result
}