public override byte[] WriteGLTF()
        {
            var target   = this._target;
            var gltfJson = new MyJson_Tree();
            //
            var assetJson = new MyJson_Tree();

            assetJson.SetInt("version", 2);
            gltfJson.Add("asset", assetJson);

            var materialsJson = new MyJson_Array();

            gltfJson.Add("materials", materialsJson);

            var extensionsRequired = new MyJson_Array();

            extensionsRequired.AddString("KHR_techniques_webgl");
            extensionsRequired.AddString("paper");
            gltfJson.Add("extensionsRequired", extensionsRequired);

            var extensionsUsed = new MyJson_Array();

            extensionsUsed.AddString("KHR_techniques_webgl");
            extensionsUsed.AddString("paper");
            gltfJson.Add("extensionsUsed", extensionsUsed);
            //
            gltfJson.SetInt("version", 4);

            //Unifrom or Defines
            var materialType = this.GetMaterialType();
            var writer       = MaterialWriterFactory.Create(materialType, target);

            var materialItemJson = writer.Write();

            materialsJson.Add(materialItemJson);
            writer.Clean();

            //
            gltfJson.isWithFormat = true;
            var jsonStr = gltfJson.ToString();

            return(System.Text.Encoding.UTF8.GetBytes(jsonStr));
        }
Beispiel #2
0
        public MyJson_Tree Write()
        {
            this.Update();
            var customConfig = ExportConfig.instance.IsCustomShader(this.shaderName);
            //
            var materialItemJson = new MyJson_Tree();
            var extensions       = new MyJson_Tree();

            materialItemJson.Add("extensions", extensions);

            var KHR_techniques_webglJson = new MyJson_Tree();

            extensions.Add("KHR_techniques_webgl", KHR_techniques_webglJson);

            var paperJson = new MyJson_Tree();

            extensions.Add("paper", paperJson);

            var valuesJson = new MyJson_Tree();

            KHR_techniques_webglJson.Add("values", valuesJson);

            //
            var source     = this.source;
            var shaderName = source.shader.name.ToLower();

            MyLog.Log("Shader:" + shaderName);
            foreach (var value in this.values)
            {
                valuesJson.Add(value.Key, value.Value);
            }
            if (customConfig != null && !string.IsNullOrEmpty(customConfig.technique))
            {
                KHR_techniques_webglJson.SetString("technique", customConfig.technique);
            }
            else
            {
                KHR_techniques_webglJson.SetString("technique", this.technique);
            }
            //paper
            paperJson.SetInt("renderQueue", this.source.renderQueue);


            if (this.defines.Count > 0)
            {
                var definesJson = new MyJson_Array();
                foreach (var define in this.defines)
                {
                    definesJson.AddString(define);
                }
                paperJson.Add("defines", definesJson);
            }

            //
            //standard
            var isDoubleSide  = this.isDoubleSide;
            var isTransparent = this.isTransparent;

            var blend      = this.blendMode;
            var statesJson = new MyJson_Tree();
            var enable     = new MyJson_Array();

            if (isDoubleSide || blend != BlendMode.None || isTransparent || (customConfig != null && customConfig.enable != null))
            {
                //states
                paperJson.Add("states", statesJson);
                var functionsJson = new MyJson_Tree();
                statesJson.Add("enable", enable);
                statesJson.Add("functions", functionsJson);
                if (customConfig != null && customConfig.enable != null)
                {
                    foreach (var value in customConfig.enable)
                    {
                        enable.AddInt(value);
                    }
                    if (customConfig.blendEquationSeparate != null)
                    {
                        this.SetBlendEquationSeparate(functionsJson, customConfig.blendEquationSeparate);
                    }
                    if (customConfig.blendFuncSeparate != null)
                    {
                        this.SetBlendFuncSeparate(functionsJson, customConfig.blendFuncSeparate);
                    }
                    if (customConfig.frontFace != null)
                    {
                        this.SetFrontFace(functionsJson, customConfig.frontFace);
                    }
                    if (customConfig.cullFace != null)
                    {
                        this.SetCullFace(functionsJson, customConfig.cullFace);
                    }
                    if (customConfig.depthFunc != null)
                    {
                        this.SetDepthFunc(functionsJson, customConfig.depthFunc);
                    }
                    if (customConfig.depthMask != null)
                    {
                        this.SetDepthMask(functionsJson, customConfig.depthMask);
                    }
                }
                else
                {
                    this.SetBlend(enable, functionsJson, blend);
                    this.SetCullFace(enable, functionsJson, !isDoubleSide);
                    this.SetDepth(enable, functionsJson, true, !isTransparent);
                }
            }
            return(materialItemJson);
        }