示例#1
0
        // TODO: Make functions...
        public bool Build(NodeHandle nodeHandle, GameObject gameObject, NodeHandle activeStateNode)
        {
            var node = nodeHandle.node;

            var cb = node as Crossboard;

            if (cb == null)
            {
                return(false);
            }

            float[] position_data;
            if (!cb.GetObjectPositions(out position_data))
            {
                return(false);
            }

            float[] object_data;
            if (!cb.GetObjectData(out object_data))
            {
                return(false);
            }

            var objects = position_data.Length / 3; // Number of objects


            // NOTE: CrossboardDatasets are copied to compute buffers when assigning the dataset,
            // thus we can reuse same dataset objects to reduce GC pressure if needed.
            CrossboardDataset dataset = new CrossboardDataset();

            dataset.POSITION    = new Vector3[objects];
            dataset.UV0ListComp = new List <Vector4>(objects);
            dataset.UV1ListComp = new List <Vector4>(objects);
            dataset.COLOR       = new Color[objects];

            var float3_index = 0;
            var float4_index = 0;

            for (var i = 0; i < objects; i++)
            {
                dataset.POSITION[i] = new Vector3(position_data[float3_index], position_data[float3_index + 1], position_data[float3_index + 2]);

                // size, heading, pitch, roll
                dataset.UV0ListComp.Add(new Vector4(object_data[float4_index] * 2f, object_data[float4_index + 1], object_data[float4_index + 2], object_data[float4_index + 3]));

                // postion offset (x - its up normal), planes offset ( xyz - in there normal direction)
                dataset.UV1ListComp.Add(new Vector4(-0.02f, 0, 0, 0));

                float3_index += 3;
                float4_index += 4;
            }

            // Shouldnt this be our settings?
            if (cb.UseColors)
            {
                float[] color_data;

                if (!cb.GetColorData(out color_data))
                {
                    return(false);
                }

                float4_index = 0;

                var colors = new Color[objects];

                for (int i = 0; i < objects; i++)
                {
                    colors[i]     = new Color(color_data[float4_index], color_data[float4_index + 1], color_data[float4_index + 2], color_data[float4_index + 3]);
                    float4_index += 4;
                }

                dataset.COLOR = colors;
            }

            var renderer = gameObject.GetComponent <CrossboardRenderer_ComputeShader>();

            if (renderer == null)
            {
                renderer = gameObject.AddComponent <CrossboardRenderer_ComputeShader>();

                renderer.OpaqueCrossboardCompute = true;
                renderer._computeShader          = GameObject.Instantiate(_computeShader);
            }


            var material = GameObject.Instantiate(_crossboardMaterial);

            // check if the texture is loaded for this state, otherwise load it
            if (activeStateNode != null)
            {
                if ((activeStateNode.stateLoadInfo & StateLoadInfo.Texture) == StateLoadInfo.None)
                {
                    var state = activeStateNode.node.State;

                    if (!StateHelper.Build(state, out StateBuildOutput buildOutput, _textureCache))
                    {
                        return(false);
                    }

                    activeStateNode.stateLoadInfo |= StateLoadInfo.Texture;
                    activeStateNode.texture        = buildOutput.Texture;
                }

                material.mainTexture = activeStateNode.texture;
            }

            renderer.SetCrossboardDataset(dataset, material);

            return(true);
        }
示例#2
0
        public bool BuildGameObject()
        {
            if (node == null)
            {
                return(false);
            }

            if (!node.IsValid())
            {
                return(false);
            }

            // ---------------------------- Crossboard check -----------------------------------

            Crossboard cb = node as Crossboard;

            if (cb != null)
            {
                if (currentMaterial == null)    // No available material
                {
                    Message.Send(ID, MessageLevel.WARNING, $"Missing material in {node.GetName()}");
                    return(false);
                }

                float[] position_data;
                float[] object_data;

                if (cb.GetObjectPositions(out position_data) && cb.GetObjectData(out object_data))
                {
                    int objects = position_data.Length / 3; // Number of objects

                    CrossboardDataset dataset = new CrossboardDataset();
                    dataset.POSITION    = new Vector3[objects];
                    dataset.UV0ListComp = new List <Vector4>(objects);
                    dataset.UV1ListComp = new List <Vector4>(objects);
                    dataset.COLOR       = new Color[objects];

                    CrossboardRenderer_ComputeShader Renderer = gameObject.AddComponent <CrossboardRenderer_ComputeShader>();
                    Renderer.OpaqueCrossboardCompute = true;

                    Renderer._computeShader = Instantiate(ComputeShader);

                    //Message.Send("NodeHandle", MessageLevel.DEBUG, "Instantiate ComputeShader");

                    int float3_index = 0;
                    int float4_index = 0;

                    for (int i = 0; i < objects; i++)
                    {
                        dataset.POSITION[i] = new Vector3(position_data[float3_index], position_data[float3_index + 1], position_data[float3_index + 2]);

                        // size, heading, pitch, roll
                        dataset.UV0ListComp.Add(new Vector4(object_data[float4_index] * 2f, object_data[float4_index + 1], object_data[float4_index + 2], object_data[float4_index + 3]));
                        // postion offset (x - its up normal), planes offset ( xyz - in there normal direction)
                        dataset.UV1ListComp.Add(new Vector4(-0.02f, 0, 0, 0));

                        float3_index += 3;
                        float4_index += 4;
                    }

                    if (cb.UseColors)
                    {
                        float[] color_data;

                        if (cb.GetColorData(out color_data))
                        {
                            float4_index = 0;

                            Color[] colors = new Color[objects];

                            for (int i = 0; i < objects; i++)
                            {
                                colors[i]     = new Color(color_data[float4_index], color_data[float4_index + 1], color_data[float4_index + 2], color_data[float4_index + 3]);
                                float4_index += 4;
                            }

                            dataset.COLOR = colors;
                        }
                    }

                    //Debug.Log("Instantiate mat");

                    Renderer.Material = Instantiate(currentMaterial);
                    Renderer.SetCrossboardDataset(dataset);
                }

                return(true);
            }


            // ---------------------------- Geometry check -------------------------------------

            Geometry geom = node as Geometry;

            if (geom != null)
            {
                float[] float_data;
                int[]   indices;

                if (geom.GetVertexData(out float_data, out indices))
                {
                    MeshFilter   filter   = gameObject.AddComponent <MeshFilter>();
                    MeshRenderer renderer = gameObject.AddComponent <MeshRenderer>();

                    Mesh mesh = new Mesh();

                    Vector3[] vertices = new Vector3[float_data.Length / 3];

                    int float_index = 0;

                    for (int i = 0; i < vertices.Length; i++)
                    {
                        vertices[i] = new Vector3(float_data[float_index], float_data[float_index + 1], float_data[float_index + 2]);

                        float_index += 3;
                    }

                    mesh.vertices  = vertices;
                    mesh.triangles = indices;


                    if (geom.GetColorData(out float_data))
                    {
                        if (float_data.Length / 4 == vertices.Length)
                        {
                            float_index = 0;

                            Color[] cols = new Color[vertices.Length];

                            for (int i = 0; i < vertices.Length; i++)
                            {
                                cols[i]      = new Color(float_data[float_index], float_data[float_index + 1], float_data[float_index + 2], float_data[float_index + 3]);
                                float_index += 4;
                            }

                            mesh.colors = cols;
                        }
                    }

                    if (geom.GetNormalData(out float_data))
                    {
                        if (float_data.Length / 3 == vertices.Length)
                        {
                            float_index = 0;

                            Vector3[] normals = new Vector3[vertices.Length];

                            for (int i = 0; i < vertices.Length; i++)
                            {
                                normals[i]   = new Vector3(float_data[float_index], float_data[float_index + 1], float_data[float_index + 2]);
                                float_index += 3;
                            }

                            mesh.normals = normals;
                        }
                    }
                    else
                    {
                        //mesh.RecalculateNormals();

                        Vector3[] normals = new Vector3[vertices.Length];

                        for (int i = 0; i < vertices.Length; i++)
                        {
                            normals[i] = new Vector3(0, 1, 0);
                        }

                        mesh.normals = normals;

                        //Vector3[] normals = new Vector3[1];       // Obviously this doesnt work. Shame!

                        //normals[0] = new Vector3(0, 1, 0);

                        //mesh.normals = normals;
                    }

                    uint texture_units = geom.GetTextureUnits();

                    if (texture_units > 0)
                    {
                        if (geom.GetTexCoordData(out float_data, 0))
                        {
                            if (float_data.Length / 2 == vertices.Length)
                            {
                                float_index = 0;

                                Vector2[] tex_coords = new Vector2[vertices.Length];

                                for (int i = 0; i < vertices.Length; i++)
                                {
                                    tex_coords[i] = new Vector2(float_data[float_index], float_data[float_index + 1]);
                                    float_index  += 2;
                                }

                                mesh.uv = tex_coords;
                            }
                        }

                        if ((texture_units > 1) && geom.GetTexCoordData(out float_data, 1))
                        {
                            if (float_data.Length / 2 == vertices.Length)
                            {
                                float_index = 0;

                                Vector2[] tex_coords = new Vector2[vertices.Length];

                                for (int i = 0; i < vertices.Length; i++)
                                {
                                    tex_coords[i] = new Vector2(float_data[float_index], float_data[float_index + 1]);
                                    float_index  += 2;
                                }

                                mesh.uv2 = tex_coords;
                            }
                        }

                        if ((texture_units > 2) && geom.GetTexCoordData(out float_data, 2))
                        {
                            if (float_data.Length / 2 == vertices.Length)
                            {
                                float_index = 0;

                                Vector2[] tex_coords = new Vector2[vertices.Length];

                                for (int i = 0; i < vertices.Length; i++)
                                {
                                    tex_coords[i] = new Vector2(float_data[float_index], float_data[float_index + 1]);
                                    float_index  += 2;
                                }

                                mesh.uv3 = tex_coords;
                            }
                        }

                        if ((texture_units > 3) && geom.GetTexCoordData(out float_data, 3))
                        {
                            if (float_data.Length / 2 == vertices.Length)
                            {
                                float_index = 0;

                                Vector2[] tex_coords = new Vector2[vertices.Length];

                                for (int i = 0; i < vertices.Length; i++)
                                {
                                    tex_coords[i] = new Vector2(float_data[float_index], float_data[float_index + 1]);
                                    float_index  += 2;
                                }

                                mesh.uv4 = tex_coords;
                            }
                        }
                    }

                    filter.sharedMesh       = mesh;
                    renderer.sharedMaterial = currentMaterial;
                }
                return(true);
            }
            return(true);
        }