awacke1 commited on
Commit
29c5a83
·
1 Parent(s): 099850b

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +72 -46
index.html CHANGED
@@ -2,7 +2,7 @@
2
  <html>
3
  <head>
4
  <meta charset="utf-8" />
5
- <title>Babylon.js Game Example</title>
6
  <script src="https://cdn.babylonjs.com/babylon.js"></script>
7
  <script src="https://cdn.babylonjs.com/loaders/babylonjs.loaders.min.js"></script>
8
  <style>
@@ -30,55 +30,81 @@
30
  // Load the texture and bump map
31
  var texture = new BABYLON.Texture("https://www.babylonjs-playground.com/textures/floor.png", scene);
32
  var bumpMap = new BABYLON.Texture("https://www.babylonjs-playground.com/textures/floor_n.jpg", scene);
33
- // Create a fountain of spheres that follow the mouse
34
- canvas.addEventListener('mousemove', function (event) {
35
- // Create a new sphere
36
- var sphere = BABYLON.MeshBuilder.CreateSphere("sphere", {diameter: Math.random() * 2 + 0.5, segments: 16}, scene);
37
- sphere.position = new BABYLON.Vector3(Math.random() * 4 - 2, Math.random() * 4 - 2, Math.random() * 4 - 2);
38
- sphere.material = new BABYLON.StandardMaterial("texture", scene);
39
- sphere.material.diffuseTexture = texture;
40
- sphere.material.bumpTexture = bumpMap;
41
- spheres.push(sphere);
42
- // Follow the mouse with the spheres
43
- var ray = scene.createPickingRay(event.clientX, event.clientY, BABYLON.Matrix.Identity(), camera);
44
- var pickInfo = scene.pickWithRay(ray, function (mesh) { return mesh == sphere; });
45
- if (pickInfo.hit) {
46
- sphere.position.copyFrom(pickInfo.pickedPoint);
 
 
 
 
 
47
  }
48
- // Animate the texture of the spheres
49
- var textureAnimation = new BABYLON.Animation("textureAnimation", "material.diffuseTexture.uOffset", 60, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
50
- var keys = [];
51
- keys.push({frame: 0, value: 0});
52
- keys.push({frame: 100, value: 1});
53
- textureAnimation.setKeys(keys);
54
- sphere.material.diffuseTexture.wrapU = BABYLON.Texture.WRAP_ADDRESSMODE;
55
- sphere.material.diffuseTexture.wrapV = BABYLON.Texture.WRAP_ADDRESSMODE;
56
- sphere.material.diffuseTexture.uScale = 10;
57
- sphere.material.diffuseTexture.vScale = 10;
58
- sphere.animations.push(textureAnimation);
59
- scene.beginAnimation(sphere, 0, 100, true);
60
- // Animate the spheres
61
- var animation = new BABYLON.Animation("myAnimation", "position.y", 30, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
62
- var keys = [];
63
- keys.push({frame: 0, value: sphere.position.y});
64
- keys.push({frame: 50, value: sphere.position.y + 2});
65
- keys.push({frame: 100, value: sphere.position.y});
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  animation.setKeys(keys);
67
  sphere.animations.push(animation);
68
  scene.beginAnimation(sphere, 0, 100, true);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  });
70
-
71
- return scene;
72
- }
73
- var scene = createScene();
74
- engine.runRenderLoop(function () {
75
- scene.render();
76
- });
77
- window.addEventListener('resize', function () {
78
- engine.resize();
79
- });
80
- });
81
-
82
  </script>
 
83
  </body>
84
- </html>
 
2
  <html>
3
  <head>
4
  <meta charset="utf-8" />
5
+ <title>Babylon.js L-system Fractal Example</title>
6
  <script src="https://cdn.babylonjs.com/babylon.js"></script>
7
  <script src="https://cdn.babylonjs.com/loaders/babylonjs.loaders.min.js"></script>
8
  <style>
 
30
  // Load the texture and bump map
31
  var texture = new BABYLON.Texture("https://www.babylonjs-playground.com/textures/floor.png", scene);
32
  var bumpMap = new BABYLON.Texture("https://www.babylonjs-playground.com/textures/floor_n.jpg", scene);
33
+ // Define the L-system rules and starting axiom
34
+ var rules = {
35
+ F: "F+F-F-F+F"
36
+ };
37
+ var axiom = "F-F-F-F";
38
+ var angle = Math.PI / 2;
39
+ var length = 1;
40
+ var iterations = 3;
41
+ // Generate the L-system string
42
+ var lSystemString = axiom;
43
+ for (var i = 0; i < iterations; i++) {
44
+ var newString = "";
45
+ for (var j = 0; j < lSystemString.length; j++) {
46
+ var char = lSystemString.charAt(j);
47
+ if (rules[char]) {
48
+ newString += rules[char];
49
+ } else {
50
+ newString += char;
51
+ }
52
  }
53
+ lSystemString = newString;
54
+ }
55
+ // Create a fountain of spheres that follow the L-system string
56
+ var position = BABYLON.Vector3.Zero();
57
+ var direction = BABYLON.Vector3.Forward();
58
+ for (var i = 0; i < lSystemString.length; i++) {
59
+ var char = lSystemString.charAt(i);
60
+ if (char === "F") {
61
+ // Create a new sphere
62
+ var sphere = BABYLON.MeshBuilder.CreateSphere("sphere", {diameter: length, segments: 16}, scene);
63
+ sphere.position = position.clone();
64
+ sphere.material = new BABYLON.StandardMaterial("texture", scene);
65
+ sphere.material.diffuseTexture = texture;
66
+ sphere.material.bumpTexture = bumpMap;
67
+ spheres.push(sphere);
68
+ // Animate the texture of the sphere
69
+ var textureAnimation = new BABYLON.Animation("textureAnimation", "material.diffuseTexture.uOffset", 60, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
70
+ var keys = [];
71
+ keys.push({frame: 0, value:0});
72
+ keys.push({frame: 100, value: 1});
73
+ textureAnimation.setKeys(keys);
74
+ sphere.material.diffuseTexture.wrapU = BABYLON.Texture.WRAP_ADDRESSMODE;
75
+ sphere.material.diffuseTexture.wrapV = BABYLON.Texture.WRAP_ADDRESSMODE;
76
+ sphere.material.diffuseTexture.uScale = 10;
77
+ sphere.material.diffuseTexture.vScale = 10;
78
+ sphere.animations.push(textureAnimation);
79
+ scene.beginAnimation(sphere, 0, 100, true);
80
+ // Animate the sphere
81
+ var animation = new BABYLON.Animation("myAnimation", "position", 30, BABYLON.Animation.ANIMATIONTYPE_VECTOR3, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
82
+ keys = [];
83
+ keys.push({frame: 0, value: position.clone()});
84
+ position.addInPlace(direction.scale(length));
85
+ keys.push({frame: 100, value: position.clone()});
86
  animation.setKeys(keys);
87
  sphere.animations.push(animation);
88
  scene.beginAnimation(sphere, 0, 100, true);
89
+ } else if (char === "+") {
90
+ // Rotate clockwise
91
+ direction.rotateByQuaternionToRef(BABYLON.Quaternion.RotationAxis(BABYLON.Vector3.Up(), -angle), direction);
92
+ } else if (char === "-") {
93
+ // Rotate counterclockwise
94
+ direction.rotateByQuaternionToRef(BABYLON.Quaternion.RotationAxis(BABYLON.Vector3.Up(), angle), direction);
95
+ }
96
+ }
97
+ return scene;
98
+ }
99
+ var scene = createScene();
100
+ engine.runRenderLoop(function () {
101
+ scene.render();
102
+ });
103
+ window.addEventListener('resize', function () {
104
+ engine.resize();
105
+ });
106
  });
 
 
 
 
 
 
 
 
 
 
 
 
107
  </script>
108
+
109
  </body>
110
+ </html>