Material CreateMaterial(Texture texture, string shaderName, int variation) { var key = new MaterialKey(texture, shaderName, variation); Shader shader = Shader.Find(shaderName); if (shader == null) { Debug.LogError("Shader not found: " + shaderName); return null; } var material = new Material(shader); material.mainTexture = texture; material.hideFlags = HideFlags.DontSave; materials.Add(key, material); return material; }
public Material MaterialFor(Texture texture, string shaderName, int variation) { if (texture == null) { Debug.LogError("null texture", this); return null; } if (shaderName == null) { Debug.LogError("null shader name", this); return null; } var key = new MaterialKey(texture, shaderName, variation); if (materials.ContainsKey(key)) { return materials[key]; } else { return CreateMaterial(texture, shaderName, 0); } }
/// <summary> /// Getting material from cache or instantiating new one. /// </summary> /// <returns></returns> public static Material GetOrCreateMaterial(Material baseMaterial, Texture2D texture, out MaterialKey materialKey) { if (baseMaterial == null || texture == null) { materialKey = null; return(null); } MaterialValue matValue; var key = materialKey = new MaterialKey(baseMaterial, texture); if (!MaterialMap.TryGetValue(key, out matValue)) { var mat = (Material)Instantiate(baseMaterial); mat.name = GeneratedMaterialName; mat.mainTexture = texture; MaterialMap[key] = matValue = new MaterialValue(mat, 1); } else { matValue.UsageCount++; } return(matValue.Material); }
unsafe void RenderInternal(CommandBuffer commandBuffer, byte[] computeBufferTemp, ComputeBuffer computeBuffer, MaterialPropCollection matPropCol, RenderTexture background) { var renderParameterCount = Plugin.GetUnityRenderParameterCount(); // var vertexBufferSize = Plugin.GetUnityRenderVertexBufferCount(); if (renderParameterCount > 0) { Plugin.UnityRenderParameter parameter = new Plugin.UnityRenderParameter(); var vertexBufferCount = Plugin.GetUnityRenderVertexBufferCount(); if (vertexBufferCount > 0) { var vertexBuffer = Plugin.GetUnityRenderVertexBuffer(); System.Runtime.InteropServices.Marshal.Copy(vertexBuffer, computeBufferTemp, 0, vertexBufferCount); computeBuffer.SetData(computeBufferTemp, 0, 0, vertexBufferCount); } for (int i = 0; i < renderParameterCount; i++) { Plugin.GetUnityRenderParameter(ref parameter, i); if (parameter.RenderMode == 1) { // Draw model var infoBuffer = Plugin.GetUnityRenderInfoBuffer(); var modelParameters = ((Plugin.UnityRenderModelParameter *)(((byte *)infoBuffer.ToPointer()) + parameter.VertexBufferOffset)); MaterialKey key = new MaterialKey(); key.Blend = (AlphaBlendType)parameter.Blend; key.ZTest = parameter.ZTest > 0; key.ZWrite = parameter.ZWrite > 0; if (parameter.Culling == 0) { key.Cull = (int)UnityEngine.Rendering.CullMode.Back; } if (parameter.Culling == 1) { key.Cull = (int)UnityEngine.Rendering.CullMode.Front; } if (parameter.Culling == 2) { key.Cull = (int)UnityEngine.Rendering.CullMode.Off; } for (int mi = 0; mi < parameter.ElementCount; mi++) { var model = EffekseerSystem.GetCachedModel(parameter.ModelPtr); if (model == null) { continue; } var prop = matPropCol.GetNext(); if (parameter.IsDistortingMode > 0) { var material = materialsModelDistortion.GetMaterial(ref key); prop.SetBuffer("buf_vertex", model.VertexBuffer); prop.SetBuffer("buf_index", model.IndexBuffer); prop.SetMatrix("buf_matrix", modelParameters[mi].Matrix); prop.SetVector("buf_uv", modelParameters[mi].UV); prop.SetVector("buf_color", modelParameters[mi].VColor); prop.SetFloat("buf_vertex_offset", model.vertexOffsets[modelParameters[mi].Time]); prop.SetFloat("buf_index_offset", model.indexOffsets[modelParameters[mi].Time]); var colorTexture = GetCachedTexture(parameter.TexturePtrs0, background); if (parameter.TextureWrapTypes[0] == 0) { colorTexture.wrapMode = TextureWrapMode.Repeat; } else { colorTexture.wrapMode = TextureWrapMode.Clamp; } if (parameter.TextureFilterTypes[0] == 0) { colorTexture.filterMode = FilterMode.Point; } else { colorTexture.filterMode = FilterMode.Bilinear; } prop.SetTexture("_ColorTex", colorTexture); if (background != null) { prop.SetTexture("_BackTex", GetCachedTexture(parameter.TexturePtrs1, background)); //Temp //prop.SetTexture("_BackTex", background); commandBuffer.DrawProcedural(new Matrix4x4(), material, 0, MeshTopology.Triangles, model.IndexCounts[0], 1, prop); } } else { var material = materialsModel.GetMaterial(ref key); prop.SetBuffer("buf_vertex", model.VertexBuffer); prop.SetBuffer("buf_index", model.IndexBuffer); prop.SetMatrix("buf_matrix", modelParameters[mi].Matrix); prop.SetVector("buf_uv", modelParameters[mi].UV); prop.SetVector("buf_color", modelParameters[mi].VColor); int modelTime = modelParameters[mi].Time; prop.SetFloat("buf_vertex_offset", model.vertexOffsets[modelTime]); prop.SetFloat("buf_index_offset", model.indexOffsets[modelTime]); var colorTexture = GetCachedTexture(parameter.TexturePtrs0, background); if (parameter.TextureWrapTypes[0] == 0) { colorTexture.wrapMode = TextureWrapMode.Repeat; } else { colorTexture.wrapMode = TextureWrapMode.Clamp; } if (parameter.TextureFilterTypes[0] == 0) { colorTexture.filterMode = FilterMode.Point; } else { colorTexture.filterMode = FilterMode.Bilinear; } prop.SetTexture("_ColorTex", GetCachedTexture(parameter.TexturePtrs0, background)); commandBuffer.DrawProcedural(new Matrix4x4(), material, 0, MeshTopology.Triangles, model.IndexCounts[0], 1, prop); } } } else { var prop = matPropCol.GetNext(); MaterialKey key = new MaterialKey(); key.Blend = (AlphaBlendType)parameter.Blend; key.ZTest = parameter.ZTest > 0; key.ZWrite = parameter.ZWrite > 0; key.Cull = (int)UnityEngine.Rendering.CullMode.Off; if (parameter.IsDistortingMode > 0) { var material = materialsDistortion.GetMaterial(ref key); prop.SetFloat("buf_offset", parameter.VertexBufferOffset / VertexDistortionSize); prop.SetBuffer("buf_vertex", computeBuffer); var colorTexture = GetCachedTexture(parameter.TexturePtrs0, background); if (parameter.TextureWrapTypes[0] == 0) { colorTexture.wrapMode = TextureWrapMode.Repeat; } else { colorTexture.wrapMode = TextureWrapMode.Clamp; } if (parameter.TextureFilterTypes[0] == 0) { colorTexture.filterMode = FilterMode.Point; } else { colorTexture.filterMode = FilterMode.Bilinear; } prop.SetTexture("_ColorTex", colorTexture); if (background != null) { prop.SetTexture("_BackTex", GetCachedTexture(parameter.TexturePtrs1, background)); commandBuffer.DrawProcedural(new Matrix4x4(), material, 0, MeshTopology.Triangles, parameter.ElementCount * 2 * 3, 1, prop); } } else { var material = materials.GetMaterial(ref key); prop.SetFloat("buf_offset", parameter.VertexBufferOffset / VertexSize); prop.SetBuffer("buf_vertex", computeBuffer); var colorTexture = GetCachedTexture(parameter.TexturePtrs0, background); if (parameter.TextureWrapTypes[0] == 0) { colorTexture.wrapMode = TextureWrapMode.Repeat; } else { colorTexture.wrapMode = TextureWrapMode.Clamp; } if (parameter.TextureFilterTypes[0] == 0) { colorTexture.filterMode = FilterMode.Point; } else { colorTexture.filterMode = FilterMode.Bilinear; } prop.SetTexture("_ColorTex", colorTexture); commandBuffer.DrawProcedural(new Matrix4x4(), material, 0, MeshTopology.Triangles, parameter.ElementCount * 2 * 3, 1, prop); } } } } }
private static IEnumerator LoadMaterial(BinaryReader source, string modelFolder, Action <Material> callback) { string texName = ReadString(source); string texLocation = Path.Combine(modelFolder, texName); string matName = ReadString(source); MaterialKey materialKey = new MaterialKey(matName, texLocation); float glossiness = 0; bool hasMaterialProperties = source.ReadBoolean(); if (hasMaterialProperties) { bool hasEmissive = source.ReadBoolean(); if (hasEmissive) { source.ReadSingle(); source.ReadSingle(); source.ReadSingle(); source.ReadSingle(); } bool hasShininess = source.ReadBoolean(); if (hasShininess) { glossiness = source.ReadSingle() / 100; if (glossiness > 1) { glossiness = 0; } } bool hasSpecular = source.ReadBoolean(); if (hasSpecular) { source.ReadSingle(); source.ReadSingle(); source.ReadSingle(); source.ReadSingle(); } bool hasTransparencyColor = source.ReadBoolean(); if (hasTransparencyColor) { source.ReadSingle(); source.ReadSingle(); source.ReadSingle(); source.ReadSingle(); } } if (cachedMaterials.ContainsKey(materialKey)) { Debug.Log("Loading material from cache"); Material cachedMaterial = cachedMaterials[materialKey]; callback.Invoke(cachedMaterial); } else { Material material = new Material(GraphicsManager.Instance.WomDefaultMaterial); material.name = matName; TextureReference textureReference = TextureReference.GetTextureReference(texLocation); Texture2D texture = null; if (textureReference != null) { yield return(textureReference.LoadOrGetTexture(loadedTexture => texture = loadedTexture)); } if (texture) { material.SetTexture(ShaderPropertyIds.MainTex, texture); } else { material.SetColor(ShaderPropertyIds.Color, new Color(1, 1, 1, 0)); } material.SetFloat(ShaderPropertyIds.Glossiness, glossiness); cachedMaterials[materialKey] = material; callback.Invoke(material); } }