-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewton_laws_gravity.js
254 lines (204 loc) · 5.8 KB
/
newton_laws_gravity.js
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import * as THREE from "three"
import {GUI as InputPanel} from "lil-gui";
import Stats from "stats.js"
import {OrbitControls} from "three/addons/controls/OrbitControls.js";
const log=console.log.bind(console);
const vec3a=new THREE.Vector3();
const vec3b=new THREE.Vector3();
const vec3c=new THREE.Vector3();
function Physics()
{
const objects=new WeakMap();
const dtFps=1/60.0;
this.speed=1;
const G=6.67*1e-11;
this.set=(object,properties)=>
{
const data={
gravityCast:false,
gravityReceive:false,
mass:1,
velocity:new THREE.Vector3(),
hardness:0,
};
for(let key in properties)
if(!(key in data))throw("there's no such physical property: "+key);
Object.assign(data,properties);
objects.set(object,data);
}
const vec3a=new THREE.Vector3(),
vec3b=new THREE.Vector3(),
vec3c=new THREE.Vector3();
this.do=scene=>{
const dt=this.speed*dtFps;
scene.traverse(obj=>{
const objList=obj.children;
const listLength=objList.length;
const data=objects.get(obj);
//I apply movement..(dx) changes
if(data){
vec3a.copy(data.velocity).multiplyScalar(dt)
obj.position.add(vec3a);
}
//you may use for(let j=i-1;.....
//it helps to avoid call map.get but causes to test gravity attributes at the end
//time to apply gravity
for(let i=0;i<listLength;i++)
{
const obj1=objList[i];
const data1=objects.get(obj1);
if(!data1) continue;
if(!data1.gravityCast)continue;
const gm=data1.mass*G;
for(let j=0;j<listLength;j++)
{
if(i==j)continue;
const obj2=objList[j];
const data2=objects.get(obj2);
if(!data2) continue;
if(!data2.gravityReceive)continue;
const dvec=vec3a;
dvec.subVectors(obj1.position,obj2.position);
const distanceSquared=dvec.lengthSq();
const acceleration=gm/(distanceSquared);
//now apply acc to v
const accVec=vec3b;
accVec.copy(dvec).setLength(acceleration*dt);
data2.velocity.add(accVec);
}//for j
}//for i
});//traverse
}//method
this.get=obj=>{
return objects.get(obj);
}
}
const stats=new Stats();
const customStat=new Stats.Panel("calls","#000","#ddd");
stats.addPanel(customStat)
document.body.appendChild(stats.dom);
stats.showPanel(0);
const inputPanel=new InputPanel();
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
camera.position.set(6,3,-12);
camera.lookAt(0,0,0);
scene.add(camera);
const renderer = new THREE.WebGLRenderer();
const orbitCon=new OrbitControls(camera,renderer.domElement );
orbitCon.enableDamping=true;
function resize(){
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setPixelRatio(devicePixelRatio);
camera.aspect=window.innerWidth/ window.innerHeight;
camera.updateProjectionMatrix()
}
resize();
addEventListener("resize",resize );
document.body.appendChild( renderer.domElement );
const physics=new Physics();
inputPanel.add(physics,"speed",0.25,10,0.25)
const sphereGeo=new THREE.SphereGeometry();
const uniforms={
planets:{value:[]}
}
function addPlanet(isStar, color ,radius, mass, pos , v)
{
let mat;
if(isStar)
mat=new THREE.MeshBasicMaterial({
color,
});
else
mat=new THREE.MeshStandardMaterial({
color,
roughness:0.3,
metalness:0,
});
let p=new THREE.Mesh(sphereGeo,mat);
if(!isStar){
p.castShadow=true;
p.receiveShadow=true;
}
p.scale.multiplyScalar(radius);
p.position.copy(pos);
physics.set(p,{mass,velocity:v.clone(), gravityCast:true, gravityReceive:true});
scene.add(p);
uniforms.planets.value.push({
pos:p.position,
mass,
});
addPlanet.count++;
return p;
}
addPlanet.count=0;
const sun=addPlanet(true,0xffff1f,1,1e10,vec3a.set(0,0,0),vec3b.set(0,0,0) );
addPlanet(false,0x8585ff,0.3,2e9,vec3a.set(0,0,8.7),vec3b.set(0.3,0.,0) );
addPlanet(false,0xf5f5ff,0.1,1,vec3a.set(0,0,8.0),vec3b.set(0.73,0.,0) );
const planeGeo=new THREE.PlaneGeometry(40,40,100,100);
planeGeo.rotateX(-Math.PI/2);
const material=new THREE.PointsMaterial({
size:0.1,
onBeforeCompile(s){
Object.assign(s.uniforms, uniforms)
s.vertexShader=`
struct Planet{
float mass;
vec3 pos;
};
uniform Planet planets[${addPlanet.count}];
${s.vertexShader}
`.replace("#include <begin_vertex>",`
#include <begin_vertex>
vec3 g=vec3(0.0);
vec3 d;
#pragma unroll_loop_start
for(int i=0;i<${addPlanet.count};i++){
d = position - planets[i].pos;
g+=(5e-10*planets[i].mass/dot(d,d))*d;
}
#pragma unroll_loop_end
transformed.y-=smoothstep(0.,3.,length(g));
`);
s.fragmentShader=s.fragmentShader.replace("#include <clipping_planes_fragment>",`
#include <clipping_planes_fragment>
//makes points round
/*{
if(length(gl_PointCoord-0.5)>0.5)discard;
}*/
//avoid implicit square root in length function
{
vec2 pd=gl_PointCoord-0.5;
if(dot(pd,pd)>0.25)discard;
}
`);
}});
const plane=new THREE.Points(planeGeo, material );
scene.add(plane);
plane.translateY(-1.0)
const light=new THREE.PointLight();
scene.add(light);
renderer.shadowMap.enabled=true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
light.shadow.mapSize.width = 256; // default 512
light.shadow.mapSize.height = 256; // default 512
light.shadow.camera.near = 0.5; // default 0.5
light.shadow.camera.far = 500; //
light.castShadow = true;
renderer.setAnimationLoop(animate);
function animate(time)
{
physics.do(scene);
orbitCon.update();
light.position.copy(sun.position);
renderer.render( scene, camera);
//Do you feel the points are big or non-perspective ? If yes, let me know.
//Do you feel some suddenly slow frames after a few 60fps seconds ?
//If yes let me know , tell me in the issues
stats.update();
}
//On second frame anything has been almost done
requestAnimationFrame(t=>
requestAnimationFrame(t=>
log(`done at ${Math.floor(performance.now())}ms`)
));