I can't seem to import a number of third-party models from SolidWorks into THREE.js with colors. The VRML file I export from SW does contain colors, but they are not shown in the scene. If I create a simple model in SolidWorks from scratch, everything is fine.
My quick investigation suggests that there are ways of specifying colors in VRML that VRMLLoader does not properly support. For example, does it support color specification per face? It seems to parse and add it to resulting BufferGeometry, but the object is colorless. Here's a shortened example of one problematic VRML file:
#VRML V2.0 utf8
Shape {
appearance Appearance {
material Material {
}
}
geometry IndexedFaceSet {
coord Coordinate {
point [ -0.006428 0.018390 -0.019135 0.007610 0.017604 -0.017431..... ]
}
colorPerVertex FALSE
color Color {
color [ 0.30 0.30 0.30 0.59 0.57 0.53 0.47 0.47 0.51 0.78 0.76 0.74 0.78 0.76 0.74 ]
}
colorIndex [ 0 0 .... ]
coordIndex [ 0 1 2 -1 ... ]
}
}
Can you please share a complete VRML file for testing?
Here you go:
castor_2.wrl.zip
The color attribute length is too short. It's either a problem with the file or the loader.
The color attribute length is too short. It's either a problem with the file or the loader.
Could you elaborate? SolidWorks VRML export is a mature feature. It may well be buggy, of course. Here's another example, all its shapes have "color" nodes with 3 elements, with a much longer (one per face?) "colorIndex" all set to 0. I tried removing all "color" nodes hoping that "diffuseColor" will be sufficient, but the object still comes out colorless.
wifi_antenna_1.wrl.zip
Here's a working example with seemingly the same VRML
structure: arm_1.wrl.zip
The semantic of colorPerVertex
is interesting (see here). If it is set to true
, the color data have to be interpreted as vertex colors. If it is set to false
like in your example files, they represent the color of a single face in context of IndexedFaceSet
.
After some debugging I think there are two problems in VRMLLoader
.
castor_2
, the coordIndex
has 1128
entries where each entry refers to a single color for a face. But it does not produce a correct color attribute since the processing in triangulateIndexArray()
provides a wrong output. The resulting array is 3378 entries long but it should be 3384.Material.vertexColor
property of the respective material.I've created a PR to solve both issues. I was able to load all three files correctly by comparing the render result with Blender.
Many thanks. I will do some thorough testing on it when it is merged.
This fixed the issue with the models above, but seems to have regressed on another model:
copper_part.wrl.zip It used to come out orange as intended before this update, now it comes out as black. It is a very basic model made from scratch in SolidWorks, with one color for the only geometry.
Can I suggest to make it an option, or even a default, to ignore per-vertex/per-face coloring? These models have correct per-shape color information in their Appearance nodes. The Color nodes seem to be an exporter artifact that adds nothing.
As a counter-example, here's a model with a single shape and per-face colors: wheel_65.wrl.zip It was partially fixed by this update: the six-ended "star" on the side surface is now properly black, but the rest of the model is colorless.
copper_part.wrl
is strange. It defines a color in the geometry section but no color indices. I could not find in one of the unofficial standard documents how this setup should be handled. For now, it's better to ignore the vertex colors in this case (see #15879).
The PR should fix copper_part.wrl
. wheel_65.wrl
seems to render correctly:
The color definition in the geometry section defines a lot of black colors.
Many thanks. You are right about wheel_65, sorry.
Regarding copper_part, to quote VRML 2.0 spec: "Color nodes are only used to specify multiple colors for a single piece of geometry, such as a different color for each face or vertex of an IndexedFaceSet. A Material node is used to specify the overall material parameters of a lighted geometry. If both a Material and a Color node are specified for a geometry, the colors should ideally replace the diffuse component of the material. " - http://graphcomp.com/info/specs/sgi/vrml/spec/part1/nodesRef.html#Color
In CAD, there's usually one material per part, so CAD VMRL exports should come out with no Color nodes. This looks like a bug in SolidWorks exporter.
Most helpful comment
The PR should fix
copper_part.wrl
.wheel_65.wrl
seems to render correctly:The color definition in the geometry section defines a lot of black colors.