public void MaterialParameterChanged(MaterialParameterViewModel changedParameter)
        {
            if (m_targetMaterial != null)
            {
                m_targetMaterial.m_activeParameters[changedParameter.HashedName] = new SShaderParameter(changedParameter.ParameterType, changedParameter.GetMaterialValue());
            }

            if (m_targetMaterialAsset != null)
            {
                bool bFoundParameter   = false;
                var  shaderParameter   = new SShaderParameter(changedParameter.ParameterType, changedParameter.GetAssetValue());
                var  materialParameter = new SMaterialParameterEntry(changedParameter.HashedName, shaderParameter);
                for (var index = 0; index < m_targetMaterialAsset.MaterialParameters.Count; index++)
                {
                    SMaterialParameterEntry parameterEntry = m_targetMaterialAsset.MaterialParameters[index];
                    if (parameterEntry.name == changedParameter.HashedName)
                    {
                        m_targetMaterialAsset.MaterialParameters[index] = materialParameter;
                        bFoundParameter = true;
                        break;
                    }
                }

                if (!bFoundParameter)
                {
                    m_targetMaterialAsset.MaterialParameters.Add(materialParameter);
                }
                CAssetRegistry.Instance.SaveAsset(m_targetMaterialAsset);
            }

            CreateMaterialProperties();
            Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() => { m_propertyInspector.ShowInspectors(MaterialProperties); }));
        }
示例#2
0
        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;
                }
            }
        }
示例#3
0
        internal void RenderWithShader(DeviceContext deviceContext, CShaderResource shaderResource, Transform transform)
        {
            System.Diagnostics.Debug.Assert(shaderResource != null);
            ShaderParameterDict parameters = new ShaderParameterDict();

            SShaderParameter worldMatrixParameter = new SShaderParameter()
            {
                parameterData = transform.WorldMatrix,
                parameterType = EShaderParameterType.Matrix
            };

            parameters[SShaderParameterNames.WorldMatrixParameterName] = worldMatrixParameter;
            worldMatrixParameter.parameterData = Matrix.Invert(transform.WorldMatrix);
            parameters[SShaderParameterNames.InvTransWorldMatrixParName] = worldMatrixParameter;

            shaderResource.Shader.SetShaderParameters(deviceContext, parameters);
            shaderResource.Shader.SetActive(deviceContext);

            deviceContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(m_vertexBuffer, m_sizePerVertex, 0));
            deviceContext.InputAssembler.SetIndexBuffer(m_indexBuffer, SharpDX.DXGI.Format.R32_UInt, 0);
            deviceContext.InputAssembler.PrimitiveTopology = m_primitiveTopology;
            deviceContext.DrawIndexed(m_indexCount, 0, 0);
        }
示例#4
0
 public SMaterialParameterEntry(SHashedName inName, SShaderParameter inParameter)
 {
     name      = inName;
     parameter = inParameter;
 }
示例#5
0
        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);
            }
        }