Exemple #1
0
        public void SetModel(string mdlPath)
        {
            if (StringComparer.InvariantCultureIgnoreCase.Equals(mdlPath, _curModel))
            {
                return;
            }
            _curModel = Model = mdlPath;

            if (string.IsNullOrEmpty(mdlPath))
            {
                _meshFilter.sharedMesh = null;
                return;
            }

            try
            {
                var mdl = World.Resources.LoadMdl(mdlPath);

                _meshFilter.sharedMesh = OnGetMesh(mdl, 0);

                var mats = new Material[_meshFilter.sharedMesh.subMeshCount];

                for (var i = 0; i < mats.Length; ++i)
                {
                    try
                    {
                        var matName = mdl.GetMaterialName(0, i);
                        var mat     = World.Resources.LoadVmt(matName);
                        mats[i] = mat.GetMaterial(World.Resources);
                    }
                    catch (Exception e)
                    {
                        Debug.LogException(e);
                        mats[i] = VmtFile.GetDefaultMaterial(false);
                    }
                }

                _renderer.sharedMaterials = mats;
            }
            catch (Exception e)
            {
                Debug.LogException(e);
                _meshFilter.sharedMesh = null;
            }
        }
Exemple #2
0
        private void PlaceAmbientDebugObjects()
        {
            const float fracScale = 1f / 256f;

            var cubeObj = GameObject.CreatePrimitive(UnityEngine.PrimitiveType.Cube);
            var cube    = cubeObj.GetComponent <MeshFilter>().sharedMesh;

            Object.Destroy(cubeObj);

            var mat = Object.Instantiate(VmtFile.GetDefaultMaterial(false));

            mat.EnableKeyword("AMBIENT_CUBE");

            var props = new MaterialPropertyBlock();

            for (var leafIndex = 0; leafIndex < Leaves.Length; ++leafIndex)
            {
                var leaf    = Leaves[leafIndex];
                var indices = AmbientIndicesHdr[leafIndex];
                var min     = new Vector3(leaf.MinX, leaf.MinY, leaf.MinZ);
                var size    = new Vector3(leaf.MaxX, leaf.MaxY, leaf.MaxZ) - min;

                for (var index = indices.FirstSample; index < indices.FirstSample + indices.SampleCount; ++index)
                {
                    var sample    = AmbientLightingHdr[index];
                    var samplePos = min + Vector3.Scale(new Vector3(sample.X, sample.Y, sample.Z) * fracScale, size);

                    var obj = new GameObject("Ambient[" + leafIndex + "," + index + "]");
                    obj.transform.position   = new Vector3(samplePos.x, samplePos.z, samplePos.y) * SourceToUnityUnits;
                    obj.transform.localScale = Vector3.one * 0.25f;
                    obj.AddComponent <MeshFilter>().sharedMesh = cube;

                    var mr = obj.AddComponent <MeshRenderer>();
                    mr.sharedMaterial = mat;

                    var samples = _ambientLightCubes[index];
                    for (var i = 0; i < 6; ++i)
                    {
                        props.SetColor("_AmbientCube" + i, samples[i]);
                    }

                    mr.SetPropertyBlock(props);
                }
            }
        }