Example #1
0
 /// <summary>
 ///     Adds the vertices to the render buffer.
 /// </summary>
 public static void AddVertices(this RenderBuffer target, ref Vector3[] vertices, ref Vector3Int offset)
 {
     for (int i = 0; i < vertices.Length; i++)
     {
         target.Positions.Add(
             new Vector3(vertices[i].x + offset.X, vertices[i].y + offset.Y, vertices[i].z + offset.Z)
             );
     }
 }
Example #2
0
        public void SimdTest()
        {
            Bounds bounds = new Bounds(new Vector3(1, 1, 1), new Vector3(3, 3, 3));
            const int positionX = 4;
            const int positionZ = 3;

            // Non-SIMD version
            Vector3Int bMin;
            Vector3Int bMax;
            Vector3Int myVec52 = new Vector3Int(Chunk.Vec5.X, Chunk.Vec5.X, Chunk.Vec5.X);
            {
                Vector3Int pom = new Vector3Int(positionX * EngineSettings.ChunkConfig.SizeX, 0, positionZ * EngineSettings.ChunkConfig.SizeZ);

                int minX = Mathf.Clamp(Mathf.FloorToInt(bounds.min.x) - pom.X - myVec52.X, 0, EngineSettings.ChunkConfig.MaskX);
                int minY = Mathf.Clamp(Mathf.FloorToInt(bounds.min.y) - myVec52.Y, 0, EngineSettings.ChunkConfig.SizeYTotal - 1);
                int minZ = Mathf.Clamp(Mathf.FloorToInt(bounds.min.z) - pom.Z - myVec52.Z, 0, EngineSettings.ChunkConfig.MaskZ);
                bMin = new Vector3Int(minX, minY, minZ);

                int maxX = Mathf.Clamp(Mathf.Clamp(bMin.X, 0, EngineSettings.ChunkConfig.MaskX), 0, EngineSettings.ChunkConfig.MaskX);
                int maxY = Mathf.Clamp(Mathf.Clamp(bMin.Y, 0, EngineSettings.ChunkConfig.SizeYTotal - 1), 0, EngineSettings.ChunkConfig.SizeYTotal-1);
                int maxZ = Mathf.Clamp(Mathf.Clamp(bMin.Z, 0, EngineSettings.ChunkConfig.MaskZ), 0, EngineSettings.ChunkConfig.MaskZ);
                bMax = new Vector3Int(maxX, maxY, maxZ);
            }

            // SIMD version
            Vector4i bMinv;
            Vector4i bMaxv;
            {
                Vector4f bMinf = new Vector4f(bounds.min.x, bounds.min.y, bounds.min.z, 0f);
                bMinv = bMinf.ConvertToIntTruncated();
                Vector4f bMaxf = new Vector4f(bounds.max.x, bounds.max.y, bounds.max.z, 0f);
                bMaxv = bMaxf.ConvertToInt();

                Vector4i pom = new Vector4i(positionX, 0, positionZ, 0)*Chunk.VecSize;
                bMinv -= pom;
                bMinv -= Chunk.Vec5;
                bMinv = bMinv.Max(Vector4i.Zero).Min(Chunk.VecSize1);
                    // Clamp to 0..size (x,z) or 0..height (y) respectively

                bMaxv -= pom;
                bMaxv += Chunk.Vec5;
                bMaxv = bMaxv.Max(Vector4i.Zero).Min(Chunk.VecSize1);
                    // Clamp to 0..size (x,z) or 0..height (y) respectively
            }

            Assert.AreEqual(bMin.X, bMinv.X);
            Assert.AreEqual(bMin.Y, bMinv.Y);
            Assert.AreEqual(bMin.Z, bMinv.Z);

            Assert.AreEqual(bMax.X, bMaxv.X);
            Assert.AreEqual(bMax.Y, bMaxv.Y);
            Assert.AreEqual(bMax.Z, bMaxv.Z);
        }
Example #3
0
        public void Build(Map map, RenderBuffer targetBuffer, ref BlockData block, ref Vector3Int worldPos,
            ref Vector3Int localPos)
        {
            float dmg = block.GetDamagePercent();
            Color32 color = BlockDatabase.GetBlockInfo(block.BlockType).Color;
            
            for (int i = 0; i<SFaces.Length; i++)
            {
                int yy = worldPos.Y+SDirections[i].Y;
                if (yy<0)
                    continue; // Nothing to do at the bottom of the world

                int xx = worldPos.X+SDirections[i].X;
                int zz = worldPos.Z+SDirections[i].Z;
                
                // Building of faces is only necessary if there's solid geometry around
                BlockData b = map.GetBlock(xx, yy, zz);
                if (!b.IsAlpha())
                    continue;

                int iface = (int)SFaces[i];

                // Fill buffers with data
                {
                    // Add indices
                    int pom = targetBuffer.Positions.Count;
                    targetBuffer.Triangles.Add(pom+2);
                    targetBuffer.Triangles.Add(pom+1);
                    targetBuffer.Triangles.Add(pom+0);
                    targetBuffer.Triangles.Add(pom+3);
                    targetBuffer.Triangles.Add(pom+2);
                    targetBuffer.Triangles.Add(pom+0);

                    // Add vertices
                    targetBuffer.Positions.Add(new Vector3(SVertices[i][0].x+localPos.X,
                                                           SVertices[i][0].y+localPos.Y,
                                                           SVertices[i][0].z+localPos.Z));
                    targetBuffer.Positions.Add(new Vector3(SVertices[i][1].x+localPos.X,
                                                           SVertices[i][1].y+localPos.Y,
                                                           SVertices[i][1].z+localPos.Z));
                    targetBuffer.Positions.Add(new Vector3(SVertices[i][2].x+localPos.X,
                                                           SVertices[i][2].y+localPos.Y,
                                                           SVertices[i][2].z+localPos.Z));
                    targetBuffer.Positions.Add(new Vector3(SVertices[i][3].x+localPos.X,
                                                           SVertices[i][3].y+localPos.Y,
                                                           SVertices[i][3].z+localPos.Z));

                    // Add colors
                    targetBuffer.Colors.Add(color);
                    targetBuffer.Colors.Add(color);
                    targetBuffer.Colors.Add(color);
                    targetBuffer.Colors.Add(color);

                    // Add UVs
                    Rect texCoords = GetTexture(iface);
                    targetBuffer.UVs.Add(new Vector2(texCoords.xMax, 1f-texCoords.yMax));
                    targetBuffer.UVs.Add(new Vector2(texCoords.xMax, 1f-texCoords.yMin));
                    targetBuffer.UVs.Add(new Vector2(texCoords.xMin, 1f-texCoords.yMin));
                    targetBuffer.UVs.Add(new Vector2(texCoords.xMin, 1f-texCoords.yMax));

                    // Add damage UVs
                    float damage = dmg*(RenderBufferExtension.DamageFrames-1);
                    int dmgFrame = Mathf.FloorToInt(damage);
                    texCoords = new Rect(dmgFrame*RenderBufferExtension.DamageMultiplier, 0f,
                                         RenderBufferExtension.DamageMultiplier, 1f);
                    targetBuffer.UV2.Add(new Vector2(texCoords.xMax, 1f-texCoords.yMin));
                    targetBuffer.UV2.Add(new Vector2(texCoords.xMax, 1f-texCoords.yMax));
                    targetBuffer.UV2.Add(new Vector2(texCoords.xMin, 1f-texCoords.yMax));
                    targetBuffer.UV2.Add(new Vector2(texCoords.xMin, 1f-texCoords.yMin));

                    // Add normals
                    targetBuffer.Normals.AddRange(SNormals[iface]);
                }
            }
        }
Example #4
0
        public void Build(Map map, RenderBuffer targetBuffer, ref BlockData block, int face, bool backFace,
            ref Vector3[] vecs, ref Vector3Int worldPos)
        {
            if (worldPos.Y<0)
                return;

            int i = face;
            int iface = (int)SFaces[i];

            float dmg = block.GetDamagePercent();
            Color32 color = BlockDatabase.GetBlockInfo(block.BlockType).Color;

            // Empty block found, create a face
            targetBuffer.AddFaceIndices(backFace);
            targetBuffer.AddVertices(ref vecs);
            targetBuffer.AddFaceColors(ref color);
            targetBuffer.AddFaceUv(GetTexture(iface));
            targetBuffer.AddDamageUVs(dmg);
            targetBuffer.AddNormals(ref SNormals[iface]);
        }
Example #5
0
 public static float SqrMagnitude(Vector3Int a)
 {
     return (a.X * a.X) + (a.Y * a.Y) + (a.Z * a.Z);
 }
Example #6
0
 public static float SqrDistance(Vector3Int a, Vector3Int b)
 {
     return (a.X - b.X) * (a.X - b.X) + (a.Y - b.Y) * (a.Y - b.Y) + (a.Z - b.Z) * (a.Z - b.Z);
 }