Beispiel #1
0
        private int createSceneGraphIter(SceneObjectKatana scnObjKtn, Transform parent, int idx)
        {
            GameObject obj = null;     // = new GameObject( scnObjKtn.rawNodeList[idx].name );

            Node node = scnObjKtn.rawNodeList[idx];


            if (node.GetType() == typeof(NodeGeo))
            {
                NodeGeo nodeGeo = (NodeGeo)Convert.ChangeType(node, typeof(NodeGeo));
                obj = createObject(nodeGeo, parent);
            }
            else if (node.GetType() == typeof(NodeLight))
            {
                NodeLight nodeLight = (NodeLight)Convert.ChangeType(node, typeof(NodeLight));
                // HACK: remove and support skydomes
                if (nodeLight.name.ToLower() == "skydome")
                {
                    print("Do Support SkyDome !");
                }
                else
                {
                    obj = createLight(nodeLight, parent);
                }
            }
            else if (node.GetType() == typeof(NodeCam))
            {
                NodeCam nodeCam = (NodeCam)Convert.ChangeType(node, typeof(NodeCam));
                obj = createCamera(nodeCam, parent);
            }
            else
            {
                obj = createNode(node, parent);
            }

            if (node.editable)
            {
                sceneEditableObjects.Add(obj);
            }



            int idxChild = idx;

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



            return(idxChild);
        }
    public bool parseNode(byte[] data)
    {
        int dataIdx = 0;

        byte[] sliceInt = new byte[size_int];

        while (dataIdx < data.Length - 1)
        {
            Array.Copy(data, dataIdx, sliceInt, 0, size_int);
            checkEndian(ref sliceInt);
            NodeType nodeType = (NodeType)BitConverter.ToInt32(sliceInt, 0);
            dataIdx += size_int;

            switch (nodeType)
            {
            case NodeType.GROUP:
                Node node = new Node();
                node.type = nodeType;
                node.Parse(ref data, ref dataIdx);
                rawNodeList.Add(node);
                break;

            case NodeType.GEO:
                NodeGeo nodeGeo = new NodeGeo();
                nodeGeo.type = nodeType;
                nodeGeo.Parse(ref data, ref dataIdx);
                rawNodeList.Add(nodeGeo);
                break;

            case NodeType.LIGHT:
                NodeLight nodeLight = new NodeLight();
                nodeLight.type = nodeType;
                nodeLight.Parse(ref data, ref dataIdx);
                rawNodeList.Add(nodeLight);
                break;

            case NodeType.CAMERA:
                NodeCam nodeCam = new NodeCam();
                nodeCam.type = nodeType;
                nodeCam.Parse(ref data, ref dataIdx);
                rawNodeList.Add(nodeCam);
                break;
            }

            // Debug.Log( "Process Node: " + rawNodeList[rawNodeList.Count-1].name );
        }

        return(true);
    }
Beispiel #3
0
        //!
        //! function create the object from mesh data
        //! @param  scnObjKtn   object which holds the data
        //!
        private GameObject createObject(NodeGeo 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());

            //Quaternion rotTest = Quaternion.Euler(60f, -45f, -20f);
            //print("rotTest: " + rotTest.ToString());
            //rot = rotTest;

            //Vector3 euler = rot.eulerAngles;
            //print( "Euler ("+nodeGeo.name+"): " + euler );

            //euler = new Vector3(euler.z-180f, euler.x, -euler.y);
            //print("Euler (" + nodeGeo.name + "): " + euler);

            // Quaternion rotY_180 = Quaternion.AngleAxis(180.0f, Vector3.up);


            // rot = Quaternion.Euler(euler.x, euler.y, euler.z); // * rotY_180;

            //euler = rot.eulerAngles;
            //print("Euler (" + nodeGeo.name + "): " + euler);

            // 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 = 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);
        }