예제 #1
0
    private void Start()
    {
        instance = this;
        Initialize();
        BlockTypes.Initialize();
        textureMapper = new TextureMapper();

        if (AudioManager.instance == null)
        {
            audioManager.Initialize();
        }
        audioManager = AudioManager.instance;


        CreateTextures();
        Structure.Initialize();
        InitializeWorld(testWorld);
        ui.Initialize();

        //_ColorHorizon, _ColorTop, _ColorBottom;
        Shader.SetGlobalColor("_SkyColorTop", new Color(0.7692239f, 0.7906416f, 0.8113208f, 1f));
        Shader.SetGlobalColor("_SkyColorHorizon", new Color(0.3632075f, 0.6424405f, 1f, 1f));
        Shader.SetGlobalColor("_SkyColorBottom", new Color(0.1632253f, 0.2146282f, 0.2641509f, 1f));
        Shader.SetGlobalFloat("_MinLightLevel", gameSettings.minimumLightLevel);
#if !UNITY_EDITOR
        showLoadingScreen = true;
#endif
        if (showLoadingScreen)
        {
            isInStartup = true;
            world.chunkManager.isInStartup = true;
            ui.loadingScreen.gameObject.SetActive(true);
        }
    }
예제 #2
0
 public ChunkDataManager()
 {
     data          = new Dictionary <Vector2Int, ChunkData>();
     loadQueue     = new List <Vector2Int>();
     dirtyChunks   = new List <Vector2Int>();
     textureMapper = GameManager.instance.textureMapper;
 }
예제 #3
0
    private void Rebuild()
    {
        Vector2 graphicAnchoredPosition = currentSelectedGraphic.anchoredPosition;

        graphicAnchoredPosition.x = highlightPositions[currentHighlighted];
        currentSelectedGraphic.anchoredPosition = graphicAnchoredPosition;
        TextureMapper textureMapper = GameManager.instance.textureMapper;

        for (int i = 0; i < 9; i++)
        {
            RawImage rawImage  = elementGraphics[i];
            int      textureId = elements[i];
            if (textureId < 0)
            {
                rawImage.enabled = false;
                continue;
            }
            rawImage.enabled = true;
            TextureMapper.TextureMap      textureMap = textureMapper.map[(byte)textureId];
            TextureMapper.TextureMap.Face face       = textureMap.front;
            Rect uvRect = new Rect(
                1.0f / 128 * face.bl.x * 16,
                1 - (1.0f / 128 * face.bl.y * 16),
                1.0f / 128 * 16,
                1.0f / 128 * 16
                );
            rawImage.uvRect = uvRect;
        }
    }
예제 #4
0
        public void UsingSphericalMappingOn3dPoint_ShouldTranslateFrom3dTo2d(Point point, double expectedU, double expectedV)
        {
            var checkers = new UvCheckers(2, 2, Color.Black, Color.White);

            var(u, v) = TextureMapper.SphericalMap(point);
            Assert.Equal(expectedU, u);
            Assert.Equal(expectedV, v);
        }
예제 #5
0
 public ChunkDataManager(WorldInfo worldInfo)
 {
     this.worldInfo = worldInfo;
     data           = new Dictionary <Vector2Int, ChunkData>();
     loadQueue      = new List <Vector2Int>();
     dirtyChunks    = new List <Vector2Int>();
     textureMapper  = GameManager.instance.textureMapper;
 }
예제 #6
0
 private void Awake()
 {
     Instance  = this;
     texMapper = GetComponentInChildren <TextureMapper>();
     if (TextureManager.Instance.CurrentTextureToApply != null)
     {
         texMapper.SetTexture(TextureManager.Instance.CurrentTextureToApply);
     }
 }
예제 #7
0
파일: CMB.cs 프로젝트: week9/Switch-Toolbox
            public CMBTextureMapWrapper(TextureMapper texMap, CMB cmb)
            {
                CMBParent      = cmb;
                TextureMapData = texMap;

                this.WrapModeS = ConvertWrapMode(TextureMapData.WrapU);
                this.WrapModeT = ConvertWrapMode(TextureMapData.WrapV);
                this.MinFilter = ConvertMinFilterMode(TextureMapData.TextureMinFilter);
                this.MagFilter = ConvertMagFilterMode(TextureMapData.TextureMagFilter);
            }
	public void InitMapper()
	{
		if (mapperInitialized)
			return;
		
		Init();
		
		PrepareObjects();
		
		PrepareDetails();
		
		PrepareTrees();
		
		PrepareMultiObjects();
		
		Textures = new TextureMapper();
	}
예제 #9
0
        protected override void LoadContent()
        {
            // Ensure we have default textures loaded.
            TextureMapper.LoadDefaults(GraphicsDevice);

            // Load any custom textures if needed.
            TextureMapper = new TextureMapper(GraphicsDevice);
            if (UserSettings.Local.SelectedTexturePack != TexturePack.Default.Name)
            {
                TextureMapper.AddTexturePack(TexturePack.FromArchive(Path.Combine(TexturePack.TexturePackPath, UserSettings.Local.SelectedTexturePack)));
            }

            Pixel = new FontRenderer(
                new Font(Content, "Fonts/Pixel", FontStyle.Regular),
                new Font(Content, "Fonts/Pixel", FontStyle.Bold),
                null, // No support for underlined or strikethrough yet. The FontRenderer will revert to using the regular font style.
                null, // (I don't think BMFont has those options?)
                new Font(Content, "Fonts/Pixel", FontStyle.Italic));
            Interfaces.Add(ChatInterface  = new ChatInterface(Client, KeyboardComponent, Pixel));
            Interfaces.Add(DebugInterface = new DebugInterface(Client, Pixel));

            ChatInterface.IsVisible  = true;
            DebugInterface.IsVisible = true;

            OpaqueEffect = new BasicEffect(GraphicsDevice);
            OpaqueEffect.EnableDefaultLighting();
            OpaqueEffect.DirectionalLight0.SpecularColor = Color.Black.ToVector3();
            OpaqueEffect.DirectionalLight1.SpecularColor = Color.Black.ToVector3();
            OpaqueEffect.DirectionalLight2.SpecularColor = Color.Black.ToVector3();
            OpaqueEffect.TextureEnabled     = true;
            OpaqueEffect.Texture            = TextureMapper.GetTexture("terrain.png");
            OpaqueEffect.FogEnabled         = true;
            OpaqueEffect.FogStart           = 512f;
            OpaqueEffect.FogEnd             = 1000f;
            OpaqueEffect.FogColor           = Color.CornflowerBlue.ToVector3();
            OpaqueEffect.VertexColorEnabled = true;

            TransparentEffect = new AlphaTestEffect(GraphicsDevice);
            TransparentEffect.AlphaFunction      = CompareFunction.Greater;
            TransparentEffect.ReferenceAlpha     = 127;
            TransparentEffect.Texture            = TextureMapper.GetTexture("terrain.png");
            TransparentEffect.VertexColorEnabled = true;

            base.LoadContent();
        }
예제 #10
0
        protected override void LoadContent()
        {
            // Ensure we have default textures loaded.
            TextureMapper.LoadDefaults(GraphicsDevice);

            // Load any custom textures if needed.
            TextureMapper = new TextureMapper(GraphicsDevice);
            if (UserSettings.Local.SelectedTexturePack != TexturePack.Default.Name)
                TextureMapper.AddTexturePack(TexturePack.FromArchive(Path.Combine(Paths.TexturePacks,
                    UserSettings.Local.SelectedTexturePack)));

            Pixel = new FontRenderer(
                new Font(Content, "Fonts/Pixel"),
                new Font(Content, "Fonts/Pixel", FontStyle.Bold), null, null,
                new Font(Content, "Fonts/Pixel", FontStyle.Italic));

            base.LoadContent();
        }
예제 #11
0
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        TextureMapper tex = target as TextureMapper;

        if (GUILayout.Button(new GUIContent("Load Renderers")))
        {
            Undo.RecordObject(tex, "Set renderers");
            tex.GetAllRenderers();
        }
        if (GUILayout.Button(new GUIContent("Preview Texture")))
        {
            tex.PreviewTex();
        }
        if (GUILayout.Button(new GUIContent("Clear Texture")))
        {
            tex.ClearTexture();
        }
    }
	private void Init()
	{
		Terrain terComponent = (Terrain)GetComponent(typeof(Terrain));
		terrainData = terComponent.terrainData; 
		
		DetailPlacement = new DetailPlacer();
		DetailPlacement.Textures = new List<DetailTextureSettings>();
		Objects = new ObjectPlacer();
		TreePlacement = new TreePlacer();
		DetailPlacement = new DetailPlacer();
		Textures = new TextureMapper();
		MultiPlacer = new MultiObjectPlacer();
		
		MinScale = 0.8f;
		MaxScale = 1.2f;
		RotateY = true;
		mapperInitialized = true;
		PlaceEnable = true;
	}
예제 #13
0
 public void UsingCylindricalMappingOn3dPoint_ShouldTranslateFrom3dTo2d(Point point, double expectedU, double expectedV)
 {
     var(u, v) = TextureMapper.CylindricalMap(point);
     Assert.Equal(expectedU, u);
     Assert.Equal(expectedV, v);
 }
예제 #14
0
    /// <summary>
    /// Transfers all Faces to our Mesh.
    /// </summary>
    public void TransferFacesToMesh()
    {
        MeshFilter mf = GetComponent <MeshFilter>();

        // no mesh or no faces
        if (mf == null || faces == null)
        {
            return;
        }

        // get mesh
        Mesh mesh = mf.sharedMesh;

        // count vertices and indices
        int numVertices = 0;
        int numIndices  = 0;

        // list of unique materials
        List <Material> allMaterials = new List <Material>();

        //
        foreach (CSGFace f in faces)
        {
//			f.Dump();

            // count number of vertices
            numVertices += f.vertices.Length;
            numIndices  += (f.vertices.Length - 2) * 3;

            // add unique materials
            if (!allMaterials.Contains(f.material))
            {
                allMaterials.Add(f.material);
            }
        }

        // TODO: add debug message
        if (allMaterials.Count == 0)
        {
            return;
        }


        // clear all states
        mesh.Clear();

        // set submesh count
        mesh.subMeshCount = allMaterials.Count;

        // set default materials
        GetComponent <Renderer>().sharedMaterial = allMaterials[0];

        // copy shared materials into new array
        Material[] tmpMatArray = new Material[allMaterials.Count];
        allMaterials.CopyTo(tmpMatArray);

        //
        GetComponent <Renderer>().sharedMaterials = tmpMatArray;

        //
//		Debug.Log( "Building Mesh with " + numVertices + " Vertices" );

        //
        Vector3[] newVertices = new Vector3[numVertices];
        Vector2[] newUVs      = new Vector2[numVertices];
        Vector4[] newTangents = new Vector4[numVertices];

        //
        int vertCounter = 0;

        // Materials sorted Vertices and Indices
        foreach (Material m in allMaterials)
        {
            // find faces with material
            foreach (CSGFace f in faces)
            {
                // if not the same material
                if (f.material != m)
                {
                    continue;
                }

                // reset vertices
                numVertices = 0;

                //
                TextureMapper texMapper = new TextureMapper(f.GetPlane(), globalTexScaleU, globalTexScaleV, globalTexOffsetU, globalTexOffsetV, 0.0f);


                Vector4[] tangents;

                f.CalculateTangents(out tangents);

                // copy vertices
                // TODO: replace with normal for loop for better index access
                foreach (Vector3 v in f.vertices)
                {
                    newVertices[vertCounter + numVertices] = transform.worldToLocalMatrix.MultiplyPoint(v);

                    // ADD Option for creating uvs or using existing...

                    // if we want planar mapping or we have no original (existing) uvs
                    if (texMode == TexMode_Planar || f.uv == null)
                    {
                        // create uvs
                        newUVs[vertCounter + numVertices] = texMapper.Project(v);
                    }
                    else
                    {
                        // copy existing uvs
                        newUVs[vertCounter + numVertices] = f.uv[numVertices];
                    }

                    //
                    newTangents[vertCounter + numVertices] = tangents[numVertices];


                    numVertices++;
                }


                // increase number of vertices
                vertCounter += numVertices;
            }
        }

        // apply vertices
        mesh.vertices = newVertices;
        mesh.uv       = newUVs;
        mesh.tangents = newTangents;


        // reset values
        vertCounter = 0;
        int indexCounter = 0;
        int matIndex     = 0;

        // Materials sorted Vertices and Indices
        foreach (Material m in allMaterials)
        {
            int numTriangles = 0;

            // find faces with material
            foreach (CSGFace f in faces)
            {
                // if not the same material
                if (f.material != m)
                {
                    continue;
                }

                // sum up all triangles
                numTriangles += (f.vertices.Length - 2) * 3;
            }

            int[] newTriangles = new int[numTriangles];

            // reset index counter
            indexCounter = 0;

            // find faces with material
            foreach (CSGFace f in faces)
            {
                // if not the same material
                if (f.material != m)
                {
                    continue;
                }


                // number of triangle (triangle fan -> triangles)
                int triangleCount = (f.vertices.Length - 2);
                //
                int indexOffset = 0;

                // copy indices
                for (int i = 0; i < triangleCount; ++i)
                {
                    newTriangles[indexCounter + indexOffset] = vertCounter;

                    newTriangles[indexCounter + indexOffset + 1] = vertCounter + i + 1;
                    newTriangles[indexCounter + indexOffset + 2] = vertCounter + i + 2;

                    indexOffset += 3;
                }


                // increase indices counter
                indexCounter += indexOffset;

                // increase vertices counter
                vertCounter += f.vertices.Length;
            }


            // upload to mesh
            mesh.SetTriangles(newTriangles, matIndex);

            //
            matIndex++;
        }


        //
        mesh.RecalculateNormals();
        mesh.RecalculateBounds();


        // Optimize Mesh
        ;
    }
예제 #15
0
	/// <summary>
	/// Transfers all Faces to our Mesh.
	/// </summary>
	public void TransferFacesToMesh()
	{
		MeshFilter mf = GetComponent<MeshFilter>();
	
		// no mesh or no faces
		if( mf == null || faces == null )
			return;
			
		// get mesh
		Mesh mesh = mf.sharedMesh;	
		
		// count vertices and indices
		int numVertices = 0;
		int numIndices = 0;
		
		// list of unique materials
		List<Material> allMaterials = new List<Material>();
		
		// 
		foreach( Face f in faces )
		{
//			f.Dump();	
		
			// count number of vertices
			numVertices += f.vertices.Length;
			numIndices += (f.vertices.Length - 2) * 3;
			
			// add unique materials
			if( !allMaterials.Contains( f.material ) )
				allMaterials.Add( f.material );
		}
		
		// TODO: add debug message
		if( allMaterials.Count == 0 )
			return;
		
		
		// clear all states
		mesh.Clear();
		
		// set submesh count
		mesh.subMeshCount = allMaterials.Count;
		
		// set default materials
		renderer.sharedMaterial = allMaterials[0];
		
		// copy shared materials into new array
		Material[] tmpMatArray = new Material[allMaterials.Count];
		allMaterials.CopyTo( tmpMatArray ); 
		
		// 
		renderer.sharedMaterials = tmpMatArray;
		
		// 
		Debug.Log( "Building Mesh with " + numVertices + " Vertices" );
		
		// 
		Vector3[] newVertices = new Vector3[numVertices];
		Vector2[] newUVs = new Vector2[numVertices];
		Vector4[] newTangents = new Vector4[numVertices];
		
		// 
		int vertCounter = 0;
				
		// Materials sorted Vertices and Indices
		foreach( Material m in allMaterials )
		{
			// find faces with material
			foreach( Face f in faces )
			{
				// if not the same material
				if( f.material != m )
					continue;
			
				// reset vertices
				numVertices = 0;
			
				// 
				TextureMapper texMapper = new TextureMapper( f.GetPlane(), globalTexScaleU, globalTexScaleV, globalTexOffsetU, globalTexOffsetV, 0.0f );
				
				
				Vector4[] tangents;
				
				f.CalculateTangents( out tangents );
				
				// copy vertices
				// TODO: replace with normal for loop for better index access
				foreach( Vector3 v in f.vertices )
				{
					newVertices[vertCounter + numVertices] = transform.worldToLocalMatrix.MultiplyPoint( v );
				
					// ADD Option for creating uvs or using existing...
					
					// if we want planar mapping or we have no original (existing) uvs
					if( texMode == TexMode_Planar || f.uv == null )
					{
						// create uvs
						newUVs[vertCounter + numVertices] = texMapper.Project( v );
					}
					else
					{
						// copy existing uvs
						newUVs[vertCounter + numVertices] = f.uv[numVertices];
					}
						
					// 
					newTangents[vertCounter + numVertices] = tangents[numVertices];
					
					
					numVertices++;
				}
					
				
				// increase number of vertices
				vertCounter += numVertices;
			}
						
		}
		
		// apply vertices
		mesh.vertices = newVertices;
		mesh.uv = newUVs;
		mesh.tangents = newTangents;
		
		
		// reset values
		vertCounter = 0;
		int indexCounter = 0;
		int matIndex = 0;
		
		// Materials sorted Vertices and Indices
		foreach( Material m in allMaterials )
		{
			int numTriangles = 0;
			
			// find faces with material
			foreach( Face f in faces )
			{
				// if not the same material
				if( f.material != m )
					continue;
			
				// sum up all triangles
				numTriangles += (f.vertices.Length - 2) * 3;
			}
			
			int[] newTriangles = new int[numTriangles];
			
			// reset index counter
			indexCounter = 0;
			
			// find faces with material
			foreach( Face f in faces )
			{
				// if not the same material
				if( f.material != m )
					continue;
			
				
				// number of triangle (triangle fan -> triangles)
				int triangleCount = (f.vertices.Length - 2);
				// 
				int indexOffset = 0;
					
				// copy indices 
				for( int i = 0; i < triangleCount; ++i )
				{
					newTriangles[indexCounter + indexOffset] = vertCounter;	
				
					newTriangles[indexCounter + indexOffset + 1] = vertCounter + i + 1;
					newTriangles[indexCounter + indexOffset + 2] = vertCounter + i + 2;
			
					indexOffset += 3;
				}
				
				
				// increase indices counter
				indexCounter += indexOffset;	
				
				// increase vertices counter
				vertCounter += f.vertices.Length;
			}
			
			
			// upload to mesh
			mesh.SetTriangles( newTriangles, matIndex );
			
			// 
			matIndex++;
		}
		
		
		// 			
		mesh.RecalculateNormals();
		mesh.RecalculateBounds();
	
		
		// Optimize Mesh
		mesh.Optimize();
				
	}
예제 #16
0
        public void PointOnCube_ShouldIdentifyTheFaceOfTheCube(Point point, CubeFace expected)
        {
            var face = TextureMapper.FaceFromPoint(point);

            Assert.Equal(expected, face);
        }
예제 #17
0
 public void GivenPointOnDownOfCube_ShouldReturnCorrectUvMapping(Point point, double expectedU, double expectedV)
 {
     var(u, v) = TextureMapper.CubeUvDown(point);
     Assert.Equal(expectedU, u);
     Assert.Equal(expectedV, v);
 }
예제 #18
0
    public void Build(ChunkDataManager chunkDataManager)
    {
//#if UNITY_EDITOR
        UnityEngine.Profiling.Profiler.BeginSample("BUILDING CHUNK");
//#endif
        Vector2Int renderPosition = 16 * position;

        transform.position = new Vector3(renderPosition.x, 0, renderPosition.y);
        mesh.Clear();

        UnityEngine.Profiling.Profiler.BeginSample("GRABBING BLOCK DATA");

        ChunkData chunkData  = chunkDataManager.data[position];
        ChunkData front      = chunkDataManager.data[position + nFront];
        ChunkData back       = chunkDataManager.data[position + nBack];
        ChunkData left       = chunkDataManager.data[position + nLeft];
        ChunkData right      = chunkDataManager.data[position + nRight];
        ChunkData frontLeft  = chunkDataManager.data[position + nFront + nLeft];
        ChunkData frontRight = chunkDataManager.data[position + nFront + nRight];
        ChunkData backLeft   = chunkDataManager.data[position + nBack + nLeft];
        ChunkData backRight  = chunkDataManager.data[position + nBack + nRight];

        byte[,,] lightMap = new byte[48, 256, 48];

        chunkMap[0, 0] = backLeft;
        chunkMap[1, 0] = back;
        chunkMap[2, 0] = backRight;
        chunkMap[0, 1] = left;
        chunkMap[1, 1] = chunkData;
        chunkMap[2, 1] = right;
        chunkMap[0, 2] = frontLeft;
        chunkMap[1, 2] = front;
        chunkMap[2, 2] = frontRight;

        UnityEngine.Profiling.Profiler.EndSample();

        UnityEngine.Profiling.Profiler.BeginSample("SIMULATING LIGHT");


        Queue <Vector3Int> simulateQueue = new Queue <Vector3Int>();

        //sunray tracing needs to start above the highest non-air block to increase performance
        //all blocks above that block need to be set to 15
        for (int z = 0; z < 48; ++z)
        {
            for (int x = 0; x < 48; ++x)
            {
                if ((x % 47) * (z % 47) == 0)                 //filters outer edges
                {
                    //Debug.Log($"these should at least 0 or 47  ->  {x} {z}");
                    for (int yy = 0; yy < 256; ++yy)                     //dont do outer edges
                    {
                        lightMap[x, yy, z] = 15;                         //set all edges to 15 to stop tracing at edges
                    }
                    continue;
                }
                int y = GetHighestNonAir(chunkMap, x, z);
                if (x < 46)
                {
                    y = Mathf.Max(y, GetHighestNonAir(chunkMap, x + 1, z));
                }
                if (x > 1)
                {
                    y = Mathf.Max(y, GetHighestNonAir(chunkMap, x - 1, z));
                }
                if (z < 46)
                {
                    y = Mathf.Max(y, GetHighestNonAir(chunkMap, x, z + 1));
                }
                if (z > 1)
                {
                    y = Mathf.Max(y, GetHighestNonAir(chunkMap, x, z - 1));
                }
                y = Mathf.Min(y + 1, 255);
                simulateQueue.Enqueue(new Vector3Int(x, y, z));

                while (y < 255)
                {
                    lightMap[x, y, z] = 15;
                    y++;
                }
            }
        }

        for (int y = 0; y < 3; ++y)
        {
            for (int x = 0; x < 3; ++x)
            {
                foreach (KeyValuePair <Vector3Int, byte> kv in chunkMap[x, y].lightSources)
                {
                    Vector3Int position = kv.Key;
                    int        lX       = (16 * x) + position.x;
                    int        lY       = position.y;
                    int        lZ       = (16 * y) + position.z;
                    lightMap[lX, lY, lZ] = kv.Value;
                    simulateQueue.Enqueue(new Vector3Int(lX, lY, lZ));
                }
            }
        }

        int simulateCount = 0;

        while (simulateQueue.Count > 0)
        {
            Vector3Int position = simulateQueue.Dequeue();
            int        x        = position.x;
            int        y        = position.y;
            int        z        = position.z;

            byte bR = (x == 47 ? BlockTypes.BEDROCK : GetBlockFromMap(chunkMap, x + 1, y, z));
            byte bL = (x == 0 ? BlockTypes.BEDROCK : GetBlockFromMap(chunkMap, x - 1, y, z));
            byte bF = (z == 47 ? BlockTypes.BEDROCK : GetBlockFromMap(chunkMap, x, y, z + 1));
            byte bB = (z == 0 ? BlockTypes.BEDROCK : GetBlockFromMap(chunkMap, x, y, z - 1));
            byte bU = (y == 255 ? BlockTypes.BEDROCK : GetBlockFromMap(chunkMap, x, y + 1, z));
            byte bD = (y == 0 ? BlockTypes.BEDROCK : GetBlockFromMap(chunkMap, x, y - 1, z));

            byte light = lightMap[x, y, z];

            if (bR == BlockTypes.AIR)
            {
                byte lightR = lightMap[x + 1, y, z];
                if (lightR < light - 1)
                {
                    lightMap[x + 1, y, z] = (byte)(light - 1);
                    simulateQueue.Enqueue(new Vector3Int(x + 1, y, z));
                }
            }
            if (bL == BlockTypes.AIR)
            {
                byte lightL = lightMap[x - 1, y, z];
                if (lightL < light - 1)
                {
                    lightMap[x - 1, y, z] = (byte)(light - 1);
                    //if (x - 1 == 0) Debug.LogError("THIS SHOULD NOT HAPPEN");
                    simulateQueue.Enqueue(new Vector3Int(x - 1, y, z));
                }
            }
            if (bD == BlockTypes.AIR)
            {
                if (light == 15)
                {
                    lightMap[x, y - 1, z] = light;
                    simulateQueue.Enqueue(new Vector3Int(x, y - 1, z));
                }
                else
                {
                    byte lightD = lightMap[x, y - 1, z];
                    if (lightD < light - 1)
                    {
                        lightMap[x, y - 1, z] = (byte)(light - 1);
                        simulateQueue.Enqueue(new Vector3Int(x, y - 1, z));
                    }
                }
            }
            if (bU == BlockTypes.AIR)
            {
                byte lightU = lightMap[x, y + 1, z];
                if (lightU < light - 1)
                {
                    lightMap[x, y + 1, z] = (byte)(light - 1);
                    simulateQueue.Enqueue(new Vector3Int(x, y + 1, z));
                }
            }
            if (bF == BlockTypes.AIR)
            {
                byte lightF = lightMap[x, y, z + 1];
                if (lightF < light - 1)
                {
                    lightMap[x, y, z + 1] = (byte)(light - 1);
                    simulateQueue.Enqueue(new Vector3Int(x, y, z + 1));
                }
            }
            if (bB == BlockTypes.AIR)
            {
                byte lightB = lightMap[x, y, z - 1];
                if (lightB < light - 1)
                {
                    lightMap[x, y, z - 1] = (byte)(light - 1);
                    simulateQueue.Enqueue(new Vector3Int(x, y, z - 1));
                }
            }
            simulateCount++;
        }
        //Debug.Log("Did " + simulateCount + " light simulations");

        UnityEngine.Profiling.Profiler.EndSample();


        UnityEngine.Profiling.Profiler.BeginSample("CREATING FACES");
        TextureMapper textureMapper = GameManager.instance.textureMapper;

        for (int z = 0; z < 16; ++z)
        {
            for (int y = 0; y < 256; ++y)
            {
                for (int x = 0; x < 16; ++x)
                {
                    byte c = chunkData.GetBlocks()[x, y, z];
                    if (c != BlockTypes.AIR)
                    {
                        int lx = x + 16;
                        int ly = y;
                        int lz = z + 16;

                        byte bR = (x == 15 ? right.GetBlocks()[0, y, z] : chunkData.GetBlocks()[x + 1, y, z]);
                        byte bL = (x == 0 ? left.GetBlocks()[15, y, z] : chunkData.GetBlocks()[x - 1, y, z]);
                        byte bF = (z == 15 ? front.GetBlocks()[x, y, 0] : chunkData.GetBlocks()[x, y, z + 1]);
                        byte bB = (z == 0 ? back.GetBlocks()[x, y, 15] : chunkData.GetBlocks()[x, y, z - 1]);
                        byte bU = (y == 255 ? BlockTypes.AIR : chunkData.GetBlocks()[x, y + 1, z]);
                        byte bD = (y == 0 ? BlockTypes.AIR : chunkData.GetBlocks()[x, y - 1, z]);

                        byte lightR = lightMap[lx + 1, ly, lz];
                        byte lightL = lightMap[lx - 1, ly, lz];
                        byte lightF = lightMap[lx, ly, lz + 1];
                        byte lightB = lightMap[lx, ly, lz - 1];
                        byte lightU = (y == 255 ? (byte)15 : lightMap[lx, ly + 1, lz]);
                        byte lightD = (y == 0 ? (byte)15 : lightMap[lx, ly - 1, lz]);

                        TextureMapper.TextureMap textureMap = textureMapper.map[c];

                        if (bR > 127)
                        {
                            AddFace(
                                new Vector3(x + 1, y, z),
                                new Vector3(x + 1, y + 1, z),
                                new Vector3(x + 1, y + 1, z + 1),
                                new Vector3(x + 1, y, z + 1),
                                Vector3.right
                                );
                            AddTextureFace(textureMap.right);
                            int  b  = (y == 0 ? 0 : 1);
                            int  t  = (y == 255 ? 0 : 1);
                            byte bl = (byte)((lightMap[lx + 1, ly, lz] + lightMap[lx + 1, ly, lz - 1] + lightMap[lx + 1, ly - b, lz] + lightMap[lx + 1, ly - b, lz - 1]) / 4);
                            byte tl = (byte)((lightMap[lx + 1, ly, lz] + lightMap[lx + 1, ly, lz - 1] + lightMap[lx + 1, ly + t, lz] + lightMap[lx + 1, ly + t, lz - 1]) / 4);
                            byte tr = (byte)((lightMap[lx + 1, ly, lz] + lightMap[lx + 1, ly, lz + 1] + lightMap[lx + 1, ly + t, lz] + lightMap[lx + 1, ly + t, lz + 1]) / 4);
                            byte br = (byte)((lightMap[lx + 1, ly, lz] + lightMap[lx + 1, ly, lz + 1] + lightMap[lx + 1, ly - b, lz] + lightMap[lx + 1, ly - b, lz + 1]) / 4);
                            AddColors(textureMap, bl, tl, tr, br);
                        }
                        if (bL > 127)
                        {
                            AddFace(
                                new Vector3(x, y, z + 1),
                                new Vector3(x, y + 1, z + 1),
                                new Vector3(x, y + 1, z),
                                new Vector3(x, y, z),
                                -Vector3.right
                                );
                            AddTextureFace(textureMap.left);
                            int  b  = (y == 0 ? 0 : 1);
                            int  t  = (y == 255 ? 0 : 1);
                            byte br = (byte)((lightMap[lx - 1, ly, lz] + lightMap[lx - 1, ly, lz - 1] + lightMap[lx - 1, ly - b, lz] + lightMap[lx - 1, ly - b, lz - 1]) / 4);
                            byte tr = (byte)((lightMap[lx - 1, ly, lz] + lightMap[lx - 1, ly, lz - 1] + lightMap[lx - 1, ly + t, lz] + lightMap[lx - 1, ly + t, lz - 1]) / 4);
                            byte tl = (byte)((lightMap[lx - 1, ly, lz] + lightMap[lx - 1, ly, lz + 1] + lightMap[lx - 1, ly + t, lz] + lightMap[lx - 1, ly + t, lz + 1]) / 4);
                            byte bl = (byte)((lightMap[lx - 1, ly, lz] + lightMap[lx - 1, ly, lz + 1] + lightMap[lx - 1, ly - b, lz] + lightMap[lx - 1, ly - b, lz + 1]) / 4);
                            AddColors(textureMap, bl, tl, tr, br);
                        }

                        if (bU > 127)
                        {
                            AddFace(
                                new Vector3(x, y + 1, z),
                                new Vector3(x, y + 1, z + 1),
                                new Vector3(x + 1, y + 1, z + 1),
                                new Vector3(x + 1, y + 1, z),
                                Vector3.up
                                );
                            AddTextureFace(textureMap.top);
                            int  b  = (y == 0 ? 0 : 1);
                            int  t  = (y == 255 ? 0 : 1);
                            byte bl = (byte)((lightMap[lx, ly + t, lz] + lightMap[lx - 1, ly + t, lz] + lightMap[lx, ly + t, lz - 1] + lightMap[lx - 1, ly + t, lz - 1]) / 4);
                            byte tl = (byte)((lightMap[lx, ly + t, lz] + lightMap[lx - 1, ly + t, lz] + lightMap[lx, ly + t, lz + 1] + lightMap[lx - 1, ly + t, lz + 1]) / 4);
                            byte tr = (byte)((lightMap[lx, ly + t, lz] + lightMap[lx + 1, ly + t, lz] + lightMap[lx, ly + t, lz + 1] + lightMap[lx + 1, ly + t, lz + 1]) / 4);
                            byte br = (byte)((lightMap[lx, ly + t, lz] + lightMap[lx + 1, ly + t, lz] + lightMap[lx, ly + t, lz - 1] + lightMap[lx + 1, ly + t, lz - 1]) / 4);
                            AddColors(textureMap, bl, tl, tr, br);
                        }
                        if (bD > 127)
                        {
                            AddFace(
                                new Vector3(x, y, z + 1),
                                new Vector3(x, y, z),
                                new Vector3(x + 1, y, z),
                                new Vector3(x + 1, y, z + 1),

                                -Vector3.up
                                );
                            AddTextureFace(textureMap.bottom);
                            int  b  = (y == 0 ? 0 : 1);
                            int  t  = (y == 255 ? 0 : 1);
                            byte tl = (byte)((lightMap[lx, ly - b, lz] + lightMap[lx - 1, ly - b, lz] + lightMap[lx, ly - b, lz - 1] + lightMap[lx - 1, ly - b, lz - 1]) / 4);
                            byte bl = (byte)((lightMap[lx, ly - b, lz] + lightMap[lx - 1, ly - b, lz] + lightMap[lx, ly - b, lz + 1] + lightMap[lx - 1, ly - b, lz + 1]) / 4);
                            byte br = (byte)((lightMap[lx, ly - b, lz] + lightMap[lx + 1, ly - b, lz] + lightMap[lx, ly - b, lz + 1] + lightMap[lx + 1, ly - b, lz + 1]) / 4);
                            byte tr = (byte)((lightMap[lx, ly - b, lz] + lightMap[lx + 1, ly - b, lz] + lightMap[lx, ly - b, lz - 1] + lightMap[lx + 1, ly - b, lz - 1]) / 4);
                            AddColors(textureMap, bl, tl, tr, br);
                        }

                        if (bF > 127)
                        {
                            AddFace(
                                new Vector3(x + 1, y, z + 1),
                                new Vector3(x + 1, y + 1, z + 1),
                                new Vector3(x, y + 1, z + 1),
                                new Vector3(x, y, z + 1),
                                Vector3.forward
                                );
                            AddTextureFace(textureMap.front);
                            int  b  = (y == 0 ? 0 : 1);
                            int  t  = (y == 255 ? 0 : 1);
                            byte br = (byte)((lightMap[lx, ly, lz + 1] + lightMap[lx - 1, ly, lz + 1] + lightMap[lx, ly - b, lz + 1] + lightMap[lx - 1, ly - b, lz + 1]) / 4);
                            byte tr = (byte)((lightMap[lx, ly, lz + 1] + lightMap[lx - 1, ly, lz + 1] + lightMap[lx, ly + t, lz + 1] + lightMap[lx - 1, ly + t, lz + 1]) / 4);
                            byte tl = (byte)((lightMap[lx, ly, lz + 1] + lightMap[lx + 1, ly, lz + 1] + lightMap[lx, ly + t, lz + 1] + lightMap[lx + 1, ly + t, lz + 1]) / 4);
                            byte bl = (byte)((lightMap[lx, ly, lz + 1] + lightMap[lx + 1, ly, lz + 1] + lightMap[lx, ly - b, lz + 1] + lightMap[lx + 1, ly - b, lz + 1]) / 4);
                            AddColors(textureMap, bl, tl, tr, br);
                        }
                        if (bB > 127)
                        {
                            AddFace(
                                new Vector3(x, y, z),
                                new Vector3(x, y + 1, z),
                                new Vector3(x + 1, y + 1, z),
                                new Vector3(x + 1, y, z),
                                -Vector3.forward
                                );
                            AddTextureFace(textureMap.back);
                            int  b  = (y == 0 ? 0 : 1);
                            int  t  = (y == 255 ? 0 : 1);
                            byte bl = (byte)((lightMap[lx, ly, lz - 1] + lightMap[lx - 1, ly, lz - 1] + lightMap[lx, ly - b, lz - 1] + lightMap[lx - 1, ly - b, lz - 1]) / 4);
                            byte tl = (byte)((lightMap[lx, ly, lz - 1] + lightMap[lx - 1, ly, lz - 1] + lightMap[lx, ly + t, lz - 1] + lightMap[lx - 1, ly + t, lz - 1]) / 4);
                            byte tr = (byte)((lightMap[lx, ly, lz - 1] + lightMap[lx + 1, ly, lz - 1] + lightMap[lx, ly + t, lz - 1] + lightMap[lx + 1, ly + t, lz - 1]) / 4);
                            byte br = (byte)((lightMap[lx, ly, lz - 1] + lightMap[lx + 1, ly, lz - 1] + lightMap[lx, ly - b, lz - 1] + lightMap[lx + 1, ly - b, lz - 1]) / 4);
                            AddColors(textureMap, bl, tl, tr, br);
                        }
                    }
                }
            }
        }
        UnityEngine.Profiling.Profiler.EndSample();

        UnityEngine.Profiling.Profiler.BeginSample("APPLYING MESH DATA");
        mesh.SetVertices(vertices);
        mesh.SetTriangles(triangles, 0);
        mesh.SetUVs(0, uvs);
        mesh.SetNormals(normals);
        mesh.SetColors(colors);
        gameObject.SetActive(true);
        vertices.Clear();
        triangles.Clear();
        colors.Clear();
        uvs.Clear();
        normals.Clear();
        meshCollider.sharedMesh = mesh;
        UnityEngine.Profiling.Profiler.EndSample();

        //#if UNITY_EDITOR
        UnityEngine.Profiling.Profiler.EndSample();
//#endif
    }
예제 #19
0
    public void Build(ChunkDataManager chunkDataManager)
    {
//#if UNITY_EDITOR
        UnityEngine.Profiling.Profiler.BeginSample("BUILDING CHUNK");
//#endif
        Vector2Int renderPosition = 16 * position;

        transform.position = new Vector3(renderPosition.x, 0, renderPosition.y);
        mesh.Clear();

        UnityEngine.Profiling.Profiler.BeginSample("GRABBING BLOCK DATA");

        ChunkData chunkData  = chunkDataManager.data[position];
        ChunkData front      = chunkDataManager.data[position + nFront];
        ChunkData back       = chunkDataManager.data[position + nBack];
        ChunkData left       = chunkDataManager.data[position + nLeft];
        ChunkData right      = chunkDataManager.data[position + nRight];
        ChunkData frontLeft  = chunkDataManager.data[position + nFront + nLeft];
        ChunkData frontRight = chunkDataManager.data[position + nFront + nRight];
        ChunkData backLeft   = chunkDataManager.data[position + nBack + nLeft];
        ChunkData backRight  = chunkDataManager.data[position + nBack + nRight];

        byte[,,] lightMap = new byte[48, 256, 48];

        chunkMap[0, 0] = backLeft;
        chunkMap[1, 0] = back;
        chunkMap[2, 0] = backRight;
        chunkMap[0, 1] = left;
        chunkMap[1, 1] = chunkData;
        chunkMap[2, 1] = right;
        chunkMap[0, 2] = frontLeft;
        chunkMap[1, 2] = front;
        chunkMap[2, 2] = frontRight;

        UnityEngine.Profiling.Profiler.EndSample();

        UnityEngine.Profiling.Profiler.BeginSample("SIMULATING LIGHT");

        UnityEngine.Profiling.Profiler.BeginSample("PREPARING LIGHT SIMULATION");


        Queue <Vector3Int> simulateQueue = new Queue <Vector3Int>();

        //sunray tracing needs to start above the highest non-air block to increase performance
        //all blocks above that block need to be set to 15
        for (int z = 0; z < 48; ++z)
        {
            for (int x = 0; x < 48; ++x)
            {
                if ((x % 47) * (z % 47) == 0)                 //filters outer edges
                {
                    //Debug.Log($"these should at least 0 or 47  ->  {x} {z}");
                    for (int yy = 0; yy < 256; ++yy)                     //dont do outer edges
                    {
                        lightMap[x, yy, z] = 15;                         //set all edges to 15 to stop tracing at edges
                    }
                    continue;
                }
                int y = GetHighestNonAir(chunkMap, x, z);
                for (int sunlight = y; sunlight < 256; ++sunlight)
                {
                    lightMap[x, sunlight, z] = 15;
                }


                if (x < 46)
                {
                    y = Mathf.Max(y, GetHighestNonAir(chunkMap, x + 1, z));
                }
                if (x > 1)
                {
                    y = Mathf.Max(y, GetHighestNonAir(chunkMap, x - 1, z));
                }
                if (z < 46)
                {
                    y = Mathf.Max(y, GetHighestNonAir(chunkMap, x, z + 1));
                }
                if (z > 1)
                {
                    y = Mathf.Max(y, GetHighestNonAir(chunkMap, x, z - 1));
                }
                y = Mathf.Min(y + 1, 255);
                if (y < 2)
                {
                    continue;
                }

                simulateQueue.Enqueue(new Vector3Int(x, y, z));
            }
        }

        for (int y = 0; y < 3; ++y)
        {
            for (int x = 0; x < 3; ++x)
            {
                foreach (KeyValuePair <Vector3Int, byte> kv in chunkMap[x, y].lightSources)
                {
                    Vector3Int position = kv.Key;
                    int        lX       = (16 * x) + position.x;
                    int        lY       = position.y;
                    int        lZ       = (16 * y) + position.z;
                    lightMap[lX, lY, lZ] = kv.Value;
                    simulateQueue.Enqueue(new Vector3Int(lX, lY, lZ));
                }
            }
        }

        UnityEngine.Profiling.Profiler.EndSample();

        UnityEngine.Profiling.Profiler.BeginSample("RUNNING LIGHT SIMULATION");


        int simulateCount = 0;

        while (simulateQueue.Count > 0)
        {
            Vector3Int position = simulateQueue.Dequeue();
            int        y        = position.y;
            int        x        = position.x;
            int        z        = position.z;


            byte light = lightMap[x, y, z];

            if (x < 47)
            {
                byte lightR = lightMap[x + 1, y, z];
                if (lightR < light - 1)
                {
                    byte bR = GetBlockFromMap(chunkMap, x + 1, y, z);
                    if (bR == BlockTypes.AIR)
                    {
                        lightMap[x + 1, y, z] = (byte)(light - 1);
                        simulateQueue.Enqueue(new Vector3Int(x + 1, y, z));
                    }
                }
            }
            if (x > 0)
            {
                byte lightL = lightMap[x - 1, y, z];
                if (lightL < light - 1)
                {
                    byte bL = GetBlockFromMap(chunkMap, x - 1, y, z);
                    if (bL == BlockTypes.AIR)
                    {
                        lightMap[x - 1, y, z] = (byte)(light - 1);
                        simulateQueue.Enqueue(new Vector3Int(x - 1, y, z));
                    }
                }
            }
            if (y > 0)
            {
                byte bD = GetBlockFromMap(chunkMap, x, y - 1, z);
                if (bD == BlockTypes.AIR)
                {
                    if (light == 15)
                    {
                        lightMap[x, y - 1, z] = light;
                        simulateQueue.Enqueue(new Vector3Int(x, y - 1, z));
                    }
                    else
                    {
                        byte lightD = lightMap[x, y - 1, z];
                        if (lightD < light - 1)
                        {
                            lightMap[x, y - 1, z] = (byte)(light - 1);
                            simulateQueue.Enqueue(new Vector3Int(x, y - 1, z));
                        }
                    }
                }
            }
            if (y < 255)
            {
                byte lightU = lightMap[x, y + 1, z];
                if (lightU < light - 1)
                {
                    byte bU = GetBlockFromMap(chunkMap, x, y + 1, z);
                    if (bU == BlockTypes.AIR)
                    {
                        lightMap[x, y + 1, z] = (byte)(light - 1);
                        simulateQueue.Enqueue(new Vector3Int(x, y + 1, z));
                    }
                }
            }
            if (z < 47)
            {
                byte lightF = lightMap[x, y, z + 1];
                if (lightF < light - 1)
                {
                    byte bF = GetBlockFromMap(chunkMap, x, y, z + 1);
                    if (bF == BlockTypes.AIR)
                    {
                        lightMap[x, y, z + 1] = (byte)(light - 1);
                        simulateQueue.Enqueue(new Vector3Int(x, y, z + 1));
                    }
                }
            }
            if (z > 0)
            {
                byte lightB = lightMap[x, y, z - 1];
                if (lightB < light - 1)
                {
                    byte bB = GetBlockFromMap(chunkMap, x, y, z - 1);
                    if (bB == BlockTypes.AIR)
                    {
                        lightMap[x, y, z - 1] = (byte)(light - 1);
                        simulateQueue.Enqueue(new Vector3Int(x, y, z - 1));
                    }
                }
            }
            simulateCount++;
        }
        UnityEngine.Profiling.Profiler.EndSample();

        UnityEngine.Profiling.Profiler.EndSample();


        UnityEngine.Profiling.Profiler.BeginSample("CREATING FACES");


        //low precision meshes possible with this? : https://docs.unity3d.com/ScriptReference/Rendering.VertexAttributeDescriptor.html
        //vBuffer = new NativeArray<VertexData>(786432, Allocator.Temp);
        vBufferLength = 0;
        TextureMapper textureMapper = GameManager.instance.textureMapper;

        for (int z = 0; z < 16; ++z)
        {
            for (int y = 0; y < 256; ++y)
            {
                for (int x = 0; x < 16; ++x)
                {
                    byte c = chunkData.GetBlocks()[x, y, z];
                    if (c != BlockTypes.AIR)
                    {
                        int lx = x + 16;
                        int ly = y;
                        int lz = z + 16;

                        byte bR = (x == 15 ? right.GetBlocks()[0, y, z] : chunkData.GetBlocks()[x + 1, y, z]);
                        byte bL = (x == 0 ? left.GetBlocks()[15, y, z] : chunkData.GetBlocks()[x - 1, y, z]);
                        byte bF = (z == 15 ? front.GetBlocks()[x, y, 0] : chunkData.GetBlocks()[x, y, z + 1]);
                        byte bB = (z == 0 ? back.GetBlocks()[x, y, 15] : chunkData.GetBlocks()[x, y, z - 1]);
                        byte bU = (y == 255 ? BlockTypes.AIR : chunkData.GetBlocks()[x, y + 1, z]);
                        byte bD = (y == 0 ? BlockTypes.AIR : chunkData.GetBlocks()[x, y - 1, z]);

                        byte lightR = lightMap[lx + 1, ly, lz];
                        byte lightL = lightMap[lx - 1, ly, lz];
                        byte lightF = lightMap[lx, ly, lz + 1];
                        byte lightB = lightMap[lx, ly, lz - 1];
                        byte lightU = (y == 255 ? (byte)15 : lightMap[lx, ly + 1, lz]);
                        byte lightD = (y == 0 ? (byte)15 : lightMap[lx, ly - 1, lz]);

                        TextureMapper.TextureMap textureMap = textureMapper.map[c];

                        if (bR > 127)
                        {
                            //AddFace(
                            //	new Vector3(x + 1, y, z),
                            //	new Vector3(x + 1, y + 1, z),
                            //	new Vector3(x + 1, y + 1, z + 1),
                            //	new Vector3(x + 1, y, z + 1),
                            //	Vector3.right
                            //);
                            //AddTextureFace(textureMap.right);
                            int  b  = (y == 0 ? 0 : 1);
                            int  t  = (y == 255 ? 0 : 1);
                            byte bl = (byte)((lightMap[lx + 1, ly, lz] + lightMap[lx + 1, ly, lz - 1] + lightMap[lx + 1, ly - b, lz] + lightMap[lx + 1, ly - b, lz - 1]) / 4);
                            byte tl = (byte)((lightMap[lx + 1, ly, lz] + lightMap[lx + 1, ly, lz - 1] + lightMap[lx + 1, ly + t, lz] + lightMap[lx + 1, ly + t, lz - 1]) / 4);
                            byte tr = (byte)((lightMap[lx + 1, ly, lz] + lightMap[lx + 1, ly, lz + 1] + lightMap[lx + 1, ly + t, lz] + lightMap[lx + 1, ly + t, lz + 1]) / 4);
                            byte br = (byte)((lightMap[lx + 1, ly, lz] + lightMap[lx + 1, ly, lz + 1] + lightMap[lx + 1, ly - b, lz] + lightMap[lx + 1, ly - b, lz + 1]) / 4);
                            //AddColors(textureMap,bl, tl, tr, br);
                            AddVertexData(
                                new Vector3(x + 1, y, z),
                                new Vector3(x + 1, y + 1, z),
                                new Vector3(x + 1, y + 1, z + 1),
                                new Vector3(x + 1, y, z + 1),
                                NORMAL_RIGHT,
                                textureMap.right,
                                bl, tl, tr, br);
                        }
                        if (bL > 127)
                        {
                            //AddFace(
                            //	new Vector3(x, y, z + 1),
                            //	new Vector3(x, y + 1, z + 1),
                            //	new Vector3(x, y + 1, z),
                            //	new Vector3(x, y, z),
                            //	-Vector3.right
                            //);
                            //AddTextureFace(textureMap.left);
                            int  b  = (y == 0 ? 0 : 1);
                            int  t  = (y == 255 ? 0 : 1);
                            byte br = (byte)((lightMap[lx - 1, ly, lz] + lightMap[lx - 1, ly, lz - 1] + lightMap[lx - 1, ly - b, lz] + lightMap[lx - 1, ly - b, lz - 1]) / 4);
                            byte tr = (byte)((lightMap[lx - 1, ly, lz] + lightMap[lx - 1, ly, lz - 1] + lightMap[lx - 1, ly + t, lz] + lightMap[lx - 1, ly + t, lz - 1]) / 4);
                            byte tl = (byte)((lightMap[lx - 1, ly, lz] + lightMap[lx - 1, ly, lz + 1] + lightMap[lx - 1, ly + t, lz] + lightMap[lx - 1, ly + t, lz + 1]) / 4);
                            byte bl = (byte)((lightMap[lx - 1, ly, lz] + lightMap[lx - 1, ly, lz + 1] + lightMap[lx - 1, ly - b, lz] + lightMap[lx - 1, ly - b, lz + 1]) / 4);
                            //AddColors(textureMap, bl, tl, tr, br);
                            AddVertexData(
                                new Vector3(x, y, z + 1),
                                new Vector3(x, y + 1, z + 1),
                                new Vector3(x, y + 1, z),
                                new Vector3(x, y, z),
                                NORMAL_LEFT,
                                textureMap.left,
                                bl, tl, tr, br);
                        }

                        if (bU > 127)
                        {
                            //AddFace(
                            //	new Vector3(x, y + 1, z),
                            //	new Vector3(x, y + 1, z + 1),
                            //	new Vector3(x + 1, y + 1, z + 1),
                            //	new Vector3(x + 1, y + 1, z),
                            //	Vector3.up
                            //);
                            //AddTextureFace(textureMap.top);
                            int  b  = (y == 0 ? 0 : 1);
                            int  t  = (y == 255 ? 0 : 1);
                            byte bl = (byte)((lightMap[lx, ly + t, lz] + lightMap[lx - 1, ly + t, lz] + lightMap[lx, ly + t, lz - 1] + lightMap[lx - 1, ly + t, lz - 1]) / 4);
                            byte tl = (byte)((lightMap[lx, ly + t, lz] + lightMap[lx - 1, ly + t, lz] + lightMap[lx, ly + t, lz + 1] + lightMap[lx - 1, ly + t, lz + 1]) / 4);
                            byte tr = (byte)((lightMap[lx, ly + t, lz] + lightMap[lx + 1, ly + t, lz] + lightMap[lx, ly + t, lz + 1] + lightMap[lx + 1, ly + t, lz + 1]) / 4);
                            byte br = (byte)((lightMap[lx, ly + t, lz] + lightMap[lx + 1, ly + t, lz] + lightMap[lx, ly + t, lz - 1] + lightMap[lx + 1, ly + t, lz - 1]) / 4);
                            //AddColors(textureMap, bl, tl, tr, br);
                            AddVertexData(
                                new Vector3(x, y + 1, z),
                                new Vector3(x, y + 1, z + 1),
                                new Vector3(x + 1, y + 1, z + 1),
                                new Vector3(x + 1, y + 1, z),
                                NORMAL_TOP,
                                textureMap.top,
                                bl, tl, tr, br);
                        }
                        if (bD > 127)
                        {
                            //AddFace(
                            //	new Vector3(x, y, z + 1),
                            //	new Vector3(x, y, z),
                            //	new Vector3(x+1, y, z),
                            //	new Vector3(x+1, y, z+1),

                            //	-Vector3.up
                            //);
                            //AddTextureFace(textureMap.bottom);
                            int  b  = (y == 0 ? 0 : 1);
                            int  t  = (y == 255 ? 0 : 1);
                            byte tl = (byte)((lightMap[lx, ly - b, lz] + lightMap[lx - 1, ly - b, lz] + lightMap[lx, ly - b, lz - 1] + lightMap[lx - 1, ly - b, lz - 1]) / 4);
                            byte bl = (byte)((lightMap[lx, ly - b, lz] + lightMap[lx - 1, ly - b, lz] + lightMap[lx, ly - b, lz + 1] + lightMap[lx - 1, ly - b, lz + 1]) / 4);
                            byte br = (byte)((lightMap[lx, ly - b, lz] + lightMap[lx + 1, ly - b, lz] + lightMap[lx, ly - b, lz + 1] + lightMap[lx + 1, ly - b, lz + 1]) / 4);
                            byte tr = (byte)((lightMap[lx, ly - b, lz] + lightMap[lx + 1, ly - b, lz] + lightMap[lx, ly - b, lz - 1] + lightMap[lx + 1, ly - b, lz - 1]) / 4);
                            //AddColors(textureMap, bl, tl, tr, br);
                            AddVertexData(
                                new Vector3(x, y, z + 1),
                                new Vector3(x, y, z),
                                new Vector3(x + 1, y, z),
                                new Vector3(x + 1, y, z + 1),
                                NORMAL_BOTTOM,
                                textureMap.bottom,
                                bl, tl, tr, br);
                        }

                        if (bF > 127)
                        {
                            //AddFace(
                            //	new Vector3(x + 1, y, z + 1),
                            //	new Vector3(x + 1, y + 1, z + 1),
                            //	new Vector3(x, y + 1, z + 1),
                            //	new Vector3(x, y, z + 1),
                            //	Vector3.forward
                            //);
                            //AddTextureFace(textureMap.front);
                            int  b  = (y == 0 ? 0 : 1);
                            int  t  = (y == 255 ? 0 : 1);
                            byte br = (byte)((lightMap[lx, ly, lz + 1] + lightMap[lx - 1, ly, lz + 1] + lightMap[lx, ly - b, lz + 1] + lightMap[lx - 1, ly - b, lz + 1]) / 4);
                            byte tr = (byte)((lightMap[lx, ly, lz + 1] + lightMap[lx - 1, ly, lz + 1] + lightMap[lx, ly + t, lz + 1] + lightMap[lx - 1, ly + t, lz + 1]) / 4);
                            byte tl = (byte)((lightMap[lx, ly, lz + 1] + lightMap[lx + 1, ly, lz + 1] + lightMap[lx, ly + t, lz + 1] + lightMap[lx + 1, ly + t, lz + 1]) / 4);
                            byte bl = (byte)((lightMap[lx, ly, lz + 1] + lightMap[lx + 1, ly, lz + 1] + lightMap[lx, ly - b, lz + 1] + lightMap[lx + 1, ly - b, lz + 1]) / 4);
                            //AddColors(textureMap,bl, tl, tr, br);
                            AddVertexData(
                                new Vector3(x + 1, y, z + 1),
                                new Vector3(x + 1, y + 1, z + 1),
                                new Vector3(x, y + 1, z + 1),
                                new Vector3(x, y, z + 1),
                                NORMAL_FRONT,
                                textureMap.front,
                                bl, tl, tr, br);
                        }
                        if (bB > 127)
                        {
                            //AddFace(
                            //	new Vector3(x, y, z),
                            //	new Vector3(x, y + 1, z),
                            //	new Vector3(x + 1, y + 1, z),
                            //	new Vector3(x + 1, y, z),
                            //	-Vector3.forward
                            //);
                            //AddTextureFace(textureMap.back);
                            int  b  = (y == 0 ? 0 : 1);
                            int  t  = (y == 255 ? 0 : 1);
                            byte bl = (byte)((lightMap[lx, ly, lz - 1] + lightMap[lx - 1, ly, lz - 1] + lightMap[lx, ly - b, lz - 1] + lightMap[lx - 1, ly - b, lz - 1]) / 4);
                            byte tl = (byte)((lightMap[lx, ly, lz - 1] + lightMap[lx - 1, ly, lz - 1] + lightMap[lx, ly + t, lz - 1] + lightMap[lx - 1, ly + t, lz - 1]) / 4);
                            byte tr = (byte)((lightMap[lx, ly, lz - 1] + lightMap[lx + 1, ly, lz - 1] + lightMap[lx, ly + t, lz - 1] + lightMap[lx + 1, ly + t, lz - 1]) / 4);
                            byte br = (byte)((lightMap[lx, ly, lz - 1] + lightMap[lx + 1, ly, lz - 1] + lightMap[lx, ly - b, lz - 1] + lightMap[lx + 1, ly - b, lz - 1]) / 4);
                            //AddColors(textureMap,bl, tl, tr, br);
                            AddVertexData(
                                new Vector3(x, y, z),
                                new Vector3(x, y + 1, z),
                                new Vector3(x + 1, y + 1, z),
                                new Vector3(x + 1, y, z),
                                NORMAL_BACK,
                                textureMap.back,
                                bl, tl, tr, br);
                        }
                    }
                }
            }
        }
        UnityEngine.Profiling.Profiler.EndSample();

        UnityEngine.Profiling.Profiler.BeginSample("APPLYING MESH DATA");
        //mesh.SetVertices(vertices);
        //mesh.SetUVs(0, uvs);
        //mesh.SetNormals(normals);
        //mesh.SetColors(colors);

        VertexAttributeDescriptor[] layout = new VertexAttributeDescriptor[]
        {
            new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 3),
            new VertexAttributeDescriptor(VertexAttribute.Color, VertexAttributeFormat.UInt8, 4),
            //new VertexAttributeDescriptor(VertexAttribute.TexCoord0, VertexAttributeFormat.Float32, 2),
        };
        mesh.SetVertexBufferParams(vBufferLength, layout);
        //VertexData[] verts = new VertexData[vertices.Count];
        ////normals 0 front; 1 back; 2 top; 3 bottom; 4 left; 5 right
        //for (int i = 0; i < verts.Length; ++i)
        //{
        //	VertexData v = new VertexData();
        //	v.x = vertices[i].x;
        //	v.y = vertices[i].y;
        //	v.z = vertices[i].z;
        //	v.a = colors[i].a;
        //	Vector3 n = normals[i];
        //	if (n == Vector3.forward) v.b = 0;
        //	if (n == Vector3.back) v.b = 1;
        //	if (n == Vector3.up) v.b = 2;
        //	if (n == Vector3.down) v.b = 3;
        //	if (n == Vector3.left) v.b = 4;
        //	if (n == Vector3.right) v.b = 5;
        //	v.r = (byte)(int)uvs[i].x;
        //	v.g = (byte)(int)uvs[i].y;

        //	//v.uv = uvs[i];
        //	//v.color = colors[i];
        //	//v.normal = normals[i];
        //	verts[i] = v;
        //}
        mesh.SetVertexBufferData(vBuffer, 0, 0, vBufferLength);
        mesh.SetTriangles(triangles, 0);
        mesh.UploadMeshData(false);


        meshRenderer.enabled = (triangles.Count != 0);
        gameObject.SetActive(true);
        //vertices.Clear();
        triangles.Clear();
        //colors.Clear();
        //uvs.Clear();
        //normals.Clear();


        meshCollider.sharedMesh = mesh;
        UnityEngine.Profiling.Profiler.EndSample();

        //#if UNITY_EDITOR
        UnityEngine.Profiling.Profiler.EndSample();
//#endif
    }
예제 #20
0
 public void Awake()
 {
     texMapper = GetComponent <TextureMapper>();
     vumark.RegisterVuMarkTargetAssignedCallback(GetVumarkID);
 }