Esempio n. 1
0
 //index < 0: new object
 /// <summary>
 /// Awake is called when the script instance is being loaded.
 /// </summary>
 void Awake()
 {
     for (int i = 0; i < objs.Length; i++)
     {
         VoxelObj VO = JsonUtility.FromJson <VoxelObj>(objs[i].text);
         objects.Add(VO);
     }
 }
Esempio n. 2
0
    /// <summary>
    /// Start is called on the frame when a script is enabled just before
    /// any of the Update methods is called the first time.
    /// </summary>
    // void Start()
    // {
    //  spawn(Vector3.zero, Vector3Int.zero, -1);
    //  spawn(Vector3.up*5, Vector3Int.zero, -1);
    // }
    void sculptObjects()
    {
        if (Cursor.lockState == CursorLockMode.None)
        {
            return;
        }
        if ((Input.GetButton("Fire1")) /*  && (Sthread == null || !Sthread.IsAlive)*/)
        {
            int        multi = 1;
            RaycastHit hit;
            Ray        ray = new Ray(cam.transform.position, cam.transform.forward);    //cam.ScreenPointToRay(new Vector2(Screen.width/2,Screen.height/2));

            if (Physics.Raycast(ray, out hit, 30f))
            {
                objectInfo objInfo = hit.transform.GetComponent <objectInfo>();
                if (objInfo != null && objInfo.enabled)
                {
                    Vector3 pos = hit.transform.InverseTransformPoint(hit.point) - hit.normal * multi;
                    //Vector3 playerPos = Player.localPosition;

                    MeshCollider collider = hit.collider as MeshCollider;
                    // Remember to handle case where collider is null because you hit a non-mesh primitive...

                    Mesh mesh  = collider.sharedMesh;
                    int  limit = hit.triangleIndex * 3;

                    //int submesh = 0;
                    // if(multi == -1){
                    //     for(submesh = 0; submesh < mesh.subMeshCount; submesh++)
                    //     {
                    //         int numIndices = mesh.GetTriangles(submesh).Length;
                    //         if(numIndices > limit)
                    //             break;

                    //         limit -= numIndices;
                    //     }
                    //     Material material = collider.GetComponent<MeshRenderer>().sharedMaterials[submesh];
                    //     submesh = m_materials.IndexOf(material);
                    // }
                    //Debug.Log(hit.transform.InverseTransformPoint(pos));
                    VoxelObj vObj = objDict[objInfo.chunk][objInfo.index];
                    Voxel[]  v    = vObj.voxels;
                    sculptingObj.sculpt(ref v, pos, 1, vObj.vSize);
                    objDict[objInfo.chunk][objInfo.index] = new VoxelObj(vObj.vSize, v);
                    spawn(hit.transform.localPosition, objInfo.chunk, objInfo.index);
                    objInfo.enabled = false;
                    // if(objDict[objInfo.chunk][objInfo.index].voxels == objects[0].voxels){
                    //  Debug.Log("fail");
                    // }

                    /* Sthread = new Thread(() => GTL(sculp.sculpt(ref voxels,multi,pos,playerPos,submesh)));
                     *
                     * Sthread.Start();*/
                }
            }
        }
    }
Esempio n. 3
0
        void Update()
        {
            while (actions.Count > 0)
            {
                Action func = actions[0];
                actions.RemoveAt(0);
                func();
            }
            //transform.Rotate(Vector3.up, 10.0f * Time.deltaTime);
            if ((Input.GetButton("Fire1") || Input.GetButton("Fire2")) && (t == null || !t.IsAlive))
            {
                float multi = 1;
                if (Input.GetButton("Fire2"))
                {
                    multi = -1;
                }
                RaycastHit hit;
                Ray        ray = cam.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));

                if (Physics.Raycast(ray, out hit))
                {
                    if (hit.transform.parent != null && hit.transform.parent.tag == "holder")
                    {
                        Vector3 pos = hit.point / scale;
                        //pos = pos- hit.normal;
                        int _x = Mathf.FloorToInt(pos.x);
                        int _y = Mathf.FloorToInt(pos.y);
                        int _z = Mathf.FloorToInt(pos.z);
                        //int idx = x+ y*width + z*width*height;
                        for (int x = _x - 4; x <= _x + 4; x++)
                        {
                            for (int y = _y - 4; y <= _y + 4; y++)
                            {
                                for (int z = _z - 4; z <= _z + 4; z++)
                                {
                                    int idx = x + y * width + z * width * height;
                                    if (x > 0 && y > 0 && z > 0 && x < width - 1 && y < height - 1 && z < length - 1)
                                    {
                                        float distancevox = Vector3.Distance(pos, new Vector3(x, y, z));
                                        voxels[idx] = new Voxel(Mathf.Clamp(voxels[idx].value - Mathf.Max(0.08f * (-0.5f * Mathf.Pow(distancevox, 2) + 1f), 0) * multi, -0.3f, 0.3f), 0);
                                    }
                                }
                            }
                        }

                        /*if(voxels.Length > idx && idx>= 0)
                         *      voxels[idx] -= 1f;	*/


                        t = new Thread(Generate);
                        t.Start();
                    }
                }
            }
                        #if UNITY_EDITOR
            if (Input.GetKeyDown("g"))
            {
                //ScriptableObject.CreateInstance("VoxelObject");
                //t = new Thread(Generate);
                //t.Start();
                // voxelObject.voxels = voxels;
                // voxelObject.vSize = new Vector3Int(width, height, length);
                // EditorUtility.SetDirty(voxelObject);
                string text = JsonUtility.ToJson(new VoxelObj(new Vector3Int(width, height, length), voxels));
                File.WriteAllText(AssetDatabase.GetAssetPath(textAsset), text);
                EditorUtility.SetDirty(textAsset);
                Debug.Log(text);
            }
            if (Input.GetKeyDown("h"))
            {
                //ScriptableObject.CreateInstance("VoxelObject");
                VoxelObj vO = JsonUtility.FromJson <VoxelObj>(File.ReadAllText(AssetDatabase.GetAssetPath(textAsset)));
                voxels = vO.voxels;
                t      = new Thread(Generate);
                t.Start();
            }
                        #endif
        }
Esempio n. 4
0
    public void spawn(Vector3 position, Vector3Int chunk, int index)
    {
        VoxelObj obj;

        Debug.Log("hello");
        if (index >= 0)
        {
            obj = objDict[chunk][index];
        }
        else
        {
            //System.Random random = new System.Random();
            VoxelObj temp = objects[0];
            //Debug.Log(UnityEngine.Random.Range(0,objects.Count));

            obj = new VoxelObj(temp.vSize, (Voxel[])temp.voxels.Clone());
            //obj.voxels = temp.voxels;
            //obj.vSize = temp.vSize;
        }

        Marching marching = new MarchingCubes();

        marching.Surface = 0;
        float[] vox = new float[obj.voxels.Length];
        for (int i = 0; i < obj.voxels.Length; i++)
        {
            vox[i] = obj.voxels[i].value;
        }
        int ind;

        if (objDict.ContainsKey(chunk))
        {
            if (objDict[chunk].Count > index && index >= 0)
            {
                objDict[chunk][index] = obj;
                ind = index;
            }
            else
            {
                objDict[chunk].Add(obj);
                ind = objDict[chunk].Count - 1;
            }
        }
        else
        {
            objDict.Add(chunk, new List <VoxelObj>());
            objDict[chunk].Add(obj);
            ind = objDict[chunk].Count - 1;
        }

        List <Vector3> verts   = new List <Vector3>();
        List <int>     indices = new List <int>();

        marching.Generate(vox, obj.vSize.x, obj.vSize.y, obj.vSize.z, verts, indices);
        //Debug.Log(obj.voxels.Length);
        List <List <int> > subIndices = new List <List <int> >()
        {
            indices
        };
        Action action = () => {
            GameObject g  = meshGeneration.genMesh(verts, subIndices, materials, transform);
            objectInfo oI = g.AddComponent <objectInfo>();
            oI.index = ind;
            oI.chunk = chunk;

            g.transform.localPosition = position;
            if (!meshes.ContainsKey(chunk))
            {
                meshes.Add(chunk, new List <GameObject>());
            }
            if (meshes[chunk].Count > ind)
            {
                Destroy(meshes[chunk][ind]);
                meshes[chunk][ind] = g;
            }
            else
            {
                meshes[chunk].Add(g);
            }
            //Debug.Log("hello");
        };

        actions.Add(action);
    }