index.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Babylon.js sample code</title>
  6. <!-- Babylon.js -->
  7. <script src="http://www.babylonjs.com/hand.minified-1.2.js"></script>
  8. <script src="http://www.babylonjs.com/cannon.js"></script>
  9. <script src="http://www.babylonjs.com/oimo.js"></script>
  10. <script src="http://www.babylonjs.com/babylon.js"></script>
  11. <style>
  12. html, body {
  13. overflow: hidden;
  14. width: 100%;
  15. height: 100%;
  16. margin: 0;
  17. padding: 0;
  18. }
  19. #renderCanvas {
  20. width: 100%;
  21. height: 100%;
  22. touch-action: none;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <canvas id="renderCanvas"></canvas>
  28. <script>
  29. var canvas = document.getElementById("renderCanvas");
  30. var engine = new BABYLON.Engine(canvas, true);
  31. var createScene = function () {
  32. // This creates a basic Babylon Scene object (non-mesh)
  33. var scene = new BABYLON.Scene(engine);
  34. var camera = new BABYLON.ArcRotateCamera('Camera', 1, .8, 28, new BABYLON.Vector3(0, 0, 0), scene);
  35. camera.setTarget(BABYLON.Vector3.Zero());
  36. camera.attachControl(canvas, false);
  37. // This creates a light, aiming 0,1,0 - to the sky (non-mesh)
  38. var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);
  39. // Default intensity is 1. Let's dim the light a small amount
  40. light.intensity = 0.7;
  41. var capsule = createStickMergemesh(scene, {
  42. key: "C#C",
  43. d: 5,
  44. kindA: {
  45. symbol: "C",
  46. color: [0.5, 0.5, 0.5]
  47. },
  48. kindB: {
  49. symbol: "C",
  50. color: [1, 0, 0]
  51. }
  52. }, 1, 0, 12);
  53. capsule.position.x = 0;
  54. capsule.position.y = 0;
  55. capsule.position.z = 0;
  56. return scene;
  57. };
  58. function createStickMergemesh(scene, binding, diameter, lodIndex, segments){
  59. console.log("create mesh template " + binding.key + " mergemesh " + lodIndex);
  60. var radius = diameter / 2;
  61. var cylinderSize = binding.d - (radius/2.5);
  62. var halfCylinderSize = cylinderSize/2;
  63. var cylinder = BABYLON.Mesh.CreateCylinder("bondtemplate" + binding.key+ "-" + lodIndex, cylinderSize, diameter, diameter, segments, 2, scene, false);
  64. var cylinderIndices = cylinder.getIndices();
  65. var sphereA = BABYLON.Mesh.CreateSphere("sphereA" + binding.key+ "-" + lodIndex, segments, diameter, scene, false);
  66. sphereA.position.y = -halfCylinderSize;
  67. var sphereB = BABYLON.Mesh.CreateSphere("sphereB" + binding.key+ "-" + lodIndex, segments, diameter, scene, false);
  68. sphereB.position.y = halfCylinderSize;
  69. var capsule = BABYLON.Mesh.MergeMeshes([sphereA, cylinder, sphereB], true);
  70. var atomAMat = new BABYLON.StandardMaterial('materialFor' + binding.key + binding.kindA.symbol+ "-" + lodIndex, scene);
  71. var atomAColor = binding.kindA.color;
  72. atomAMat.diffuseColor = new BABYLON.Color3(atomAColor[0], atomAColor[1], atomAColor[2]);
  73. atomAMat.specularColor = new BABYLON.Color3(0.4, 0.4, 0.4);
  74. atomAMat.emissiveColor = new BABYLON.Color3(0.2, 0.2, 0.2);
  75. var atomBMat = new BABYLON.StandardMaterial('materialFor' + binding.key + binding.kindB.symbol+ "-" + lodIndex, scene);
  76. var atomBColor = binding.kindB.color;
  77. atomBMat.diffuseColor = new BABYLON.Color3(atomBColor[0], atomBColor[1], atomBColor[2]);
  78. atomBMat.specularColor = new BABYLON.Color3(0.4, 0.4, 0.4);
  79. atomBMat.emissiveColor = new BABYLON.Color3(0.2, 0.2, 0.2);
  80. var rootMat = new BABYLON.MultiMaterial('materialFor' + binding.key+ "-" + lodIndex, scene);
  81. rootMat.subMaterials.push(atomAMat);
  82. rootMat.subMaterials.push(atomBMat);
  83. var verticesCount = capsule.getTotalVertices();
  84. var indices = capsule.getIndices();
  85. console.log("has submeshes ? " + capsule.subMeshes.length + " indices " + indices.length);
  86. console.log(indices);
  87. capsule.subMeshes = [];
  88. var halfindices = ((indices.length/2) >> 0);
  89. capsule.subMeshes.push(new BABYLON.SubMesh(0, 0, verticesCount, 0, halfindices, capsule));
  90. capsule.subMeshes.push(new BABYLON.SubMesh(1, 0, verticesCount, halfindices, indices.length - halfindices, capsule));
  91. for(var i in rootMat.subMaterials){
  92. rootMat.subMaterials[i].bumpTexture = new BABYLON.Texture("textures/bump2.jpg", scene);
  93. }
  94. capsule.material = rootMat;
  95. return capsule;
  96. }
  97. var scene = createScene();
  98. engine.runRenderLoop(function () {
  99. scene.render();
  100. });
  101. // Resize
  102. window.addEventListener("resize", function () {
  103. engine.resize();
  104. });
  105. </script>
  106. </body>
  107. </html>