-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
262 lines (231 loc) · 8.41 KB
/
index.html
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
253
254
255
256
257
258
259
260
261
262
<!-- TP: Introduction WebGL - Lucas Gandel et Danaële Puechmaille - 5ETI CPE LYON
License : Apache 2.0.-->
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Babylon - Basic scene</title>
<style>
html, body
{
overflow: hidden;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
#renderCanvas
{
width: 100%;
height: 100%;
touch-action: none;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babylonjs/2.4.1/babylon.js"></script>
<script src="waterMaterial_babylon_extension/babylon.waterMaterial.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/cannon.js/0.6.2/cannon.min.js"></script>
</head>
<body>
<canvas id="renderCanvas"></canvas>
<script type="text/javascript">
// Get the canvas element from our HTML below
var canvas = document.querySelector("#renderCanvas");
// Load the BABYLON 3D engine
var engine = new BABYLON.Engine(canvas, true);
// Scene global variable
var sphere;
var camera;
var skybox;
var waterBool = false;//Indicate wether or not to draw water
//******************//
// Clamp
//******************//
var clamp = function( nb, min, max )
{
if( nb < min )
return min;
if( nb > max )
return max;
return nb;
}
//******************//
// Key Down Event
//******************//
var OnKeyDown = function( event )
{
// Compute cameraDirection
var cameraDirection = new BABYLON.Vector3(camera.position.x - sphere.position.x,0,camera.position.z - sphere.position.z);
cameraDirection = cameraDirection.normalize();
var up = new BABYLON.Vector3(0, 1, 0);
switch( event.keyCode )
{
// Update velocity using "zqsd" keys
case 38: //UP
sphere.physicsImpostor.applyImpulse( cameraDirection.negate(), sphere.getAbsolutePosition() );
break;
case 40: //DOWN
sphere.physicsImpostor.applyImpulse( cameraDirection, sphere.getAbsolutePosition() );
break;
case 39: //RIGHT
sphere.physicsImpostor.applyImpulse( BABYLON.Vector3.Cross(cameraDirection, up), sphere.getAbsolutePosition() );
break;
case 37: //LEFT
sphere.physicsImpostor.applyImpulse( BABYLON.Vector3.Cross(cameraDirection, up).negate(), sphere.getAbsolutePosition() );
break;
// Reset sphere position and velocity
case 32: //SPACE
sphere.position = new BABYLON.Vector3( 0, 20, 0 );
sphere.physicsImpostor.setLinearVelocity( new BABYLON.Vector3( 0, 0, 0 ) );
sphere.physicsImpostor.setAngularVelocity( new BABYLON.Vector3( 0, 0, 0 ) );
break;
// Water effect active / disabled
case 87: //KEY_W
waterBool = !waterBool;
if(waterBool)
{
scene = createScene();
}
else
{
scene = createScene();
}
break;
}
//Clamp velocity
speedLimit = 10;
x_velocity = clamp( sphere.physicsImpostor.getLinearVelocity().x, -speedLimit, speedLimit );
y_velocity = clamp( sphere.physicsImpostor.getLinearVelocity().y, -speedLimit, speedLimit );
z_velocity = clamp( sphere.physicsImpostor.getLinearVelocity().z, -speedLimit, speedLimit );
sphere.physicsImpostor.setLinearVelocity( new BABYLON.Vector3( x_velocity, y_velocity, z_velocity ) );
}
//*****************************//
// Create Instruction Canvas
//*****************************//
var createInstructionCanvas = function (scene)
{
var text2D = "ROLL ON EARTH \n\nSPACE : restart \nARROWS : move sphere \nw : activate / desactivate water effect"
var canvas = new BABYLON.ScreenSpaceCanvas2D( scene, {
id: "canvas",
size: new BABYLON.Size(400, 150),
backgroundFill: "#4040408F",
backgroundRoundRadius: 50,
children: [ new BABYLON.Text2D( text2D, {
id: "InstructionText",
marginAlignment: "h: center, v:center",
fontName: "15pt Arial" } ) ] } );
return canvas;
};
//****************//
// Create Scene
//****************//
var createScene = function()
{
var scene = new BABYLON.Scene(engine);
// Enable physics
var gravityVector = new BABYLON.Vector3( 0,-9.81, 0 );
var physicsPlugin = new BABYLON.CannonJSPlugin();
scene.enablePhysics( gravityVector, physicsPlugin );
//Sphere
sphere = BABYLON.Mesh.CreateSphere( "sphere", 10, 10, scene );
sphere.position.y = 20;
var sphereMaterial = new BABYLON.StandardMaterial( "sphere", scene );
sphereMaterial.diffuseTexture = new BABYLON.Texture( "data/earth.jpg", scene );
sphere.material = sphereMaterial;
sphere.physicsImpostor = new BABYLON.PhysicsImpostor( sphere,
BABYLON.PhysicsImpostor.SphereImpostor,
{ mass: 1, restitution: 0, friction: 0.1 },
scene );
// Ground
var ground = BABYLON.Mesh.CreateGroundFromHeightMap( "ground",
"data/worldHeightMap.jpg",
200, 200,
250,
0, 10,
scene,
false,
function()
{
ground.setPhysicsState(BABYLON.PhysicsEngine.HeightmapImpostor, { mass: 0 });
} );
var groundMaterial = new BABYLON.StandardMaterial( "ground", scene );
groundMaterial.diffuseTexture = new BABYLON.Texture( "data/earth.jpg", scene );
ground.material = groundMaterial;
// Camera
camera = new BABYLON.TargetCamera("Camera", new BABYLON.Vector3(0, 20, 30), scene);
camera.lockedTarget = sphere;
// Skybox
var skybox = BABYLON.Mesh.CreateBox( "skyBox", 800.0, scene );
var skyboxMaterial = new BABYLON.StandardMaterial( "skyBox", scene );
skyboxMaterial.backFaceCulling = false;
skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture( "data/sky11", scene );
skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
skyboxMaterial.diffuseColor = new BABYLON.Color3( 0, 0, 0 );
skyboxMaterial.specularColor = new BABYLON.Color3( 0, 0, 0 );
skyboxMaterial.disableLighting = true;
skybox.material = skyboxMaterial;
// Light
var spot = new BABYLON.PointLight( "spot", new BABYLON.Vector3(0, 30, 10), scene );
spot.diffuse = new BABYLON.Color3( 1, 1, 1 );
spot.specular = new BABYLON.Color3( 0, 0, 0 );
//Sphere to see the light's position
var sun = BABYLON.Mesh.CreateSphere( "sun", 10, 4, scene );
sun.material = new BABYLON.StandardMaterial( "sun", scene );
sun.material.emissiveColor = new BABYLON.Color3( 1, 1, 0 );
//Sun animation
var t = 180;
var dt = 0.01;
var R = 100;
scene.registerBeforeRender(
function()
{
// Update mesh position
sun.position = spot.position;
// Update light position
t -= dt;
x = R * Math.cos(t);
y = R * Math.sin(t);
spot.position = new BABYLON.Vector3( x, y, 0 );
// Decrease light intensity when sun is set to avoid shadows on the sphere
if( spot.position.y < 0 )
{
spot.intensity -= 0.05*Math.cos(t) ;
t -= 5*dt;
}
else
{
spot.intensity = 1;
}
} );
// Water effect
if( waterBool ) // if water effect is active
{
var waterMesh = BABYLON.Mesh.CreateGround( "waterMesh", 2048, 2048, 16, scene, false );
var water = new BABYLON.WaterMaterial( "water", scene, new BABYLON.Vector2(512, 512) );
water.backFaceCulling = true;
water.bumpTexture = new BABYLON.Texture( "data/bump.jpg", scene );
water.windForce = 0.5;
water.waveHeight = 0.007;
water.bumpHeight = 0.1;
water.windDirection = new BABYLON.Vector2( 1, 1 );
water.waterColor = new BABYLON.Color3( 0, 0, 221/255 );
water.colorBlendFactor = 0.0;
water.addToRenderList( skybox );
water.addToRenderList( ground );
waterMesh.material = water;
waterMesh.position = new BABYLON.Vector3(0,0.48,0);
}
// Show Instructions canvas
createInstructionCanvas( scene );
return scene;
}
document.onkeydown = OnKeyDown;
// Now, call the createScene function that you just finished creating
var scene = createScene();
console.log(sphere);
// Register a render loop to repeatedly render the scene
engine.runRenderLoop( function(){ scene.render(); } );
// Watch for browser/canvas resize events
window.addEventListener( "resize", function(){ engine.resize(); } );
</script>
</body>
</html>