private GameObject CreateMeshPrimitive(MeshPrimitive primitive) { var primitiveObj = new GameObject("Primitive"); var meshFilter = primitiveObj.AddComponent <MeshFilter>(); var attributes = _attributesCache[primitive]; var mesh = new UnityEngine.Mesh { vertices = attributes.Vertices, normals = attributes.Normals, uv = attributes.Uv, uv2 = attributes.Uv2, uv3 = attributes.Uv3, uv4 = attributes.Uv4, colors = attributes.Colors, triangles = attributes.Triangles, tangents = attributes.Tangents }; meshFilter.mesh = mesh; var meshRenderer = primitiveObj.AddComponent <MeshRenderer>(); UnityEngine.Material material = null; if (primitive.Material != null) { var materialCacheKey = new MaterialCacheKey { Material = primitive.Material.Value, UseVertexColors = attributes.Colors != null }; try { material = FindOrCreateMaterial(materialCacheKey); } catch (Exception e) { Debug.LogException(e); Debug.LogWarningFormat("Failed to create material from {0}, using default", materialCacheKey.Material.Name); } } if (material == null) { var materialCacheKey = new MaterialCacheKey { Material = new Material(), UseVertexColors = attributes.Colors != null }; material = FindOrCreateMaterial(materialCacheKey); } meshRenderer.material = material; return(primitiveObj); }
public bool Equals(ref MaterialCacheKey rhs) { return((Material == rhs.Material) && (RasterizerState == rhs.RasterizerState) && (DepthStencilState == rhs.DepthStencilState) && (BlendState == rhs.BlendState)); }
private UnityEngine.Material FindOrCreateMaterial(MaterialCacheKey materialKey) { UnityEngine.Material material; if (_materialCache.TryGetValue(materialKey, out material)) { return(material); } material = CreateMaterial(materialKey.Material, materialKey.UseVertexColors); _materialCache.Add(materialKey, material); return(material); }
private GameObject CreateMeshPrimitive(GLTFMeshPrimitive primitive) { var primitiveObj = new GameObject("Primitive"); var meshFilter = primitiveObj.AddComponent <MeshFilter>(); var attributes = _attributesCache[primitive]; var mesh = new Mesh { vertices = attributes.Vertices, normals = attributes.Normals, uv = attributes.Uv, uv2 = attributes.Uv2, uv3 = attributes.Uv3, uv4 = attributes.Uv4, colors = attributes.Colors, triangles = attributes.Triangles, tangents = attributes.Tangents }; meshFilter.mesh = mesh; var meshRenderer = primitiveObj.AddComponent <MeshRenderer>(); if (primitive.Material != null) { var materialCacheKey = new MaterialCacheKey { Material = primitive.Material.Value, UseVertexColors = attributes.Colors != null }; meshRenderer.material = FindOrCreateMaterial(materialCacheKey); } else { var materialCacheKey = new MaterialCacheKey { Material = new GLTFMaterial(), UseVertexColors = attributes.Colors != null }; meshRenderer.material = FindOrCreateMaterial(materialCacheKey); } return(primitiveObj); }
/// <summary> /// Returns a new version of a given material with rasterizer, depth/stencil, and blend state(s) optionally applied to it. This new version is cached. /// If no states are provided, the base material is returned. /// </summary> /// <param name="baseMaterial">The base material.</param> /// <param name="rasterizerState">The new rasterizer state, or null.</param> /// <param name="depthStencilState">The new depth/stencil state, or null.</param> /// <param name="blendState">The new blend state, or null.</param> /// <returns>The material with state(s) applied.</returns> public Material Get(Material baseMaterial, RasterizerState rasterizerState = null, DepthStencilState depthStencilState = null, BlendState blendState = null) { if ( (rasterizerState == null) && (depthStencilState == null) && (blendState == null) ) { return(baseMaterial); } var key = new MaterialCacheKey(baseMaterial, rasterizerState, depthStencilState, blendState); Material result; if (!MaterialCache.TryGetValue(key, out result)) { result = baseMaterial.SetStates(rasterizerState, depthStencilState, blendState); MaterialCache.Add(key, result); } return(result); }