Created
July 22, 2017 21:17
-
-
Save unity3dcollege/2f47de25d19272c8f6c40fb07999ca81 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.Networking; | |
public class PlayerBrush : NetworkBehaviour | |
{ | |
#region Initialization | |
[Server] | |
private void Start() | |
{ | |
var data = PaintCanvas.GetAllTextureData(); | |
var zippeddata = data.Compress(); | |
RpcSendFullTexture(zippeddata); | |
} | |
[ClientRpc] | |
private void RpcSendFullTexture(byte[] textureData) | |
{ | |
PaintCanvas.SetAllTextureData(textureData.Decompress()); | |
} | |
#endregion | |
private void Update() | |
{ | |
if (Input.GetMouseButton(0)) | |
{ | |
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); | |
RaycastHit hit; | |
if (Physics.Raycast(ray, out hit)) | |
{ | |
var pallet = hit.collider.GetComponent<PaintCanvas>(); | |
if (pallet != null) | |
{ | |
Debug.Log(hit.textureCoord); | |
Debug.Log(hit.point); | |
Renderer rend = hit.transform.GetComponent<Renderer>(); | |
MeshCollider meshCollider = hit.collider as MeshCollider; | |
if (rend == null || rend.sharedMaterial == null || rend.sharedMaterial.mainTexture == null || meshCollider == null) | |
return; | |
Texture2D tex = rend.material.mainTexture as Texture2D; | |
Vector2 pixelUV = hit.textureCoord; | |
pixelUV.x *= tex.width; | |
pixelUV.y *= tex.height; | |
CmdBrushAreaWithColorOnServer(pixelUV, ColorPicker.SelectedColor, BrushSizeSlider.BrushSize); | |
BrushAreaWithColor(pixelUV, ColorPicker.SelectedColor, BrushSizeSlider.BrushSize); | |
} | |
} | |
} | |
} | |
[Command] | |
private void CmdBrushAreaWithColorOnServer(Vector2 pixelUV, Color color, int size) | |
{ | |
RpcBrushAreaWithColorOnClients(pixelUV, color, size); | |
BrushAreaWithColor(pixelUV, color, size); | |
} | |
[ClientRpc] | |
private void RpcBrushAreaWithColorOnClients(Vector2 pixelUV, Color color, int size) | |
{ | |
BrushAreaWithColor(pixelUV, color, size); | |
} | |
private void BrushAreaWithColor(Vector2 pixelUV, Color color, int size) | |
{ | |
for (int x = -size; x < size; x++) | |
{ | |
for (int y = -size; y < size; y++) | |
{ | |
PaintCanvas.Texture.SetPixel((int)pixelUV.x + x, (int)pixelUV.y + y, color); | |
} | |
} | |
PaintCanvas.Texture.Apply(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment