public BufferGeometry4(Mesh[] Meshes, Matrix4x4 PositionTransform, int Version = 0) { metadata = new BufferGeometry4_Meta(); metadata.version = Version; data = new BufferGeometry4_Data(); data.attributes = new BufferGeometry4_Attributes(); ModifyVector3 ReorientatePosition = (ref Vector3 p) => { p = PositionTransform.MultiplyPoint(p); WorldUnityToThreeJs(ref p); /*gr: old method needed setting... * if ( PositionTransform.HasValue ) * { * var TransformedP = PositionTransform.Value * new Vector4( p.x, p.y, p.z, 1 ); * p.Set( TransformedP.x, TransformedP.y, TransformedP.z ); * } */ }; // serialise data for (int m = 0; m < Meshes.Length; m++) { var mesh = Meshes[m]; int FirstVertex = WriteAttribute(ref data.attributes.position, mesh.vertices, ReorientatePosition); WriteAttribute(ref data.attributes.normal, mesh.normals); WriteAttribute(ref data.attributes.uv, mesh.uv); WriteAttribute(ref data.attributes.uv2, mesh.uv2); WriteAttribute(ref data.attributes.color, mesh.colors); // gr: if an attrib is null, unity still serialises an "empty" class. if its empty.. three js will throw an exception... so make empty attribs PadAttribute(ref data.attributes.position); PadAttribute(ref data.attributes.normal); PadAttribute(ref data.attributes.uv); PadAttribute(ref data.attributes.uv2); PadAttribute(ref data.attributes.color); // write triangle indexes WriteAttribute(ref data.index, mesh.triangles, FirstVertex); } }
public BufferGeometry4(Mesh mesh, Matrix4x4 Transform, int Version = 0) { metadata = new BufferGeometry4_Meta(); metadata.version = Version; data = new BufferGeometry4_Data(); data.attributes = new BufferGeometry4_Attributes(); ModifyVector3 ReorientatePosition = (ref Vector3 p) => { p = Transform.MultiplyPoint(p); // +y is up in threejs (by default) //p.y = -p.y; // xz seems to be wrong too. Experimenting suggests just z needs correcting //p.x = -p.x; p.z = -p.z; }; // serialise data // note: we write them all WriteAttribute(ref data.attributes.position, mesh.vertices, ReorientatePosition); WriteAttribute(ref data.attributes.normal, mesh.normals); WriteAttribute(ref data.attributes.uv, mesh.uv); WriteAttribute(ref data.attributes.uv2, mesh.uv2); WriteAttribute(ref data.attributes.color, mesh.colors); // gr: if an attrib is null, unity still serialises an "empty" class. if its empty.. three js will throw an exception... so make empty attribs PadNullAttribute(ref data.attributes.position); PadNullAttribute(ref data.attributes.normal); PadNullAttribute(ref data.attributes.uv); PadNullAttribute(ref data.attributes.uv2); PadNullAttribute(ref data.attributes.color); // write triangle indexes - currently just a direct copy data.index = new BufferGeometry4_Attribute_Int(); data.index.array = mesh.triangles; }