Example #1
0
        /// <summary>
        /// Fills the vertexUnit with the quad data.
        /// </summary>
        /// <param name="vertexUnit">The vertexUnit to fill with the quad data.</param>
        /// <param name="index">The start index in the index buffer.</param>
        public override void FillVertexUnit(Purple.Graphics.VertexUnit vertexUnit, int index)
        {
            Vector2 a     = this.Position;
            Vector2 right = QuadManager.Instance.RotateUnit(new Vector2(Size.X * this.Scale.X, 0), this.RotationZ);
            Vector2 down  = QuadManager.Instance.RotateUnit(new Vector2(0, Size.Y * this.Scale.Y), this.RotationZ);

            if (dirty)
            {
                PositionStream2 ps = PositionStream2.From(QuadFactory.VertexUnit);
                ColorStream     cs = ColorStream.From(QuadFactory.VertexUnit);
                TextureStream   ts = TextureStream.From(QuadFactory.VertexUnit);
                RectangleF      tc = Texture.TextureCoordinates;

                for (int y = 0; y < vertices.GetLength(1); y++)
                {
                    for (int x = 0; x < vertices.GetLength(0); x++)
                    {
                        MeshVertex vertex = vertices[x, y];
                        Vector2    pos    = a + right * vertex.Position.X + down * vertex.Position.Y;
                        ps[index] = new Vector2((pos.X - 0.5f) * 2, -(pos.Y - 0.5f) * 2);
                        cs[index] = Color.SetAlpha(vertex.Color, (int)(Color.GetAlpha(vertex.Color) * Alpha));
                        Vector2 texture = new Vector2(
                            tc.Left + tc.Width * (TextureRectangle.Left + TextureRectangle.Width * vertex.Texture.X),
                            tc.Top + tc.Height * (TextureRectangle.Top + TextureRectangle.Height * vertex.Texture.Y));
                        ts[index] = texture;
                        index++;
                    }
                }
                dirty = false;
            }
            positionCache.CopyTo(vertexUnit[typeof(PositionStream2)].Data, index);
            colorCache.CopyTo(vertexUnit[typeof(ColorStream)].Data, index);
            textureCache.CopyTo(vertexUnit[typeof(TextureStream)].Data, index);
        }
Example #2
0
 public virtual void InitState()
 {
     for (int i = 0; i < 4; i++)
     {
         allInputs[i] = new ColorStream();
     }
 }
Example #3
0
 public ColorStream(ColorStream stream)
 {
     isValid    = stream.isValid;
     isPolluted = stream.isPolluted;
     r          = stream.r;
     g          = stream.g;
     b          = stream.b;
 }
        private void initStreams()
        {
            StreamBase color = new ColorStream();

            _streams.Add(color);
            Stream = color;
            StreamBase depth = new DepthStream();

            _streams.Add(depth);
        }
Example #5
0
        //---------------------------------------------------------------
        #endregion
        //---------------------------------------------------------------

        //---------------------------------------------------------------
        #region Methods
        //---------------------------------------------------------------
        /// <summary>
        /// Updates the particle system.
        /// </summary>
        /// <param name="deltaTime">The time since the last update.</param>
        public override void Update(float deltaTime)
        {
            base.Update(deltaTime);

            Matrix4 m     = Device.Instance.Transformations.View;
            Vector3 right = m.RightVector;
            Vector3 up    = m.UpVector;

            // update streams
            PositionStream posStream     = (PositionStream)vertexUnit[typeof(PositionStream)];
            ColorStream    colorStream   = (ColorStream)vertexUnit[typeof(ColorStream)];
            TextureStream  textureStream = (TextureStream)vertexUnit[typeof(TextureStream)];

            // Update all particles
            for (int i = 0; i < particles.Count; i++)
            {
                int         offset     = i * 4;
                IParticle   particle   = particles[i] as IParticle;
                IParticle3d particle3d = particles[i] as IParticle3d;
                if (particle3d != null)
                {
                    Vector3 position = particle3d.Position;
                    posStream[offset]     = position - particle.Size.X * right + particle.Size.Y * up;
                    posStream[offset + 1] = position + particle.Size.X * right + particle.Size.Y * up;
                    posStream[offset + 2] = position + particle.Size.X * right - particle.Size.Y * up;
                    posStream[offset + 3] = position - particle.Size.X * right - particle.Size.Y * up;
                }
                IParticleColor particleColor = particles[i] as IParticleColor;
                if (particleColor != null)
                {
                    colorStream[offset]     = particleColor.Color;
                    colorStream[offset + 1] = particleColor.Color;
                    colorStream[offset + 2] = particleColor.Color;
                    colorStream[offset + 3] = particleColor.Color;
                }
                IParticleIndex            particleIndex = particles[i] as IParticleIndex;
                System.Drawing.RectangleF tc;
                if (particleIndex == null || subTextures == null)
                {
                    tc = (Texture as ITexture2d).TextureCoordinates;
                }
                else
                {
                    tc = subTextures[(int)particleIndex.TextureIndex % subTextures.Length].TextureCoordinates;
                }
                textureStream[offset]     = new Vector2(tc.Left, tc.Top);
                textureStream[offset + 1] = new Vector2(tc.Right, tc.Top);
                textureStream[offset + 2] = new Vector2(tc.Right, tc.Bottom);
                textureStream[offset + 3] = new Vector2(tc.Left, tc.Bottom);
            }
            posStream.Upload();
            colorStream.Upload();
            textureStream.Upload();
        }
Example #6
0
    public void InputState(int direct, ColorStream color)
    {
        switch (current_type)
        {
        case ModuleType.SignalGen:
        case ModuleType.BridgeOut:
        {
            if (direct == 4)
            {
                allInputs[0] = new ColorStream(color);
            }
            break;
        }

        case ModuleType.SignalRev:
        case ModuleType.BridgeIn:
        case ModuleType.SPipe:
        case ModuleType.TPipe:
        case ModuleType.XPipe:
        case ModuleType.NotGate:
        {
            if (direct == this.direct)
            {
                allInputs[direct] = new ColorStream(color);
            }
            break;
        }

        case ModuleType.AndGate:
        case ModuleType.OrGate:
        {
            if (direct == this.direct || direct == (this.direct + 3) % 4)
            {
                allInputs[direct] = new ColorStream(color);
            }
            break;
        }

        default:
        {
            break;
        }
        }

        if (direct < 0 || direct > 3)
        {
            return;
        }
        allInputs[direct] = new ColorStream(color);
        OutputState();
    }
Example #7
0
        //---------------------------------------------------------------
        #endregion
        //---------------------------------------------------------------

        //---------------------------------------------------------------
        #region Methods
        //---------------------------------------------------------------
        /// <summary>
        /// Updates the particle system.
        /// </summary>
        /// <param name="deltaTime">The time since the last update.</param>
        public override void Update(float deltaTime)
        {
            UpdateAge(deltaTime);
            // delete dead particles
            FixedRoundBuffer buffer = particles as FixedRoundBuffer;

            while (buffer.Count > 0 && !(buffer[0] as IParticle).IsAlive)
            {
                buffer.RemoveFirst();
            }

            UpdateParticles(deltaTime);
            EmitParticles(deltaTime);

            ITexture2d texture = Texture;

            if (particles.Count >= 2)
            {
                // update streams
                PositionStream posStream     = (PositionStream)vertexUnit[typeof(PositionStream)];
                ColorStream    colorStream   = (ColorStream)vertexUnit[typeof(ColorStream)];
                TextureStream  textureStream = (TextureStream)vertexUnit[typeof(TextureStream)];
                // Update all particles
                for (int i = 0; i < particles.Count; i++)
                {
                    int         offset     = i * 2;
                    IParticle   particle   = particles[i] as IParticle;
                    IParticle3d particle3d = particles[i] as IParticle3d;
                    if (particle3d != null)
                    {
                        Vector3 position = particle3d.Position;
                        posStream[offset]     = position + particle.Size.X * (particle as IParticleOrientation).Orientation;
                        posStream[offset + 1] = position - particle.Size.X * (particle as IParticleOrientation).Orientation;
                    }
                    IParticleColor particleColor = particles[i] as IParticleColor;
                    if (particleColor != null)
                    {
                        colorStream[offset]     = particleColor.Color;
                        colorStream[offset + 1] = particleColor.Color;
                    }
                    float tx = texture.TextureCoordinates.Left + texture.TextureCoordinates.Width * particle.Age / particle.LifeTime;
                    textureStream[offset]     = new Vector2(tx, texture.TextureCoordinates.Top);
                    textureStream[offset + 1] = new Vector2(tx, texture.TextureCoordinates.Bottom);
                }
                posStream.Upload();
                colorStream.Upload();
                textureStream.Upload();
            }
        }
Example #8
0
        //---------------------------------------------------------------
        #endregion
        //---------------------------------------------------------------

        //---------------------------------------------------------------
        #region Methods
        //---------------------------------------------------------------
        private void Bind()
        {
            BinormalStream.Bind();
            BoneIndicesStream.Bind();
            BoneWeightsStream.Bind();
            SoftwareBoneIndicesStream.Bind();
            SoftwareBoneWeightsStream.Bind();
            ColorStream.Bind();
            CompressedNormalStream.Bind();
            FloatStream.Bind();
            IndexStream16.Bind();
            IndexStream32.Bind();
            IntStream.Bind();
            NormalStream.Bind();
            PositionStream.Bind();
            PositionStream2.Bind();
            PositionStream4.Bind();
            TangentStream.Bind();
            TextureStream.Bind();
        }
Example #9
0
    private void InitializeStreams()
    {
        try
        {
            _streamSet           = Astra.StreamSet.Open();
            _reader1             = _streamSet.CreateReader();
            _reader1.FrameReady += FrameReady;

            _reader2             = _streamSet.CreateReader();
            _reader2.FrameReady += FrameReady;

            _reader3             = _streamSet.CreateReader();
            _reader3.FrameReady += FrameReady;

            _reader4             = _streamSet.CreateReader();
            _reader4.FrameReady += FrameReady;

            _reader5             = _streamSet.CreateReader();
            _reader5.FrameReady += FrameReady;

            _depthStream = _reader1.GetStream <DepthStream>();

            var       depthModes        = _depthStream.AvailableModes;
            ImageMode selectedDepthMode = depthModes[0];

    #if ASTRA_UNITY_ANDROID_NATIVE
            int targetDepthWidth  = 160;
            int targetDepthHeight = 120;
            int targetDepthFps    = 30;
    #else
            int targetDepthWidth  = 320;
            int targetDepthHeight = 240;
            int targetDepthFps    = 60;
    #endif

            foreach (var m in depthModes)
            {
                if (m.Width == targetDepthWidth &&
                    m.Height == targetDepthHeight &&
                    m.FramesPerSecond == targetDepthFps)
                {
                    selectedDepthMode = m;
                    break;
                }
            }

            _depthStream.SetMode(selectedDepthMode);

            _colorStream = _reader2.GetStream <ColorStream>();

            var       colorModes        = _colorStream.AvailableModes;
            ImageMode selectedColorMode = colorModes[0];

    #if ASTRA_UNITY_ANDROID_NATIVE
            int targetColorWidth  = 320;
            int targetColorHeight = 240;
            int targetColorFps    = 30;
    #else
            int targetColorWidth  = 640;
            int targetColorHeight = 480;
            int targetColorFps    = 60;
    #endif

            foreach (var m in colorModes)
            {
                if (m.Width == targetColorWidth &&
                    m.Height == targetColorHeight &&
                    m.FramesPerSecond == targetColorFps)
                {
                    selectedColorMode = m;
                    break;
                }
            }

            _colorStream.SetMode(selectedColorMode);

            _bodyStream = _reader3.GetStream <BodyStream>();

            _maskedColorStream = _reader4.GetStream <MaskedColorStream>();

            _colorizedBodyStream = _reader5.GetStream <ColorizedBodyStream>();

            _areStreamsInitialized = true;
        }
        catch (AstraException e)
        {
            Debug.Log("AstraController: Couldn't initialize streams: " + e.ToString());
            UninitializeStreams();
        }
    }
Example #10
0
    private void InitializeStreams()
    {
        try
        {
            AstraUnityContext.Instance.WaitForUpdate(AstraBackgroundUpdater.WaitIndefinitely);

            _streamSet = Astra.StreamSet.Open();

            _readerDepth         = _streamSet.CreateReader();
            _readerColor         = _streamSet.CreateReader();
            _readerBody          = _streamSet.CreateReader();
            _readerMaskedColor   = _streamSet.CreateReader();
            _readerColorizedBody = _streamSet.CreateReader();
            _readerPoint         = _streamSet.CreateReader();

            _depthStream = _readerDepth.GetStream <DepthStream>();

            var       depthModes        = _depthStream.AvailableModes;
            ImageMode selectedDepthMode = depthModes[0];

    #if ASTRA_UNITY_ANDROID_NATIVE
            int targetDepthWidth  = 160;
            int targetDepthHeight = 120;
            int targetDepthFps    = 30;
    #else
            int targetDepthWidth  = 320;
            int targetDepthHeight = 240;
            int targetDepthFps    = 30;
    #endif

            foreach (var m in depthModes)
            {
                if (m.Width == targetDepthWidth &&
                    m.Height == targetDepthHeight &&
                    m.FramesPerSecond == targetDepthFps)
                {
                    selectedDepthMode = m;
                    break;
                }
            }

            _depthStream.SetMode(selectedDepthMode);

            _colorStream = _readerColor.GetStream <ColorStream>();

            var       colorModes        = _colorStream.AvailableModes;
            ImageMode selectedColorMode = colorModes[0];

    #if ASTRA_UNITY_ANDROID_NATIVE
            int targetColorWidth  = 320;
            int targetColorHeight = 240;
            int targetColorFps    = 30;
    #else
            int targetColorWidth  = 640;
            int targetColorHeight = 480;
            int targetColorFps    = 30;
    #endif

            foreach (var m in colorModes)
            {
                if (m.Width == targetColorWidth &&
                    m.Height == targetColorHeight &&
                    m.FramesPerSecond == targetColorFps)
                {
                    selectedColorMode = m;
                    break;
                }
            }

            _colorStream.SetMode(selectedColorMode);

            _bodyStream = _readerBody.GetStream <BodyStream>();

            _maskedColorStream = _readerMaskedColor.GetStream <MaskedColorStream>();

            _colorizedBodyStream = _readerColorizedBody.GetStream <ColorizedBodyStream>();

            _pointStream = _readerPoint.GetStream <PointStream>();

            _areStreamsInitialized = true;
        }
        catch (AstraException e)
        {
            Debug.Log("AstraController: Couldn't initialize streams: " + e.ToString());
            UninitializeStreams();
        }
    }
Example #11
0
        /// <summary>
        /// Import a mesh from a stream.
        /// </summary>
        /// <param name="stream">Stream containing mesh data.</param>
        public void Import(Stream stream)
        {
            Profiler.Instance.Begin("Import binary mesh");
            // Header
            BinaryReader reader = new BinaryReader(stream);

            if (ReadString(reader) != "mesh" || ReadString(reader) != "v0.3")
            {
                throw new NotSupportedException("Can't load mesh, file not supported!");
            }

            // Joints
            int jointNum = ReadInt(reader);

            Joint[]   joints     = new Joint[jointNum];
            Matrix4[] jointArray = new Matrix4[jointNum];
            Hashtable jointTable = new Hashtable(joints.Length);

            for (int i = 0; i < joints.Length; i++)
            {
                string name   = ReadString(reader);
                string parent = ReadString(reader);

                reader.Read(matrixBytes, 0, matrixBytes.Length);
                Matrix4 m = Matrix4.From(matrixBytes);

                Joint parentJoint = null;
                if (parent != null && jointTable.Contains(parent))
                {
                    parentJoint = (Joint)jointTable[parent];
                }
                joints[i]        = new Joint(name, i, parentJoint);
                jointArray[i]    = m;
                jointTable[name] = joints[i];
            }
            skeleton = new Skeleton(jointArray, joints);

            // SubSet
            int subSetNum = ReadInt(reader);

            for (int i = 0; i < subSetNum; i++)
            {
                ArrayList streams = new ArrayList(10);
                // Header
                if (ReadString(reader) != "subset")
                {
                    throw new NotSupportedException("Error on loading subSet!");
                }
                string name        = ReadString(reader);
                string parentJoint = ReadString(reader);

                int attributeCount          = ReadInt(reader);
                StringDictionary attributes = new StringDictionary();
                for (int t = 0; t < attributeCount; t++)
                {
                    attributes.Add(ReadString(reader), ReadString(reader));
                }

                // IndexStream
                // Todo Replace ushort.MaxValue with size of vertex unit
                IndexStream indexStream = IndexStream.Create(ReadInt(reader), ushort.MaxValue);
                byte[]      indexBuffer = new byte[indexStream.Size * 4];
                reader.Read(indexBuffer, 0, indexStream.Size * 4);
                for (int t = 0; t < indexStream.Size; t++)
                {
                    indexStream[t] = BitConverter.ToInt32(indexBuffer, t * 4);
                }

                int            vertexSize = ReadInt(reader);
                PositionStream posStream  = new PositionStream(vertexSize);
                streams.Add(posStream);
                byte[] vertexBuffer = new byte[vertexSize * 12];
                reader.Read(vertexBuffer, 0, vertexSize * 12);
                for (int t = 0; t < vertexSize; t++)
                {
                    posStream[t] = Vector3.From(vertexBuffer, 12 * t);
                }

                NormalStream normalStream = new NormalStream(vertexSize);
                streams.Add(normalStream);
                reader.Read(vertexBuffer, 0, vertexSize * 12);
                for (int t = 0; t < vertexSize; t++)
                {
                    normalStream[t] = Vector3.From(vertexBuffer, t * 12);
                }

                ColorStream colorStream = new ColorStream(vertexSize);
                streams.Add(colorStream);
                reader.Read(vertexBuffer, 0, vertexSize * 12);
                for (int t = 0; t < vertexSize; t++)
                {
                    int r = Math.Basic.Clamp((int)(System.BitConverter.ToSingle(vertexBuffer, t * 12) * 255 + 0.5f), 0, 255);
                    int g = Math.Basic.Clamp((int)(System.BitConverter.ToSingle(vertexBuffer, 4 + t * 12) * 255 + 0.5f), 0, 255);
                    int b = Math.Basic.Clamp((int)(System.BitConverter.ToSingle(vertexBuffer, 8 + t * 12) * 255 + 0.5f), 0, 255);
                    colorStream[t] = System.Drawing.Color.FromArgb(r, g, b).ToArgb();
                }

                TextureStream[] textureStreams = new TextureStream[ReadInt(reader)];
                for (int t = 0; t < textureStreams.Length; t++)
                {
                    TextureStream texStream = new TextureStream(vertexSize);
                    streams.Add(texStream);
                    reader.Read(vertexBuffer, 0, vertexSize * 8);
                    for (int j = 0; j < vertexSize; j++)
                    {
                        texStream[j] = Vector2.From(vertexBuffer, j * 8);
                    }
                    textureStreams[t] = texStream;
                }

                IBoneIndicesStream boneStream    = null;
                IBoneWeightsStream weightsStream = null;
                int weightNum = ReadInt(reader);
                if (weightNum != 0)
                {
                    if (HardwareSkinning)
                    {
                        boneStream    = new BoneIndicesStream(vertexSize);
                        weightsStream = new BoneWeightsStream(vertexSize);
                    }
                    else
                    {
                        boneStream    = new SoftwareBoneIndicesStream(vertexSize);
                        weightsStream = new SoftwareBoneWeightsStream(vertexSize);
                    }
                    streams.Add(boneStream);
                    streams.Add(weightsStream);
                    ArrayList[] indicesList = new ArrayList[vertexSize];
                    ArrayList[] weightsList = new ArrayList[vertexSize];
                    for (int t = 0; t < vertexSize; t++)
                    {
                        indicesList[t] = new ArrayList(8);
                        weightsList[t] = new ArrayList(8);
                    }


                    byte[] weightBuffer = new byte[weightNum * 12];
                    reader.Read(weightBuffer, 0, weightNum * 12);
                    for (int t = 0; t < weightNum; t++)
                    {
                        int   vertexIndex = BitConverter.ToInt32(weightBuffer, t * 12);
                        int   jointIndex  = BitConverter.ToInt32(weightBuffer, 4 + t * 12);
                        float weight      = BitConverter.ToSingle(weightBuffer, 8 + t * 12);
                        indicesList[vertexIndex].Add((byte)jointIndex);
                        weightsList[vertexIndex].Add(weight);
                    }

                    for (int t = 0; t < vertexSize; t++)
                    {
                        boneStream.SetIndices(t, (byte[])indicesList[t].ToArray(typeof(byte)));
                        weightsStream.SetWeights(t, (float[])weightsList[t].ToArray(typeof(float)));
                    }
                }

                VertexUnit vertexUnit = new VertexUnit(streams);
                Mesh       mesh       = new Mesh(new SubSet(vertexUnit, indexStream));
                if (model == null)
                {
                    if (skeleton.Joints.Length != 0)
                    {
                        model = new Model(new SkinnedMesh(mesh, skeleton), skeleton);
                    }
                    else
                    {
                        model = new Model(mesh, skeleton);
                    }
                }
                else
                {
                    Joint attachTo = skeleton.RootJoint;
                    if (parentJoint != "")
                    {
                        attachTo = (jointTable[parentJoint] as Joint);
                    }
                    model.AttachModel(new Model(mesh, skeleton), attachTo);
                }
            }
            reader.Close();

            Profiler.Instance.End("Import binary mesh");
        }
Example #12
0
    public void InitializeStreams()
    {
        try
        {
            AstraUnityContext.Instance.WaitForUpdate(AstraBackgroundUpdater.WaitIndefinitely);

            _streamSet = Astra.StreamSet.Open();

            _readerDepth         = _streamSet.CreateReader();
            _readerColor         = _streamSet.CreateReader();
            _readerNV21Color     = _streamSet.CreateReader();
            _readerBody          = _streamSet.CreateReader();
            _readerMaskedColor   = _streamSet.CreateReader();
            _readerColorizedBody = _streamSet.CreateReader();

            _depthStream = _readerDepth.GetStream <DepthStream>();

            var       depthModes        = _depthStream.AvailableModes;
            ImageMode selectedDepthMode = depthModes[0];

            int targetDepthWidth, targetDepthHeight, targetDepthFps;
#if ASTRA_UNITY_ANDROID_NATIVE
            //Deeyea and Dabai doesn't support qqvga and qvga resolution.
            //use 640*400.
            if (_depthStream.usbInfo.Pid == 0x60b ||
                _depthStream.usbInfo.Pid == 0x60e ||
                _depthStream.usbInfo.Pid == 0x608 ||
                _depthStream.usbInfo.Pid == 0x617)
            {
                targetDepthWidth  = 640;
                targetDepthHeight = 400;
                targetDepthFps    = 30;
            }
            else
            {
                targetDepthWidth  = 160;
                targetDepthHeight = 120;
                targetDepthFps    = 30;
            }
#else
            if (_depthStream.usbInfo.Pid == 0x60b ||
                _depthStream.usbInfo.Pid == 0x60e ||
                _depthStream.usbInfo.Pid == 0x608 ||
                _depthStream.usbInfo.Pid == 0x617)
            {
                targetDepthWidth  = 640;
                targetDepthHeight = 400;
                targetDepthFps    = 30;
            }
            else
            {
                targetDepthWidth  = 320;
                targetDepthHeight = 240;
                targetDepthFps    = 30;
            }
    #endif

            foreach (var m in depthModes)
            {
                if (m.Width == targetDepthWidth &&
                    m.Height == targetDepthHeight &&
                    m.FramesPerSecond == targetDepthFps)
                {
                    selectedDepthMode = m;
                    break;
                }
            }

            _depthStream.SetMode(selectedDepthMode);

            _colorStream = _readerColor.GetStream <ColorStream>();

            var       colorModes        = _colorStream.AvailableModes;
            ImageMode selectedColorMode = colorModes[0];

            if (_depthStream.usbInfo.Pid == 0x60b ||
                _depthStream.usbInfo.Pid == 0x617)
            {
                //for deeyea, set mirror to false to match depth.
                _colorStream.IsMirroring = false;
            }
#if ASTRA_UNITY_ANDROID_NATIVE
            int targetColorWidth, targetColorHeight, targetColorFps;
            if (_depthStream.usbInfo.Pid == 0x608 ||
                _depthStream.usbInfo.Pid == 0x60f ||
                _depthStream.usbInfo.Pid == 0x617)
            {
                targetColorWidth  = 640;
                targetColorHeight = 480;
                targetColorFps    = 30;
            }
            else
            {
                targetColorWidth  = 320;
                targetColorHeight = 240;
                targetColorFps    = 30;
            }
#else
            int targetColorWidth  = 640;
            int targetColorHeight = 480;
            int targetColorFps    = 30;
#endif

            foreach (var m in colorModes)
            {
                if (m.Width == targetColorWidth &&
                    m.Height == targetColorHeight &&
                    m.FramesPerSecond == targetColorFps)
                {
                    selectedColorMode = m;
                    break;
                }
            }

            _colorStream.SetMode(selectedColorMode);

#if ASTRA_UNITY_ANDROID_NATIVE
            _nv21ColorStream = _readerNV21Color.GetStream <ColorStream>(Astra.Core.StreamSubType.COLOR_NV21_SUBTYPE);
            if (_nv21ColorStream.IsAvailable)
            {
                //COLOR_NV21_SUBTYPE is only available when using astra pro and astra pro plus.
                colorModes        = _nv21ColorStream.AvailableModes;
                selectedColorMode = colorModes[0];

                foreach (var m in colorModes)
                {
                    if (m.Width == targetColorWidth &&
                        m.Height == targetColorHeight &&
                        m.FramesPerSecond == targetColorFps)
                    {
                        selectedColorMode = m;
                        break;
                    }
                }

                _nv21ColorStream.SetMode(selectedColorMode);
                if (_depthStream.usbInfo.Pid == 0x60b ||
                    _depthStream.usbInfo.Pid == 0x617)
                {
                    //for deeyea, set mirror to false to match depth.
                    _nv21ColorStream.IsMirroring = false;
                }
            }
            else
            {
                _readerNV21Color.Dispose();
                _readerNV21Color = null;
                _nv21ColorStream = null;
            }
#endif

            _bodyStream = _readerBody.GetStream <BodyStream>();

            _maskedColorStream = _readerMaskedColor.GetStream <MaskedColorStream>();

            _colorizedBodyStream = _readerColorizedBody.GetStream <ColorizedBodyStream>();

            _areStreamsInitialized = true;
        }
        catch (AstraException e)
        {
            Debug.Log("AstraController: Couldn't initialize streams: " + e.ToString());
            UninitializeStreams();
        }
    }
Example #13
0
    private void InitializeStreams()
    {
        try
        {
            AstraUnityContext.Instance.WaitForUpdate(AstraBackgroundUpdater.WaitIndefinitely);

            _streamSet = Astra.StreamSet.Open();

            _readerDepth         = _streamSet.CreateReader();
            _readerColor         = _streamSet.CreateReader();
            _readerBody          = _streamSet.CreateReader();
            _readerMaskedColor   = _streamSet.CreateReader();
            _readerColorizedBody = _streamSet.CreateReader();

            _depthStream = _readerDepth.GetStream <DepthStream>();

            _depthModes = _depthStream.AvailableModes;
            ImageMode selectedDepthMode = _depthModes[0];

#if ASTRA_UNITY_ANDROID_NATIVE
            int targetDepthWidth  = 160;
            int targetDepthHeight = 120;
            int targetDepthFps    = 30;
#else
            int targetDepthWidth  = 320;
            int targetDepthHeight = 240;
            int targetDepthFps    = 30;
#endif

            foreach (var m in _depthModes)
            {
                Debug.Log("Depth mode: " + m.Width + "x" + m.Height + "@" + m.FramesPerSecond);
                if (m.Width == targetDepthWidth &&
                    m.Height == targetDepthHeight)
                {
                    selectedDepthMode = m;
                    break;
                }
            }

            _depthStream.SetMode(selectedDepthMode);

            _colorStream = _readerColor.GetStream <ColorStream>();
            try
            {
                _colorModes = _colorStream.AvailableModes;
                ImageMode selectedColorMode = _colorModes[0];

#if ASTRA_UNITY_ANDROID_NATIVE
                int targetColorWidth  = 320;
                int targetColorHeight = 240;
                int targetColorFps    = 30;
#else
                int targetColorWidth  = 640;
                int targetColorHeight = 480;
                int targetColorFps    = 30;
#endif

                foreach (var m in _colorModes)
                {
                    Debug.Log("Color mode: " + m.Width + "x" + m.Height + "@" + m.FramesPerSecond);
                    if (m.Width == targetColorWidth &&
                        m.Height == targetColorHeight)
                    {
                        selectedColorMode = m;
                        break;
                    }
                }

                _colorStream.SetMode(selectedColorMode);
            }
            catch (System.Exception e)
            {
                Debug.Log("Couldn't initialize color stream: " + e.ToString());
            }

            _bodyStream = _readerBody.GetStream <BodyStream>();

            _maskedColorStream = _readerMaskedColor.GetStream <MaskedColorStream>();

            _colorizedBodyStream = _readerColorizedBody.GetStream <ColorizedBodyStream>();

            _areStreamsInitialized = true;

            Debug.Log("Stream initialize success");
            OnInitializeSuccess.Invoke();
        }
        catch (System.Exception e)
        {
            Debug.Log("Couldn't initialize streams: " + e.ToString());
            UninitializeStreams();

            if (_initializeCount > 0)
            {
                _initializeCount--;
            }
            else
            {
                Debug.Log("Initialize failed");
                OnInitializeFailed.Invoke();
            }
        }
    }
Example #14
0
        //---------------------------------------------------------------
        #endregion
        //---------------------------------------------------------------

        //---------------------------------------------------------------
        #region Methods
        //---------------------------------------------------------------
        /// <summary>
        /// Updates the particle system.
        /// </summary>
        /// <param name="deltaTime">The time since the last update.</param>
        public override void Update(float deltaTime)
        {
            base.Update(deltaTime);

            // update streams
            PositionStream2 posStream     = (PositionStream2)vertexUnit[typeof(PositionStream2)];
            ColorStream     colorStream   = (ColorStream)vertexUnit[typeof(ColorStream)];
            TextureStream   textureStream = (TextureStream)vertexUnit[typeof(TextureStream)];

            // Update all particles
            for (int i = 0; i < particles.Count; i++)
            {
                int         offset     = i * 4;
                IParticle   particle   = particles[i] as IParticle;
                IParticle2d particle2d = particles[i] as IParticle2d;
                if (particle2d != null)
                {
                    Vector2 position = particle2d.Position;
                    posStream[offset]     = position + new Vector2(-particle.Size.X, particle.Size.Y);
                    posStream[offset + 1] = position + new Vector2(particle.Size.X, particle.Size.Y);
                    posStream[offset + 2] = position + new Vector2(particle.Size.X, -particle.Size.Y);
                    posStream[offset + 3] = position + new Vector2(-particle.Size.X, -particle.Size.Y);
                }
                IParticleColor particleColor = particles[i] as IParticleColor;
                if (particleColor != null)
                {
                    colorStream[offset]     = particleColor.Color;
                    colorStream[offset + 1] = particleColor.Color;
                    colorStream[offset + 2] = particleColor.Color;
                    colorStream[offset + 3] = particleColor.Color;
                }
                IParticleIndex            particleIndex = particles[i] as IParticleIndex;
                System.Drawing.RectangleF tc;
                if (particleIndex == null || subTextures == null)
                {
                    tc = (Texture as ITexture2d).TextureCoordinates;
                }
                else
                {
                    tc = subTextures[(int)particleIndex.TextureIndex % subTextures.Length].TextureCoordinates;
                }
                textureStream[offset]     = new Vector2(tc.Left, tc.Top);
                textureStream[offset + 1] = new Vector2(tc.Right, tc.Top);
                textureStream[offset + 2] = new Vector2(tc.Right, tc.Bottom);
                textureStream[offset + 3] = new Vector2(tc.Left, tc.Bottom);
            }
            posStream.Upload();
            colorStream.Upload();
            textureStream.Upload();

            // Render all particles
            Device.Instance.VertexUnit  = vertexUnit;
            Device.Instance.IndexStream = indexStream;
            textures.Apply();
            int steps = Effect.Begin();

            for (int i = 0; i < steps; i++)
            {
                Effect.BeginPass(i);
                Effect.CommitChanges(); // Oct Update BUG!!!
                Device.Instance.DrawIndexed(vertexUnit.Position, 0, particles.Count * 4, indexStream.Position, particles.Count * 2);
                Effect.EndPass();
            }
            Effect.End();
        }
Example #15
0
        /// <summary>
        /// import a mesh from a stream
        /// </summary>
        /// <param name="stream">stream containing mesh data</param>
        public void Import(Stream stream)
        {
            model    = null;
            skeleton = null;
            IndexStream      indexStream   = null;
            IVertexStream    currentStream = null;
            ArrayList        streams       = new ArrayList();
            StringDictionary attributes    = new StringDictionary();
            XmlTextReader    reader        = new XmlTextReader(stream);

            Matrix4[] jointArray   = null;
            Joint[]   joints       = null;
            Hashtable jointTable   = null;
            int       index        = 0;
            int       currentJoint = 0;
            int       vertexCount  = 0;
            int       binding      = -1;

            ArrayList[] indicesList = null;
            ArrayList[] weightsList = null;

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    switch (reader.Name)
                    {
                    // <mesh>
                    case "mesh":
                        if (model != null)
                        {
                            throw new GraphicsException("Only one mesh allowed in mesh stream!");
                        }
                        model = new Model();
                        break;

                    // <subset>
                    case "subset":
                        string parentJoint = reader.GetAttribute("parentJoint");
                        if (parentJoint != null && parentJoint != "")
                        {
                            binding = (jointTable[parentJoint] as Joint).Index;
                        }
                        else
                        {
                            binding = -1;
                        }
                        break;

                    // <attributes>
                    case "attributes":
                        break;

                    case "attribute":
                        // todo !!!
                        attributes.Add(reader.GetAttribute("name"), reader.GetAttribute("value"));
                        break;

                    //<indexStream>
                    case "indexStream": {
                        index = 0;
                        int size = int.Parse(reader.GetAttribute("size"), culture);
                        indexStream = new IndexStream16(size);
                    }
                    break;

                    //<triangle>
                    case "triangle": {
                        int a = int.Parse(reader.GetAttribute("a"), culture);
                        int b = int.Parse(reader.GetAttribute("b"), culture);
                        int c = int.Parse(reader.GetAttribute("c"), culture);
                        indexStream[index++] = a;
                        indexStream[index++] = b;
                        indexStream[index++] = c;
                    }
                    break;

                    //<positionStream>
                    case "positionStream": {
                        index         = 0;
                        vertexCount   = int.Parse(reader.GetAttribute("size"), culture);
                        currentStream = new PositionStream(vertexCount);
                        streams.Add(currentStream);
                    }
                    break;

                    //<vector3>
                    case "vector3": {
                        float x = float.Parse(reader.GetAttribute("x"), culture);
                        float y = float.Parse(reader.GetAttribute("y"), culture);
                        float z = float.Parse(reader.GetAttribute("z"), culture);
                        (currentStream as PositionStream)[index++] = new Vector3(x, y, z);
                    }
                    break;

                    //<normalStream>
                    case "normalStream": {
                        index = 0;
                        int size = int.Parse(reader.GetAttribute("size"), culture);
                        currentStream = new NormalStream(size);
                        streams.Add(currentStream);
                    }
                    break;

                    //<colorStream>
                    case "colorStream": {
                        index = 0;
                        int size = int.Parse(reader.GetAttribute("size"), culture);
                        currentStream = new ColorStream(size);
                        streams.Add(currentStream);
                    }
                    break;

                    //<color>
                    case "color": {
                        int r = (int)((float.Parse(reader.GetAttribute("r"), culture)) * 255.0f + 0.5f);
                        int g = (int)((float.Parse(reader.GetAttribute("g"), culture)) * 255.0f + 0.5f);
                        int b = (int)((float.Parse(reader.GetAttribute("b"), culture)) * 255.0f + 0.5f);
                        (currentStream as ColorStream)[index++] = System.Drawing.Color.FromArgb(r, g, b).ToArgb();
                    }
                    break;

                    //<textureStream>
                    case "textureStream": {
                        index = 0;
                        int size = int.Parse(reader.GetAttribute("size"), culture);
                        currentStream = new TextureStream(size);
                        streams.Add(currentStream);
                    }
                    break;

                    //<vector2>
                    case "vector2": {
                        float x = float.Parse(reader.GetAttribute("x"), culture);
                        float y = float.Parse(reader.GetAttribute("y"), culture);
                        (currentStream as TextureStream)[index++] = new Vector2(x, y);
                    }
                    break;

                    case "joints": {
                        int size = int.Parse(reader.GetAttribute("size"), culture);
                        jointArray   = new Matrix4[size];
                        joints       = new Joint[size];
                        jointTable   = new Hashtable();
                        currentJoint = 0;
                    }
                    break;

                    case "joint": {
                        string  jointName  = reader.GetAttribute("name");
                        string  parentName = reader.GetAttribute("parent");
                        Matrix4 m          = new Matrix4(float.Parse(reader.GetAttribute("a1"), culture),
                                                         float.Parse(reader.GetAttribute("a2"), culture),
                                                         float.Parse(reader.GetAttribute("a3"), culture),
                                                         float.Parse(reader.GetAttribute("a4"), culture),
                                                         float.Parse(reader.GetAttribute("b1"), culture),
                                                         float.Parse(reader.GetAttribute("b2"), culture),
                                                         float.Parse(reader.GetAttribute("b3"), culture),
                                                         float.Parse(reader.GetAttribute("b4"), culture),
                                                         float.Parse(reader.GetAttribute("c1"), culture),
                                                         float.Parse(reader.GetAttribute("c2"), culture),
                                                         float.Parse(reader.GetAttribute("c3"), culture),
                                                         float.Parse(reader.GetAttribute("c4"), culture),
                                                         float.Parse(reader.GetAttribute("d1"), culture),
                                                         float.Parse(reader.GetAttribute("d2"), culture),
                                                         float.Parse(reader.GetAttribute("d3"), culture),
                                                         float.Parse(reader.GetAttribute("d4"), culture));
                        jointArray[currentJoint] = m; //new Joint(jointName, m);
                        Joint parent = null;
                        if (parentName != null && jointTable.Contains(parentName))
                        {
                            parent = (Joint)jointTable[parentName];
                        }
                        joints[currentJoint]  = new Joint(jointName, currentJoint, parent);
                        jointTable[jointName] = joints[currentJoint];
                        currentJoint++;
                    }
                    break;

                    case "weights": {
                        index = 0;
                        //vertexCount = int.Parse(reader.GetAttribute("size"), culture);
                        indicesList = new ArrayList[vertexCount];
                        weightsList = new ArrayList[vertexCount];
                        for (int i = 0; i < vertexCount; i++)
                        {
                            indicesList[i] = new ArrayList(8);
                            weightsList[i] = new ArrayList(8);
                        }
                    }
                    break;

                    case "weight": {
                        int   vertexIndex = int.Parse(reader.GetAttribute("vertexIndex"));
                        byte  jointIndex  = byte.Parse(reader.GetAttribute("jointIndex"));
                        float value       = float.Parse(reader.GetAttribute("weight"), culture);
                        indicesList[vertexIndex].Add(jointIndex);
                        weightsList[vertexIndex].Add(value);
                    }
                    break;
                    }
                }

                if (reader.NodeType == XmlNodeType.EndElement)
                {
                    if (reader.Name.Equals("weights"))
                    {
                        IBoneIndicesStream bis = null;
                        IBoneWeightsStream bws = null;
                        if (HardwareSkinning)
                        {
                            bis = new BoneIndicesStream(vertexCount);
                            bws = new BoneWeightsStream(vertexCount);
                        }
                        else
                        {
                            bis = new SoftwareBoneIndicesStream(vertexCount);
                            bws = new SoftwareBoneWeightsStream(vertexCount);
                        }
                        for (int i = 0; i < vertexCount; i++)
                        {
                            bis.SetIndices(i, (byte[])indicesList[i].ToArray(typeof(byte)));
                            bws.SetWeights(i, (float[])weightsList[i].ToArray(typeof(float)));
                        }
                        streams.Add(bis);
                        streams.Add(bws);
                    }
                    else if (reader.Name.Equals("subset"))
                    {
                        VertexUnit vertexUnit = new VertexUnit(streams);
                        if (binding == -1)
                        {
                            model.Mesh = new Mesh(new SubSet(vertexUnit, indexStream));
                        }
                        else
                        {
                            model.AttachModel(new Model(new Mesh(new SubSet(vertexUnit, indexStream)), null), binding);
                        }
                        streams.Clear();
                    }
                }
            }
            ;

            reader.Close();
            if (jointArray != null && joints != null)
            {
                skeleton = new Skeleton(jointArray, joints);
            }
            model.Skeleton = skeleton;
        }