Beispiel #1
0
        private static void PrintScriptBody(IKeyValueCollection node, IndentedTextWriter writer)
        {
            var content = node.GetProperty <string>("name");

            writer.Write("<script><![CDATA[");
            writer.Write(content);
            writer.WriteLine("]]></script>");
        }
        /// <summary>
        /// Construct an animation class from the animation description.
        /// </summary>
        private void ConstructFromDesc(
            IKeyValueCollection animDesc,
            IKeyValueCollection decodeKey,
            AnimDecoderType[] decoderArray,
            IKeyValueCollection[] segmentArray)
        {
            // Get animation properties
            Name = animDesc.GetProperty <string>("m_name");
            Fps  = animDesc.GetFloatProperty("fps");

            var pDataObject = animDesc.GetProperty <object>("m_pData");
            var pData       = pDataObject is NTROValue[] ntroArray
                ? ntroArray[0].ValueObject as IKeyValueCollection
                : pDataObject as IKeyValueCollection;
            var frameBlockArray = pData.GetArray("m_frameblockArray");

            FrameCount = (int)pData.GetIntegerProperty("m_nFrames");
            var frameArray = new Frame[FrameCount];

            // Figure out each frame
            for (var frame = 0; frame < FrameCount; frame++)
            {
                // Create new frame object
                frameArray[frame] = new Frame();

                // Read all frame blocks
                foreach (var frameBlock in frameBlockArray)
                {
                    var startFrame = frameBlock.GetIntegerProperty("m_nStartFrame");
                    var endFrame   = frameBlock.GetIntegerProperty("m_nEndFrame");

                    // Only consider blocks that actual contain info for this frame
                    if (frame >= startFrame && frame <= endFrame)
                    {
                        var segmentIndexArray = frameBlock.GetIntegerArray("m_segmentIndexArray");

                        foreach (var segmentIndex in segmentIndexArray)
                        {
                            var segment = segmentArray[segmentIndex];
                            ReadSegment(frame - startFrame, segment, decodeKey, decoderArray, ref frameArray[frame]);
                        }
                    }
                }
            }
            Frames = frameArray;
        }
Beispiel #3
0
        public static bool IsCompressedNormalTangent(IKeyValueCollection drawCall)
        {
            if (drawCall.ContainsKey("m_bUseCompressedNormalTangent"))
            {
                return(drawCall.GetProperty <bool>("m_bUseCompressedNormalTangent"));
            }

            if (!drawCall.ContainsKey("m_nFlags"))
            {
                return(false);
            }

            var flags = drawCall.GetProperty <object>("m_nFlags");

            return(flags switch
            {
                string flagsString => flagsString.Contains("MESH_DRAW_FLAGS_USE_COMPRESSED_NORMAL_TANGENT", StringComparison.InvariantCulture),
                long flagsLong =>
                // TODO: enum
                (flagsLong & 2) == 2,
                _ => false
            });
Beispiel #4
0
        public static ulong GetUnsignedIntegerProperty(this IKeyValueCollection collection, string name)
        {
            var value = collection.GetProperty <object>(name);

            if (value is int i)
            {
                unchecked
                {
                    return((ulong)i);
                }
            }

            return(Convert.ToUInt64(value));
        }
Beispiel #5
0
        public static bool IsCompressedNormalTangent(IKeyValueCollection drawCall)
        {
            if (drawCall.ContainsKey("m_bUseCompressedNormalTangent"))
            {
                return(drawCall.GetProperty <bool>("m_bUseCompressedNormalTangent"));
            }

            if (drawCall.ContainsKey("m_nFlags"))
            {
                var flags = drawCall.GetProperty <object>("m_nFlags");

                switch (flags)
                {
                case string flagsString:
                    return(flagsString.Contains("MESH_DRAW_FLAGS_USE_COMPRESSED_NORMAL_TANGENT"));

                case long flagsLong:
                    // TODO: enum
                    return((flagsLong & 2) == 2);
                }
            }

            return(false);
        }
Beispiel #6
0
        private static void PrintSnippet(IKeyValueCollection node, IndentedTextWriter writer)
        {
            var nodeChildren = NodeChildren(node);

            var name = node.GetProperty <string>("name");

            writer.WriteLine($"<snippet name=\"{name}\">");
            writer.Indent++;

            foreach (var child in nodeChildren)
            {
                PrintNode(child, writer);
            }

            writer.Indent--;
            writer.WriteLine("</snippet>");
        }
Beispiel #7
0
        public RandomSequence(IKeyValueCollection keyValues)
        {
            if (keyValues.ContainsKey("m_nSequenceMin"))
            {
                sequenceMin = (int)keyValues.GetIntegerProperty("m_nSequenceMin");
            }

            if (keyValues.ContainsKey("m_nSequenceMax"))
            {
                sequenceMax = (int)keyValues.GetIntegerProperty("m_nSequenceMax");
            }

            if (keyValues.ContainsKey("m_bShuffle"))
            {
                shuffle = keyValues.GetProperty <bool>("m_bShuffle");
            }
        }
        public OscillateScalar(IKeyValueCollection keyValues)
        {
            if (keyValues.ContainsKey("m_nField"))
            {
                outputField = (ParticleField)keyValues.GetIntegerProperty("m_nField");
            }

            if (keyValues.ContainsKey("m_RateMin"))
            {
                rateMin = keyValues.GetFloatProperty("m_RateMin");
            }

            if (keyValues.ContainsKey("m_RateMax"))
            {
                rateMax = keyValues.GetFloatProperty("m_RateMax");
            }

            if (keyValues.ContainsKey("m_FrequencyMin"))
            {
                frequencyMin = keyValues.GetFloatProperty("m_FrequencyMin");
            }

            if (keyValues.ContainsKey("m_FrequencyMax"))
            {
                frequencyMax = keyValues.GetFloatProperty("m_FrequencyMax");
            }

            if (keyValues.ContainsKey("m_flOscMult"))
            {
                oscillationMultiplier = keyValues.GetFloatProperty("m_flOscMult");
            }

            if (keyValues.ContainsKey("m_flOscAdd"))
            {
                oscillationOffset = keyValues.GetFloatProperty("m_flOscAdd");
            }

            if (keyValues.ContainsKey("m_bProportionalOp"))
            {
                proportional = keyValues.GetProperty <bool>("m_bProportionalOp");
            }

            random = new Random();
        }
Beispiel #9
0
        public static IVectorProvider GetVectorProvider(this IKeyValueCollection keyValues, string propertyName)
        {
            var property = keyValues.GetProperty <object>(propertyName);

            if (property is IKeyValueCollection numberProviderParameters && numberProviderParameters.ContainsKey("m_nType"))
            {
                var type = numberProviderParameters.GetProperty <string>("m_nType");
                switch (type)
                {
                case "PVEC_TYPE_LITERAL":
                    return(new LiteralVectorProvider(numberProviderParameters.GetArray <double>("m_vLiteralValue")));

                default:
                    throw new InvalidCastException($"Could not create vector provider of type {type}.");
                }
            }

            return(new LiteralVectorProvider(keyValues.GetArray <double>(propertyName)));
        }
Beispiel #10
0
        public static INumberProvider GetNumberProvider(this IKeyValueCollection keyValues, string propertyName)
        {
            var property = keyValues.GetProperty <object>(propertyName);

            if (property is IKeyValueCollection numberProviderParameters)
            {
                var type = numberProviderParameters.GetProperty <string>("m_nType");
                switch (type)
                {
                case "PF_TYPE_LITERAL":
                    return(new LiteralNumberProvider(numberProviderParameters.GetDoubleProperty("m_flLiteralValue")));

                default:
                    throw new InvalidCastException($"Could not create number provider of type {type}.");
                }
            }
            else
            {
                return(new LiteralNumberProvider(Convert.ToDouble(property)));
            }
        }
        public RingWave(IKeyValueCollection keyValues)
        {
            if (keyValues.ContainsKey("m_bEvenDistribution"))
            {
                evenDistribution = keyValues.GetProperty <bool>("m_bEvenDistribution");
            }

            if (keyValues.ContainsKey("m_flParticlesPerOrbit"))
            {
                particlesPerOrbit = keyValues.GetFloatProperty("m_flParticlesPerOrbit");
            }

            if (keyValues.ContainsKey("m_flInitialRadius"))
            {
                initialRadius = keyValues.GetFloatProperty("m_flInitialRadius");
            }

            if (keyValues.ContainsKey("m_flThickness"))
            {
                thickness = keyValues.GetFloatProperty("m_flThickness");
            }
        }
Beispiel #12
0
 public static double GetDoubleProperty(this IKeyValueCollection collection, string name)
 => Convert.ToDouble(collection.GetProperty <object>(name));
Beispiel #13
0
        private static void PrintPanel(IKeyValueCollection node, IndentedTextWriter writer)
        {
            var name = node.GetProperty <string>("name");

            PrintPanelBase(name, node, writer);
        }
Beispiel #14
0
 private static bool IsAttribute(IKeyValueCollection node) => node.GetProperty <string>("eType") == "PANEL_ATTRIBUTE";
        /// <summary>
        /// Read segment.
        /// </summary>
        private void ReadSegment(long frame, IKeyValueCollection segment, IKeyValueCollection decodeKey, AnimDecoderType[] decoderArray, ref Frame outFrame)
        {
            // Clamp the frame number to be between 0 and the maximum frame
            frame = frame < 0 ? 0 : frame;
            frame = frame >= FrameCount ? FrameCount - 1 : frame;

            var localChannel = segment.GetIntegerProperty("m_nLocalChannel");
            var dataChannel  = decodeKey.GetArray("m_dataChannelArray")[localChannel];
            var boneNames    = dataChannel.GetArray <string>("m_szElementNameArray");

            var channelAttribute = dataChannel.GetProperty <string>("m_szVariableName");

            // Read container
            var container = segment.GetArray <byte>("m_container");

            using (var containerReader = new BinaryReader(new MemoryStream(container)))
            {
                var elementIndexArray = dataChannel.GetIntegerArray("m_nElementIndexArray");
                var elementBones      = new int[decodeKey.GetProperty <int>("m_nChannelElements")];
                for (var i = 0; i < elementIndexArray.Length; i++)
                {
                    elementBones[elementIndexArray[i]] = i;
                }

                // Read header
                var decoder     = decoderArray[containerReader.ReadInt16()];
                var cardinality = containerReader.ReadInt16();
                var numBones    = containerReader.ReadInt16();
                var totalLength = containerReader.ReadInt16();

                // Read bone list
                var elements = new List <int>();
                for (var i = 0; i < numBones; i++)
                {
                    elements.Add(containerReader.ReadInt16());
                }

                // Skip data to find the data for the current frame.
                // Structure is just | Bone 0 - Frame 0 | Bone 1 - Frame 0 | Bone 0 - Frame 1 | Bone 1 - Frame 1|
                if (containerReader.BaseStream.Position + (decoder.Size() * frame * numBones) < containerReader.BaseStream.Length)
                {
                    containerReader.BaseStream.Position += decoder.Size() * frame * numBones;
                }

                // Read animation data for all bones
                for (var element = 0; element < numBones; element++)
                {
                    // Get the bone we are reading for
                    var bone = elementBones[elements[element]];

                    // Look at the decoder to see what to read
                    switch (decoder)
                    {
                    case AnimDecoderType.CCompressedStaticFullVector3:
                    case AnimDecoderType.CCompressedFullVector3:
                    case AnimDecoderType.CCompressedDeltaVector3:
                        outFrame.SetAttribute(boneNames[bone], channelAttribute, new Vector3(
                                                  containerReader.ReadSingle(),
                                                  containerReader.ReadSingle(),
                                                  containerReader.ReadSingle()));
                        break;

                    case AnimDecoderType.CCompressedAnimVector3:
                    case AnimDecoderType.CCompressedStaticVector3:
                        outFrame.SetAttribute(boneNames[bone], channelAttribute, new Vector3(
                                                  ReadHalfFloat(containerReader),
                                                  ReadHalfFloat(containerReader),
                                                  ReadHalfFloat(containerReader)));
                        break;

                    case AnimDecoderType.CCompressedAnimQuaternion:
                    case AnimDecoderType.CCompressedFullQuaternion:
                    case AnimDecoderType.CCompressedStaticQuaternion:
                        outFrame.SetAttribute(boneNames[bone], channelAttribute, ReadQuaternion(containerReader));
                        break;

#if DEBUG
                    default:
                        if (channelAttribute != "data")
                        {
                            Console.WriteLine($"Unhandled animation bone decoder type '{decoder}'");
                        }

                        break;
#endif
                    }
                }
            }
        }
Beispiel #16
0
 public static long GetIntegerProperty(this IKeyValueCollection collection, string name)
 => Convert.ToInt64(collection.GetProperty <object>(name));
Beispiel #17
0
 public static IKeyValueCollection GetSubCollection(this IKeyValueCollection collection, string name)
 => collection.GetProperty <IKeyValueCollection>(name);
        private DrawCall CreateDrawCall(IKeyValueCollection objectDrawCall, VBIB vbib, IDictionary <string, bool> shaderArguments, RenderMaterial material)
        {
            var drawCall = new DrawCall();

            string primitiveType = objectDrawCall.GetProperty <object>("m_nPrimitiveType") switch
            {
                string primitiveTypeString => primitiveTypeString,
                byte primitiveTypeByte =>
                (primitiveTypeByte == 5) ? "RENDER_PRIM_TRIANGLES" : ("UNKNOWN_" + primitiveTypeByte),
                _ => throw new NotImplementedException("Unknown PrimitiveType in drawCall!")
            };

            switch (primitiveType)
            {
            case "RENDER_PRIM_TRIANGLES":
                drawCall.PrimitiveType = PrimitiveType.Triangles;
                break;

            default:
                throw new NotImplementedException("Unknown PrimitiveType in drawCall! (" + primitiveType + ")");
            }

            SetupDrawCallMaterial(drawCall, shaderArguments, material);

            var indexBufferObject = objectDrawCall.GetSubCollection("m_indexBuffer");

            var indexBuffer = default(DrawBuffer);

            indexBuffer.Id       = Convert.ToUInt32(indexBufferObject.GetProperty <object>("m_hBuffer"));
            indexBuffer.Offset   = Convert.ToUInt32(indexBufferObject.GetProperty <object>("m_nBindOffsetBytes"));
            drawCall.IndexBuffer = indexBuffer;

            var indexElementSize = vbib.IndexBuffers[(int)drawCall.IndexBuffer.Id].ElementSizeInBytes;

            //drawCall.BaseVertex = Convert.ToUInt32(objectDrawCall.GetProperty<object>("m_nBaseVertex"));
            //drawCall.VertexCount = Convert.ToUInt32(objectDrawCall.GetProperty<object>("m_nVertexCount"));
            drawCall.StartIndex = Convert.ToUInt32(objectDrawCall.GetProperty <object>("m_nStartIndex")) * indexElementSize;
            drawCall.IndexCount = Convert.ToInt32(objectDrawCall.GetProperty <object>("m_nIndexCount"));

            if (objectDrawCall.ContainsKey("m_vTintColor"))
            {
                var tintColor = objectDrawCall.GetSubCollection("m_vTintColor").ToVector3();
                drawCall.TintColor = new OpenTK.Vector3(tintColor.X, tintColor.Y, tintColor.Z);
            }

            if (indexElementSize == 2)
            {
                //shopkeeper_vr
                drawCall.IndexType = DrawElementsType.UnsignedShort;
            }
            else if (indexElementSize == 4)
            {
                //glados
                drawCall.IndexType = DrawElementsType.UnsignedInt;
            }
            else
            {
                throw new Exception("Unsupported index type");
            }

            var m_vertexBuffer = objectDrawCall.GetArray("m_vertexBuffers")[0]; // TODO: Not just 0

            var vertexBuffer = default(DrawBuffer);

            vertexBuffer.Id       = Convert.ToUInt32(m_vertexBuffer.GetProperty <object>("m_hBuffer"));
            vertexBuffer.Offset   = Convert.ToUInt32(m_vertexBuffer.GetProperty <object>("m_nBindOffsetBytes"));
            drawCall.VertexBuffer = vertexBuffer;

            drawCall.VertexArrayObject = guiContext.MeshBufferCache.GetVertexArrayObject(
                vbib,
                drawCall.Shader,
                drawCall.VertexBuffer.Id,
                drawCall.IndexBuffer.Id);

            return(drawCall);
        }
        private DrawCall CreateDrawCall(IKeyValueCollection objectDrawCall, VBIB vbib, GPUMeshBuffers gpuMeshBuffers, IDictionary <string, bool> shaderArguments, RenderMaterial material)
        {
            var drawCall = new DrawCall();

            switch (objectDrawCall.GetProperty <string>("m_nPrimitiveType"))
            {
            case "RENDER_PRIM_TRIANGLES":
                drawCall.PrimitiveType = PrimitiveType.Triangles;
                break;

            default:
                throw new Exception("Unknown PrimitiveType in drawCall! (" + objectDrawCall.GetProperty <string>("m_nPrimitiveType") + ")");
            }

            drawCall.Material = material;
            // Add shader parameters from material to the shader parameters from the draw call
            var combinedShaderParameters = shaderArguments
                                           .Concat(material.Material.GetShaderArguments())
                                           .ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

            // Load shader
            drawCall.Shader = guiContext.ShaderLoader.LoadShader(drawCall.Material.Material.ShaderName, combinedShaderParameters);

            //Bind and validate shader
            GL.UseProgram(drawCall.Shader.Program);

            var indexBufferObject = objectDrawCall.GetSubCollection("m_indexBuffer");

            var indexBuffer = default(DrawBuffer);

            indexBuffer.Id       = Convert.ToUInt32(indexBufferObject.GetProperty <object>("m_hBuffer"));
            indexBuffer.Offset   = Convert.ToUInt32(indexBufferObject.GetProperty <object>("m_nBindOffsetBytes"));
            drawCall.IndexBuffer = indexBuffer;

            var indexElementSize = vbib.IndexBuffers[(int)drawCall.IndexBuffer.Id].Size;

            //drawCall.BaseVertex = Convert.ToUInt32(objectDrawCall.GetProperty<object>("m_nBaseVertex"));
            //drawCall.VertexCount = Convert.ToUInt32(objectDrawCall.GetProperty<object>("m_nVertexCount"));
            drawCall.StartIndex = Convert.ToUInt32(objectDrawCall.GetProperty <object>("m_nStartIndex")) * indexElementSize;
            drawCall.IndexCount = Convert.ToInt32(objectDrawCall.GetProperty <object>("m_nIndexCount"));

            if (objectDrawCall.ContainsKey("m_vTintColor"))
            {
                var tintColor = objectDrawCall.GetSubCollection("m_vTintColor").ToVector3();
                drawCall.TintColor = new OpenTK.Vector3(tintColor.X, tintColor.Y, tintColor.Z);
            }

            if (!drawCall.Material.Textures.ContainsKey("g_tTintMask"))
            {
                drawCall.Material.Textures.Add("g_tTintMask", MaterialLoader.CreateSolidTexture(1f, 1f, 1f));
            }

            if (!drawCall.Material.Textures.ContainsKey("g_tNormal"))
            {
                drawCall.Material.Textures.Add("g_tNormal", MaterialLoader.CreateSolidTexture(0.5f, 1f, 0.5f));
            }

            if (indexElementSize == 2)
            {
                //shopkeeper_vr
                drawCall.IndexType = DrawElementsType.UnsignedShort;
            }
            else if (indexElementSize == 4)
            {
                //glados
                drawCall.IndexType = DrawElementsType.UnsignedInt;
            }
            else
            {
                throw new Exception("Unsupported index type");
            }

            var m_vertexBuffers = objectDrawCall.GetSubCollection("m_vertexBuffers");
            var m_vertexBuffer  = m_vertexBuffers.GetSubCollection("0"); // TODO: Not just 0

            var vertexBuffer = default(DrawBuffer);

            vertexBuffer.Id       = Convert.ToUInt32(m_vertexBuffer.GetProperty <object>("m_hBuffer"));
            vertexBuffer.Offset   = Convert.ToUInt32(m_vertexBuffer.GetProperty <object>("m_nBindOffsetBytes"));
            drawCall.VertexBuffer = vertexBuffer;

            drawCall.VertexArrayObject = guiContext.MeshBufferCache.GetVertexArrayObject(
                vbib,
                drawCall.Shader,
                drawCall.VertexBuffer.Id,
                drawCall.IndexBuffer.Id);

            return(drawCall);
        }
 public static uint GetUInt32Property(this IKeyValueCollection collection, string name)
 => Convert.ToUInt32(collection.GetProperty <object>(name));