private void OnCreateMaterial(object e) { CMaterialAsset newMaterial = new CMaterialAsset(); newMaterial.Name = "Material"; newMaterial.LoadFinished(); CAssetRegistry.Instance.RegisterAsset(newMaterial, ActiveDirectory, false); UpdateShownAssets(); }
internal override void InitFromAsset(Device device, CAsset asset) { CMaterialAsset materialAsset = (CMaterialAsset)asset; System.Diagnostics.Debug.Assert(materialAsset != null && materialAsset.IsLoaded); if (materialAsset.Shader != null) { ShaderResource = CRenderer.Instance.ResourceManager.RequestResourceFromAsset <CShaderResource>(materialAsset.Shader); } else { ShaderResource = CRenderer.Instance.ResourceManager.DefaultShader; } SetColorParameter(new SHashedName("tintColor"), Vector4.One); SetColorParameter(new SHashedName("specularColor"), Vector4.One); SetScalarParameter(new SHashedName("specularPower"), 10); for (int i = 0; i < materialAsset.MaterialParameters.Count; i++) { var parameterDesc = materialAsset.MaterialParameters[i]; if (parameterDesc.parameter.parameterData == null) { continue; } if (parameterDesc.parameter.parameterType == EShaderParameterType.Texture) { CAssetReference <CTextureAsset> textureReference = (CAssetReference <CTextureAsset>)parameterDesc.parameter.parameterData; if (textureReference.GetAsset() != null) { CTextureSampler sampler = CRenderer.Instance.ResourceManager.RequestResourceFromAsset <CTextureSampler>(textureReference.GetAsset()); SShaderParameter activeParameter = new SShaderParameter() { parameterData = sampler, parameterType = EShaderParameterType.Texture }; m_activeParameters[parameterDesc.name] = activeParameter; } } else { m_activeParameters[parameterDesc.name] = parameterDesc.parameter; } } }
public CMeshRenderNode(object outer, CMeshAsset meshAsset, CMaterialAsset materialOverride, Transform transform) : base(outer) { m_sourceAsset = meshAsset; m_sourceOverrideMaterial = materialOverride; m_transform = transform; }
private void LoadMeshInternal(int meshIndex, CMeshAsset asset, CMeshLoadingJob loadingJob, string assetPath, string nameOverride = null, bool bAlwaysImport = false) { Assimp.Mesh assimpMesh = loadingJob.Scene.Meshes[meshIndex]; // Load texture and material from the file if present //todo henning extract more textures Material material = loadingJob.Scene.Materials[assimpMesh.MaterialIndex]; if (material != null && material.GetMaterialTextureCount(TextureType.Diffuse) > 0) { if (material.GetMaterialTexture(TextureType.Diffuse, 0, out TextureSlot texture)) { if (loadingJob.LoadedMaterials == null || !loadingJob.LoadedMaterials.TryGetValue(material.Name, out CMaterialAsset materialAsset)) { materialAsset = new CMaterialAsset(); loadingJob.LoadedMaterials?.Add(material.Name, materialAsset); // Make sure we only load each referenced texture once if (loadingJob.LoadedTextures == null || !loadingJob.LoadedTextures.TryGetValue(texture.FilePath, out CTextureAsset textureAsset)) { textureAsset = CImportManager.Instance.TextureImporter.ImportTextureAsync(loadingJob.BasePath + "\\" + texture.FilePath, assetPath + "Textures/"); loadingJob.LoadedTextures?.Add(texture.FilePath, textureAsset); } SShaderParameter textureParameter = new SShaderParameter() { parameterData = new CAssetReference <CTextureAsset>(textureAsset), parameterType = EShaderParameterType.Texture }; materialAsset.MaterialParameters.Add(new SMaterialParameterEntry(new SHashedName("DiffuseTexture"), textureParameter)); materialAsset.Name = material.Name; if (CAssetRegistry.Instance.RequestRegisterAsset(materialAsset, assetPath + "Materials/", out CMaterialAsset existingMaterial)) { existingMaterial.WaitUntilLoaded(); existingMaterial.CopyFrom(existingMaterial); } materialAsset.LoadFinished(); } asset.MaterialAsset = materialAsset; } } bool hasTexCoords = assimpMesh.HasTextureCoords(0); bool hasColors = assimpMesh.HasVertexColors(0); bool hasNormals = assimpMesh.HasNormals; bool hasTangents = assimpMesh.Tangents != null && assimpMesh.Tangents.Count > 0; bool hasBiTangents = assimpMesh.BiTangents != null && assimpMesh.BiTangents.Count > 0; switch (assimpMesh.PrimitiveType) { case PrimitiveType.Point: asset.PrimitiveTopology = PrimitiveTopology.PointList; break; case PrimitiveType.Line: asset.PrimitiveTopology = PrimitiveTopology.LineList; break; case PrimitiveType.Triangle: asset.PrimitiveTopology = PrimitiveTopology.TriangleList; break; default: throw new ArgumentOutOfRangeException("Primtive Type not supported: " + assimpMesh.PrimitiveType.ToString()); } asset.FaceCount = assimpMesh.FaceCount; asset.VertexData = new SVertexInfo[assimpMesh.VertexCount]; Vector3 boundingBoxMin = new Vector3(1e10f, 1e10f, 1e10f); Vector3 boundingBoxMax = new Vector3(-1e10f, -1e10f, -1e10f); for (int i = 0; i < assimpMesh.VertexCount; i++) { SVertexInfo vertexInfo = new SVertexInfo(); vertexInfo.position = FromAssimpVector(assimpMesh.Vertices[i]); boundingBoxMin.X = Math.Min(vertexInfo.position.X, boundingBoxMin.X); boundingBoxMin.Y = Math.Min(vertexInfo.position.Y, boundingBoxMin.Y); boundingBoxMin.Z = Math.Min(vertexInfo.position.Z, boundingBoxMin.Z); boundingBoxMax.X = Math.Max(vertexInfo.position.X, boundingBoxMax.X); boundingBoxMax.Y = Math.Max(vertexInfo.position.Y, boundingBoxMax.Y); boundingBoxMax.Z = Math.Max(vertexInfo.position.Z, boundingBoxMax.Z); if (hasColors) { vertexInfo.color = FromAssimpColor(assimpMesh.VertexColorChannels[0][i]); } else { vertexInfo.color = Vector4.One; } if (hasNormals) { vertexInfo.normal = FromAssimpVector(assimpMesh.Normals[i]); } if (hasBiTangents) { vertexInfo.biTangent = FromAssimpVector(assimpMesh.BiTangents[i]); } if (hasTangents) { vertexInfo.tangent = FromAssimpVector(assimpMesh.Tangents[i]); } if (hasTexCoords) { Vector3D assimpTexCoord = assimpMesh.TextureCoordinateChannels[0][i]; vertexInfo.texCoord = new Vector2(assimpTexCoord.X, assimpTexCoord.Y); } asset.VertexData[i] = vertexInfo; } asset.AABBMin = boundingBoxMin; asset.AABBMax = boundingBoxMax; asset.IndexData = assimpMesh.GetIndices(); asset.Name = nameOverride ?? assimpMesh.Name; if (CAssetRegistry.Instance.RequestRegisterAsset(asset, assetPath, out CMeshAsset existingAsset, true)) { existingAsset.WaitUntilLoaded(); asset.CopyFrom(existingAsset); } }