示例#1
0
        public static GameObject BuildNode(ref SceneNode node, Transform parent, GameObject obj)
        {
            if (node.GetType() == typeof(SceneNodeGeo))
            {
                SceneNodeGeo nodeGeo = (SceneNodeGeo)Convert.ChangeType(node, typeof(SceneNodeGeo));
                return(CreateObject(nodeGeo, parent));
            }
            else if (node.GetType() == typeof(SceneNodeLight))
            {
                SceneNodeLight nodeLight = (SceneNodeLight)Convert.ChangeType(node, typeof(SceneNodeLight));
                GameObject     _obj      = CreateLight(nodeLight, parent);
                SceneLoader.SelectableLights.Add(_obj);
                return(_obj);
            }
            else if (node.GetType() == typeof(SceneNodeCam))
            {
                SceneNodeCam nodeCam = (SceneNodeCam)Convert.ChangeType(node, typeof(SceneNodeCam));
                // make the camera editable
                nodeCam.editable = true;
                return(CreateCamera(nodeCam, parent));
            }
            else if (node.GetType() == typeof(SceneNode))
            {
                return(CreateNode(node, parent));
            }

            return(null);
        }
示例#2
0
        public static SceneNode ParseNode(NodeType nodeType, ref byte[] nodesByteData, ref int dataIdx)
        {
            switch (nodeType)
            {
            case NodeType.GROUP:
                SceneNode sceneNode = SceneDataHandler.ByteArrayToStructure <SceneNode>(nodesByteData, ref dataIdx);
                return(sceneNode);

                break;

            case NodeType.GEO:
                SceneNodeGeo sceneNodeGeo = SceneDataHandler.ByteArrayToStructure <SceneNodeGeo>(nodesByteData, ref dataIdx);
                return(sceneNodeGeo);

                break;

            case NodeType.LIGHT:
                SceneNodeLight sceneNodeLight = SceneDataHandler.ByteArrayToStructure <SceneNodeLight>(nodesByteData, ref dataIdx);
                return(sceneNodeLight);

                break;

            case NodeType.CAMERA:
                SceneNodeCam sceneNodeCamera = SceneDataHandler.ByteArrayToStructure <SceneNodeCam>(nodesByteData, ref dataIdx);
                return(sceneNodeCamera);

                break;
            }
            return(null);
        }
示例#3
0
        private int createSceneGraphIter(Transform parent, int idx)
        {
            GameObject obj = null;     // = new GameObject( scnObjKtn.rawNodeList[idx].name );

            SceneNode node = sceneDataHandler.NodeList[idx];


            if (node.GetType() == typeof(SceneNodeGeo))
            {
                SceneNodeGeo nodeGeo = (SceneNodeGeo)Convert.ChangeType(node, typeof(SceneNodeGeo));
                obj = createObject(nodeGeo, parent);
            }
            else if (node.GetType() == typeof(SceneNodeLight))
            {
                SceneNodeLight nodeLight = (SceneNodeLight)Convert.ChangeType(node, typeof(SceneNodeLight));
                obj = createLight(nodeLight, parent);
            }
            else if (node.GetType() == typeof(SceneNodeCam))
            {
                SceneNodeCam nodeCam = (SceneNodeCam)Convert.ChangeType(node, typeof(SceneNodeCam));
                obj = createCamera(nodeCam, parent);
                // make the camera editable
                nodeCam.editable = true;
            }
            else if (node.GetType() == typeof(SceneNodeMocap))
            {
                SceneNodeMocap nodeMocap = (SceneNodeMocap)Convert.ChangeType(node, typeof(SceneNodeMocap));
                obj = createMocap(nodeMocap, parent);

                // HACK
                SceneObject scnObj = obj.AddComponent <SceneObject>();
                scnObj.isMocapTrigger = true;
            }
            else
            {
                obj = createNode(node, parent);
            }

            // add scene object to editable
            if (node.editable)
            {
                sceneEditableObjects.Add(obj);
            }

            // recursive call
            int idxChild = idx;

            for (int k = 1; k <= node.childCount; k++)
            {
                idxChild = createSceneGraphIter(obj.transform, idxChild + 1);
            }

            return(idxChild);
        }
示例#4
0
        private byte[] getNodesByteArrayV1100()
        {
            Byte[] nodesByteData = new byte[0];

            foreach (SceneNode node in sceneDataHandler.NodeList)
            {
                byte[] nodeBinary;
                byte[] nodeTypeBinary;
                if (node.GetType() == typeof(SceneNodeGeo))
                {
                    nodeTypeBinary = BitConverter.GetBytes((int)NodeType.GEO);

                    SceneNodeGeo nodeGeo = (SceneNodeGeo)Convert.ChangeType(node, typeof(SceneNodeGeo));

                    // change to V1100 geo node
                    SceneNodeGeoV1100 nodeGeoV1100 = new  SceneNodeGeoV1100();
                    copyProperties(nodeGeo, nodeGeoV1100);
                    nodeGeoV1100.materialId = -1;
                    //PrintProperties(nodeGeoV1100);
                    nodeBinary = SceneDataHandler.StructureToByteArray <SceneNodeGeoV1100>(nodeGeoV1100);
                }
                else if (node.GetType() == typeof(SceneNodeLight))
                {
                    nodeTypeBinary = BitConverter.GetBytes((int)NodeType.LIGHT);
                    SceneNodeLight nodeLight = (SceneNodeLight)Convert.ChangeType(node, typeof(SceneNodeLight));
                    nodeBinary = SceneDataHandler.StructureToByteArray <SceneNodeLight>(nodeLight);
                }
                else if (node.GetType() == typeof(SceneNodeCam))
                {
                    nodeTypeBinary = BitConverter.GetBytes((int)NodeType.CAMERA);
                    SceneNodeCam nodeCam = (SceneNodeCam)Convert.ChangeType(node, typeof(SceneNodeCam));
                    nodeBinary = SceneDataHandler.StructureToByteArray <SceneNodeCam>(nodeCam);
                }
                else if (node.GetType() == typeof(SceneNodeMocap))
                {
                    nodeTypeBinary = BitConverter.GetBytes((int)NodeType.MOCAP);
                    SceneNodeMocap nodeMocap = (SceneNodeMocap)Convert.ChangeType(node, typeof(SceneNodeMocap));
                    nodeBinary = SceneDataHandler.StructureToByteArray <SceneNodeMocap>(nodeMocap);
                }
                else
                {
                    nodeTypeBinary = BitConverter.GetBytes((int)NodeType.GROUP);
                    nodeBinary     = SceneDataHandler.StructureToByteArray <SceneNode>(node);
                }
                // concate arrays
                nodesByteData = SceneDataHandler.Concat <byte>(nodesByteData, nodeTypeBinary);
                nodesByteData = SceneDataHandler.Concat <byte>(nodesByteData, nodeBinary);
            }

            return(nodesByteData);
        }
示例#5
0
        private void convertNodesByteStream()
        {
            m_nodeList = new List <SceneNode>();

            int dataIdx = 0;

            while (dataIdx < m_nodesByteData.Length - 1)
            {
                SceneNode node      = new SceneNode();
                int       numValues = BitConverter.ToInt32(m_nodesByteData, dataIdx);
                dataIdx += size_int;
                //checkEndian(ref sliceInt);
                NodeType nodeType = (NodeType)numValues;

                switch (nodeType)
                {
                case NodeType.GROUP:
                    SceneNode sceneNode = SceneDataHandler.ByteArrayToStructure <SceneNode>(m_nodesByteData, ref dataIdx);
                    node = sceneNode;
                    break;

                case NodeType.GEO:
                    SceneNodeGeo sceneNodeGeo = SceneDataHandler.ByteArrayToStructure <SceneNodeGeo>(m_nodesByteData, ref dataIdx);
                    node = sceneNodeGeo;
                    break;

                case NodeType.LIGHT:
                    SceneNodeLight sceneNodeLight = SceneDataHandler.ByteArrayToStructure <SceneNodeLight>(m_nodesByteData, ref dataIdx);
                    node = sceneNodeLight;
                    break;

                case NodeType.CAMERA:
                    SceneNodeCam sceneNodeCamera = SceneDataHandler.ByteArrayToStructure <SceneNodeCam>(m_nodesByteData, ref dataIdx);
                    node = sceneNodeCamera;
                    break;

                case NodeType.MOCAP:
                    SceneNodeMocap sceneNodeMocap = SceneDataHandler.ByteArrayToStructure <SceneNodeMocap>(m_nodesByteData, ref dataIdx);
                    node = sceneNodeMocap;
                    break;
                }

                m_nodeList.Add(node);
            }

            Array.Clear(m_nodesByteData, 0, m_nodesByteData.Length);
            m_nodesByteData = null;
            GC.Collect();
        }
示例#6
0
        private void getNodesByteArray()
        {
            nodesByteData = new byte[0];
            foreach (SceneNode node in nodeList)
            {
                byte[] nodeBinary;
                byte[] nodeTypeBinary;
                byte[] nodeLengthBinary;
                if (node.GetType() == typeof(SceneNodeGeo))
                {
                    nodeTypeBinary = BitConverter.GetBytes((int)NodeType.GEO);
                    SceneNodeGeo nodeGeo = (SceneNodeGeo)Convert.ChangeType(node, typeof(SceneNodeGeo));
                    nodeBinary = SceneDataHandler.StructToByteArray <SceneNodeGeo>(nodeGeo);
                }
                else if (node.GetType() == typeof(SceneNodeSkinnedGeo))
                {
                    nodeTypeBinary = BitConverter.GetBytes((int)NodeType.SKINNEDMESH);
                    SceneNodeSkinnedGeo nodeGeo = (SceneNodeSkinnedGeo)Convert.ChangeType(node, typeof(SceneNodeSkinnedGeo));
                    nodeBinary = SceneDataHandler.StructToByteArray <SceneNodeSkinnedGeo>(nodeGeo);
                }
                else if (node.GetType() == typeof(SceneNodeLight))
                {
                    nodeTypeBinary = BitConverter.GetBytes((int)NodeType.LIGHT);
                    SceneNodeLight nodeLight = (SceneNodeLight)Convert.ChangeType(node, typeof(SceneNodeLight));
                    nodeBinary = SceneDataHandler.StructToByteArray <SceneNodeLight>(nodeLight);
                }
                else if (node.GetType() == typeof(SceneNodeCam))
                {
                    nodeTypeBinary = BitConverter.GetBytes((int)NodeType.CAMERA);
                    SceneNodeCam nodeCam = (SceneNodeCam)Convert.ChangeType(node, typeof(SceneNodeCam));
                    nodeBinary = SceneDataHandler.StructToByteArray <SceneNodeCam>(nodeCam);
                }
                else
                {
                    nodeTypeBinary = BitConverter.GetBytes((int)NodeType.GROUP);
                    nodeBinary     = SceneDataHandler.StructToByteArray <SceneNode>(node);
                }
                nodeLengthBinary = BitConverter.GetBytes(nodeBinary.Length);

                // concate arrays
                nodesByteData = SceneDataHandler.Concat <byte>(nodesByteData, nodeLengthBinary);
                nodesByteData = SceneDataHandler.Concat <byte>(nodesByteData, nodeTypeBinary);
                nodesByteData = SceneDataHandler.Concat <byte>(nodesByteData, nodeBinary);
            }
        }
示例#7
0
        public static SceneNode ParseNode(NodeType nodeType, ref byte[] nodesByteData, int dataIdx, int length)
        {
            switch (nodeType)
            {
            case NodeType.GROUP:
                SceneNode sceneNode = new SceneNode();
                SceneDataHandler.ByteArrayToStruct <SceneNode>(ref nodesByteData, out sceneNode, dataIdx, length);
                return(sceneNode);

                break;

            case NodeType.GEO:
                SceneNodeGeo sceneNodeGeo = new SceneNodeGeo();
                SceneDataHandler.ByteArrayToStruct <SceneNodeGeo>(ref nodesByteData, out sceneNodeGeo, dataIdx, length);
                return(sceneNodeGeo);

                break;

            case NodeType.SKINNEDMESH:
                SceneNodeSkinnedGeo sceneNodeSkinnedGeo = new SceneNodeSkinnedGeo();
                SceneDataHandler.ByteArrayToStruct <SceneNodeSkinnedGeo>(ref nodesByteData, out sceneNodeSkinnedGeo, dataIdx, length);
                return(sceneNodeSkinnedGeo);

                break;

            case NodeType.LIGHT:
                SceneNodeLight sceneNodeLight = new SceneNodeLight();
                SceneDataHandler.ByteArrayToStruct <SceneNodeLight>(ref nodesByteData, out sceneNodeLight, dataIdx, length);
                return(sceneNodeLight);

                break;

            case NodeType.CAMERA:
                SceneNodeCam sceneNodeCamera = new SceneNodeCam();
                SceneDataHandler.ByteArrayToStruct <SceneNodeCam>(ref nodesByteData, out sceneNodeCamera, dataIdx, length);
                return(sceneNodeCamera);

                break;
            }
            return(null);
        }
示例#8
0
        public static GameObject BuildNode(ref SceneNode node, Transform parent, GameObject obj, bool resetID, ref List <Tuple <Renderer, string, string[]> > skinnedMeshRootBones)
        {
            if (resetID)
            {
                idCount = 0;
            }
            if (node.GetType() == typeof(SceneNodeGeo))
            {
                SceneNodeGeo nodeGeo = (SceneNodeGeo)Convert.ChangeType(node, typeof(SceneNodeGeo));
                return(CreateObject(nodeGeo, parent));
            }
            else if (node.GetType() == typeof(SceneNodeSkinnedGeo))
            {
                SceneNodeSkinnedGeo nodeSkinnedGeo = (SceneNodeSkinnedGeo)Convert.ChangeType(node, typeof(SceneNodeSkinnedGeo));
                return(CreateSkinnedObject(nodeSkinnedGeo, parent, ref skinnedMeshRootBones));
            }
            else if (node.GetType() == typeof(SceneNodeLight))
            {
                SceneNodeLight nodeLight = (SceneNodeLight)Convert.ChangeType(node, typeof(SceneNodeLight));
                GameObject     _obj      = CreateLight(nodeLight, parent);
                SceneLoader.SelectableLights.Add(_obj);
                return(_obj);
            }
            else if (node.GetType() == typeof(SceneNodeCam))
            {
                SceneNodeCam nodeCam = (SceneNodeCam)Convert.ChangeType(node, typeof(SceneNodeCam));
                // make the camera editable
                // nodeCam.editable = true;
                return(CreateCamera(nodeCam, parent));
            }
            else if (node.GetType() == typeof(SceneNode))
            {
                return(CreateNode(node, parent));
            }


            return(null);
        }
示例#9
0
        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;
            }
            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;
            }
            else if (location.GetComponent <MeshFilter>() != null)
            {
                SceneNodeGeo nodeGeo = new SceneNodeGeo();
                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[3] {
                            mat.color.r, mat.color.g, mat.color.b
                        };
                    }

                    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;
            }
            else if (location.GetComponent <SkinnedMeshRenderer>() != null)
            {
                SceneNodeGeo nodeGeo = new SceneNodeGeo();
                nodeGeo.geoId = processGeometry(location.GetComponent <SkinnedMeshRenderer>().sharedMesh);

                if (location.GetComponent <SkinnedMeshRenderer>().material != null)
                {
                    Material mat = location.GetComponent <SkinnedMeshRenderer>().material;

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

                    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;
            }
            else if (location.parent.GetComponent <AnimatorObject>() != null)
            {
                SceneNodeMocap nodeMocap = new SceneNodeMocap();
                node = nodeMocap;
            }


            node.editable = (location.gameObject.tag == "editable");
            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 = Encoding.ASCII.GetBytes(location.name); //TODO: MAC Naming ccorrupted by this function?

            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);
        }
示例#10
0
        public static void MapMaterialProperties(Material material, SceneNodeGeo nodeGeo)
        {
            //available parameters in this physically based standard shader:
            // _Color                   diffuse color (color including alpha)
            // _MainTex                 diffuse texture (2D texture)
            // _MainTex_ST
            // _Cutoff                  alpha cutoff
            // _Glossiness              smoothness of surface
            // _Metallic                matallic look of the material
            // _MetallicGlossMap        metallic texture (2D texture)
            // _BumpScale               scale of the bump map (float)
            // _BumpMap                 bumpmap (2D texture)
            // _Parallax                scale of height map
            // _ParallaxMap             height map (2D texture)
            // _OcclusionStrength       scale of occlusion
            // _OcclusionMap            occlusionMap (2D texture)
            // _EmissionColor           color of emission (color without alpha)
            // _EmissionMap             emission strength map (2D texture)
            // _DetailMask              detail mask (2D texture)
            // _DetailAlbedoMap         detail diffuse texture (2D texture)
            // _DetailAlbedoMap_ST
            // _DetailNormalMap
            // _DetailNormalMapScale    scale of detail normal map (float)
            // _DetailAlbedoMap         detail normal map (2D texture)
            // _UVSec                   UV Set for secondary textures (float)
            // _Mode                    rendering mode (float) 0 -> Opaque , 1 -> Cutout , 2 -> Transparent
            // _SrcBlend                source blend mode (enum is UnityEngine.Rendering.BlendMode)
            // _DstBlend                destination blend mode (enum is UnityEngine.Rendering.BlendMode)
            // test texture
            // WWW www = new WWW("file://F:/XML3D_Examples/tex/casual08a.jpg");
            // Texture2D texture = www.texture;

            foreach (KeyValuePair <string, KeyValuePair <string, Type> > pair in VPETSettings.ShaderPropertyMap)
            {
                FieldInfo fieldInfo    = nodeGeo.GetType().GetField(pair.Value.Key, BindingFlags.Instance | BindingFlags.Public);
                Type      propertyType = pair.Value.Value;

                if (material.HasProperty(pair.Key) && fieldInfo != null)
                {
                    if (propertyType == typeof(int))
                    {
                        material.SetInt(pair.Key, (int)Convert.ChangeType(fieldInfo.GetValue(nodeGeo), propertyType));
                    }
                    else if (propertyType == typeof(float))
                    {
                        material.SetFloat(pair.Key, (float)Convert.ChangeType(fieldInfo.GetValue(nodeGeo), propertyType));
                    }
                    else if (propertyType == typeof(Color))
                    {
                        float[] v = (float[])fieldInfo.GetValue(nodeGeo);
                        float   a = v.Length > 3 ? v[3] : 1.0f;
                        Color   c = new Color(v[0], v[1], v[2], a);
                        material.SetColor(pair.Key, c);
                    }
                    else if (propertyType == typeof(Texture))
                    {
                        int id = (int)Convert.ChangeType(fieldInfo.GetValue(nodeGeo), typeof(int));

                        if (id > -1 && id < SceneLoader.SceneTextureList.Count)
                        {
                            Texture2D texRef = SceneLoader.SceneTextureList[nodeGeo.textureId];

                            material.SetTexture(pair.Key, texRef);

                            // set materials render mode to fate to senable alpha blending
                            // TODO these values should be part of the geo node or material package !?
                            if (Textures.hasAlpha(texRef))
                            {
                                // set rendering mode
                                material.SetFloat("_Mode", 1);
                                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
                                material.SetInt("_ZWrite", 1);
                                material.EnableKeyword("_ALPHATEST_ON");
                                material.DisableKeyword("_ALPHABLEND_ON");
                                material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                                material.renderQueue = 2450;
                            }
                        }
                    }
                    else
                    {
                        Debug.LogWarning("Can not map material property " + pair.Key);
                    }


                    // TODO implement the rest
                    // .
                    // .
                    // .
                }
            }
        }
示例#11
0
        //!
        //! function to create the object from mesh data for non-skinned meshes
        //! @param  scnObjKtn   object which holds the data
        //!
        public static GameObject CreateObject(SceneNodeGeo nodeGeo, Transform parentTransform)
        {
            GameObject objMain;

            // Transform / convert handiness
            Vector3 pos = new Vector3(nodeGeo.position[0], nodeGeo.position[1], nodeGeo.position[2]);

            // Rotation / convert handiness
            Quaternion rot = new Quaternion(nodeGeo.rotation[0], nodeGeo.rotation[1], nodeGeo.rotation[2], nodeGeo.rotation[3]);

            // Scale
            Vector3 scl = new Vector3(nodeGeo.scale[0], nodeGeo.scale[1], nodeGeo.scale[2]);

            if (!parentTransform.Find(Encoding.ASCII.GetString(nodeGeo.name)))
            {
                // Material Properties and Textures
                Material mat;
#if TRUNK
                // assign material from material list
                if (nodeGeo.materialId > -1 && nodeGeo.materialId < SceneLoader.SceneMaterialList.Count)
                {
                    mat = SceneLoader.SceneMaterialList[nodeGeo.materialId];
                }
                else // or set standard
                {
                    mat = new Material(Shader.Find("Standard"));
                }
#else
                mat = new Material(Shader.Find("Standard"));
#endif

                // map properties
                if (VPETSettings.Instance.doLoadTextures)
                {
                    SceneLoader.MapMaterialProperties(mat, nodeGeo);
                }

                // set up object basics
                objMain      = new GameObject();
                objMain.name = Encoding.ASCII.GetString(nodeGeo.name);

                // Add Material
                Renderer renderer;
                renderer = objMain.AddComponent <MeshRenderer>();

                renderer.material = mat;

                // Add Mesh
                if (nodeGeo.geoId > -1 && nodeGeo.geoId < SceneLoader.SceneMeshList.Count)
                {
                    Mesh[] meshes = SceneLoader.SceneMeshList[nodeGeo.geoId];

                    objMain.AddComponent <MeshFilter>();
                    objMain.GetComponent <MeshFilter>().mesh = meshes[0];
                    VPETSettings.Instance.sceneBoundsMax     = Vector3.Max(VPETSettings.Instance.sceneBoundsMax, renderer.bounds.max);
                    VPETSettings.Instance.sceneBoundsMin     = Vector3.Min(VPETSettings.Instance.sceneBoundsMin, renderer.bounds.min);

                    for (int i = 1; i < meshes.Length; i++)
                    {
                        GameObject subObj = new GameObject(objMain.name + "_part" + i.ToString());

                        Renderer subRenderer;
                        subRenderer = subObj.AddComponent <MeshRenderer>();
                        subObj.AddComponent <MeshFilter>();
                        subObj.GetComponent <MeshFilter>().mesh = meshes[i];

                        subRenderer.material    = mat;
                        subObj.transform.parent = objMain.transform;
                        VPETSettings.Instance.sceneBoundsMax = Vector3.Max(VPETSettings.Instance.sceneBoundsMax, subRenderer.bounds.max);
                        VPETSettings.Instance.sceneBoundsMin = Vector3.Min(VPETSettings.Instance.sceneBoundsMin, subRenderer.bounds.min);
                    }

                    Vector3 sceneExtends = VPETSettings.Instance.sceneBoundsMax - VPETSettings.Instance.sceneBoundsMin;
                    VPETSettings.Instance.maxExtend = Mathf.Max(Mathf.Max(sceneExtends.x, sceneExtends.y), sceneExtends.z);
                }

                //place object
                objMain.transform.parent = parentTransform; // GameObject.Find( "Scene" ).transform;
            }
            else
            {
                objMain = parentTransform.Find(Encoding.ASCII.GetString(nodeGeo.name)).gameObject;
            }

            objMain.transform.localPosition = pos; // new Vector3( 0, 0, 0 );
            objMain.transform.localRotation = rot; //  Quaternion.identity;
            objMain.transform.localScale    = scl; // new Vector3( 1, 1, 1 );

            if (nodeGeo.editable)
            {
                SceneObject sceneObject = objMain.AddComponent <SceneObject>();
                sceneObject.id = idCount++;
            }

            return(objMain);
        }
示例#12
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.rootBoneID  = gameObjectList.IndexOf(sRenderer.rootBone.gameObject);
                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;
                //Debug.Log("Bindpose Length: " + sRenderer.sharedMesh.bindposes.Length.ToString());
                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;
                }

                nodeGeo.skinnedMeshBoneIDs = Enumerable.Repeat(-1, 99).ToArray <int>();

                for (int i = 0; i < sRenderer.bones.Length; i++)
                {
                    nodeGeo.skinnedMeshBoneIDs[i] = gameObjectList.IndexOf(sRenderer.bones[i].gameObject);
                }

                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);
        }
示例#13
0
        //!
        //! function create the object from mesh data
        //! @param  scnObjKtn   object which holds the data
        //!
        private GameObject createObject(SceneNodeGeo nodeGeo, Transform parentTransform)
        {
            // Material
            Material mat = new Material(Shader.Find("Standard"));

            //available parameters in this physically based shader:
            // _Color                   diffuse color (color including alpha)
            // _MainTex                 diffuse texture (2D texture)
            // _Cutoff                  alpha cutoff
            // _Glossiness              smoothness of surface
            // _Metallic                matallic look of the material
            // _MetallicGlossMap        metallic texture (2D texture)
            // _BumpScale               scale of the bump map (float)
            // _BumpMap                 bumpmap (2D texture)
            // _Parallax                scale of height map
            // _ParallaxMap             height map (2D texture)
            // _OcclusionStrength       scale of occlusion
            // _OcclusionMap            occlusionMap (2D texture)
            // _EmissionColor           color of emission (color without alpha)
            // _EmissionMap             emission strength map (2D texture)
            // _DetailMask              detail mask (2D texture)
            // _DetailAlbedoMap         detail diffuse texture (2D texture)
            // _DetailNormalMapScale    scale of detail normal map (float)
            // _DetailAlbedoMap         detail normal map (2D texture)
            // _UVSec                   UV Set for secondary textures (float)
            // _Mode                    rendering mode (float) 0 -> Opaque , 1 -> Cutout , 2 -> Transparent
            // _SrcBlend                source blend mode (enum is UnityEngine.Rendering.BlendMode)
            // _DstBlend                destination blend mode (enum is UnityEngine.Rendering.BlendMode)
            // test texture
            // WWW www = new WWW("file://F:/XML3D_Examples/tex/casual08a.jpg");
            // Texture2D texture = www.texture;
            // meshRenderer.material.SetTexture("_MainTex",texture);

            // Material Properties
            mat.color = new Color(nodeGeo.color[0], nodeGeo.color[1], nodeGeo.color[2]);
            mat.SetFloat("_Glossiness", nodeGeo.roughness);

            // Texture
            if (nodeGeo.textureId > -1 && nodeGeo.textureId < sceneTextureList.Count)
            {
                Texture2D texRef = sceneTextureList[nodeGeo.textureId];

                mat.SetTexture("_MainTex", texRef);

                // set materials render mode to fate to senable alpha blending
                if (hasAlpha(texRef))
                {
                    mat.SetFloat("_Mode", 2);
                    mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
                    mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                    mat.SetInt("_ZWrite", 0);
                    mat.DisableKeyword("_ALPHATEST_ON");
                    mat.EnableKeyword("_ALPHABLEND_ON");
                    mat.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                    mat.renderQueue = 3000;
                }
            }

            // Tranform / convert handiness
            Vector3 pos = new Vector3(nodeGeo.position[0], nodeGeo.position[1], nodeGeo.position[2]);
            //print( "Position: " + pos );
            // Rotation / convert handiness
            Quaternion rot = new Quaternion(nodeGeo.rotation[0], nodeGeo.rotation[1], nodeGeo.rotation[2], nodeGeo.rotation[3]);
            //print("rot: " + rot.ToString());



            // Scale
            Vector3 scl = new Vector3(nodeGeo.scale[0], nodeGeo.scale[1], nodeGeo.scale[2]);
            //print( "Scale: " + scl );


            // set up object basics
            GameObject objMain = new GameObject();

            objMain.name = Encoding.ASCII.GetString(nodeGeo.name);

            // Add Material
            MeshRenderer meshRenderer = objMain.AddComponent <MeshRenderer>();

            meshRenderer.material = mat;

            // print(objMain.name + " and " + nodeGeo.geoId);


            // Add Mesh
            if (nodeGeo.geoId > -1 && nodeGeo.geoId < sceneMeshList.Count)
            {
                Mesh[] meshes = sceneMeshList[nodeGeo.geoId];
                objMain.AddComponent <MeshFilter>();
                objMain.GetComponent <MeshFilter>().mesh = meshes[0];
                for (int i = 1; i < meshes.Length; i++)
                {
                    GameObject subObj = new GameObject(objMain.name + "_part" + i.ToString());
                    subObj.AddComponent <MeshFilter>();
                    subObj.GetComponent <MeshFilter>().mesh = meshes[i];
                    MeshRenderer subMeshRenderer = subObj.AddComponent <MeshRenderer>();
                    subMeshRenderer.material = mat;
                    subObj.transform.parent  = objMain.transform;
                }
            }

            //place object
            objMain.transform.parent        = parentTransform; // GameObject.Find( "Scene" ).transform;
            objMain.transform.localPosition = pos;             // new Vector3( 0, 0, 0 );
            objMain.transform.localRotation = rot;             //  Quaternion.identity;
            objMain.transform.localScale    = scl;             // new Vector3( 1, 1, 1 );
            //objMain.layer = 0;


            return(objMain);
        }