-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.go
171 lines (149 loc) · 4.18 KB
/
engine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"math"
"sync/atomic"
"time"
mol "github.com/LiterMC/molecular"
// "github.com/g3n/engine/core"
"github.com/g3n/engine/geometry"
"github.com/g3n/engine/graphic"
"github.com/g3n/engine/material"
"github.com/g3n/engine/math32"
"github.com/g3n/engine/util/helper"
)
var (
sunRad = 6.9634e8
earthRad = 6.371e6
moonRad = 1.7374e6
sunMass = 3.955e30
earthMass = 5.972e24
moonMass = 7.34767309e22
)
const posScale = 1 << 23
type PlanetBlock struct {
object atomic.Pointer[mol.Object]
mass float64
radius float64
outline *mol.Cube
lastDis float64
lastN int
// render
mat material.IMaterial
geo *geometry.Geometry
Node *graphic.Mesh
}
var _ mol.Block = (*PlanetBlock)(nil)
func NewPlanetBlock(mass float64, radius float64, mat material.IMaterial) *PlanetBlock {
return &PlanetBlock{
mass: mass,
radius: radius,
outline: mol.NewCubeFromCenter(mol.Vec3{radius * 2, radius * 2, radius * 2}),
mat: mat,
}
}
func (b *PlanetBlock) InitNode(dist float64) {
var n int = 16
if dist < b.radius {
n = 512
} else {
const (
camNear = 0.01
camFOV = 60.0
)
camConst := camNear / math.Tan(camFOV/2/180*math.Pi)
n = int(math.Sqrt(2*math.Pi*b.radius)*b.radius/dist*camConst + 8.5)
if n > 512 {
n = 512
}
}
if b.lastN == n {
return
}
b.lastN = n
if b.geo == nil {
b.geo = new(geometry.Geometry)
}
*b.geo = *geometry.NewSphere(b.radius/posScale, n, n)
if b.Node == nil {
b.Node = graphic.NewMesh(b.geo, b.mat)
b.Node.Add(helper.NewAxes(float32(b.radius) / posScale * 2))
}
}
func (b *PlanetBlock) SetObject(o *mol.Object) {
b.object.Store(o)
}
func (b *PlanetBlock) Mass() float64 {
return b.mass
}
func (b *PlanetBlock) Material(f mol.Facing) *mol.Material {
return nil
}
func (b *PlanetBlock) Outline() *mol.Cube {
return b.outline
}
func (b *PlanetBlock) Tick(dt float64) {
}
func (b *PlanetBlock) renderTick(r *Runner, dt time.Duration) {
cPos := r.cam.Position()
camPos := ToMolVec3(&cPos)
camPos.ScaleN(posScale)
obj := b.object.Load()
pos := obj.AbsPosLocked()
diff := pos.Subbed(camPos)
dist := diff.Len()
if b.lastDis == 0 {
b.InitNode(dist)
} else if d := dist - b.lastDis; d < -50 || 100 < d {
b.InitNode(dist)
b.lastDis = dist
return
}
pos.ScaleN(1. / posScale)
b.Node.SetPosition(float32(pos.X), float32(pos.Y), float32(pos.Z))
}
func InitPlanet(p *mol.Object, mass float64, radius float64, mat material.IMaterial, dist float64) (b *PlanetBlock) {
p.SetRadius(radius)
p.FillGfields()
b = NewPlanetBlock(mass, radius, mat)
pos := p.AbsPos()
pos.ScaleN(1. / posScale)
b.InitNode(dist)
b.Node.SetPosition(float32(pos.X), float32(pos.Y), float32(pos.Z))
p.AddBlock(b)
return
}
func (r *Runner) initSunMoon() {
sun := r.intEng.NewObject(mol.NaturalObj, nil, mol.Vec3{0, 0, 0}, func(sun *mol.Object) {
println("sun:", sun.String())
sun.SetVelocity(mol.Vec3{0, 1, 0})
sunColor := &math32.Color{1.0, 0.5, 0.2}
sunMat := material.NewStandard(sunColor)
// sunMat := material.NewPhysical().
// SetBaseColorFactor(&math32.Color4{sunColor.R, sunColor.G, sunColor.B, 1}).
// SetMetallicFactor(0).
// SetRoughnessFactor(1).
// SetEmissiveFactor(sunColor)
sunNode := InitPlanet(sun, sunMass, sunRad, sunMat,
r.playerObj.AbsPos().Subbed(sun.AbsPos()).Len()).Node
r.mainScene.Add(sunNode)
})
earth := r.intEng.NewObject(mol.NaturalObj, sun, mol.Vec3{-1.496e11, 0, 0}, func(earth *mol.Object) {
println("earth:", earth.String())
earth.SetVelocity(mol.Vec3{0, 0, 2.97222e4})
earthColor := &math32.Color{0.0, 0.0, 1.0}
earthMat := material.NewStandard(earthColor)
r.earth = InitPlanet(earth, earthMass, earthRad, earthMat,
r.playerObj.AbsPos().Subbed(earth.AbsPos()).Len()).Node
r.mainScene.Add(r.earth)
})
r.playerObj.AttachTo(earth)
r.playerObj.SetPos(mol.Vec3{-earthRad, earthRad + 1e7, 0})
r.playerObj.SetVelocity(mol.Vec3{0, 0, 0})
r.intEng.NewObject(mol.NaturalObj, earth, mol.Vec3{-3e8, 1e7, 0}, func(moon *mol.Object) {
println("moon:", moon.String())
moon.SetVelocity(mol.Vec3{0, 0, -1.022e3})
r.mainScene.Add(InitPlanet(moon, moonMass, moonRad,
material.NewStandard(&math32.Color{0.6, 0.6, 0.6}),
r.playerObj.AbsPos().Subbed(moon.AbsPos()).Len()).Node)
})
}