How to use the troika-three-utils.getShadersForMaterial function in troika-three-utils

To help you get started, we’ve selected a few troika-three-utils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github protectwise / troika / packages / troika-3d / src / facade / instancing / InstancingManager.js View on Github external
// 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()
    }
github protectwise / troika / packages / troika-3d / src / facade / instancing / InstancingManager.js View on Github external
_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
  }
github protectwise / troika / packages / troika-3d / src / facade / instancing / InstancingManager.js View on Github external
_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
  }

troika-three-utils

Various utilities related to Three.js

MIT
Latest version published 1 month ago

Package Health Score

83 / 100
Full package analysis

Similar packages