gokul/cm4-media-carrier

A tscircuit package created by gokul.

Version
0.1.0
License
unset
Stars
0

tools/micro-b-model.mjs

import { Document, NodeIO } from "@gltf-transform/core"

// Mechanical envelope only, from the EDAC connector drawing.
const document = new Document()
const buffer = document.createBuffer()
const scene = document.createScene()
const faces = [[0,3,2,1],[4,5,6,7],[0,1,5,4],[1,2,6,5],[2,3,7,6],[3,0,4,7]]
const normals = [[0,0,-1],[0,0,1],[0,-1,0],[1,0,0],[0,1,0],[-1,0,0]]
function box({ name, min, max, color }) {
  const vertices = [[min[0],min[1],min[2]],[max[0],min[1],min[2]],[max[0],max[1],min[2]],[min[0],max[1],min[2]],
    [min[0],min[1],max[2]],[max[0],min[1],max[2]],[max[0],max[1],max[2]],[min[0],max[1],max[2]]]
  const positions = faces.flatMap(face => face.flatMap(index => vertices[index]))
  const normalVectors = faces.flatMap((face,index) => face.flatMap(() => normals[index]))
  const indices = faces.flatMap((_,index) => [0,1,2,0,2,3].map(vertex => 4 * index + vertex))
  const position = document.createAccessor().setType("VEC3").setArray(new Float32Array(positions)).setBuffer(buffer)
  const normal = document.createAccessor().setType("VEC3").setArray(new Float32Array(normalVectors)).setBuffer(buffer)
  const triangles = document.createAccessor().setType("SCALAR").setArray(new Uint16Array(indices)).setBuffer(buffer)
  const material = document.createMaterial(name).setBaseColorFactor(color).setRoughnessFactor(0.6)
  const primitive = document.createPrimitive().setAttribute("POSITION",position).setAttribute("NORMAL",normal).setIndices(triangles).setMaterial(material)
  scene.addChild(document.createNode(name).setMesh(document.createMesh(name).addPrimitive(primitive)))
}
// EDAC 690-005-298-486: mechanical envelope, not vendor CAD.
box({name:"shell-top",min:[-3.75,-2.22,2.69],max:[3.75,2.78,2.94],color:[.65,.67,.69,1]})
box({name:"shell-left",min:[-3.75,-2.22,.05],max:[-3.5,2.78,2.69],color:[.65,.67,.69,1]})
box({name:"shell-right",min:[3.5,-2.22,.05],max:[3.75,2.78,2.69],color:[.65,.67,.69,1]})
box({name:"shell-bottom",min:[-3.5,-2.22,.05],max:[3.5,2.78,.3],color:[.65,.67,.69,1]})
box({name:"housing",min:[-3.4,1.9,.3],max:[3.4,2.78,2.69],color:[.07,.07,.08,1]})
box({name:"tongue",min:[-2.4,-1.8,1.3],max:[2.4,1.9,1.75],color:[.07,.07,.08,1]})
for(let i=0;i<5;i++) box({name:"contact-"+(i+1),min:[-1.44+.65*i,-1.6,1.25],max:[-1.16+.65*i,2.78,1.3],color:[.8,.61,.14,1]})
await new NodeIO().write("../smart-tv-board/models/micro-b.glb",document)