Beispiel #1
0
        //!
        //! Recursively iterate over scene and prepare data to be send to clients
        //!
        private bool iterLocation(Transform location)
        {
            // check LOD and retur if not match
            if (location.gameObject.layer != lodLowLayer && location.gameObject.layer != lodMixedLayer)
            {
                return(false);
            }

            SceneNode node = new SceneNode();

            // print("Location: " + location);

            if (location.GetComponent <Light>() != null)
            {
                SceneNodeLight nodeLight = new SceneNodeLight();

                Light light = location.GetComponent <Light>();
                nodeLight.intensity = light.intensity;
                nodeLight.color     = new float[3] {
                    light.color.r, light.color.g, light.color.b
                };
                nodeLight.lightType = light.type;
                nodeLight.exposure  = 0;
                nodeLight.angle     = light.spotAngle;
                nodeLight.range     = light.range;

                node          = nodeLight;
                node.editable = true;
                SceneObject sObj = location.gameObject.AddComponent <SceneObject>();
                sObj.id = globalID;
                globalID++;
            }
            else if (location.GetComponent <Camera>() != null)
            {
                SceneNodeCam nodeCamera = new SceneNodeCam();

                Camera camera = location.GetComponent <Camera>();
                nodeCamera.fov  = camera.fieldOfView;
                nodeCamera.near = camera.nearClipPlane;
                nodeCamera.far  = camera.farClipPlane;
                node            = nodeCamera;

                node.editable = true;
                SceneObject sObj = location.gameObject.AddComponent <SceneObject>();
                sObj.id = globalID;
                globalID++;
            }
            else if (location.GetComponent <MeshFilter>() != null)
            {
                SceneNodeGeo nodeGeo = new SceneNodeGeo();
                nodeGeo.color = new float[4] {
                    0, 0, 0, 0
                };
                nodeGeo.geoId = processGeometry(location.GetComponent <MeshFilter>().sharedMesh);

                if (location.GetComponent <Renderer>() != null && location.GetComponent <Renderer>().sharedMaterial != null)
                {
                    Material mat = location.GetComponent <Renderer>().sharedMaterial;
#if TRUNK
                    // if materials's shader is not standard, add this material to material package.
                    // Currently this will only get the material name and try to load it on client side. If this fails, it will fallback to Standard.
                    nodeGeo.materialId = processMaterial(location.GetComponent <Renderer>().sharedMaterial);
#endif
                    if (mat.HasProperty("_Color"))
                    {
                        nodeGeo.color = new float[4] {
                            mat.color.r, mat.color.g, mat.color.b, mat.color.a
                        };
                    }

                    if (mat.HasProperty("_Glossiness"))
                    {
                        nodeGeo.roughness = mat.GetFloat("_Glossiness");
                    }

                    if (mat.mainTexture != null)
                    {
                        Texture2D mainTex = (Texture2D)mat.mainTexture;
                        nodeGeo.textureId = processTexture(mainTex);
                    }
                    else
                    {
                        nodeGeo.textureId = -1;
                    }
                }
                else
                {
                    nodeGeo.textureId = -1;
                }

                node = nodeGeo;

                if (location.gameObject.tag == "editable")
                {
                    node.editable = true;
                    bool gotHighLod = false;
                    foreach (Transform child in location.parent)
                    {
                        if (child.name == location.name && child.gameObject.layer == lodHighLayer)
                        {
                            SceneObject sObj = child.gameObject.AddComponent <SceneObject>();
                            sObj.id = globalID;
                            globalID++;
                            gotHighLod = true;
                        }
                    }
                    if (!gotHighLod)
                    {
                        SceneObject sObj = location.gameObject.AddComponent <SceneObject>();
                        sObj.id = globalID;
                        globalID++;
                    }
                }
                else
                {
                    node.editable = false;
                }
            }
            else if (location.GetComponent <SkinnedMeshRenderer>() != null)
            {
                SkinnedMeshRenderer sRenderer = location.GetComponent <SkinnedMeshRenderer>();
                SceneNodeSkinnedGeo nodeGeo   = new SceneNodeSkinnedGeo();
                Material            mat       = location.GetComponent <Renderer>().sharedMaterial;
#if TRUNK
                // if materials's shader is not standard, add this material to material package.
                // Currently this will only get the material name and try to load it on client side. If this fails, it will fallback to Standard.
                nodeGeo.materialId = processMaterial(location.GetComponent <Renderer>().sharedMaterial);
#endif

                nodeGeo.color = new float[4] {
                    0, 0, 0, 0
                };
                nodeGeo.geoId           = processGeometry(sRenderer.sharedMesh);
                nodeGeo.rootBoneDagPath = vpet.Extensions.getPathString(sRenderer.rootBone, scene);
                nodeGeo.boundCenter     = new float[3] {
                    sRenderer.localBounds.center.x, sRenderer.localBounds.center.y, sRenderer.localBounds.center.z
                };
                nodeGeo.boundExtents = new float[3] {
                    sRenderer.localBounds.extents.x, sRenderer.localBounds.extents.y, sRenderer.localBounds.extents.z
                };
                nodeGeo.bindPoseLength = sRenderer.sharedMesh.bindposes.Length;
                nodeGeo.bindPoses      = new float[nodeGeo.bindPoseLength * 16];

                for (int i = 0; i < nodeGeo.bindPoseLength; i++)
                {
                    nodeGeo.bindPoses[i * 16]      = sRenderer.sharedMesh.bindposes[i].m00;
                    nodeGeo.bindPoses[i * 16 + 1]  = sRenderer.sharedMesh.bindposes[i].m01;
                    nodeGeo.bindPoses[i * 16 + 2]  = sRenderer.sharedMesh.bindposes[i].m02;
                    nodeGeo.bindPoses[i * 16 + 3]  = sRenderer.sharedMesh.bindposes[i].m03;
                    nodeGeo.bindPoses[i * 16 + 4]  = sRenderer.sharedMesh.bindposes[i].m10;
                    nodeGeo.bindPoses[i * 16 + 5]  = sRenderer.sharedMesh.bindposes[i].m11;
                    nodeGeo.bindPoses[i * 16 + 6]  = sRenderer.sharedMesh.bindposes[i].m12;
                    nodeGeo.bindPoses[i * 16 + 7]  = sRenderer.sharedMesh.bindposes[i].m13;
                    nodeGeo.bindPoses[i * 16 + 8]  = sRenderer.sharedMesh.bindposes[i].m20;
                    nodeGeo.bindPoses[i * 16 + 9]  = sRenderer.sharedMesh.bindposes[i].m21;
                    nodeGeo.bindPoses[i * 16 + 10] = sRenderer.sharedMesh.bindposes[i].m22;
                    nodeGeo.bindPoses[i * 16 + 11] = sRenderer.sharedMesh.bindposes[i].m23;
                    nodeGeo.bindPoses[i * 16 + 12] = sRenderer.sharedMesh.bindposes[i].m30;
                    nodeGeo.bindPoses[i * 16 + 13] = sRenderer.sharedMesh.bindposes[i].m31;
                    nodeGeo.bindPoses[i * 16 + 14] = sRenderer.sharedMesh.bindposes[i].m32;
                    nodeGeo.bindPoses[i * 16 + 15] = sRenderer.sharedMesh.bindposes[i].m33;
                }

                List <string> skinnedMeshBones = new List <string>();
                foreach (Transform t in sRenderer.bones)
                {
                    skinnedMeshBones.Add(Extensions.getPathString(t, scene));
                }
                foreach (string s in skinnedMeshBones)
                {
                    nodeGeo.skinnedMeshBonesArray += s;
                    if (s != skinnedMeshBones[skinnedMeshBones.Count - 1])
                    {
                        nodeGeo.skinnedMeshBonesArray += '\r';
                    }
                }

                if (sRenderer.material != null)
                {
                    mat = sRenderer.material;

                    if (mat.HasProperty("_Color"))
                    {
                        nodeGeo.color = new float[4] {
                            mat.color.r, mat.color.g, mat.color.b, mat.color.a
                        };
                    }

                    if (mat.HasProperty("_Glossiness"))
                    {
                        nodeGeo.roughness = mat.GetFloat("_Glossiness");
                    }

                    if (mat.mainTexture != null)
                    {
                        Texture2D mainTex = (Texture2D)mat.mainTexture;
                        nodeGeo.textureId = processTexture(mainTex);
                    }
                    else
                    {
                        nodeGeo.textureId = -1;
                    }
                }
                else
                {
                    nodeGeo.textureId = -1;
                }

                node = nodeGeo;

                if (location.gameObject.tag == "editable")
                {
                    node.editable = true;
                    bool gotHighLod = false;
                    foreach (Transform child in location.parent)
                    {
                        if (child.name == location.name && child.gameObject.layer == lodHighLayer)
                        {
                            SceneObject sObj = child.gameObject.AddComponent <SceneObject>();
                            sObj.id = globalID;
                            globalID++;
                            gotHighLod = true;
                        }
                    }
                    if (!gotHighLod)
                    {
                        SceneObject sObj = location.gameObject.AddComponent <SceneObject>();
                        sObj.id = globalID;
                        globalID++;
                    }
                }
                else
                {
                    node.editable = false;
                }
            }
            else if (location.gameObject.tag == "editable")
            {
                node.editable = true;
                SceneObject sObj = location.gameObject.AddComponent <SceneObject>();
                sObj.id = globalID;
                globalID++;
            }


            Animator animator = location.GetComponent <Animator>();

            if (animator != null)
            {
                animator.logWarnings = false;
                processCharacter(animator);
            }

            //if (location.gameObject.tag == "editable")
            //{
            //    node.editable = true;
            //    SceneObject sObj = location.gameObject.AddComponent<SceneObject>();
            //    sObj.id = globalID;
            //    globalID++;
            //}
            //else
            //    node.editable = false;

            node.position = new float[3] {
                location.localPosition.x, location.localPosition.y, location.localPosition.z
            };
            node.scale = new float[3] {
                location.localScale.x, location.localScale.y, location.localScale.z
            };
            node.rotation = new float[4] {
                location.localRotation.x, location.localRotation.y, location.localRotation.z, location.localRotation.w
            };
            node.name = new byte[256];
            byte[] tmpName = Encoding.ASCII.GetBytes(location.name);
            for (int i = 0; i < tmpName.Length; i++)
            {
                node.name[i] = tmpName[i];
            }

            if (location.name != "root")
            {
                // print("Added: " + location.name);
                nodeList.Add(node);
            }

            // recursive children
            int childCounter = 0;
            if (location.childCount > 0)
            {
                foreach (Transform child in location)
                {
                    if (!child.gameObject.activeSelf)
                    {
                        continue;
                    }
                    if (iterLocation(child))
                    {
                        childCounter++;
                    }
                }
            }
            node.childCount = childCounter;

            if (doAssignSceneObjects)
            {
                if (location.gameObject.tag == "editable")
                {
#if UNITY_EDITOR
                    // add recorder
                    if (sceneName != "" && shotName != "" && takeName != "")
                    {
                        UnityAnimationRecorder animRecorder = location.gameObject.AddComponent <UnityAnimationRecorder>();
                        animRecorder.savePath   = recordPath;
                        animRecorder.fileName   = String.Format("{0}_{1}_{2}_{3}", sceneName, shotName, takeName, location.name);
                        animRecorder.showLogGUI = true;
                    }
#endif
                }
                else if (location.GetComponent <Light>() != null)
                {
                    // Add light prefab
                    GameObject lightUber          = Resources.Load <GameObject>("VPET/Prefabs/UberLight");
                    GameObject _lightUberInstance = Instantiate(lightUber);
                    _lightUberInstance.name = lightUber.name;
                    _lightUberInstance.transform.SetParent(location, false);

                    SceneNodeLight nodeLight      = (SceneNodeLight)Convert.ChangeType(node, typeof(SceneNodeLight));
                    Light          lightComponent = _lightUberInstance.GetComponent <Light>();
                    lightComponent.type      = nodeLight.lightType;
                    lightComponent.color     = new Color(nodeLight.color[0], nodeLight.color[1], nodeLight.color[2]);
                    lightComponent.intensity = nodeLight.intensity * VPETSettings.Instance.lightIntensityFactor;
                    lightComponent.spotAngle = Mathf.Min(150, nodeLight.angle);
                    lightComponent.range     = nodeLight.range;

                    location.GetComponent <Light>().enabled = false;

#if UNITY_EDITOR
                    // add recorder
                    if (sceneName != "" && shotName != "" && takeName != "")
                    {
                        UnityAnimationRecorder animRecorder = location.gameObject.AddComponent <UnityAnimationRecorder>();
                        animRecorder.savePath   = recordPath;
                        animRecorder.fileName   = String.Format("{0}_{1}_{2}_{3}", sceneName, shotName, takeName, location.name);
                        animRecorder.showLogGUI = true;
                    }
#endif
                }
                else if (location.GetComponent <Camera>() != null)
                {
                    // add camera dummy mesh
                    GameObject cameraObject   = Resources.Load <GameObject>("VPET/Prefabs/cameraObject");
                    GameObject cameraInstance = Instantiate(cameraObject);
                    cameraInstance.SetActive(false);
                    cameraInstance.name = cameraObject.name;
                    cameraInstance.transform.SetParent(location.transform, false);
                    cameraInstance.transform.localScale    = new Vector3(1, 1, 1);
                    cameraInstance.transform.localPosition = new Vector3(0, 0, -0.5f);

#if UNITY_EDITOR
                    // add recorder
                    if (sceneName != "" && shotName != "" && takeName != "")
                    {
                        UnityAnimationRecorder animRecorder = location.gameObject.AddComponent <UnityAnimationRecorder>();
                        animRecorder.savePath   = recordPath;
                        animRecorder.fileName   = String.Format("{0}_{1}_{2}_{3}", sceneName, shotName, takeName, location.name);
                        animRecorder.showLogGUI = true;
                    }
#endif
                }
            }
            return(true);
        }
        public override void SendObject(string id, SceneObject sceneObject, string dagPath, NodeType nodeType, params object[] args)
        {
            // HACK check missing '/' upstream
            dagPath = "/" + dagPath;

            if (sceneObject.GetType() == typeof(SceneObject))
            {
                if (nodeType == NodeType.LIGHT)
                {
                    if (sceneObject.IsLight)
                    {
                        Light light = sceneObject.SourceLight;

                        sendMessageQueue.Add(String.Format(lightIntensityColorTemplate,
                                                           dagPath,
                                                           ((LightTypeKatana)(light.type)).ToString(),
                                                           light.intensity / VPETSettings.Instance.lightIntensityFactor,
                                                           light.color.r + " " + light.color.g + " " + light.color.b,
                                                           sceneObject.exposure,
                                                           light.spotAngle));
                    }
                }
                else if (nodeType == NodeType.CAMERA)
                {
                }
                else                         // send transform
                {
                    if (sceneObject.IsLight) // do transform for lights to katana differently
                    {
                        Transform obj = sceneObject.transform;

                        Vector3    pos = obj.localPosition;
                        Quaternion rot = obj.localRotation;
                        Vector3    scl = obj.localScale;

                        Quaternion rotY180 = Quaternion.AngleAxis(180, Vector3.up);
                        rot = rot * rotY180;
                        float   angle = 0;
                        Vector3 axis  = Vector3.zero;
                        rot.ToAngleAxis(out angle, out axis);

                        sendMessageQueue.Add(String.Format(lightTransRotTemplate,
                                                           dagPath,
                                                           (-pos.x + " " + pos.y + " " + pos.z),
                                                           (angle + " " + axis.x + " " + -axis.y + " " + -axis.z),
                                                           (scl.x + " " + scl.y + " " + scl.z)));
                    }
                    else if (sceneObject.transform.GetComponent <CameraObject>() != null)                    // do camera different too --> in fact is the same as for lights??
                    {
                        Transform obj = sceneObject.transform;

                        Vector3    pos = obj.localPosition;
                        Quaternion rot = obj.localRotation;
                        Vector3    scl = obj.localScale;


                        Quaternion rotY180 = Quaternion.AngleAxis(180, Vector3.up);
                        rot = rot * rotY180;
                        float   angle = 0;
                        Vector3 axis  = Vector3.zero;
                        rot.ToAngleAxis(out angle, out axis);

                        sendMessageQueue.Add(String.Format(camTransRotTemplate,
                                                           dagPath,
                                                           (-pos.x + " " + pos.y + " " + pos.z),
                                                           (angle + " " + axis.x + " " + -axis.y + " " + -axis.z),
                                                           (scl.x + " " + scl.y + " " + scl.z)));
                    }
                    else
                    {
                        Transform obj = sceneObject.transform;

                        Vector3    pos = obj.localPosition;
                        Quaternion rot = obj.localRotation;
                        Vector3    scl = obj.localScale;

                        float   angle = 0;
                        Vector3 axis  = Vector3.zero;
                        rot.ToAngleAxis(out angle, out axis);

                        sendMessageQueue.Add(String.Format(objTemplateQuat,
                                                           dagPath,
                                                           (-pos.x + " " + pos.y + " " + pos.z),
                                                           (angle + " " + axis.x + " " + -axis.y + " " + -axis.z),
                                                           (scl.x + " " + scl.y + " " + scl.z)));
                    }
                }
            }
            else             //light, camera if ( sobj.GetType() == typeof(SceneObject) )
            {
            }
        }
Beispiel #3
0
        private void parameterButtonHandle(IMenuButton button, int idx)
        {
            parameterMenu.reset();
            button.Toggled = true;

            // if point to move or link to camera mode switch to translation
            if (mainController.ActiveMode == MainController.Mode.pointToMoveMode || mainController.ActiveMode == MainController.Mode.objectLinkCamera)
            {
                mainController.ActiveMode = MainController.Mode.translationMode;
            }

            rangeSlider.MinValue = float.MinValue;
            rangeSlider.MaxValue = float.MaxValue;

            SceneObject sco = mainController.getCurrentSelection().GetComponent <SceneObject>();

            switch (idx)
            {
            case 0:     // X
                if (mainController.ActiveMode == MainController.Mode.translationMode)
                {
                    mainController.ConnectRangeSlider(sco, "TranslateX", 2f * VPETSettings.Instance.controllerSpeed);
                }
                else if (mainController.ActiveMode == MainController.Mode.scaleMode)
                {
                    mainController.ConnectRangeSlider(sco, "ScaleX", 0.02f);
                }
                else if (mainController.ActiveMode == MainController.Mode.rotationMode)
                {
                    mainController.ConnectRangeSlider(sco, "RotateX", 1f);
                }
                else if (mainController.ActiveMode == MainController.Mode.lightSettingsMode || mainController.ActiveMode == MainController.Mode.lookThroughLightMode)
                {
                    SceneObjectLight scl = (SceneObjectLight)sco;
                    if (scl)
                    {
                        lightSettingsWidget.hide();
                        mainController.ConnectRangeSlider(scl.setLightIntensity, scl.getLightIntensity(), 0.1f / VPETSettings.Instance.lightIntensityFactor);
                        rangeSlider.MinValue = 0f;
                    }
                }
                break;

            case 1:
                if (mainController.ActiveMode == MainController.Mode.translationMode)
                {
                    mainController.ConnectRangeSlider(sco, "TranslateY", 2f * VPETSettings.Instance.controllerSpeed);
                }
                else if (mainController.ActiveMode == MainController.Mode.scaleMode)
                {
                    mainController.ConnectRangeSlider(sco, "ScaleY", 0.02f);
                }
                else if (mainController.ActiveMode == MainController.Mode.rotationMode)
                {
                    mainController.ConnectRangeSlider(sco, "RotateY", 1f);
                }
                else if (mainController.ActiveMode == MainController.Mode.lightSettingsMode || mainController.ActiveMode == MainController.Mode.lookThroughLightMode)
                {
                    SceneObjectLight scl = (SceneObjectLight)sco;
                    if (scl)
                    {
                        lightSettingsWidget.hide();
                        mainController.ConnectRangeSlider(scl.setLightRange, scl.getLightRange(), 10f * VPETSettings.Instance.sceneScale);
                        rangeSlider.MinValue = 0.1f;
                    }
                }
                break;

            case 2:
                if (mainController.ActiveMode == MainController.Mode.translationMode)
                {
                    mainController.ConnectRangeSlider(sco, "TranslateZ", 2f * VPETSettings.Instance.controllerSpeed);
                }
                else if (mainController.ActiveMode == MainController.Mode.scaleMode)
                {
                    mainController.ConnectRangeSlider(sco, "ScaleZ", 0.02f);
                }
                else if (mainController.ActiveMode == MainController.Mode.rotationMode)
                {
                    mainController.ConnectRangeSlider(sco, "RotateZ", 1f);
                }
                else if (mainController.ActiveMode == MainController.Mode.lightSettingsMode || mainController.ActiveMode == MainController.Mode.lookThroughLightMode)
                {
                    SceneObjectLight scl = (SceneObjectLight)sco;
                    if (scl)
                    {
                        lightSettingsWidget.hide();
                        mainController.ConnectRangeSlider(mainController.setLightParamAngle, scl.getLightAngle());
                        rangeSlider.MinValue = 1f;
                        rangeSlider.MaxValue = 179f;
                    }
                }
                break;

            case 3:
                if (mainController.ActiveMode == MainController.Mode.lightSettingsMode || mainController.ActiveMode == MainController.Mode.lookThroughLightMode)
                {
                    SceneObjectLight scl = (SceneObjectLight)sco;
                    if (scl)
                    {
                        hideRangeSlider();
                        lightSettingsWidget.SetSliderType(LightSettingsWidget.SliderType.COLOR);
                        lightSettingsWidget.show(mainController.getCurrentSelection().GetComponent <SceneObjectLight>());
                        //mainController.buttonLightColorClicked(button.Toggled);
                    }
                }
                break;
            }
        }