void OnMaterialInfoReceived(string name, MaterialInfo matInfo) { Material mat; Shader refShader = null; bool result = customShaders.GetTable().TryGetValue(name.TrimEnd('\0'), out refShader); if (result) { mat = new Material(refShader); } else { mat = new Material(defaultShader); } if (mat != null) { foreach (MaterialPropertyInfo info in matInfo.properties) { switch (info.type) { case 0://ShaderUtil.ShaderPropertyType.Color: Color col = JsonUtility.FromJson <Color>(info.value); mat.SetColor(info.name, col); break; case 1://ShaderUtil.ShaderPropertyType.Vector: Vector4 vec = JsonUtility.FromJson <Vector4>(info.value); mat.SetVector(info.name, vec); break; case 2://ShaderUtil.ShaderPropertyType.Float: float fValue = JsonUtility.FromJson <float>(info.value); mat.SetFloat(info.name, fValue); break; case 3://ShaderUtil.ShaderPropertyType.Range: float rValue = JsonUtility.FromJson <float>(info.value); mat.SetFloat(info.name, rValue); break; case 4://ShaderUtil.ShaderPropertyType.TexEnv: foreach (KeyValuePair <string, Texture2D> pair in m_textureList) { if (pair.Key == info.value) { Texture2D texture = pair.Value; mat.SetTexture(info.name, pair.Value); } } break; } } //end of foreach } else { Debug.LogError("Not found ANY shader!"); } KeyValuePair <string, Material> matPair = new KeyValuePair <string, Material>(matInfo.name, mat); m_materialList.Add(matPair); }
public void Send(MaterialInfo materialInfo, int index) { string json = JsonUtility.ToJson(materialInfo); base.Send("material=" + index.ToString(), json, true); }
public MaterialInfo CreateMaterialInfo(Material material) { #if UNITY_EDITOR MaterialInfo materialInfo = new MaterialInfo() { name = material.name }; Shader shader = material.shader; if (shader == null) { return(null); } int propertyCount = ShaderUtil.GetPropertyCount(shader); materialInfo.properties = new List <MaterialPropertyInfo>(); for (int i = 0; i < propertyCount; i++) { string propertyName = ShaderUtil.GetPropertyName(shader, i); ShaderUtil.ShaderPropertyType propertyType = ShaderUtil.GetPropertyType(shader, i); MaterialPropertyInfo propertyInfo = new MaterialPropertyInfo() { name = propertyName, type = (int)propertyType }; switch (propertyType) { case ShaderUtil.ShaderPropertyType.Color: propertyInfo.value = JsonUtility.ToJson(material.GetColor(propertyName)); break; case ShaderUtil.ShaderPropertyType.Vector: propertyInfo.value = JsonUtility.ToJson(material.GetVector(propertyName)); break; case ShaderUtil.ShaderPropertyType.Float: propertyInfo.value = JsonUtility.ToJson(material.GetFloat(propertyName)); break; case ShaderUtil.ShaderPropertyType.Range: propertyInfo.value = JsonUtility.ToJson(material.GetFloat(propertyName)); break; case ShaderUtil.ShaderPropertyType.TexEnv: Texture tex = material.GetTexture(propertyName); if (tex != null) { propertyInfo.value = tex.name; } else { continue; } break; } materialInfo.properties.Add(propertyInfo); } return(materialInfo); #else return(null); #endif }
void CreateInfos() { if (serializer == null || renderers == null || targetGameObject == null) { string errorString = ""; errorString += (serializer == null ? "serializer " : ""); errorString += (renderers == null ? "rendrers " : ""); errorString += (targetGameObject == null ? "target GameObject " : ""); errorString += "is null on create channel!"; Debug.LogError(errorString); } Dictionary <string, Material> materials = new Dictionary <string, Material>(); Dictionary <string, Texture> textures = new Dictionary <string, Texture>(); List <MeshInfo> meshInfos = new List <MeshInfo>(); List <MaterialInfo> materialInfos = new List <MaterialInfo>(); List <string> meshNames = new List <string>(); List <string> materialNames = new List <string>(); List <string> textureNames = new List <string>(); for (int i = 0; i < renderers.Count; i++) { SkinnedMeshRenderer renderer = renderers[i]; if (renderer != null) { MeshInfo meshInfo = serializer.CreateMeshInfo(renderer); if (meshInfo == null) { continue; } meshInfos.Add(meshInfo); meshNames.Add("mesh" + i); for (int j = 0; j < renderer.sharedMaterials.Length; j++) { Material mat = renderer.sharedMaterials[j]; Material dummy; if (materials.TryGetValue(mat.name, out dummy)) { continue; } materials.Add(mat.name, mat); MaterialInfo materialInfo = serializer.CreateMaterialInfo(mat); if (materialInfo == null) { continue; } materialInfos.Add(materialInfo); materialNames.Add("material" + materialNames.Count); List <KeyValuePair <string, Texture> > texturePairs = serializer.GetTexturesFromMaterial(mat); foreach (KeyValuePair <string, Texture> texturePair in texturePairs) { Texture dummyTexture; if (textures.TryGetValue(texturePair.Key, out dummyTexture)) { continue; } textures.Add(texturePair.Key, texturePair.Value); textureNames.Add(texturePair.Key); } } } } List <int> meshBuffersSize = new List <int>(); List <int> matBuffersSize = new List <int>(); List <int> texBuffersSize = new List <int>(); List <byte> combinedBuffer = new List <byte>(); foreach (KeyValuePair <string, Texture> texturePair in textures) { Texture texture = texturePair.Value; byte[] buffer = STMHttpSerializer.GetTextureToPNGByteArray(texture, true); texBuffersSize.Add(buffer.Length); combinedBuffer.AddRange(buffer); } for (int i = 0; i < materialInfos.Count; i++) { MaterialInfo matInfo = materialInfos[i]; string matString = JsonUtility.ToJson(matInfo); byte[] buffer = Encoding.UTF8.GetBytes(matString); matBuffersSize.Add(buffer.Length); combinedBuffer.AddRange(buffer); } for (int i = 0; i < meshInfos.Count; i++) { MeshInfo meshInfo = meshInfos[i]; string meshString = JsonUtility.ToJson(meshInfo); byte[] buffer = Encoding.UTF8.GetBytes(meshString); meshBuffersSize.Add(buffer.Length); combinedBuffer.AddRange(buffer); } byte[] compressedBuffer = StreamingMesh.Lib.ExternalTools.Compress(combinedBuffer.ToArray()); ChannelInfo channelInfo = serializer.CreateChannelInfo( containerSize, packageSize, frameInterval, combinedFrames, meshNames, materialNames, textureNames, meshBuffersSize, matBuffersSize, texBuffersSize ); serializer.Send(channelInfo); serializer.Send(compressedBuffer); }
void CreateInfos() { Dictionary <string, Material> materials = new Dictionary <string, Material>(); Dictionary <string, Texture> textures = new Dictionary <string, Texture>(); List <MeshInfo> meshInfos = new List <MeshInfo>(); List <MaterialInfo> materialInfos = new List <MaterialInfo>(); List <string> meshNames = new List <string>(); List <string> materialNames = new List <string>(); List <string> textureNames = new List <string>(); for (int i = 0; i < renderers.Count; i++) { SkinnedMeshRenderer renderer = renderers[i]; if (renderer != null) { MeshInfo meshInfo = serializer.CreateMeshInfo(renderer); if (meshInfo == null) { continue; } meshInfos.Add(meshInfo); meshNames.Add("mesh" + i + ".json"); for (int j = 0; j < renderer.sharedMaterials.Length; j++) { Material mat = renderer.sharedMaterials[j]; Material dummy; if (materials.TryGetValue(mat.name, out dummy)) { continue; } materials.Add(mat.name, mat); MaterialInfo materialInfo = serializer.CreateMaterialInfo(mat); if (materialInfo == null) { continue; } materialInfos.Add(materialInfo); materialNames.Add("material" + materialNames.Count + ".json"); List <KeyValuePair <string, Texture> > texturePairs = serializer.GetTexturesFromMaterial(mat); foreach (KeyValuePair <string, Texture> texturePair in texturePairs) { Texture dummyTexture; if (textures.TryGetValue(texturePair.Key, out dummyTexture)) { continue; } textures.Add(texturePair.Key, texturePair.Value); textureNames.Add(texturePair.Key + ".png"); } } } } ChannelInfo channelInfo = serializer.CreateChannelInfo( areaRange, packageSize, frameInterval, combinedFrames, meshNames, materialNames, textureNames ); serializer.Send(channelInfo); for (int i = 0; i < meshInfos.Count; i++) { MeshInfo meshInfo = meshInfos[i]; serializer.Send(meshInfo, i); } for (int i = 0; i < materialInfos.Count; i++) { MaterialInfo matInfo = materialInfos[i]; serializer.Send(matInfo, i); } foreach (KeyValuePair <string, Texture> texturePair in textures) { Texture texture = texturePair.Value; serializer.Send(texture, true); } }
void OnMaterialInfoReceived(string name, MaterialInfo info) { Material mat; Shader refShader = null; bool result = customShaders.GetTable().TryGetValue(name.TrimEnd('\0'), out refShader); if (result) { if (refShader != null) { mat = new Material(refShader); } else { mat = new Material(defaultShader); } } else { mat = new Material(defaultShader); } if (mat != null) { foreach (MaterialPropertyInfo tinfo in info.properties) { switch (tinfo.type) { case 0: //ShaderUtil.ShaderPropertyType.Color: { Color col = JsonUtility.FromJson <Color>(tinfo.value); mat.SetColor(tinfo.name, col); } break; case 1: //ShaderUtil.ShaderPropertyType.Vector: { Vector4 vec = JsonUtility.FromJson <Vector4>(tinfo.value); mat.SetVector(tinfo.name, vec); } break; case 2: //ShaderUtil.ShaderPropertyType.Float: { float value = JsonUtility.FromJson <float>(tinfo.value); mat.SetFloat(tinfo.name, value); } break; case 3: //ShaderUtil.ShaderPropertyType.Range: { float value = JsonUtility.FromJson <float>(tinfo.value); mat.SetFloat(tinfo.name, value); } break; case 4: //ShaderUtil.ShaderPropertyType.TexEnv: { foreach (KeyValuePair <string, Texture2D> pair in textureList) { if (pair.Key == tinfo.value) { Texture2D texture = pair.Value; mat.SetTexture(tinfo.name, pair.Value); } } } break; } } //end of foreach } KeyValuePair <string, Material> matPair = new KeyValuePair <string, Material>(info.name, mat); materialList.Add(matPair); requestQueue--; }