func (g *Geometry) ApplyMatrix(matrix *math3d.Matrix4) { normalMatrix := math3d.NewMatrix3().GetNormalMatrix(matrix) for _, v := range g.Vertices { v.ApplyMatrix4(matrix) } for _, face := range g.Faces { face.Normal.ApplyMatrix3(normalMatrix).Normalize() for _, vn := range face.VertexNormals { vn.ApplyMatrix3(normalMatrix).Normalize() } } if g.BoundingBox != nil { g.ComputeBoundingBox() } if g.BoundingSphere != nil { g.ComputeBoundingSphere() } g.VerticesNeedUpdate = true g.NormalsNeedUpdate = true }
func NewObject3D() *Object3D { Object3DIdCount++ object3d := Object3D{ NewEventDispatcher(), Id: Object3DIdCount, Uuid: math.GenerateUUID(), Name: "", Type: "Object3D", Parent: nil, Channels: NewChannels(), Children: make([]Object3D, 0), Up: DefaultUp.Clone(), Position: math.NewEmptyVector3(), Rotation: math.NewEmptyEuler(), Quaternion: math.NewEmptyQuaternion(), Scale: math.NewVector3(1.0, 1.0, 1.0), RotationAutoUpdate: true, Matrix: math.NewMatrix4(), MatrixWorld: math.NewMatrix4(), MatrixAutoUpdate: DefaultMatrixAutoUpdate, MatrixWorldNeedsUpdate: false, Visible: true, CastShadow: false, ReceiveShadow: false, FrustumCulled: true, RenderOrder: 0, UserData: make(map[string]string), ModelViewMatrix: math.NewMatrix4(), NormalMatrix: *math.NewMatrix3(), Geometry: nil, } onRotationChange := func() { object3d.Quaternion.SetFromEuler(object3d.Rotation, false) } onQuaternionChange := func() { object3d.Rotation.SetFromQuaternion(object3d.Quaternion, nil, false) } object3d.Rotation.OnChange(onRotationChange) object3d.Quaternion.OnChange(onQuaternionChange) object3d.GetWorldQuaternion = object3d.buildGetWorldQuaternion() object3d.GetWorldRotation = object3d.buildGetWorldRotation() object3d.GetWorldScale() = object3d.buildGetWorldScale() object3d.GetWorldDirection = object3d.buildGetWorldDirection() object3d.LookAt = object3d.buildLookAt() return &object3d }
func (g *Geometry) Merge(geometry *Geometry, matrix *math3d.Matrix4, materialIndexOffset int) { var normalMatrix *math3d.Matrix3 vertexOffset := len(g.Vertices) vertices1 := g.Vertices vertices2 := geometry.Vertices faces1 := g.Faces faces2 := geometry.Faces uvs1 := g.FaceVertexUvs[0] uvs2 := geometry.FaceVertexUvs[0] if materialIndexOffset == nil { materialIndexOffset = 0 } if matrix != nil { normalMatrix = math3d.NewMatrix3().GetNormalMatrix(matrix) } // vertices for _, vertex := range vertices2 { vertexCopy := vertex.Clone() if matrix != nil { vertexCopy.ApplyMatrix4(matrix) } vertices1 = append(vertices1, vertexCopy) } // faces for _, face := range faces2 { faceVertexNormals := face.VertexNormals faceVertexColors := face.VertexColors faceCopy := NewDefaultFace3(face.A+vertexOffset, face.B+vertexOffset, face.C+vertexOffset) faceCopy.Normal.Copy(face.Normal) if normalMatrix != nil { faceCopy.Normal.ApplyMatrix3(normalMatrix).Normalize() } for _, fn := range faceVertexNormals { normal := fn.Clone() if normalMatrix != nil { normal.ApplyMatrix3(normalMatrix).Normalize() } faceCopy.VertexNormals = append(faceCopy.VertexNormals, normal) } faceCopy.Color.Copy(face.Color) for _, color := range faceVertexColors { faceCopy.VertexColors = append(faceCopy.VertexColors, color.Clone()) } faceCopy.MaterialIndex = face.MaterialIndex + materialIndexOffset faces1 = append(faces1, faceCopy) } // uvs for _, uv := range uvs2 { if uv == nil { continue } uvCopy := make([]*math3d.Vector2, len(uv)) for _, u := range uv { uvCopy = append(uvCopy, u.Clone()) } uvs1 = append(uvs1, uvCopy) } }