Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

X3D serializer: Material RGB colors #1232

Merged
merged 6 commits into from
Apr 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions packages/io/x3d-serializer/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Notes:
const { geometries, modifiers } = require('@jscad/modeling')
const { geom2, geom3, path2, poly2, poly3 } = geometries

const { flatten, toArray } = require('@jscad/array-utils')
const { flatten } = require('@jscad/array-utils')

const stringify = require('onml/lib/stringify')

Expand Down Expand Up @@ -89,11 +89,11 @@ const serialize = (options, ...objects) => {
body.push(['head', {},
['meta', { name: 'creator', content: 'Created by JSCAD' }],
['meta', { name: 'reference', content: 'https://www.openjscad.xyz' }],
['meta', { name: 'created', content: new Date().toISOString()}]
['meta', { name: 'created', content: new Date().toISOString() }]
])
} else {
body.push(['head', {},
['meta', { name: 'creator', content: 'Created by JSCAD' }],
['meta', { name: 'creator', content: 'Created by JSCAD' }]
])
}
body = body.concat(convertObjects(objects, options))
Expand Down Expand Up @@ -138,7 +138,7 @@ const convertObjects = (objects, options) => {
const convertPath2 = (object, options) => {
const points = path2.toPoints(object).slice()
if (points.length > 1 && object.isClosed) points.push(points[0])
shape = ['Shape', {}, convertPolyline2D(poly2.create(points), options)]
const shape = ['Shape', {}, convertPolyline2D(poly2.create(points), options)]
if (object.color) {
shape.push(convertAppearance(object, options))
}
Expand Down Expand Up @@ -167,13 +167,18 @@ const convertGeom2 = (object, options) => {
*/
const convertPolyline2D = (object, options) => {
const lineSegments = object.vertices.map((p) => `${p[0]} ${p[1]}`).join(' ')
return ['Polyline2D', {lineSegments}]
return ['Polyline2D', { lineSegments }]
}

/*
* Convert color to Appearance
*/
const convertAppearance = (object, options) => {
const diffuseColor = object.color.join(' ')
const emissiveColor = object.color.join(' ')
return ['Appearance', ['Material', {diffuseColor, emissiveColor}]]
const colorRGB = object.color.slice(0, 3)
const diffuseColor = colorRGB.join(' ')
const emissiveColor = colorRGB.join(' ')
const transparency = 1.0 - object.color[3]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late change. I had never pushed this correction.

return ['Appearance', ['Material', { diffuseColor, emissiveColor, transparency }]]
}

/*
Expand All @@ -198,9 +203,9 @@ const convertMesh = (object, options) => {
const faceset = [
'IndexedTriangleSet',
{ ccw: 'true', colorPerVertex: 'false', solid: 'false', index: indexList },
['Coordinate', { point: pointList }],
['Coordinate', { point: pointList }]
]
if (! object.color) {
if (!object.color) {
faceset.push(['Color', { color: colorList }])
}
return faceset
Expand Down
7 changes: 3 additions & 4 deletions packages/io/x3d-serializer/tests/geom3ToX3D.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ test('serialize 3D geometry to X3D IndexedTriangleSet', (t) => {
t.is(countOf('Coordinate', obs), 1)
t.is(countOf('Color', obs), 1)


const geom3 = colors.colorize([0.5, 1, 0.5, 1.0], transforms.center({ relativeTo: [5, 5, 5] }, primitives.cube()))

results = serializer.serialize({metadata: false}, geom2, geom3)
Expand All @@ -59,6 +58,6 @@ test('serialize 3D geometry to X3D IndexedTriangleSet', (t) => {
// for color
t.is(countOf('Color', obs), 3)
t.is(countOf('Appearance', obs), 2)

})

// check RGB
t.is(countOf('diffuseColor="0.5 1 0.5"', obs), 1)
})