index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. var globals = {};
  2. var createScene = function()
  3. {
  4. // This creates a basic Babylon Scene object (non-mesh)
  5. var scene = new BABYLON.Scene(engine);
  6. // This creates and positions a free camera (non-mesh)
  7. var camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 5, -5), scene);
  8. // This targets the camera to scene origin
  9. camera.setTarget(BABYLON.Vector3.Zero());
  10. // This attaches the camera to the canvas
  11. camera.attachControl(canvas, true);
  12. // This creates a light, aiming 0,1,0 - to the sky (non-mesh)
  13. var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);
  14. // Default intensity is 1. Let's dim the light a small amount
  15. light.intensity = 0.7;
  16. var material0 = new BABYLON.StandardMaterial("mat0", scene);
  17. material0.diffuseColor = new BABYLON.Color3(1, 0, 0);
  18. //material0.bumpTexture = new BABYLON.Texture("normalMap.jpg", scene);
  19. var material1 = new BABYLON.StandardMaterial("mat1", scene);
  20. material1.diffuseColor = new BABYLON.Color3(0, 0, 1);
  21. var material2 = new BABYLON.StandardMaterial("mat2", scene);
  22. material2.emissiveColor = new BABYLON.Color3(0.4, 0, 0.4);
  23. var multimat = new BABYLON.MultiMaterial("multi", scene);
  24. multimat.subMaterials.push(material0);
  25. multimat.subMaterials.push(material1);
  26. multimat.subMaterials.push(material2);
  27. globals.multimat = multimat;
  28. var sphere = BABYLON.Mesh.CreateSphere("Sphere0", 16, 3, scene);
  29. sphere.material = multimat;
  30. sphere.subMeshes = [];
  31. var verticesCount = sphere.getTotalVertices();
  32. sphere.subMeshes.push(new BABYLON.SubMesh(0, 0, verticesCount, 0, 900, sphere));
  33. sphere.subMeshes.push(new BABYLON.SubMesh(1, 0, verticesCount, 900, 900, sphere));
  34. sphere.subMeshes.push(new BABYLON.SubMesh(2, 0, verticesCount, 1800, 2088, sphere));
  35. // material0.diffuseTexture = new BABYLON.Texture("/assets/textures/leopard_fur.jpg", scene, true, true, BABYLON.Texture.BILINEAR_SAMPLINGMODE);
  36. scene.actionManager = new BABYLON.ActionManager(scene);
  37. scene.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnKeyUpTrigger, function (evt) {
  38. if (evt.sourceEvent.key == "t")
  39. {
  40. globals.multimat.subMaterials[0].diffuseColor = new BABYLON.Color3(1, 1, 1);
  41. globals.multimat.subMaterials[0].diffuseTexture = new BABYLON.Texture("/assets/textures/leopard_fur.jpg", scene, true, true, BABYLON.Texture.BILINEAR_SAMPLINGMODE);
  42. }
  43. else if (evt.sourceEvent.key == "m")
  44. {
  45. var material0 = new BABYLON.StandardMaterial("mat0", scene);
  46. material0.diffuseColor = new BABYLON.Color3(0, 1, 0);
  47. material0.diffuseTexture = new BABYLON.Texture("/assets/textures/leopard_fur.jpg", scene, true, true, BABYLON.Texture.BILINEAR_SAMPLINGMODE);
  48. globals.multimat.subMaterials[0] = material0;
  49. }
  50. }));
  51. return scene;
  52. };