Esempio n. 1
0
        public void ConvMaterial(MaterialLightComponent matComp)
        {
            var effect = LookupMaterial(matComp);

            _currentNode.Components.Add(new ShaderEffectComponent {
                Effect = effect
            });
        }
Esempio n. 2
0
        private ShaderEffect LookupMaterial(MaterialLightComponent mc)
        {
            if (_lightMatMap.TryGetValue(mc, out var mat))
            {
                return(mat);
            }

            mat = ShaderCodeBuilder.MakeShaderEffectFromMatComp(mc, _currentNode.GetWeights());
            _lightMatMap.Add(mc, mat);
            return(mat);
        }
Esempio n. 3
0
        private void AddApplyLightCalculation(StringBuilder ps, MaterialLightComponent mlc)
        {
            if (!_hasApplyLightString)
            {
                return;
            }

            if (!CheckApplyLightStringForErrors(mlc.ApplyLightString))
            {
                throw new ArgumentException($"Error while compiling ApplyLight(). Are you sure this:\n {mlc.ApplyLightString} \nis correct?" + $"\nPerhaps (re)consult the documentation of MaterialLightComponent class.");
            }

            ps.Append("\n\n" + mlc.ApplyLightString);
        }
Esempio n. 4
0
        /// <summary>
        /// If we have a MaterialLightComponent this constructor is called.
        /// </summary>
        /// <param name="mlc">The MaterialLightComponent</param>
        /// <param name="mesh">The Mesh</param>
        /// <param name="wc">WeightCompoennt for animations</param>
        public LegacyShaderCodeBuilder(MaterialLightComponent mlc, Mesh mesh, WeightComponent wc = null)
        {
            // Check WC
            if (wc != null)
            {
                _hasWeightMap = true;
                _nBones       = wc.Joints.Count;
            }
            else
            {
                _nBones = 0;
            }

            //float f1 = 1;
            //var type = f1.GetType();
            _normalizeNormals = true;

            // Check for mesh
            if (mesh != null)
            {
                AnalyzeMesh(mesh);
            }
            else
            {
                _hasVertices = _hasNormals = _hasUVs = true;
            }

            // Analyze the Material
            AnalyzeMaterial(mlc);


            // VS
            StringBuilder vs = new StringBuilder();

            MeshInputDeclarations(vs);
            MatrixDeclarations(vs);
            VSBody(vs);
            _vs = vs.ToString();

            // PS
            StringBuilder ps = new StringBuilder();

            PixelInputDeclarations(ps);
            PSCustomBody(ps, mlc);
            _ps = ps.ToString();
        }
Esempio n. 5
0
        private void AnalyzeMaterial(MaterialLightComponent mlc)
        {
            _hasDiffuse = mlc.HasDiffuse;
            if (_hasDiffuse)
            {
                _hasDiffuseTexture = (mlc.Diffuse.Texture != null);
            }
            _hasSpecular = mlc.HasSpecular;
            if (_hasSpecular)
            {
                _hasSpecularTexture = (mlc.Specular.Texture != null);
            }
            _hasEmissive = mlc.HasEmissive;
            if (_hasEmissive)
            {
                _hasEmissiveTexture = (mlc.Emissive.Texture != null);
            }
            _hasBump = mlc.HasBump; // always has a texture...

            _hasApplyLightString = (mlc.ApplyLightString != null);
            _hasFragmentString   = (mlc.FragmentShaderString != null);
        }
Esempio n. 6
0
        // This is called when MaterialLightComponent is found
        private void PSCustomBody(StringBuilder ps, MaterialLightComponent mlc)
        {
            ParseLights(ps);


            AddApplyLightCalculation(ps, mlc);

            ps.Append("\n\n  void main()\n  {\n");
            ps.Append("    vec3 result = vec3(0, 0, 0);\n\n");

            AddNormalVec(ps);
            AddCameraVec(ps);

            // ApplyLight() is always called
            ps.Append("\n   for(int i = 0; i < 3000; i++) { \n ");
            ps.Append("\n   if(i > MAX_LIGHTS) break; \n ");
            ps.Append("\n   result += ApplyLight(allLights[i]); \n ");
            ps.Append("\n   } \n ");
            ps.Append("\n   vec3 gamma = vec3(1.0/2.2);\n ");
            ps.Append("\n   vec3 final_light = pow(result, gamma); \n ");
            ps.Append("\n   gl_FragColor = vec4(final_light, 1.0);\n");
            ps.Append("  }\n\n");
        }