static CubeDefinitionManager() { // TODO: Load from XML or JSON. var grass = new CubeDefinition(); grass.defaultTextureIndex = new TextureIndex(2, 15); // Default to MUD! grass.perFaceOverrideForTextureIndex.Add(5, new TextureIndex(0, 15)); // Grass on the top! AddDefinition("Grass", grass); }
public void WriteData(Chunk chunk, int cubeVertexStart, int cubeTriangleStart) { for (int face = 0; face < numFaces; face++) { for (int vertex = 0; vertex < numVerticesPerFace; vertex++) { var axis = new Vector3(((vertex / 1) % 2) == 0 ? -0.5f : 0.5f, ((vertex / 2) % 2) == 0 ? -0.5f : 0.5f, 0.0f); Vector3 rotationAxis = Vector3.up; float rotationAngle = face * 90.0f; if (face >= 4) { rotationAxis = Vector3.right; rotationAngle = (face * 180.0f) + 90.0f; } int vertexIndex = (face * numVerticesPerFace) + vertex + cubeVertexStart; var rotation = Quaternion.AngleAxis(rotationAngle, rotationAxis); chunk.verticies[vertexIndex] = Position.ToVector3() - (rotation * new Vector3(Scale.x * axis.x, Scale.y * axis.y, Scale.z * axis.z)) + (rotation * Vector3.forward * 0.5f); chunk.normals[vertexIndex] = rotation * Vector3.forward; } int vertexOffset = (face * numVerticesPerFace) + cubeVertexStart; CubeDefinition cubeDefinition = CubeDefinitionManager.GetDefinition("Grass"); cubeDefinition.GetTextureCoords(face, out chunk.uvs[vertexOffset + 0], out chunk.uvs[vertexOffset + 1], out chunk.uvs[vertexOffset + 2], out chunk.uvs[vertexOffset + 3]); int triangleOffset = (face * (numTrianglesPerFace * numVerticiesPerTriangle)) + cubeTriangleStart; chunk.triangles[triangleOffset + 0] = vertexOffset + 0; chunk.triangles[triangleOffset + 1] = vertexOffset + 1; chunk.triangles[triangleOffset + 2] = vertexOffset + 2; chunk.triangles[triangleOffset + 3] = vertexOffset + 3; chunk.triangles[triangleOffset + 4] = vertexOffset + 2; chunk.triangles[triangleOffset + 5] = vertexOffset + 1; } }
public static void AddDefinition(string cubeType, CubeDefinition cubeDefinition) { Definitions.Add(cubeType, cubeDefinition); }
void BlockEdit() { blockEditScroll = GUILayout.BeginScrollView(blockEditScroll, new GUIStyle(GUI.skin.box) { padding = new RectOffset(15, 15, 15, 15), margin = new RectOffset(15, 0, 0, 19) }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true) }); if (selectedBlock < blocks.Length && selectedBlock >= 0 && blocks.Length != 0 && textureNames.Count > 0) { GUILayout.Label("Edit: " + definitions[selectedBlock].GetType(), new GUIStyle { fontSize = 24, alignment = TextAnchor.MiddleCenter }, new GUILayoutOption[] { GUILayout.ExpandWidth(true) }); GUILayout.Space(30); GUILayout.BeginHorizontal(); GUILayout.Label("Name", new GUIStyle { fontSize = 14, alignment = TextAnchor.MiddleCenter }, new GUILayoutOption[] { GUILayout.Width(200) }); definitions[selectedBlock].blockName = EditorGUILayout.TextField(definitions[selectedBlock].blockName, new GUIStyle(GUI.skin.textField) { fontSize = 14, }, new GUILayoutOption[] { GUILayout.Height(18), GUILayout.ExpandWidth(true) }); GUILayout.EndHorizontal(); GUILayout.Space(10); if (definitions[selectedBlock].GetType() == typeof(CrossMeshDefinition)) { GUILayout.BeginHorizontal(); CrossMeshDefinition crossMesh = (CrossMeshDefinition)definitions[selectedBlock]; GUILayout.Label("Texture", new GUIStyle { fontSize = 14, alignment = TextAnchor.MiddleCenter }, new GUILayoutOption[] { GUILayout.Width(200) }); int i = EditorGUILayout.Popup(textureNames.IndexOf(crossMesh.texture), textureNames.ToArray(), new GUIStyle(GUI.skin.textField) { fontSize = 14 }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) }); if (i >= 0) { crossMesh.texture = textureNames[i]; } GUILayout.EndHorizontal(); } else if (definitions[selectedBlock].GetType() == typeof(CubeDefinition)) { CubeDefinition cube = (CubeDefinition)definitions[selectedBlock]; GUILayout.Space(10); int i = 0; GUILayout.BeginHorizontal(); GUILayout.Label("Texture Top", new GUIStyle { fontSize = 14, alignment = TextAnchor.MiddleCenter }, new GUILayoutOption[] { GUILayout.Width(200) }); i = EditorGUILayout.Popup(textureNames.IndexOf(cube.textures[0]), textureNames.ToArray(), new GUIStyle(GUI.skin.textField) { fontSize = 14 }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) }); if (i >= 0) { cube.textures[0] = textureNames[i]; } GUILayout.EndHorizontal(); GUILayout.Space(10); GUILayout.BeginHorizontal(); GUILayout.Label("Texture Bottom", new GUIStyle { fontSize = 14, alignment = TextAnchor.MiddleCenter }, new GUILayoutOption[] { GUILayout.Width(200) }); i = EditorGUILayout.Popup(textureNames.IndexOf(cube.textures[1]), textureNames.ToArray(), new GUIStyle(GUI.skin.textField) { fontSize = 14 }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) }); if (i >= 0) { cube.textures[1] = textureNames[i]; } GUILayout.EndHorizontal(); GUILayout.Space(10); GUILayout.BeginHorizontal(); GUILayout.Label("Texture North", new GUIStyle { fontSize = 14, alignment = TextAnchor.MiddleCenter }, new GUILayoutOption[] { GUILayout.Width(200) }); i = EditorGUILayout.Popup(textureNames.IndexOf(cube.textures[2]), textureNames.ToArray(), new GUIStyle(GUI.skin.textField) { fontSize = 14 }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) }); if (i >= 0) { cube.textures[2] = textureNames[i]; } GUILayout.EndHorizontal(); GUILayout.Space(10); GUILayout.BeginHorizontal(); GUILayout.Label("Texture East", new GUIStyle { fontSize = 14, alignment = TextAnchor.MiddleCenter }, new GUILayoutOption[] { GUILayout.Width(200) }); i = EditorGUILayout.Popup(textureNames.IndexOf(cube.textures[3]), textureNames.ToArray(), new GUIStyle(GUI.skin.textField) { fontSize = 14 }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) }); if (i >= 0) { cube.textures[3] = textureNames[i]; } GUILayout.EndHorizontal(); GUILayout.Space(10); GUILayout.BeginHorizontal(); GUILayout.Label("Texture South", new GUIStyle { fontSize = 14, alignment = TextAnchor.MiddleCenter }, new GUILayoutOption[] { GUILayout.Width(200) }); i = EditorGUILayout.Popup(textureNames.IndexOf(cube.textures[4]), textureNames.ToArray(), new GUIStyle(GUI.skin.textField) { fontSize = 14 }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) }); if (i >= 0) { cube.textures[4] = textureNames[i]; } GUILayout.EndHorizontal(); GUILayout.Space(10); GUILayout.BeginHorizontal(); GUILayout.Label("Texture West", new GUIStyle { fontSize = 14, alignment = TextAnchor.MiddleCenter }, new GUILayoutOption[] { GUILayout.Width(200) }); i = EditorGUILayout.Popup(textureNames.IndexOf(cube.textures[5]), textureNames.ToArray(), new GUIStyle(GUI.skin.textField) { fontSize = 14 }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) }); if (i >= 0) { cube.textures[5] = textureNames[i]; } GUILayout.EndHorizontal(); GUILayout.Space(30); GUILayout.BeginHorizontal(); GUILayout.Label("Block is solid", new GUIStyle { fontSize = 14, alignment = TextAnchor.MiddleCenter }, new GUILayoutOption[] { GUILayout.Width(200) }); cube.blockIsSolid = EditorGUILayout.Toggle(cube.blockIsSolid, new GUIStyle(GUI.skin.toggle) { fontSize = 14 }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) }); GUILayout.EndHorizontal(); if (!cube.blockIsSolid) { GUILayout.Space(10); GUILayout.BeginHorizontal(); GUILayout.Label("Solid towards same type", new GUIStyle { fontSize = 14, alignment = TextAnchor.MiddleCenter }, new GUILayoutOption[] { GUILayout.Width(200) }); cube.solidTowardsSameType = EditorGUILayout.Toggle(cube.solidTowardsSameType, new GUIStyle(GUI.skin.toggle) { fontSize = 14 }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) }); GUILayout.EndHorizontal(); } } else if (definitions[selectedBlock].GetType() == typeof(MeshDefinition)) { MeshDefinition mesh = (MeshDefinition)definitions[selectedBlock]; GUILayout.BeginHorizontal(); GUILayout.Label("Mesh name", new GUIStyle { fontSize = 14, alignment = TextAnchor.MiddleCenter }, new GUILayoutOption[] { GUILayout.Width(200) }); mesh.meshName = EditorGUILayout.TextField(mesh.meshName, new GUIStyle(GUI.skin.textField) { fontSize = 14, }, new GUILayoutOption[] { GUILayout.Height(18), GUILayout.ExpandWidth(true) }); GUILayout.EndHorizontal(); GUILayout.Space(10); GUILayout.BeginHorizontal(); GUILayout.Label("Texture", new GUIStyle { fontSize = 14, alignment = TextAnchor.MiddleCenter }, new GUILayoutOption[] { GUILayout.Width(200) }); int i = EditorGUILayout.Popup(textureNames.IndexOf(mesh.texture), textureNames.ToArray(), new GUIStyle(GUI.skin.textField) { fontSize = 14 }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) }); if (i >= 0) { mesh.texture = textureNames[i]; } GUILayout.EndHorizontal(); GUILayout.Space(10); GUILayout.BeginHorizontal(); GUILayout.Label("Block is solid upwards", new GUIStyle { fontSize = 14, alignment = TextAnchor.MiddleCenter }, new GUILayoutOption[] { GUILayout.Width(200) }); mesh.blockIsSolid[0] = EditorGUILayout.Toggle(mesh.blockIsSolid[0], new GUIStyle(GUI.skin.toggle) { fontSize = 14 }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) }); GUILayout.EndHorizontal(); GUILayout.Space(10); GUILayout.BeginHorizontal(); GUILayout.Label("Block is solid downwards", new GUIStyle { fontSize = 14, alignment = TextAnchor.MiddleCenter }, new GUILayoutOption[] { GUILayout.Width(200) }); mesh.blockIsSolid[1] = EditorGUILayout.Toggle(mesh.blockIsSolid[1], new GUIStyle(GUI.skin.toggle) { fontSize = 14 }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) }); GUILayout.EndHorizontal(); GUILayout.Space(10); GUILayout.BeginHorizontal(); GUILayout.Label("Block is solid north", new GUIStyle { fontSize = 14, alignment = TextAnchor.MiddleCenter }, new GUILayoutOption[] { GUILayout.Width(200) }); mesh.blockIsSolid[2] = EditorGUILayout.Toggle(mesh.blockIsSolid[2], new GUIStyle(GUI.skin.toggle) { fontSize = 14 }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) }); GUILayout.EndHorizontal(); GUILayout.Space(10); GUILayout.BeginHorizontal(); GUILayout.Label("Block is solid east", new GUIStyle { fontSize = 14, alignment = TextAnchor.MiddleCenter }, new GUILayoutOption[] { GUILayout.Width(200) }); mesh.blockIsSolid[3] = EditorGUILayout.Toggle(mesh.blockIsSolid[3], new GUIStyle(GUI.skin.toggle) { fontSize = 14 }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) }); GUILayout.EndHorizontal(); GUILayout.Space(10); GUILayout.BeginHorizontal(); GUILayout.Label("Block is solid south", new GUIStyle { fontSize = 14, alignment = TextAnchor.MiddleCenter }, new GUILayoutOption[] { GUILayout.Width(200) }); mesh.blockIsSolid[4] = EditorGUILayout.Toggle(mesh.blockIsSolid[4], new GUIStyle(GUI.skin.toggle) { fontSize = 14 }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) }); GUILayout.EndHorizontal(); GUILayout.Space(10); GUILayout.BeginHorizontal(); GUILayout.Label("Block is solid west", new GUIStyle { fontSize = 14, alignment = TextAnchor.MiddleCenter }, new GUILayoutOption[] { GUILayout.Width(200) }); mesh.blockIsSolid[5] = EditorGUILayout.Toggle(mesh.blockIsSolid[5], new GUIStyle(GUI.skin.toggle) { fontSize = 14 }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) }); GUILayout.EndHorizontal(); GUILayout.Space(10); GUILayout.BeginHorizontal(); GUILayout.Label("Mesh position offset", new GUIStyle { fontSize = 14, alignment = TextAnchor.MiddleCenter }, new GUILayoutOption[] { GUILayout.Width(200) }); mesh.positionOffset = EditorGUILayout.Vector3Field("", mesh.positionOffset, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) }); GUILayout.EndHorizontal(); } GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUILayout.BeginVertical(); GUILayout.FlexibleSpace(); if (GUILayout.Button("Delete", new GUIStyle(GUI.skin.button) { fontSize = 20, padding = new RectOffset(10, 10, 10, 10) }, new GUILayoutOption[] { GUILayout.Width(200), GUILayout.Height(40) } )) { Object.DestroyImmediate(definitions[selectedBlock]); GetDefinedBlocks(); selectedBlock = -1; } GUILayout.EndVertical(); GUILayout.EndHorizontal(); if (GUI.changed) { EditorUtility.SetDirty(definitions[selectedBlock]); } } else if (selectedBlock == -1) { NewBlock(); } GUILayout.EndScrollView(); }