示例#1
0
// UnmarshalJSON implements the json.Unmarshaler interface
func (s *AnySampler) UnmarshalJSON(data []byte) error {
	numberSampler := &NumberSampler{}
	err := json.Unmarshal(data, &numberSampler)
	if err == nil {
		*s = AnySampler{numberSampler}
		return nil
	}

	vec3Sampler := &Vec3Sampler{}
	err = json.Unmarshal(data, &vec3Sampler)
	if err == nil {
		*s = AnySampler{vec3Sampler}
		return nil
	}

	samplerType, err := jsonutil.ObjectType(data)
	if err != nil {
		return err
	}

	switch samplerType {
	case "image_texture":
		sampler := &ImageTexture{}
		err = json.Unmarshal(data, &sampler)
		if err != nil {
			return err
		}
		*s = AnySampler{sampler}
	default:
		return fmt.Errorf("Unknown texture sampler: '%s'", samplerType)
	}

	return nil
}
示例#2
0
// UnmarshalJSON implements the json.Unmarshaler interface
func (m *AnyMaterial) UnmarshalJSON(data []byte) error {
	materialType, err := jsonutil.ObjectType(data)
	if err != nil {
		return err
	}

	switch materialType {
	case "emissive":
		material := &EmissiveMaterial{}
		err = json.Unmarshal(data, &material)
		if err != nil {
			return err
		}
		*m = AnyMaterial{material}
	case "lambert":
		material := &LambertMaterial{}
		err = json.Unmarshal(data, &material)
		if err != nil {
			return err
		}
		*m = AnyMaterial{material}
	case "reflective":
		material := &ReflectiveMaterial{}
		err = json.Unmarshal(data, &material)
		if err != nil {
			return err
		}
		*m = AnyMaterial{material}
	case "refractive":
		material := &RefractiveMaterial{}
		err = json.Unmarshal(data, &material)
		if err != nil {
			return err
		}
		*m = AnyMaterial{material}
	case "mixed":
		material := &MixedMaterial{}
		err = json.Unmarshal(data, &material)
		if err != nil {
			return err
		}
		*m = AnyMaterial{material}
	default:
		return fmt.Errorf("Unknown material type: '%s'", materialType)
	}

	return nil
}
示例#3
0
// UnmarshalJSON implements the json.Unmarshaler interface
func (c *AnyCamera) UnmarshalJSON(data []byte) error {
	cameraType, err := jsonutil.ObjectType(data)
	if err != nil {
		return err
	}

	switch cameraType {
	case "pinhole":
		camera := &PinholeCamera{}
		err := json.Unmarshal(data, &camera)
		if err != nil {
			return err
		}
		*c = AnyCamera{camera}
	default:
		return fmt.Errorf("Unknown camera type: '%s'", cameraType)
	}

	return nil
}