Beispiel #1
0
func NewCamera() *Camera {
	c := &Camera{
		core.NewObject3D(),
		Type:               "Camera",
		MatrixWorldInverse: math.NewMatrix4(),
		ProjectionMatrix:   math.NewMatrix4(),
	}
	c.LookAt = c.buildLookAt()
	return c
}
Beispiel #2
0
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
}
Beispiel #3
0
func (o *Object3D) buildLookAt() func(*math.Vector3) {
	// This routine does not support objects with rotated and/or translated parent(s)
	var m1 = math.NewMatrix4()
	return func(vector *math.Vector3) {
		m1.LookAt(vector, o.Position, o.Up)
		o.Quaternion.SetFromRotationMatrix(m1)
	}
}
Beispiel #4
0
func (c *Camera) buildLookAt() func(*math.Vector3) {
	// This routine does not support cameras with rotated and/or translated parent(s)
	m1 := math.NewMatrix4()
	return func(vector *math.Vector3) {
		m1.LookAt(c.Position, vector, c.Up)
		c.Quaternion.SetFromRotationMatrix(m1)
	}
}
Beispiel #5
0
func (g *Geometry) buildRotateY() func(angle float64) *Geometry {
	// rotate geometry around world y-axis
	var m1 math3d.Matrix4
	return func(angle float64) *Geometry {
		if m1 == nil {
			m1 = math3d.NewMatrix4()
		}
		m1.MakeRotationY(angle)
		g.ApplyMatrix(m1)
		return g
	}
}
Beispiel #6
0
func (g *Geometry) buildScale() func(float64, float64, float64) *Geometry {
	// scale geometry
	var m1 math3d.Matrix4
	return func(x, y, z float64) *Geometry {
		if m1 == nil {
			m1 = math3d.NewMatrix4()
		}
		m1.MakeScale(x, y, z)
		g.ApplyMatrix(m1)
		return g
	}
}
Beispiel #7
0
func (g *Geometry) buildTranslate() func(float64, float64, float64) *Geometry {
	// translate geometry
	var m1 math3d.Matrix4
	return func(x, y, z float64) *Geometry {
		if m1 == nil {
			m1 = math3d.NewMatrix4()
		}
		m1.MakeTranslation(x, y, z)
		g.ApplyMatrix(m1)
		return g
	}
}
Beispiel #8
0
func (g *Geometry) Normalize() *Geometry {
	g.ComputeBoundingSphere()

	center := g.BoundingSphere.Center
	radius := g.BoundingSphere.Radius

	s := 1
	if radius != 0 {
		s = 1.0 / radius
	}

	matrix := math3d.NewMatrix4()
	matrix.Set(
		s, 0, 0, -s*center.X,
		0, s, 0, -s*center.Y,
		0, 0, s, -s*center.Z,
		0, 0, 0, 1,
	)
	g.ApplyMatrix(matrix)
	return g
}