Esempio n. 1
0
        void GenerateHeightMapBlobs()
        {
            var commandBuffer = _barrier.CreateCommandBuffer().ToConcurrent();

            Entities
            .WithNone <RegionHeightMap>()
            .WithAll <GenerateRegion>()
            .ForEach((int entityInQueryIndex, Entity e,
                      in GenerateRegionHeightMapSettings settings,
                      in RegionIndex regionIndex) =>
            {
                var heightMapBlob = HeightMapBuilder.BuildHeightMap(
                    regionIndex.value * Constants.ChunkSize.xz, Constants.ChunkSurfaceSize,
                    settings, Allocator.Persistent);

                var blobComponent = new RegionHeightMap {
                    heightMapBlob = heightMapBlob
                };

                commandBuffer.AddComponent(entityInQueryIndex, e, blobComponent);

                commandBuffer.RemoveComponent <GenerateRegionHeightMapSettings>(entityInQueryIndex, e);
                commandBuffer.RemoveComponent <GenerateRegion>(entityInQueryIndex, e);

                commandBuffer.AddComponent <GenerateRegionChunks>(entityInQueryIndex, e);
            }).ScheduleParallel();
Esempio n. 2
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            basicEffect = new BasicEffect(this.GraphicsDevice);
            Texture2D houseTexture1 = Content.Load <Texture2D>("Farmhouse Texture");
            Model     houseModel    = Content.Load <Model>("farmhouse_obj");
            Model     tree          = Content.Load <Model>("Leaf_Oak");

            houseTexture2 = Content.Load <Texture2D>("TexturesGreen");

            texture         = Content.Load <Texture2D>("US_Canyon");
            textureImage    = Content.Load <Texture2D>("sand");
            drawGameObjects = new DrawGameObjects();
            HeightMapBuilder heightMap = new HeightMapBuilder()
                                         .SetHeightMapTextureData(Content.Load <Texture2D>("US_Canyon"), Content.Load <Texture2D>("sand"))
                                         .SetHeights()
                                         .SetVertices()
                                         .SetIndices()
                                         .InitNormal()
                                         .SetEffects(graphics.GraphicsDevice)
                                         .SetWorldMatrix(Matrix.CreateTranslation(new Vector3(0, 0, 1080)))
                                         .Build();

            entityFactory.CreateCameraEntity();
            robot             = entityFactory.CreateRobot(Vector3.Zero, heightmapSystem.GetHeightMapWorld(), new BasicEffect(graphics.GraphicsDevice), Content.Load <Texture2D>("US_Canyon"));
            robotCameraSystem = new RobotCameraSystem(GraphicsDevice, robot);



            drawGameObjects.gameObjects.AddRange(entityFactory.CreateHouseStaticObject(
                                                     amount: 100,
                                                     houses: houseModel,
                                                     texture: houseTexture1));

            drawGameObjects.gameObjects.Add(robot);
        }
Esempio n. 3
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            chopper = Content.Load <Model>("Chopper");

            //Matrix viewMatrix = Matrix.CreateLookAt(new Vector3(60, 80, -80), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
            //Matrix projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, GraphicsDevice.Viewport.AspectRatio, 1.0f, 300.0f);

            //CameraComponent cc = new CameraComponent(entityId, viewMatrix, projectionMatrix);
            HeightMapBuilder heightMap = new HeightMapBuilder()
                                         .SetHeightMapTextureData(Content.Load <Texture2D>("US_Canyon"), Content.Load <Texture2D>("sand"))
                                         .SetHeights()
                                         .SetVertices()
                                         .InitNormal()
                                         .SetIndices()
                                         .SetEffects(graphics.GraphicsDevice)
                                         .Build();

            CreateEntities();
            cameraSystem.SetCameraView();
        }
Esempio n. 4
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            //this is how we load our EFFECT
            myEffect = Content.Load <Effect>("test1");
            var test = myEffect;


            texture      = Content.Load <Texture2D>("US_Canyon");
            textureImage = Content.Load <Texture2D>("sand");
            CreateEntities();
            HeightMapBuilder heightMap = new HeightMapBuilder()
                                         .SetHeightMapTextureData(Content.Load <Texture2D>("US_Canyon"), Content.Load <Texture2D>("sand"))
                                         .SetHeights()
                                         .SetVertices()
                                         .SetIndices()
                                         .InitNormal()
                                         .SetEffects(graphics.GraphicsDevice)
                                         .SetWorldMatrix(Matrix.CreateTranslation(new Vector3(0, 0, 1080)))
                                         .Build();
            // TODO: use this.Content to load your game content here
        }
Esempio n. 5
0
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        if (GUILayout.Button("Refresh all polygons"))
        {
            builder.Refresh();

            MarkSceneDirty();
        }
        EditorGUILayout.Separator();
        GUILayout.BeginHorizontal();
        if (GUILayout.Button("Clear NavMesh"))
        {
            UnityEditor.AI.NavMeshBuilder.ClearAllNavMeshes();
        }
        if (GUILayout.Button("Bake NavMesh"))
        {
            BakeNavMesh();
        }
        GUILayout.EndHorizontal();
        EditorGUILayout.Separator();
        GUILayout.BeginHorizontal();
        if (GUILayout.Button("Clear walkable colliders"))
        {
            foreach (GameObject go in SceneManager.GetActiveScene().GetRootGameObjects())
            {
                foreach (Renderer rd in go.GetComponentsInChildren <Renderer>(true))
                {
                    if (rd.gameObject.layer == Layer.sWalkable && rd.gameObject.name != "laya_heightmap")
                    {
                        Collider cld = rd.GetComponent <Collider>();
                        if (cld)
                        {
                            GameObject.DestroyImmediate(cld);
                        }
                    }
                }
            }
            MarkSceneDirty();
        }
        if (GUILayout.Button("Attach walkable colliders"))
        {
            foreach (GameObject go in SceneManager.GetActiveScene().GetRootGameObjects())
            {
                foreach (Renderer rd in go.GetComponentsInChildren <Renderer>(true))
                {
                    if (rd.gameObject.layer == Layer.sWalkable && rd.gameObject.name != "laya_heightmap")
                    {
                        Collider cld = rd.GetComponent <Collider>();
                        if (!cld)
                        {
                            cld = rd.gameObject.AddComponent <MeshCollider>();
                        }
                    }
                }
            }
            MarkSceneDirty();
        }
        GUILayout.EndHorizontal();

        if (GUILayout.Button("Export laya hightmap mesh"))
        {
            List <Vector3>        newVerts      = new List <Vector3>();
            List <Vector3>        newNormals    = new List <Vector3>();
            Dictionary <int, int> indexOldToNew = new Dictionary <int, int>();

            Mesh combineMesh      = new Mesh();
            CombineInstance[] cis = new CombineInstance[builder.polys.Count];
            for (int i = 0; i < builder.polys.Count; i++)
            {
                cis[i] = new CombineInstance()
                {
                    mesh      = builder.polys[i].meshFilter.sharedMesh,
                    transform = builder.polys[i].transform.localToWorldMatrix,
                };
            }
            combineMesh.CombineMeshes(cis, false, false);
            combineMesh.RecalculateNormals();

            Vector3[] verts   = combineMesh.vertices;
            int[]     tris    = combineMesh.triangles;
            Vector3[] normals = combineMesh.normals;
            Vector3   center  = combineMesh.bounds.center;
            for (int i = 0; i < verts.Length; i++)
            {
                int existedIndex = -1;
                for (int j = 0; j < newVerts.Count; j++)
                {
                    if (Vector3.Distance(verts[i], newVerts[j]) < 0.01)
                    {
                        existedIndex = j;
                        break;
                    }
                }
                if (existedIndex < 0)
                {
                    indexOldToNew[i] = newVerts.Count;
                    newVerts.Add(verts[i] - center);
                    newNormals.Add(normals[i]);
                }
                else
                {
                    indexOldToNew[i] = existedIndex;
                }
                //Debug.LogError(verts[i] + "  " + existedIndex);
            }

            GameObject.DestroyImmediate(combineMesh);

            string dir = SceneManager.GetActiveScene().path.Replace(".unity", "");
            if (!System.IO.Directory.Exists(dir))
            {
                System.IO.Directory.CreateDirectory(dir);
            }

            string path     = dir + "/laya_heightmap.obj";
            string contents = "";
            for (int i = 0; i < newVerts.Count; i++)
            {
                // obj x-axis is different with unity
                contents += string.Format("v {0:0.000000} {1:0.000000} {2:0.000000}\r\n", -newVerts[i].x, newVerts[i].y, newVerts[i].z);
            }
            for (int i = 0; i < newVerts.Count; i++)
            {
                contents += string.Format("vn {0:0.000000} {1:0.000000} {2:0.000000}\r\n", newNormals[i].x, newNormals[i].y, newNormals[i].z);
            }
            for (int i = 0; i < tris.Length; i += 3)
            {
                // obj x-axis is different with unity
                contents += string.Format("f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}\r\n",
                                          indexOldToNew[tris[i + 2]] + 1, indexOldToNew[tris[i + 1]] + 1, indexOldToNew[tris[i]] + 1);
            }
            System.IO.File.WriteAllText(path, contents);

            AssetDatabase.ImportAsset(path);
            ModelImporter mi = AssetImporter.GetAtPath(path) as ModelImporter;
            mi.importMaterials = false;

            if (System.IO.Directory.Exists(dir + "/Materials"))
            {
                System.IO.Directory.Delete(dir + "/Materials", true);
                System.IO.File.Delete(dir + "/Materials.meta");
            }

            GameObject go = GameObject.Find("laya_heightmap");
            if (go)
            {
                GameObject.DestroyImmediate(go);
            }
            go = new GameObject("laya_heightmap");
            go.transform.position         = center;
            go.transform.localEulerAngles = new Vector3(0, 0, 0);
            go.transform.localScale       = new Vector3(1, 1, 1);
            go.layer = Layer.sWalkable;

            Mesh       mesh   = AssetDatabase.LoadAssetAtPath <Mesh>(path);
            MeshFilter filter = go.AddComponent <MeshFilter>();
            filter.sharedMesh = mesh;
            MeshCollider meshCollider = go.AddComponent <MeshCollider>();
            meshCollider.sharedMesh = mesh;
            MeshRenderer render = go.AddComponent <MeshRenderer>();

            Collider collider = meshCollider;

            Bounds aabb = mesh.bounds;
            aabb.center += center;
            //Transform trans = GameObject.CreatePrimitive(PrimitiveType.Plane).transform;
            //trans.localScale = aabb.size;
            //trans.position = aabb.center;
            //collider = trans.GetComponent<Collider>();

            Texture2D heightMap  = HeightMapBuilder.Create(aabb, collider);
            byte[]    heightData = heightMap.EncodeToPNG();
            GameObject.DestroyImmediate(heightMap);
            string imgPath = dir + "/laya_heightmap.png";
            System.IO.File.WriteAllBytes(imgPath, heightData);
            AssetDatabase.ImportAsset(imgPath);
            TextureImporter ti = AssetImporter.GetAtPath(imgPath) as TextureImporter;
            ti.isReadable         = true;
            ti.textureCompression = TextureImporterCompression.Uncompressed;
            ti.npotScale          = TextureImporterNPOTScale.None;
            ti.filterMode         = FilterMode.Point;
            ti.mipmapEnabled      = false;

            //Texture2D aStarMap = AStarMapBuilder.Create(aabb, collider);
            //byte[] aStarData = aStarMap.EncodeToPNG();
            //GameObject.DestroyImmediate(aStarMap);
            //string astarPath = dir + "/laya_astar.png";
            //System.IO.File.WriteAllBytes(astarPath, aStarData);
            //AssetDatabase.ImportAsset(astarPath);
            //TextureImporter astarTI = AssetImporter.GetAtPath(astarPath) as TextureImporter;
            //astarTI.isReadable = true;
            //astarTI.textureCompression = TextureImporterCompression.Uncompressed;
            //astarTI.npotScale = TextureImporterNPOTScale.None;
            //astarTI.filterMode = FilterMode.Point;
            //astarTI.mipmapEnabled = false;

            string   matPath = dir + "/laya_heightmap.mat";
            Shader   shader  = Shader.Find("LayaAir3D/BlinnPhong");
            Material mat     = new Material(shader);
            mat.SetTexture("_MainTex", AssetDatabase.LoadAssetAtPath <Texture>(imgPath));
            //mat.SetTexture("_SpecGlossMap", AssetDatabase.LoadAssetAtPath<Texture>(astarPath));
            AssetDatabase.CreateAsset(mat, matPath);

            string heightInfoPath = dir + "/laya_heightinfo.txt";
            string heightInfo     = "";
            heightInfo += string.Format("min_x: {0}", aabb.min.x);
            heightInfo += "\r\n";
            heightInfo += string.Format("max_x: {0}", aabb.max.x);
            heightInfo += "\r\n";
            heightInfo += string.Format("min_y: {0}", aabb.min.y);
            heightInfo += "\r\n";
            heightInfo += string.Format("max_y: {0}", aabb.max.y);
            heightInfo += "\r\n";
            heightInfo += string.Format("min_z: {0}", aabb.min.z);
            heightInfo += "\r\n";
            heightInfo += string.Format("max_z: {0}", aabb.max.z);
            heightInfo += "\r\n";
            heightInfo += string.Format("size_x: {0}", aabb.size.x);
            heightInfo += "\r\n";
            heightInfo += string.Format("size_y: {0}", aabb.size.y);
            heightInfo += "\r\n";
            heightInfo += string.Format("size_z: {0}", aabb.size.z);
            heightInfo += "\r\n";
            System.IO.File.WriteAllText(heightInfoPath, heightInfo);

            render.material = AssetDatabase.LoadAssetAtPath <Material>(matPath);

            GameObject goHeightRange = new GameObject("height_range");
            goHeightRange.transform.SetParent(go.transform);
            goHeightRange.transform.localPosition = Vector3.zero;
            goHeightRange.transform.localRotation = Quaternion.identity;
            goHeightRange.transform.localScale    = new Vector3(aabb.min.y, aabb.max.y, 0);

            AssetDatabase.Refresh();

            MarkSceneDirty();
        }

        if (GUILayout.Button("Export WP nav mesh data"))
        {
            string json;
            if (WP.NavMeshExport.Serialize(builder, out json))
            {
                string dir = SceneManager.GetActiveScene().path.Replace(".unity", "");
                if (!System.IO.Directory.Exists(dir))
                {
                    System.IO.Directory.CreateDirectory(dir);
                }

                string path = dir + "/wp_navmesh.json";

                System.IO.File.WriteAllText(path, json);
                AssetDatabase.ImportAsset(path);
            }
        }
    }