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) {
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
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)
// Put the instance's world matrix into the batch geometry's instancing attributes
let attrOffset = batchGeometry.maxInstancedCount++
let attrs = batchGeometry._instanceAttrs.matrix
let elements = instanceObject.matrixWorld.elements //column order
attrs[0].setXYZW(attrOffset, elements[0], elements[4], elements[8], elements[12])
attrs[1].setXYZW(attrOffset, elements[1], elements[5], elements[9], elements[13])
attrs[2].setXYZW(attrOffset, elements[2], elements[6], elements[10], elements[14])
// Put the instance's values for instanceUniforms into the corresponding attributes
if (instanceUniforms) {
attrs = batchGeometry._instanceAttrs.uniforms
for (let i = instanceUniforms.length; i--;) {
let uniform = instanceUniforms[i]
let attr = attrs[uniform]
let facadeUniforms = facade._instanceUniforms
let value = facadeUniforms && uniform in facadeUniforms ? facadeUniforms[uniform] : getShadersForMaterial(protoObject.material).uniforms[uniform].value //TODO clean up
setAttributeValue(attr, attrOffset, value)
}
}
// Save pointers for possible reuse next frame
facade._instancingBatchObject = batchObject
facade._instancingBatchAttrOffset = attrOffset
} else {
facade._instancingBatchObject = facade._instancingBatchAttrOffset = null
}
}
// Dispose any old batch geometries that were unused during this render pass
// TODO should this be delayed any to prevent thrashing?
geometryPool.disposeUnused()
}
_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
}
_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
}
_getBatchKey(object) {
let cache = this._batchKeysCache || (this._batchKeysCache = Object.create(null)) //cache results for duration of this frame
let key = cache && cache[object.id]
if (!key) {
let mat = object.material
let shaders = getShadersForMaterial(mat)
let uniforms = mat.instanceUniforms
key = `${object.geometry.id}|${mat.id}|${shaders.vertexShader}|${shaders.fragmentShader}|${uniforms ? uniforms.sort().join(',') : ''}`
cache[object.id] = key
}
return key
}
constructor(parent) {
super(parent, new BezierMesh())
this.radius = 0.01
this.opacity = 1
this.color = 0xffffff
this.dashArray = [0, 0]
this.dashOffset = 0
}