// Retrieves materials from last generate result and creates according Unity materials Material[] GetMaterials(Shader shader) { int numMaterials = Prt4Unity.GetMaterialCount(ctx); Material[] materials = new Material[numMaterials]; for (int i = 0; i < numMaterials; i++) { IntPtr name, colorPtr, diffuseTexture; Prt4Unity.GetMaterial(ctx, i, out name, out colorPtr, out diffuseTexture); Material material = new Material(shader); material.name = Marshal.PtrToStringUni(name); materials[i] = material; float[] color = new float[3]; Marshal.Copy(colorPtr, color, 0, 3); material.SetColor("_Color", new Color(color[0], color[1], color[2])); string diffuseTextureName = Marshal.PtrToStringUni(diffuseTexture); if (!String.IsNullOrEmpty(diffuseTextureName)) { diffuseTextureName = diffuseTextureName.Substring(diffuseTextureName.IndexOf("/Assets/") + 1); Texture tex = Resources.LoadAssetAtPath <Texture>(diffuseTextureName); if (tex != null) { material.mainTexture = tex; } else { Debug.LogWarning("Cannot assign diffuse texture " + diffuseTextureName); } } } return(materials); }
// Retrieves meshes from last generate result and creates according Unity meshes void GetMeshes(Material[] materials, Transform loc2WorldTrafo, out OutputMesh[] meshes) { int numMeshes = Prt4Unity.GetMeshCount(ctx); meshes = new OutputMesh[numMeshes]; for (int i = 0; i < numMeshes; i++) { IntPtr name, pVertices, pNormals, pTexcoords; int numVertices; Prt4Unity.GetMesh(ctx, i, out name, out numVertices, out pVertices, out pNormals, out pTexcoords); OutputMesh outputMesh = new OutputMesh(); meshes[i] = outputMesh; outputMesh.mesh = new Mesh(); outputMesh.mesh.name = Marshal.PtrToStringUni(name); float[] floats = new float[numVertices * 3]; Marshal.Copy(pVertices, floats, 0, numVertices * 3); Vector3[] vertices = new Vector3[numVertices]; Vector3 preScale = loc2WorldTrafo.localScale; for (int v = 0; v < numVertices; v++) { vertices[v] = new Vector3(floats[3 * v + 0], floats[3 * v + 1], floats[3 * v + 2]); vertices[v] = loc2WorldTrafo.InverseTransformPoint(vertices[v]); vertices[v].x *= preScale.x; vertices[v].y *= preScale.y; vertices[v].z *= preScale.z; } outputMesh.mesh.vertices = vertices; Marshal.Copy(pNormals, floats, 0, numVertices * 3); Vector3[] normals = new Vector3[numVertices]; for (int v = 0; v < numVertices; v++) { normals[v] = new Vector3(floats[3 * v + 0], floats[3 * v + 1], floats[3 * v + 2]); } outputMesh.mesh.normals = normals; if (pTexcoords != IntPtr.Zero) { Marshal.Copy(pTexcoords, floats, 0, numVertices * 2); Vector2[] texcoords = new Vector2[numVertices]; for (int v = 0; v < numVertices; v++) { texcoords[v] = new Vector2(floats[2 * v + 0], floats[2 * v + 1]); } outputMesh.mesh.uv = texcoords; } int numSubMeshes = Prt4Unity.GetSubMeshCount(ctx, i); outputMesh.mesh.subMeshCount = (int)numSubMeshes; outputMesh.materials = new Material[numSubMeshes]; for (int s = 0; s < numSubMeshes; s++) { IntPtr pIndices; int numIndices, materialIndex; Prt4Unity.GetSubMesh(ctx, i, s, out pIndices, out numIndices, out materialIndex); int[] indices = new int[numIndices]; Marshal.Copy(pIndices, indices, 0, numIndices); outputMesh.mesh.SetTriangles(indices, (int)s); outputMesh.materials[s] = materials[materialIndex]; } } }
public void OnEnable() { shape = (Prt4Unity)target; if (!shape.IsInitialized()) { EditorUtility.DisplayProgressBar("Prt4Unity", "Reinitializing ...", 0.0f); shape.EnsureInit(); EditorUtility.ClearProgressBar(); } }
public void OnEnable() { shape = (Prt4Unity)target; if(!shape.IsInitialized()) { EditorUtility.DisplayProgressBar("Prt4Unity", "Reinitializing ...", 0.0f); shape.EnsureInit(); EditorUtility.ClearProgressBar(); } }
public string[] GetStartRules() { int numRules = Prt4Unity.GetStartRuleCount(ctx); string[] rules = new string[numRules]; for (int i = 0; i < numRules; i++) { rules[i] = Prt4Unity.GetStartRuleName(ctx, i); } return(rules); }
public string[] GetRuleFiles() { int numFiles = Prt4Unity.GetRuleFileCount(ctx); string[] files = new string[numFiles]; for (int i = 0; i < numFiles; i++) { files[i] = Prt4Unity.GetRuleFile(ctx, i); } return(files); }
public bool Generate(Mesh startShape, Transform loc2WorldTrafo, Shader shader, out OutputMesh[] meshes, string specialMaterial) { Vector3[] vertices = new Vector3[startShape.vertices.Length]; for (int i = 0; i < vertices.Length; i++) { vertices[i] = loc2WorldTrafo.TransformPoint(startShape.vertices[i]); } if (!Prt4Unity.Generate(ctx, vertices, vertices.Length, startShape.triangles, startShape.triangles.Length, specialMaterial)) { meshes = null; return(false); } Material[] materials = GetMaterials(shader); GetMeshes(materials, loc2WorldTrafo, out meshes); Prt4Unity.ReleaseMeshesAndMaterials(ctx); return(true); }
public Attribute[] GetAttributes() { int numAttributes = Prt4Unity.GetAttributeCount(ctx); Attribute[] attributes = new Attribute[numAttributes]; for (int i = 0; i < numAttributes; i++) { string name = Prt4Unity.GetAttributeName(ctx, i); int type = Prt4Unity.GetAttributeType(ctx, i); Attribute attr = new Attribute(); attr.name = name; attr.index = i; attributes[i] = attr; switch ((AttributeType)type) { case AttributeType.ATTR_BOOL: { attr.type = Prt4Unity.Attribute.Type.BoolAttribute; Prt4Unity.GetBoolAttributeValue(ctx, i, out attr.boolValue); break; } case AttributeType.ATTR_FLOAT: { attr.type = Prt4Unity.Attribute.Type.FloatAttribute; Prt4Unity.GetFloatAttributeValue(ctx, i, out attr.floatValue, out attr.floatMin, out attr.floatMax); break; } case AttributeType.ATTR_STRING: { attr.type = Prt4Unity.Attribute.Type.StringAttribute; IntPtr ptr; Prt4Unity.GetStringAttributeValue(ctx, i, out ptr); attr.stringValue = Marshal.PtrToStringUni(ptr); break; } case AttributeType.ATTR_DIRECTORY: { attr.type = Prt4Unity.Attribute.Type.DirectoryAttribute; IntPtr ptr; Prt4Unity.GetDirectoryAttributeValue(ctx, i, out ptr); attr.stringValue = Marshal.PtrToStringUni(ptr); break; } case AttributeType.ATTR_FILE: { attr.type = Prt4Unity.Attribute.Type.FileAttribute; IntPtr ptr, ptrExt; Prt4Unity.GetFileAttributeValue(ctx, i, out ptr, out ptrExt); attr.stringValue = Marshal.PtrToStringUni(ptr); attr.fileExt = Marshal.PtrToStringUni(ptrExt); break; } case AttributeType.ATTR_COLOR: { attr.type = Prt4Unity.Attribute.Type.ColorAttribute; IntPtr ptr; Prt4Unity.GetColorAttributeValue(ctx, i, out ptr); attr.stringValue = Marshal.PtrToStringUni(ptr); break; } case AttributeType.ATTR_ENUM_BOOL: case AttributeType.ATTR_ENUM_FLOAT: case AttributeType.ATTR_ENUM_STRING: { attr.type = Prt4Unity.Attribute.Type.EnumAttribute; Prt4Unity.GetEnumAttributeValue(ctx, i, out attr.enumValue); int numFields = Prt4Unity.GetEnumAttributeFieldCount(ctx, i); attr.enumFields = new string[numFields]; for (int j = 0; j < numFields; j++) { IntPtr ptr; Prt4Unity.GetEnumAttributeField(ctx, i, j, out ptr); attr.enumFields[j] = Marshal.PtrToStringUni(ptr); } break; } } } return(attributes); }
public bool SetStartRule(int index) { return(Prt4Unity.SetStartRule(ctx, index)); }
public bool SetRulePackage(string filename, string unpackPath) { return(Prt4Unity.SetRulePackage(ctx, filename, unpackPath)); }