proceduralTexture.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. var createScene = function () {
  2. var CreateBosquet = function (name, x, y, z, scene, shadowGenerator, woodMaterial, grassMaterial) {
  3. var bosquet = BABYLON.Mesh.CreateBox(name, 2, scene);
  4. bosquet.position = new BABYLON.Vector3(x, y, z);
  5. bosquet.material = grassMaterial;
  6. var bosquetbawl = BABYLON.Mesh.CreateBox(name + "bawl", 1, scene);
  7. bosquetbawl.position = new BABYLON.Vector3(x, y + 1, z);
  8. bosquetbawl.material = grassMaterial;
  9. shadowGenerator.getShadowMap().renderList.push(bosquet);
  10. shadowGenerator.getShadowMap().renderList.push(bosquetbawl);
  11. }
  12. var CreateTree = function (name, x, y, z, scene, shadowGenerator, woodMaterial, grassMaterial) {
  13. var trunk = BABYLON.Mesh.CreateCylinder(name + "trunk", 7, 2, 2, 12, 1, scene);
  14. trunk.position = new BABYLON.Vector3(x, y, z);
  15. trunk.material = woodMaterial;
  16. var leafs = BABYLON.Mesh.CreateSphere(name + "leafs", 20, 7, scene);
  17. leafs.position = new BABYLON.Vector3(x, y + 5.0, z);
  18. leafs.material = grassMaterial;
  19. shadowGenerator.getShadowMap().renderList.push(trunk);
  20. shadowGenerator.getShadowMap().renderList.push(leafs);
  21. }
  22. var createFontain = function (name, x, y, z, scene, shadowGenerator, marbleMaterial, fireMaterial) {
  23. var torus = BABYLON.Mesh.CreateTorus("torus", 5, 1, 20, scene);
  24. torus.position = new BABYLON.Vector3(x, y, z);
  25. torus.material = marbleMaterial;
  26. var fontainGround = BABYLON.Mesh.CreateBox("fontainGround", 4, scene);
  27. fontainGround.position = new BABYLON.Vector3(x, y - 2, z);
  28. fontainGround.material = marbleMaterial;
  29. var fontainSculptur1 = BABYLON.Mesh.CreateCylinder("fontainSculptur1", 2, 2, 1, 10, 1, scene);
  30. fontainSculptur1.position = new BABYLON.Vector3(x, y, z);
  31. fontainSculptur1.material = marbleMaterial;
  32. var fontainSculptur2 = BABYLON.Mesh.CreateSphere("fontainSculptur2", 7, 1.7, scene);
  33. fontainSculptur2.position = new BABYLON.Vector3(x, y + 0.9, z);
  34. fontainSculptur2.material = fireMaterial;
  35. fontainSculptur2.rotate(new BABYLON.Vector3(1.0, 0.0, 0.0), Math.PI / 2.0, BABYLON.Space.Local);
  36. shadowGenerator.getShadowMap().renderList.push(torus);
  37. shadowGenerator.getShadowMap().renderList.push(fontainSculptur1);
  38. shadowGenerator.getShadowMap().renderList.push(fontainSculptur2);
  39. }
  40. var createTorch = function (name, x, y, z, scene, shadowGenerator, brickMaterial, woodMaterial, grassMaterial) {
  41. //createBrickBlock
  42. var brickblock = BABYLON.Mesh.CreateBox(name + "brickblock", 1, scene);
  43. brickblock.position = new BABYLON.Vector3(x, y, z);
  44. brickblock.material = brickMaterial;
  45. //createWood
  46. var torchwood = BABYLON.Mesh.CreateCylinder(name + "torchwood", 2, 0.25, 0.1, 12, 1, scene);
  47. torchwood.position = new BABYLON.Vector3(x, y + 1, z);
  48. torchwood.material = woodMaterial;
  49. //leafs
  50. var leafs2 = BABYLON.Mesh.CreateSphere(name + "leafs2", 10, 1.2, scene);
  51. leafs2.position = new BABYLON.Vector3(x, y + 2, z);
  52. leafs2.material = grassMaterial;
  53. shadowGenerator.getShadowMap().renderList.push(torchwood);
  54. shadowGenerator.getShadowMap().renderList.push(leafs2);
  55. shadowGenerator.getShadowMap().renderList.push(brickblock);
  56. }
  57. //Ok, enough helpers, let the building start
  58. var scene = new BABYLON.Scene(engine);
  59. var camera = new BABYLON.ArcRotateCamera("Camera", 1, 1.2, 25, new BABYLON.Vector3(10, 0, 0), scene);
  60. camera.upperBetaLimit = 1.2;
  61. camera.attachControl(canvas, true);
  62. //Material declaration
  63. var woodMaterial = new BABYLON.StandardMaterial(name, scene);
  64. var woodTexture = new BABYLON.WoodProceduralTexture(name + "text", 1024, scene);
  65. woodTexture.ampScale = 50.0;
  66. woodMaterial.diffuseTexture = woodTexture;
  67. var grassMaterial = new BABYLON.StandardMaterial(name + "bawl", scene);
  68. var grassTexture = new BABYLON.GrassProceduralTexture(name + "textbawl", 256, scene);
  69. grassMaterial.ambientTexture = grassTexture;
  70. var marbleMaterial = new BABYLON.StandardMaterial("torus", scene);
  71. var marbleTexture = new BABYLON.MarbleProceduralTexture("marble", 512, scene);
  72. marbleTexture.numberOfBricksHeight = 5;
  73. marbleTexture.numberOfBricksWidth = 5;
  74. marbleMaterial.ambientTexture = marbleTexture;
  75. var fireMaterial = new BABYLON.StandardMaterial("fontainSculptur2", scene);
  76. var fireTexture = new BABYLON.FireProceduralTexture("fire", 256, scene);
  77. fireMaterial.diffuseTexture = fireTexture;
  78. fireMaterial.opacityTexture = fireTexture;
  79. var brickMaterial = new BABYLON.StandardMaterial(name, scene);
  80. var brickTexture = new BABYLON.BrickProceduralTexture(name + "text", 512, scene);
  81. brickTexture.numberOfBricksHeight = 2;
  82. brickTexture.numberOfBricksWidth = 3;
  83. brickMaterial.diffuseTexture = brickTexture;
  84. //light
  85. var light = new BABYLON.DirectionalLight("dir01", new BABYLON.Vector3(-0.5, -1, -0.5), scene);
  86. light.diffuse = new BABYLON.Color3(1, 1, 1);
  87. light.specular = new BABYLON.Color3(1, 1, 1);
  88. light.groundColor = new BABYLON.Color3(0, 0, 0);
  89. light.position = new BABYLON.Vector3(20, 40, 20);
  90. //Create a square of grass using a custom procedural texture
  91. var square = BABYLON.Mesh.CreateGround("square", 20, 20, 2, scene);
  92. square.position = new BABYLON.Vector3(0, 0, 0);
  93. var customMaterial = new BABYLON.StandardMaterial("custommat", scene);
  94. var customProcText = new BABYLON.CustomProceduralTexture("customtext", "./textures/customProceduralTextures/land", 1024, scene);
  95. customMaterial.ambientTexture = customProcText;
  96. square.material = customMaterial;
  97. //Applying some shadows
  98. var shadowGenerator = new BABYLON.ShadowGenerator(1024, light);
  99. square.receiveShadows = true;
  100. //Creating 4 bosquets
  101. CreateBosquet("b1", -9, 1, 9, scene, shadowGenerator, woodMaterial, grassMaterial);
  102. CreateBosquet("b2", -9, 1, -9, scene, shadowGenerator, woodMaterial, grassMaterial);
  103. CreateBosquet("b3", 9, 1, 9, scene, shadowGenerator, woodMaterial, grassMaterial);
  104. CreateBosquet("b4", 9, 1, -9, scene, shadowGenerator, woodMaterial, grassMaterial);
  105. CreateTree("a1", 0, 3.5, 0, scene, shadowGenerator, woodMaterial, grassMaterial);
  106. //Creating macadam
  107. var macadam = BABYLON.Mesh.CreateGround("square", 20, 20, 2, scene);
  108. macadam.position = new BABYLON.Vector3(20, 0, 0);
  109. var customMaterialmacadam = new BABYLON.StandardMaterial("macadam", scene);
  110. var customProcTextmacadam = new BABYLON.RoadProceduralTexture("customtext", 512, scene);
  111. customMaterialmacadam.diffuseTexture = customProcTextmacadam;
  112. macadam.material = customMaterialmacadam;
  113. macadam.receiveShadows = true;
  114. //Creating a fontain
  115. createFontain("fontain", 20, 0.25, 0, scene, shadowGenerator, marbleMaterial, fireMaterial);
  116. createTorch("torch1", 15, 0.5, 5, scene, shadowGenerator, brickMaterial, woodMaterial, grassMaterial);
  117. createTorch("torch2", 15, 0.5, -5, scene, shadowGenerator, brickMaterial, woodMaterial, grassMaterial);
  118. createTorch("torch3", 25, 0.5, 5, scene, shadowGenerator, brickMaterial, woodMaterial, grassMaterial);
  119. createTorch("torch4", 25, 0.5, -5, scene, shadowGenerator, brickMaterial, woodMaterial, grassMaterial);
  120. //Using a procedural texture to create the sky
  121. var boxCloud = BABYLON.Mesh.CreateSphere("boxCloud", 100, 1000, scene);
  122. boxCloud.position = new BABYLON.Vector3(0, 0, 12);
  123. var cloudMaterial = new BABYLON.StandardMaterial("cloudMat", scene);
  124. var cloudProcText = new BABYLON.CloudProceduralTexture("cloud", 1024, scene);
  125. cloudMaterial.emissiveTexture = cloudProcText;
  126. cloudMaterial.backFaceCulling = false;
  127. cloudMaterial.emissiveTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
  128. boxCloud.material = cloudMaterial;
  129. scene.registerBeforeRender(function () {
  130. camera.alpha += 0.001 * scene.getAnimationRatio();
  131. });
  132. return scene;
  133. };