feat: Implement Translations (#19)

Also fixes bug: Translations and Rotations should not be inherited
  into inner grouping nodes.

Reviewed-on: #19
Co-authored-by: Glen Whitney <glen@studioinfinity.org>
Co-committed-by: Glen Whitney <glen@studioinfinity.org>
This commit is contained in:
Glen Whitney 2024-02-14 05:36:28 +00:00 committed by Glen Whitney
parent 1409d39665
commit a6a6e60894
3 changed files with 8 additions and 3 deletions

View File

@ -15,7 +15,7 @@ VRML 1.0. Overall, it recognizes and converts
* All Light nodes * All Light nodes
* Grouping nodes including Transform, Separator, Group, Switch, and WWWAnchor * Grouping nodes including Transform, Separator, Group, Switch, and WWWAnchor
* ShapeHints * ShapeHints
* Rotations * Rotations and Translations
* All shape nodes including Cube, Cone, Cylinder, Sphere, IndexedFaceSet, * All shape nodes including Cube, Cone, Cylinder, Sphere, IndexedFaceSet,
IndexedLineSet, PointSet, Coordinate3, and Normal IndexedLineSet, PointSet, Coordinate3, and Normal
* All material nodes including Material, TextureCoordinate2, and Texture2 * All material nodes including Material, TextureCoordinate2, and Texture2

View File

@ -1,6 +1,6 @@
{ {
name: 'vrml1to97', name: 'vrml1to97',
version: '0.3.0', version: '0.3.1',
description: 'JavaScript converter from VRML 1 to VRML97', description: 'JavaScript converter from VRML 1 to VRML97',
scripts: { scripts: {
test: 'echo "Error: no test specified" && exit 1', test: 'echo "Error: no test specified" && exit 1',

View File

@ -205,13 +205,16 @@ function parse(stream: Lexer, tree: DefTree = {}): DefTree
matches GroupNode matches GroupNode
parent := parent :=
held.value.endsWith('Separator') ? 'Transform' : 'Group' held.value.endsWith('Separator') ? 'Transform' : 'Group'
{children, ...context} := tree {children, Translation, Rotation, ...context} := tree
subTree := parse stream, context subTree := parse stream, context
if newKids := subTree.children if newKids := subTree.children
newChild .= `${parent} {\n ` newChild .= `${parent} {\n `
if 'Rotation' in subTree if 'Rotation' in subTree
newChild += renderList subTree.Rotation newChild += renderList subTree.Rotation
newChild += "\n " newChild += "\n "
if 'Translation' in subTree
newChild += renderList subTree.Translation
newChild += "\n "
newChild += `children [\n ${renderList newKids} ] }\n` newChild += `children [\n ${renderList newKids} ] }\n`
addChild newChild, tree addChild newChild, tree
'ShapeHints' 'ShapeHints'
@ -227,6 +230,8 @@ function parse(stream: Lexer, tree: DefTree = {}): DefTree
tree._definitions[lastDefinition] = [hints] tree._definitions[lastDefinition] = [hints]
'Rotation' 'Rotation'
tree.Rotation = toksUntilClose stream tree.Rotation = toksUntilClose stream
'Translation'
tree.Translation = toksUntilClose stream
'Coordinate3' 'Coordinate3'
tree.Coordinate = toksUntilClose stream tree.Coordinate = toksUntilClose stream
'Normal' 'Normal'