app.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. var BABYLON = require("babylonjs");
  2. var LOADERS = require("babylonjs-loaders");
  3. global.XMLHttpRequest = require('xhr2').XMLHttpRequest;
  4. var engine = new BABYLON.NullEngine();
  5. // //Adding a light
  6. // var light = new BABYLON.PointLight("Omni", new BABYLON.Vector3(20, 20, 100), scene);
  7. // //Adding an Arc Rotate Camera
  8. // var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0.8, 100, BABYLON.Vector3.Zero(), scene);
  9. // // The first parameter can be used to specify which mesh to import. Here we import all meshes
  10. // BABYLON.SceneLoader.ImportMesh("", "https://playground.babylonjs.com/scenes/", "skull.babylon", scene, function (newMeshes) {
  11. // // Set the target of the camera to the first imported mesh
  12. // camera.target = newMeshes[0];
  13. // console.log("Meshes loaded from babylon file: " + newMeshes.length);
  14. // for (var index = 0; index < newMeshes.length; index++) {
  15. // console.log(newMeshes[index].toString());
  16. // }
  17. // BABYLON.SceneLoader.ImportMesh("", "https://www.babylonjs.com/Assets/DamagedHelmet/glTF/", "DamagedHelmet.gltf", scene, function (meshes) {
  18. // console.log("Meshes loaded from gltf file: " + meshes.length);
  19. // for (var index = 0; index < meshes.length; index++) {
  20. // console.log(meshes[index].toString());
  21. // }
  22. // });
  23. // console.log("render started")
  24. // engine.runRenderLoop(function() {
  25. // scene.render();
  26. // })
  27. // });
  28. // Setup environment
  29. // var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0.8, 90, BABYLON.Vector3.Zero(), scene);
  30. // camera.lowerBetaLimit = 0.1;
  31. // camera.upperBetaLimit = (Math.PI / 2) * 0.9;
  32. // camera.lowerRadiusLimit = 30;
  33. // camera.upperRadiusLimit = 150;
  34. // // light1
  35. // var light = new BABYLON.DirectionalLight("dir01", new BABYLON.Vector3(-1, -2, -1), scene);
  36. // light.position = new BABYLON.Vector3(20, 40, 20);
  37. // light.intensity = 0.5;
  38. // var lightSphere = BABYLON.Mesh.CreateSphere("sphere", 10, 2, scene);
  39. // lightSphere.position = light.position;
  40. // lightSphere.material = new BABYLON.StandardMaterial("light", scene);
  41. // lightSphere.material.emissiveColor = new BABYLON.Color3(1, 1, 0);
  42. // // light2
  43. // var light2 = new BABYLON.SpotLight("spot02", new BABYLON.Vector3(30, 40, 20),
  44. // new BABYLON.Vector3(-1, -2, -1), 1.1, 16, scene);
  45. // light2.intensity = 0.5;
  46. // var lightSphere2 = BABYLON.Mesh.CreateSphere("sphere", 10, 2, scene);
  47. // lightSphere2.position = light2.position;
  48. // lightSphere2.material = new BABYLON.StandardMaterial("light", scene);
  49. // lightSphere2.material.emissiveColor = new BABYLON.Color3(1, 1, 0);
  50. // // Ground
  51. // var ground = BABYLON.Mesh.CreateGround("ground1", 6, 6, 2, scene);
  52. // var groundMaterial = new BABYLON.StandardMaterial("ground", scene);
  53. // groundMaterial.diffuseTexture = new BABYLON.Texture("textures/ground.jpg", scene);
  54. // groundMaterial.diffuseTexture.uScale = 6;
  55. // groundMaterial.diffuseTexture.vScale = 6;
  56. // groundMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
  57. // ground.position.y = -2.05;
  58. // ground.material = groundMaterial;
  59. // // Torus
  60. // var torus = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  61. // // Box
  62. // var box = BABYLON.Mesh.CreateBox("box", 3);
  63. // box.parent = torus;
  64. // // Shadows
  65. // var shadowGenerator = new BABYLON.ShadowGenerator(1024, light);
  66. // shadowGenerator.addShadowCaster(torus);
  67. // shadowGenerator.useExponentialShadowMap = true;
  68. // var shadowGenerator2 = new BABYLON.ShadowGenerator(1024, light2);
  69. // shadowGenerator2.addShadowCaster(torus);
  70. // shadowGenerator2.usePoissonSampling = true;
  71. // ground.receiveShadows = true;
  72. // // Animations
  73. // var alpha = 0;
  74. // scene.registerBeforeRender(function () {
  75. // torus.rotation.x += 0.01;
  76. // torus.rotation.z += 0.02;
  77. // torus.position = new BABYLON.Vector3(Math.cos(alpha) * 30, 10, Math.sin(alpha) * 30);
  78. // alpha += 0.01;
  79. // });
  80. // //Adding a light
  81. // var light = new BABYLON.PointLight("Omni", new BABYLON.Vector3(20, 20, 100), scene);
  82. // //Adding an Arc Rotate Camera
  83. // var camera = new BABYLON.ArcRotateCamera("Camera", -0.5, 2.2, 100, BABYLON.Vector3.Zero(), scene);
  84. // // The first parameter can be used to specify which mesh to import. Here we import all meshes
  85. // BABYLON.SceneLoader.ImportMesh("", "https://www.babylonjs-playground.com/scenes/", "skull.babylon", scene, function (newMeshes) {
  86. // // Set the target of the camera to the first imported mesh
  87. // camera.target = newMeshes[0];
  88. // newMeshes[0].material = new BABYLON.StandardMaterial("skull", scene);
  89. // newMeshes[0].material.emissiveColor = new BABYLON.Color3(0.2, 0.2, 0.2);
  90. // });
  91. // // Create the "God Rays" effect (volumetric light scattering)
  92. // var godrays = new BABYLON.VolumetricLightScatteringPostProcess('godrays', 1.0, camera, null, 100, BABYLON.Texture.BILINEAR_SAMPLINGMODE, engine, false);
  93. // // By default it uses a billboard to render the sun, just apply the desired texture
  94. // // position and scale
  95. // godrays.mesh.material.diffuseTexture = new BABYLON.Texture('https://www.babylonjs-playground.com/textures/sun.png', scene, true, false, BABYLON.Texture.BILINEAR_SAMPLINGMODE);
  96. // godrays.mesh.material.diffuseTexture.hasAlpha = true;
  97. // godrays.mesh.position = new BABYLON.Vector3(-150, 150, 150);
  98. // godrays.mesh.scaling = new BABYLON.Vector3(350, 350, 350);
  99. // light.position = godrays.mesh.position;
  100. // engine.runRenderLoop(function() {
  101. // scene.render();
  102. // })
  103. // new BABYLON.Scene(engine).dispose()
  104. // BABYLON.SceneLoader.Load("https://playground.babylonjs.com/scenes/", "skull.babylon", engine, (scene) => {
  105. // console.log('scene loaded!');
  106. // for (var index = 0; index < scene.meshes.length; index++) {
  107. // console.log(scene.meshes[index].name);
  108. // }
  109. // engine.dispose();
  110. // // engine.runRenderLoop(function() {
  111. // // scene.render();
  112. // //});
  113. // }, progress => {}, (scene, err) => console.error('error:', err));
  114. // var scene = new BABYLON.Scene(engine);
  115. // var camera = new BABYLON.ArcRotateCamera("camera", 0, 0, 0, BABYLON.Vector3.Zero(), scene);
  116. // scene.render();
  117. // var pos = BABYLON.Vector3.Project(
  118. // new BABYLON.Vector3(0.5, 0.5, 0.5),
  119. // BABYLON.Matrix.Identity(),
  120. // scene.getTransformMatrix(),
  121. // scene.activeCamera.viewport.toGlobal(
  122. // engine.getRenderWidth(),
  123. // engine.getRenderHeight()
  124. // ));;
  125. // console.log(pos);
  126. // const scene = new BABYLON.Scene(engine);
  127. // new BABYLON.PBRMetallicRoughnessMaterial("asdfasf", scene);
  128. // scene.dispose();
  129. // BABYLON.Tools.LogLevels = BABYLON.Tools.ErrorLogLevel & BABYLON.Tools.WarningLogLevel;
  130. // const scene = new BABYLON.Scene(engine);
  131. // BABYLON.ParticleHelper.CreateAsync("sun", scene)
  132. // .then((system) => {
  133. // console.log("ok");
  134. // });
  135. // const camera = new BABYLON.ArcRotateCamera("camera", 0, 0, 10, BABYLON.Vector3.Zero(), scene);
  136. // const mesh = BABYLON.MeshBuilder.CreateBox("box", {}, scene);
  137. // mesh.position.set(0.5, 0.5, 0.5);
  138. // mesh.isPickable = true;
  139. // scene.render();
  140. // engine.dispose();
  141. // var scene = new BABYLON.Scene(engine);
  142. // var light = new BABYLON.PointLight("Omni", new BABYLON.Vector3(20, 20, 100), scene);
  143. // var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0.8, 100, BABYLON.Vector3.Zero(), scene);
  144. // var assetsManager = new BABYLON.AssetsManager(scene);
  145. // var meshTask = assetsManager.addMeshTask("skull task", "", "https://raw.githubusercontent.com/RaggarDK/Baby/baby/", "he4.babylon");
  146. // meshTask.onSuccess = function (task) {
  147. //     
  148. // //0 = ground plane, 1,2,3 = boxes
  149. // for(var i=1;i<task.loadedMeshes.length;i++){
  150. // var mesh = task.loadedMeshes[i];
  151. // //mesh.computeWorldMatrix(true);
  152. // var position = mesh.position.clone();
  153. // var rotation = mesh.rotationQuaternion.clone();
  154. // var scaling = mesh.getBoundingInfo().boundingBox.extendSize;
  155. // var centerWorld = mesh.getBoundingInfo().boundingBox.centerWorld;
  156. // console.log(position);
  157. // console.log(mesh.getBoundingInfo());
  158. // var box = BABYLON.MeshBuilder.CreateBox("box"+i,{height:2,width:2,depth:2});
  159. // box.scaling.copyFromFloats(scaling.x,scaling.y,scaling.z);
  160. // box.rotationQuaternion = rotation;
  161. // //box.position = position;
  162. // box.position.set(centerWorld.x,centerWorld.y,centerWorld.z);
  163. // var material = new BABYLON.StandardMaterial("mat", scene);
  164. // material.diffuseColor = new BABYLON.Color3(0.5,1,0.5);
  165. // box.material = material;
  166. // }
  167. // }
  168. // scene.registerBeforeRender(function () {
  169. // light.position = camera.position;
  170. // });
  171. // assetsManager.onFinish = function (tasks) {
  172. // engine.runRenderLoop(function () {
  173. // scene.render();
  174. // });
  175. // };
  176. // assetsManager.load();
  177. // var scene = new BABYLON.Scene(engine);
  178. // var camera = new BABYLON.ArcRotateCamera("Camera", -Math.PI / 1, Math.PI / 1, 5, BABYLON.Vector3.Zero(), scene);
  179. // camera.setPosition(new BABYLON.Vector3(-800,1200,-2000));
  180. // camera.setTarget(BABYLON.Vector3.Zero());
  181. // var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);
  182. // light.intensity = 1;
  183. // function createPart(name,opt,parent){
  184. // var part = BABYLON.MeshBuilder.CreateBox(name, opt.size, scene);
  185. // part.position = new BABYLON.Vector3(opt.pos.x, opt.pos.y, opt.pos.z);
  186. // let mate = new BABYLON.StandardMaterial('mat-'+name, scene);
  187. // if(parent) {
  188. // mate.specularPower = 200;
  189. // mate.specularColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  190. // mate.diffuseTexture = new BABYLON.Texture(opt.mat.url,scene);
  191. // mate.diffuseTexture.wAng = opt.mat.grain*Math.PI/180;
  192. // part.parent = parent;
  193. // }else{
  194. // mate.alpha = 0;
  195. // }
  196. // part.material = mate;
  197. // return part;
  198. // }
  199. // var parent;
  200. // function createUnit(x,y,z,b){
  201. // var item = {
  202. // size:{width:x,depth:y,height:z},
  203. // pos:{x:0,y:0,z:0},
  204. // mat:{url:false,grain:0},
  205. // child:{
  206. // left:{
  207. // size:{width:b,depth:y,height:z},
  208. // pos:{x:-(x-b)/2,y:0,z:0},
  209. // mat:{url:"/Playground/textures/crate.png",grain:90}
  210. // },
  211. // right:{
  212. // size:{width:b,depth:y,height:z},
  213. // pos:{x:(x-b)/2,y:0,z:0},
  214. // mat:{url:"/Playground/textures/crate.png",grain:90}
  215. // },
  216. // top:{
  217. // size:{width:x-(b*2),depth:y,height:b},
  218. // pos:{x:0,y:(z-b-1)/2,z:0},
  219. // mat:{url:"/Playground/textures/albedo.png",grain:0}
  220. // },
  221. // bottom:{
  222. // size:{width:x-(b*2),depth:y,height:b},
  223. // pos:{x:0,y:-(z-b-1)/2,z:0},
  224. // mat:{url:"/Playground/textures/albedo.png",grain:0}
  225. // },
  226. // back:{
  227. // size:{width:x-(b*2),depth:b,height:z-(b*2)-1},
  228. // pos:{x:0,y:0,z:(y-b)/2-20},
  229. // mat:{url:"/Playground/textures/albedo.png",grain:0}
  230. // },
  231. // shelf:{
  232. // size:{width:x-(b*2)-1,depth:y-b-30,height:b},
  233. // pos:{x:0,y:0,z:-((b+20)/2)+5},
  234. // mat:{url:"textures/crate.png",grain:45}
  235. // }
  236. // }
  237. // };
  238. // if(parent){
  239. // parent.dispose();
  240. // }
  241. // parent = createPart("Unit",item,false);
  242. // Object.keys(item.child).forEach(function(key) {
  243. // createPart(key,item.child[key],parent);
  244. // });
  245. // return item;
  246. // }
  247. // createUnit(600,300,900,18);
  248. // var serialized = BABYLON.SceneSerializer.SerializeMesh(parent, true, true);
  249. // console.log(serialized);
  250. // var scene = new BABYLON.Scene(engine);
  251. // var light = new BABYLON.PointLight("Omni", new BABYLON.Vector3(20, 20, 100), scene);
  252. // var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0.8, 100, BABYLON.Vector3.Zero(), scene);
  253. // BABYLON.SceneLoader.ImportMesh("", "https://playground.babylonjs.com/scenes/", "skull.babylon", scene, function(newMeshes) {
  254. // camera.target = newMeshes[0];
  255. // console.log("Meshes loaded from babylon file: " + newMeshes.length);
  256. // for (var index = 0; index < newMeshes.length; index++) {
  257. // console.log(newMeshes[index].toString());
  258. // }
  259. // BABYLON.SceneLoader.ImportMesh("", "https://www.babylonjs.com/Assets/DamagedHelmet/glTF/", "DamagedHelmet.gltf", scene, function(meshes) {
  260. // console.log("Meshes loaded from gltf file: " + meshes.length);
  261. // for (var index = 0; index < meshes.length; index++) {
  262. // console.log(meshes[index].toString());
  263. // }
  264. // });
  265. // console.log("render started")
  266. // engine.runRenderLoop(function() {
  267. // scene.render();
  268. // })
  269. // });
  270. var scene = new BABYLON.Scene(engine);
  271. var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0.8, 100, BABYLON.Vector3.Zero(), scene);
  272. const scaling = engine.getHardwareScalingLevel();
  273. const pos = BABYLON.Vector3.Project(
  274. new BABYLON.Vector3(5, 10, 3),
  275. BABYLON.Matrix.IdentityReadOnly,
  276. scene.getTransformMatrix(),
  277. scene.activeCamera.viewport.toGlobal(
  278. engine.getRenderWidth(),
  279. engine.getRenderHeight(),
  280. ),
  281. );
  282. pos.x *= scaling;
  283. pos.y *= scaling;
  284. pos.z *= scaling;
  285. return pos;