How to use the troika-3d.createDerivedMaterial function in troika-3d

To help you get started, we’ve selected a few troika-3d 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-examples / arcs / ArcFacade.js View on Github external
const customShaderMaterial = new ShaderMaterial({
  uniforms: {
    color: { value: baseColor },
    opacity: { value: 1 },
    startAngle: { value: 0 },
    endAngle: { value: Math.PI / 2 },
    startRadius: { value: 1 },
    endRadius: { value: 2 }
  },
  vertexShader: arcVertexShader,
  fragmentShader: arcFragmentShader,
  transparent: true
})


const derivedMaterial = createDerivedMaterial(
  new MeshStandardMaterial({
    transparent: true
  }),
  // baseMaterial1,
  {
    uniforms: {
      startAngle: { value: 0 },
      endAngle: { value: Math.PI / 2 },
      startRadius: { value: 1 },
      endRadius: { value: 2 }
    },
    vertexDefs: `
      uniform float startAngle;
      uniform float endAngle;
      uniform float startRadius;
      uniform float endRadius;
github protectwise / troika / packages / troika-examples / arcs / ArcFacade.js View on Github external
position = vec3(
        cos(angle) * radius,
        sin(angle) * radius,
        position.z
      );
      
      // Rotate the normal by the same angle so lighting is correct
      float normalRotZ = angle - PI2;
      normal = normalize(vec3(
        vec4(normal, 1.0) * mat4( cos(normalRotZ), -sin(normalRotZ), 0, 0, sin(normalRotZ), cos(normalRotZ), 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 )
      ));
    `
  }
)

const doubleDerivedMaterial = createDerivedMaterial(derivedMaterial, {
  vertexDefs: `
    varying vec2 vXY;
  `,
  vertexTransform: `
    vXY = vec2(position);
  `,
  fragmentDefs: `
    varying vec2 vXY;
  `,
  fragmentColorTransform: `
    gl_FragColor.x *= 6.0 * (vXY.x + .5);
    gl_FragColor.y *= 2.0 * (vXY.y + .5);
    if (length(vXY) < 0.2) {
      discard;
    }
  `
github protectwise / troika / packages / troika-examples / globe-connections / Globe.js View on Github external
// Coords may be from a single 'Polygon' or a 'MultiPolygon', handle either
    if (typeof countryCoordData[0][0][0] === 'number') {
      handleCoordsGroup(countryCoordData)
    } else {
      countryCoordData.forEach(handleCoordsGroup)
    }
  })
})
const countryBordersGeometry = new BufferGeometry()
const positionAttr = new BufferAttribute(new Float32Array(lineSegmentPositions), 3)
countryBordersGeometry.setAttribute('position', positionAttr)
countryBordersGeometry.setAttribute('normal', positionAttr) //positions are based off r=1 so they can be used directly as normals

const sphereGeometry = new SphereBufferGeometry(1, 32, 24)
const sphereMaterial = createDerivedMaterial(new MeshBasicMaterial({
  color: 0x6666ff,
  transparent: true,
  opacity: 0.6,
  side: BackSide
}), {
  vertexDefs: 'varying vec3 vCameraSpaceNormal;',
  vertexMainIntro: `vCameraSpaceNormal = normalize((modelViewMatrix * vec4(normal, 0.0)).xyz);`,
  fragmentDefs: 'varying vec3 vCameraSpaceNormal;',
  fragmentColorTransform: `gl_FragColor.w = 0.15 + 0.5 * pow(length(cross(vCameraSpaceNormal, vec3(0.,0.,1.))), 8.0);`
})

class Globe extends Object3DFacade {
  constructor (parent) {
    const lines = new LineSegments(
      countryBordersGeometry,
      new MeshStandardMaterial({
github protectwise / troika / packages / troika-3d-ui / src / facade / UIBlockLayerDerivedMaterial.js View on Github external
export function createUIBlockLayerDerivedMaterial(baseMaterial, isBorder) {
  const material = createDerivedMaterial(baseMaterial, {
    defines: {
      [`TROIKA_UI_${isBorder ? 'BORDER' : 'BG'}`]: ''
    },
    extensions: {
      derivatives: true
    },
    uniforms: {
      uTroikaBlockSize: {value: new Vector2()},
      uTroikaClipRect: {value: new Vector4()},
      uTroikaCornerRadii: {value: new Vector4()},
      uTroikaBorderWidth: {value: new Vector4()}
    },
    vertexDefs: VERTEX_DEFS,
    vertexTransform: VERTEX_TRANSFORM,
    fragmentDefs: FRAGMENT_DEFS,
    fragmentColorTransform: FRAGMENT_COLOR_TRANSFORM
github protectwise / troika / packages / troika-examples / text / TextExample.jsx View on Github external
Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.

  But, in a larger sense, we can not dedicate — we can not consecrate — we can not hallow — this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us — that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion — that we here highly resolve that these dead shall not have died in vain — that this nation, under God, shall have a new birth of freedom — and that government of the people, by the people, for the people, shall not perish from the earth.

Abraham Lincoln
November 19, 1863`,
  'ABC123': 'abc def ghi jkl mno pqrs tuv wxyz ABC DEF GHI JKL MNO PQRS TUV WXYZ !"§ $%& /() =?* \'<> #|; ²³~ @`´ ©«» ¤¼× {} abc def ghi jkl mno pqrs tuv wxyz ABC DEF GHI JKL MNO PQRS TUV WXYZ !"§ $%& /() =?* \'<> #|; ²³~ @`´ ©«» ¤¼× {} abc def ghi jkl mno pqrs tuv wxyz ABC DEF GHI JKL MNO PQRS TUV WXYZ !"§ $%& /() =?* \'<> #|; ²³~ @`´ ©«» ¤¼× {} abc def ghi jkl mno pqrs tuv wxyz ABC DEF GHI JKL MNO PQRS TUV WXYZ !"§ $%& /() =?* \'<> #|; ²³~ @`´ ©«» ¤¼× {} abc def ghi jkl mno pqrs tuv wxyz ABC DEF GHI JKL MNO PQRS TUV WXYZ !"§ $%& /() =?* \'<> #|; ²³~ @`´ ©«» ¤¼× {} abc def ghi jkl mno pqrs tuv wxyz ABC DEF GHI JKL MNO PQRS TUV WXYZ !"§ $%& /() =?* \'<> #|; ²³~ @`´ ©«» ¤¼× {}',
  [CUSTOM_LBL]: 'Edit me!'
}

const TEXTURE = new TextureLoader().load('shader-anim/lava.jpg')
const MATERIALS = {
  'MeshBasicMaterial': new MeshBasicMaterial(),
  'MeshStandardMaterial': new MeshStandardMaterial(),
  'Custom Vertex Shader': createDerivedMaterial(new MeshStandardMaterial(), {
    timeUniform: 'elapsed',
    vertexTransform: `
      float waveAmplitude = 0.1;
      float waveX = uv.x * PI * 4.0 - mod(elapsed / 300.0, PI2);
      float waveZ = sin(waveX) * waveAmplitude;
      normal.xyz = normalize(vec3(-cos(waveX) * waveAmplitude, 0.0, 1.0));
      position.z += waveZ;
    `
  })
}
const MATERIAL_OPTS = Object.keys(MATERIALS)
Object.keys(MATERIALS).forEach(name => {
  MATERIALS[name + '+Texture'] = MATERIALS[name].clone()
  MATERIALS[name + '+Texture'].map = TEXTURE
})

troika-3d

Troika 3D

MIT
Latest version published 1 month ago

Package Health Score

75 / 100
Full package analysis

Similar packages